1+ import json
12import os
23import sys
34import unittest
45from unittest .mock import create_autospec
56
7+ from fhir .resources .immunization import Immunization
8+
69sys .path .append (f"{ os .path .dirname (os .path .abspath (__file__ ))} /../src" )
710
811from fhir_controller import FhirController
@@ -23,4 +26,56 @@ def test_create_response(self):
2326 self .assertDictEqual (headers , {
2427 "Content-Type" : "application/fhir+json" ,
2528 })
26- self .assertEqual (res ["body" ], "a bodys" )
29+ self .assertEqual (res ["body" ], "a body" )
30+
31+
32+ class TestFhirControllerGetImmunisationById (unittest .TestCase ):
33+ def setUp (self ):
34+ self .service = create_autospec (FhirService )
35+ self .controller = FhirController (self .service )
36+
37+ def test_get_imms_by_id (self ):
38+ """it should return Immunization resource if it exists"""
39+ # Given
40+ imms_id = "a-id"
41+ self .service .get_immunisation_by_id .return_value = Immunization .construct ()
42+ lambda_event = {"pathParameters" : {"id" : imms_id }}
43+
44+ # When
45+ response = self .controller .get_immunisation_by_id (lambda_event )
46+
47+ # Then
48+ self .service .get_immunisation_by_id .assert_called_once_with (imms_id )
49+
50+ self .assertEqual (response ["statusCode" ], 200 )
51+ body = json .loads (response ["body" ])
52+ self .assertEqual (body ["resourceType" ], "Immunization" )
53+
54+ def test_not_found (self ):
55+ """it should return not-found OperationOutcome if it doesn't exist"""
56+ # Given
57+ imms_id = "a-non-existing-id"
58+ self .service .get_immunisation_by_id .return_value = None
59+ lambda_event = {"pathParameters" : {"id" : imms_id }}
60+
61+ # When
62+ response = self .controller .get_immunisation_by_id (lambda_event )
63+
64+ # Then
65+ self .service .get_immunisation_by_id .assert_called_once_with (imms_id )
66+
67+ self .assertEqual (response ["statusCode" ], 404 )
68+ body = json .loads (response ["body" ])
69+ self .assertEqual (body ["resourceType" ], "OperationOutcome" )
70+ self .assertEqual (body ["issue" ][0 ]["code" ], "not-found" )
71+
72+ def test_validate_imms_id (self ):
73+ """it should validate lambda's immunisation id"""
74+ invalid_id = {"pathParameters" : {"id" : "invalid %$ id" }}
75+
76+ response = self .controller .get_immunisation_by_id (invalid_id )
77+
78+ self .assertEqual (self .service .get_immunisation_by_id .call_count , 0 )
79+ self .assertEqual (response ["statusCode" ], 400 )
80+ outcome = json .loads (response ["body" ])
81+ self .assertEqual (outcome ["resourceType" ], "OperationOutcome" )
0 commit comments