Skip to content

Commit

Permalink
cleanup, tests, and splitting function
Browse files Browse the repository at this point in the history
  • Loading branch information
tblanarik committed Sep 11, 2024
1 parent 32a2852 commit d459641
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/main_hamalertspotbot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ jobs:
- name: Install dependencies
run: pip install -r requirements.txt

# Optional: Add step to run tests here
- name: Run Python unit tests
run: python -u -m unittest test.py

- name: Zip artifact for deployment
run: zip release.zip ./* -r
Expand Down
7 changes: 6 additions & 1 deletion function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def spotbot(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')

req_body = {}
logging.info(f"Received this: {req}")

try:
req_body = req.get_json()
Expand All @@ -19,6 +18,10 @@ def spotbot(req: func.HttpRequest) -> func.HttpResponse:
logging.error('Invalid JSON received')
return func.HttpResponse("Invalid JSON", status_code=400)

content = create_content
return call_target(content)

def create_content(req_body):
fullCallsign = req_body.get('fullCallsign', 'Unknown')
source = req_body.get('source', 'Unknown')
frequency = req_body.get('frequency', 'Unknown')
Expand All @@ -27,7 +30,9 @@ def spotbot(req: func.HttpRequest) -> func.HttpResponse:
wwffRef = req_body.get('wwffRef', '')

content = {"content": f"{fullCallsign} | {source} | freq: {frequency} | mode: {mode} | loc: {summitRef}{wwffRef}"}
return content

def call_target(content):
target_url = os.getenv('TARGET_URL')
response = requests.post(target_url, json=content)
return func.HttpResponse(response.text, status_code=response.status_code)
19 changes: 19 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import unittest
import function_app

class TestSpotBot(unittest.TestCase):

def test_function_app_basic(self):
req_body = {"fullCallsign": "KI7HSG", "source": "POTA", "frequency": "14.074", "mode": "FT8", "wwffRef":"US-0052"}
content = function_app.create_content(req_body)
expected = {'content': 'KI7HSG | POTA | freq: 14.074 | mode: FT8 | loc: US-0052'}
self.assertDictEqual(content, expected)

def test_function_app(self):
req_body = {"fullCallsign": "KI7HSG", "source": "sotawatch", "frequency": "14.074", "mode": "FT8", "summitRef": "ABCD"}
content = function_app.create_content(req_body)
expected = {'content': 'KI7HSG | sotawatch | freq: 14.074 | mode: FT8 | loc: ABCD'}
self.assertDictEqual(content, expected)

if __name__ == '__main__':
unittest.main()

0 comments on commit d459641

Please sign in to comment.