77 lines
2.5 KiB
Python
77 lines
2.5 KiB
Python
from config import client as config_client
|
|
|
|
|
|
class _FakeWorksheet:
|
|
def __init__(self, records=None):
|
|
self._records = records or []
|
|
self.batch_updates = []
|
|
self.cell_updates = []
|
|
|
|
def batch_update(self, updates):
|
|
self.batch_updates.append(updates)
|
|
|
|
def get_all_records(self):
|
|
return self._records
|
|
|
|
def update_cell(self, row, col, value):
|
|
self.cell_updates.append((row, col, value))
|
|
|
|
|
|
class _FakeSheet:
|
|
def __init__(self, worksheets):
|
|
self._worksheets = worksheets
|
|
|
|
def worksheet(self, name):
|
|
return self._worksheets[name]
|
|
|
|
|
|
class _FakeGspreadClient:
|
|
def __init__(self, sheet):
|
|
self._sheet = sheet
|
|
|
|
def open_by_key(self, sheet_id):
|
|
return self._sheet
|
|
|
|
|
|
def test_write_to_sheet_only_writes_non_empty_fields(monkeypatch):
|
|
ws = _FakeWorksheet()
|
|
fake_sheet = _FakeSheet({'Standard Competitive Tab': ws})
|
|
monkeypatch.setattr(config_client, 'get_sheet_client', lambda: _FakeGspreadClient(fake_sheet))
|
|
|
|
config_client.write_to_sheet(
|
|
'Standard Competitive Tab', col_index=4,
|
|
data={'tripName': 'Peru Classic', 'tripCode': None, 'majorityPrice': ''},
|
|
fields={'tripName': 9, 'tripCode': 10, 'majorityPrice': 25}
|
|
)
|
|
|
|
assert len(ws.batch_updates) == 1
|
|
updates = ws.batch_updates[0]
|
|
assert len(updates) == 1 # only tripName had a non-empty value
|
|
assert updates[0]['values'] == [['Peru Classic']]
|
|
|
|
|
|
def test_write_to_sheet_skips_batch_update_when_nothing_to_write(monkeypatch):
|
|
ws = _FakeWorksheet()
|
|
fake_sheet = _FakeSheet({'Standard Competitive Tab': ws})
|
|
monkeypatch.setattr(config_client, 'get_sheet_client', lambda: _FakeGspreadClient(fake_sheet))
|
|
|
|
config_client.write_to_sheet(
|
|
'Standard Competitive Tab', col_index=4, data={'tripName': None}, fields={'tripName': 9}
|
|
)
|
|
|
|
assert ws.batch_updates == []
|
|
|
|
|
|
def test_get_competitors_from_db_filters_active_only(monkeypatch):
|
|
ws = _FakeWorksheet(records=[
|
|
{'Travel Style': 'Classic', 'Competitor Name': 'Intrepid', 'Website URL': 'https://intrepidtravel.com', 'Active': 'Yes'},
|
|
{'Travel Style': 'Premium', 'Competitor Name': 'Flash Pack', 'Website URL': 'https://flashpack.com', 'Active': 'No'},
|
|
])
|
|
fake_sheet = _FakeSheet({'Competitor DB': ws})
|
|
monkeypatch.setattr(config_client, 'get_sheet_client', lambda: _FakeGspreadClient(fake_sheet))
|
|
|
|
result = config_client.get_competitors_from_db()
|
|
|
|
assert len(result) == 1
|
|
assert result[0]['name'] == 'Intrepid'
|