diff --git a/spotbot.py b/spotbot.py index eb108a9..e59bedb 100644 --- a/spotbot.py +++ b/spotbot.py @@ -2,15 +2,13 @@ import os import datetime from hamalertmessage import HamAlertMessage -from tables import HamAlertTable -from discord_http import DiscordHttp class SpotBot: - def __init__(self, http_req): + def __init__(self, http_req, table, discord_http): self.http_req = http_req self.ham = HamAlertMessage(http_req.get_json()) - self.table = HamAlertTable() - self.discord_http = DiscordHttp() + self.table = table + self.discord_http = discord_http def process(self): logging.info('Processing HamAlert message') diff --git a/tables.py b/tables.py index d30db98..83ce344 100644 --- a/tables.py +++ b/tables.py @@ -10,6 +10,12 @@ def __init__(self): table_service_client = TableServiceClient.from_connection_string(conn_str=connection_string) self.table_client = table_service_client.get_table_client(table_name=table_name) + def initialize_table(self): + connection_string = os.getenv('AzureWebJobsStorage') + table_name = os.getenv('TABLE_NAME') + table_service_client = TableServiceClient.from_connection_string(conn_str=connection_string) + self.table_client = table_service_client.get_table_client(table_name=table_name) + def query_for_entity(self, callsign): entities = [ent for ent in self.table_client.query_entities(f"PartitionKey eq '{callsign}' and RowKey eq '{callsign}'")] if len(entities) > 0: diff --git a/test.py b/test.py index bc447ef..c35d9cd 100644 --- a/test.py +++ b/test.py @@ -5,21 +5,49 @@ class TestSpotBot(unittest.TestCase): - ''' - def test_function_app_basic(self): - dd = datetime.strptime("2024-10-13T01:05:03", "%Y-%m-%dT%H:%M:%S") - req_body = {"callsign":"KI7HSG", "source": "pota", "frequency": "14.074", "mode": "FT8", "wwffRef":"US-0052"} - content = spotbot.create_content(req_body, dd) - expected = '01:05 | KI7HSG | [pota](https://api.pota.app/spot/comments/KI7HSG/US-0052) | freq: 14.074 | mode: FT8 | loc: US-0052' - self.assertEqual(content, expected) - - def test_function_app(self): - dd = datetime.strptime("2024-10-13T01:05:03", "%Y-%m-%dT%H:%M:%S") - req_body = {"callsign":"KI7HSG", "source": "sotawatch", "frequency": "14.074", "mode": "FT8", "summitRef": "ABCD"} - content = spotbot.create_content(req_body, dd) - expected = '01:05 | KI7HSG | [sotawatch](https://sotl.as/activators/KI7HSG) | freq: 14.074 | mode: FT8 | loc: ABCD' - self.assertEqual(content, expected) - ''' - + def test_spotbot(self): + sb = spotbot.SpotBot(FakeHttpRequest(), table=FakeHamAlertTable(FakeEntity("1234", "KI7HSG")), discord_http=FakeDiscordHttp()) + sb.process() + self.assertEqual(sb.table.saved_callsign, "KI7HSG") + self.assertEqual(sb.table.saved_messageId, "9876") + dt = sb.ham.received_time_pt.strftime("%H:%M") + self.assertEqual(sb.discord_http.posted_message, f"~~01:05 | KI7HSG | [pota](https://api.pota.app/spot/comments/KI7HSG/US-0052) | freq: 14.074 | mode: FT8 | loc: US-0052~~\n{dt} | KI7HSG | [sotawatch](https://sotl.as/activators/KI7HSG) | freq: 14.074 | mode: FT8 | loc: ABCD") + +''' +Fake classes for testing +''' + +class FakeHttpRequest: + def get_json(self): + return {"callsign":"KI7HSG", "source": "sotawatch", "frequency": "14.074", "mode": "FT8", "summitRef": "ABCD"} + +class FakeHamAlertTable: + def __init__(self, fake_entity): + self.saved_callsign = None + self.saved_messageId = None + self.fake_entity = fake_entity + def query_for_entity(self, callsign): + return self.fake_entity + def upsert_entity(self, callsign, messageId): + self.saved_callsign = callsign + self.saved_messageId = messageId + +class FakeEntity(dict): + def __init__(self, messageId, callsign): + self['MessageId'] = messageId + self['PartitionKey'] = callsign + self['RowKey'] = callsign + self.metadata = {"timestamp": datetime.now(timezone('US/Pacific'))} + +class FakeDiscordHttp: + def __init__(self): + self.posted_message = None + def post_message(self, content, messageId=None): + self.posted_message = content + return "9876" + def get_message_from_id(self, messageId): + return '01:05 | KI7HSG | [pota](https://api.pota.app/spot/comments/KI7HSG/US-0052) | freq: 14.074 | mode: FT8 | loc: US-0052' + + if __name__ == '__main__': unittest.main()