Skip to content

Commit

Permalink
Refactor to use more explicit import syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
steventux committed Oct 24, 2024
1 parent c2575e5 commit 924f9cc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/functions/notify/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(SCRIPT_DIR))

from helper import send_messages
import helper

import azure.functions as func

Expand All @@ -22,4 +22,4 @@ def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info(f"JSON body: {json_body}")

if notification_type == "message":
return send_messages(json_body)
return helper.send_messages(json_body)
6 changes: 3 additions & 3 deletions src/functions/notify/tests/test_function_app.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from function_app import main
import function_app
import azure.functions as func
import json


class TestFunction:
def test_main(self, mocker):
mock = mocker.patch("function_app.send_messages")
mock = mocker.patch("helper.send_messages")
data = {
"routing_plan": "breast-screening-pilot",
"recipients": [
Expand All @@ -25,7 +25,7 @@ def test_main(self, mocker):
route_params={"notification_type": "message"},
)

func_call = main.build().get_user_function()
func_call = function_app.main.build().get_user_function()
func_call(req)

mock.assert_called_once()
Expand Down
44 changes: 26 additions & 18 deletions src/functions/notify/tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import requests_mock
import uuid

from helper import send_messages
from helper import send_message
from helper import ROUTING_PLANS
from helper import message_body
from helper import reference_uuid
import helper


class TestHelper:
Expand All @@ -27,22 +23,34 @@ def test_send_messages(self, mocker):
]
}
with requests_mock.Mocker() as rm:
adapter = rm.post(
rm.post(
"http://example.com/comms", text="access_token"
)

response = send_messages(data)
response = helper.send_messages(data)

assert send_message_mock.call_count == 3

assert response == "OK\nOK\nOK"
send_message_mock.assert_any_call("access_token", ROUTING_PLANS["breast-screening-pilot"], {"nhs_number": "0000000000"})
send_message_mock.assert_any_call("access_token", ROUTING_PLANS["breast-screening-pilot"], {"nhs_number": "0000000001"})
send_message_mock.assert_any_call("access_token", ROUTING_PLANS["breast-screening-pilot"], {"nhs_number": "0000000002"})
send_message_mock.assert_any_call(
"access_token",
helper.ROUTING_PLANS["breast-screening-pilot"],
{"nhs_number": "0000000000"},
)
send_message_mock.assert_any_call(
"access_token",
helper.ROUTING_PLANS["breast-screening-pilot"],
{"nhs_number": "0000000001"},
)
send_message_mock.assert_any_call(
"access_token",
helper.ROUTING_PLANS["breast-screening-pilot"],
{"nhs_number": "0000000002"},
)

def test_send_messages_with_individual_routing_plans(self, mocker):
mocker.patch("helper.get_access_token", return_value="access_token")
test_routing_plans = ROUTING_PLANS.copy()
test_routing_plans = helper.ROUTING_PLANS.copy()
test_routing_plans["cervical-screening-pilot"] = "c838b13c-f98c-4def-93f0-515d4e4f4ee1"
test_routing_plans["bowel-cancer-screening-pilot"] = "0b1e3b13c-f98c-4def-93f0-515d4e4f4ee1"
routing_plans_mock = mocker.patch("helper.ROUTING_PLANS", test_routing_plans)
Expand All @@ -58,10 +66,10 @@ def test_send_messages_with_individual_routing_plans(self, mocker):
send_message_mock = mocker.patch("helper.send_message", return_value="OK")

with requests_mock.Mocker() as rm:
adapter = rm.post(
rm.post(
"http://example.com/comms", text="access_token"
)
response = send_messages(data)
response = helper.send_messages(data)

assert send_message_mock.call_count == 3
assert response == "OK\nOK\nOK"
Expand All @@ -72,7 +80,7 @@ def test_send_messages_with_individual_routing_plans(self, mocker):
def test_send_message(self, setup):
access_token = "access_token"
routing_plan = "breast-screening-pilot"
routing_plan_id = ROUTING_PLANS[routing_plan]
routing_plan_id = helper.ROUTING_PLANS[routing_plan]
patient_data = {
"nhs_number": "0000000000",
"date_of_birth": "1981-10-07",
Expand Down Expand Up @@ -109,8 +117,8 @@ def test_send_message(self, setup):
adapter = rm.post(
"http://example.com/comms/v1/messages", text=response_text
)
send_message(access_token, routing_plan_id, message_data)
expected_request_body = message_body(routing_plan_id, patient_data)
helper.send_message(access_token, routing_plan_id, message_data)
expected_request_body = helper.message_body(routing_plan_id, patient_data)

assert adapter.called
assert adapter.call_count == 1
Expand All @@ -128,13 +136,13 @@ def test_message_body(self):
"appointment_location": "Breast Screening Clinic, 123 High Street, London",
}

actual = message_body(routing_plan_id, data)
actual = helper.message_body(routing_plan_id, data)

expected = {
"data": {
"type": "Message",
"attributes": {
"messageReference": reference_uuid(data["nhs_number"]),
"messageReference": helper.reference_uuid(data["nhs_number"]),
"routingPlanId": routing_plan_id,
"recipient": {
"nhsNumber": "0000000000",
Expand Down

0 comments on commit 924f9cc

Please sign in to comment.