Skip to content

Commit

Permalink
testing the SpotBot.process with fake classes
Browse files Browse the repository at this point in the history
  • Loading branch information
tblanarik committed Oct 19, 2024
1 parent 5474d12 commit b4d8baa
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
8 changes: 3 additions & 5 deletions spotbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
6 changes: 6 additions & 0 deletions tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
60 changes: 44 additions & 16 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit b4d8baa

Please sign in to comment.