1515
1616
1717class TestSearchImmunizationByIdentifier (ImmunizationBaseTest ):
18- # NOTE: In each test, the result may contain more hits. We only assert if the resource that we created is
19- # in the result set and assert the one that we don't expect is not present.
20- # This is to make these tests stateless otherwise; we need to clean up the db after each test
2118
2219 def store_records (self , * resources ):
2320 ids = []
@@ -35,36 +32,43 @@ def test_search_imms(self):
3532 covid_19_p2 = generate_imms_resource ()
3633 rsv_p1 = generate_imms_resource ()
3734 rsv_p2 = generate_imms_resource ()
38- covid_19_p1_id , covid_19_p2_id = self .store_records (covid_19_p1 , covid_19_p2 )
39- rsv_p1_id , rsv_p2_id = self .store_records (rsv_p1 , rsv_p2 )
40- covid_response_p1 = imms_api .get_immunization_by_id (covid_19_p1_id )
41- rsv_response_p1 = imms_api .get_immunization_by_id (rsv_p1_id )
35+ covid_ids = self .store_records (covid_19_p1 , covid_19_p2 )
36+ rsv_ids = self .store_records (rsv_p1 , rsv_p2 )
4237
43- # When
44- # response = imms_api.search_immunization_by_identifier(identifier_system, identifier_value)
45- identifier_value_covid = covid_response_p1 ["identifier" ][0 ]["value" ]
46- identifier_system_covid = covid_response_p1 ["identifier" ][0 ]["system" ]
47- response = imms_api .search_immunization_by_identifier (identifier_system_covid , identifier_value_covid )
38+ # Retrieve the resources to get the identifier system and value via read API
39+ covid_resource = imms_api .get_immunization_by_id (covid_ids [0 ])
40+ rsv_resource = imms_api .get_immunization_by_id (rsv_ids [0 ])
4841
49- identifier_value_rsv = rsv_response_p1 [ " identifier" ][ 0 ][ "value" ]
50- identifier_system_rsv = rsv_response_p1 [ "identifier" ][ 0 ][ "system" ]
51- response = imms_api . search_immunization_by_identifier ( identifier_system_covid , identifier_value_covid )
52- response_rsv = imms_api . search_immunization_by_identifier ( identifier_system_rsv , identifier_value_rsv )
42+ # Extract identifier components safely for covid resource
43+ identifiers = covid_resource . get ( "identifier" , [])
44+ identifier_system = identifiers [ 0 ]. get ( "system" )
45+ identifier_value = identifiers [ 0 ]. get ( "value" )
5346
54- # Then
55- self . assertEqual ( response . status_code , 200 , response . text )
56- body = response . json ( )
57- self . assertEqual ( body [ "resourceType" ], "Bundle " )
47+ # Extract identifier components safely for rsv resource
48+ rsv_identifiers = rsv_resource . get ( "identifier" , [] )
49+ rsv_identifier_system = rsv_identifiers [ 0 ]. get ( "system" )
50+ rsv_identifier_value = rsv_identifiers [ 0 ]. get ( "value " )
5851
59- resource_identifier = [entity ["resource" ]["identifier" ]["value" ] for entity in body ["entry" ]]
60- self .assertTrue (covid_19_p1_id in resource_identifier )
61-
62- self .assertEqual (response_rsv .status_code , 200 , response_rsv .text )
63- body_rsv = response_rsv .json ()
64- self .assertEqual (body_rsv ["resourceType" ], "Bundle" )
52+ # When
53+ search_response = imms_api .search_immunization_by_identifier (identifier_system , identifier_value )
54+ self .assertEqual (search_response .status_code , 200 , search_response .text )
55+ bundle = search_response .json ()
56+ self .assertEqual (bundle .get ("resourceType" ), "Bundle" , bundle )
57+ entries = bundle .get ("entry" , [])
58+ self .assertTrue (entries , "Expected at least one match in Bundle.entry" )
59+ self .assertEqual (len (entries ), 1 , f"Expected exactly one match, got { len (entries )} " )
6560
66- resource_identifier = [entity ["resource" ]["id" ] for entity in body_rsv ["entry" ]]
67- self .assertTrue (rsv_p1_id in resource_identifier )
61+ # When
62+ rsv_search_response = imms_api .search_immunization_by_identifier (
63+ rsv_identifier_system ,
64+ rsv_identifier_value
65+ )
66+ self .assertEqual (rsv_search_response .status_code , 200 , search_response .text )
67+ rsv_bundle = rsv_search_response .json ()
68+ self .assertEqual (bundle .get ("resourceType" ), "Bundle" , rsv_bundle )
69+ entries = rsv_bundle .get ("entry" , [])
70+ self .assertTrue (entries , "Expected at least one match in Bundle.entry" )
71+ self .assertEqual (len (entries ), 1 , f"Expected exactly one match, got { len (entries )} " )
6872
6973 def test_search_patient_multiple_diseases (self ):
7074 # Given patient has two vaccines
0 commit comments