55
66from methods .api .pfp_api_methods import get_prescriptions
77from methods .shared .common import assert_that , get_auth
8+ from methods .api .eps_api_methods import call_validator
9+ from messages .eps_fhir .prescription import Prescription
810
911
1012@when ("I am authenticated with {app} app" )
@@ -25,6 +27,29 @@ def i_request_my_prescriptions(context):
2527 get_prescriptions (context )
2628
2729
30+ @when ("I check the prescription item statuses for '{status}'" )
31+ def i_check_the_prescription_item_statuses_for_status (context , status ):
32+ json_response = json .loads (context .response .content )
33+ entries = json_response ["entry" ]
34+ bundles = [
35+ entry for entry in entries if entry ["resource" ]["resourceType" ] == "Bundle"
36+ ]
37+
38+ # Extract all status codes from MedicationRequest extensions
39+ status_codes = [
40+ extension ["valueCoding" ]["code" ]
41+ for bundle in bundles
42+ for entry in bundle ["resource" ]["entry" ]
43+ if entry ["resource" ]["resourceType" ] == "MedicationRequest"
44+ for extension in entry ["resource" ]["extension" ][0 ]["extension" ]
45+ if extension ["url" ] == "status"
46+ ]
47+
48+ # Assert all status codes match the expected status
49+ for status_code in status_codes :
50+ assert_that (status_code ).is_equal_to (status )
51+
52+
2853@then ("I can see my prescription" )
2954def i_can_see_my_prescription (context ):
3055 json_response = json .loads (context .response .content )
@@ -46,3 +71,144 @@ def i_can_see_my_prescription(context):
4671 assert_that (bundle ["groupIdentifier" ]["value" ]).is_equal_to (
4772 expected_prescription_id
4873 )
74+
75+
76+ @then ("I can see '{number}' of my prescriptions" )
77+ def i_can_see_my_prescriptions (context , number ):
78+ json_response = json .loads (context .response .content )
79+ entries = json_response ["entry" ]
80+ total = json_response ["total" ]
81+ prescription_bundles = [
82+ entry for entry in entries if entry ["resource" ]["resourceType" ] == "Bundle"
83+ ]
84+
85+ assert_that (total ).is_equal_to (int (number ))
86+ assert_that (len (prescription_bundles )).is_equal_to (int (number ))
87+ assert_that (total ).is_less_than_or_equal_to (25 )
88+
89+
90+ @then ("I cannot see my unreleased prescriptions" )
91+ def i_cannot_see_my_prescription (context ):
92+ json_response = json .loads (context .response .content )
93+ entries = json_response ["entry" ]
94+ bundle_entries = [
95+ entry for entry in entries if entry ["resource" ]["resourceType" ] == "Bundle"
96+ ]
97+ assert_that (len (bundle_entries )).is_equal_to (0 )
98+ assert_that (json_response ["total" ]).is_equal_to (0 )
99+
100+
101+ @then ("I validate the response for FHIR compliance" )
102+ def i_validate_the_response_for_fhir_compliance (context ):
103+ context .nomination_code = "0004"
104+ context .intent = "order"
105+ context .type_code = "acute"
106+ validate_body = Prescription (context ).body
107+
108+ call_validator (context , "eps_fhir_dispensing" , "true" , validate_body )
109+
110+ print ("validation response:" )
111+ print (context .response .content )
112+ print (context .response .status_code )
113+
114+
115+ @then ("I do not see an eRD prescription" )
116+ def i_do_not_see_an_erd_prescription (context ):
117+ json_response = json .loads (context .response .content )
118+ entries = json_response ["entry" ]
119+ bundle_entries = [
120+ entry for entry in entries if entry ["resource" ]["resourceType" ] == "Bundle"
121+ ]
122+ for bundle_entry in bundle_entries :
123+ prescription_type = bundle_entry ["resource" ]["courseOfTherapyType" ]["coding" ][
124+ 0
125+ ]["code" ]
126+ assert_that (prescription_type ).is_not_equal_to ("continuous-repeat-dispensing" )
127+
128+
129+ @then ("I validate the prescription matches my prepared prescription" )
130+ def i_validate_the_response_prescription_matches_my_prepared_prescription (context ):
131+ json_response = json .loads (context .response .content )
132+ entries = json_response ["entry" ]
133+
134+ # Mock prescription construct builds the same "prescription" each time
135+ # so using 0 index is safe for asserting values
136+ bundle = [
137+ entry for entry in entries if entry ["resource" ]["resourceType" ] == "Bundle"
138+ ]
139+
140+ prescription = bundle [0 ]["resource" ]["entry" ]
141+
142+ # Dynamically test against Medication Requests
143+ expected_entries = json .loads (context .prepare_body )["entry" ]
144+ returned_medication_codeable_concepts = [
145+ each
146+ for each in prescription
147+ if each ["resource" ]["resourceType" ] == "MedicationRequest"
148+ ]
149+ expected_medication_codeable_concepts = [
150+ each
151+ for each in expected_entries
152+ if each ["resource" ]["resourceType" ] == "MedicationRequest"
153+ ]
154+
155+ expected_items = expected_medication_codeable_concepts [0 ]["resource" ][
156+ "medicationCodeableConcept"
157+ ]["coding" ]
158+ returned_items = returned_medication_codeable_concepts [0 ]["resource" ][
159+ "medicationCodeableConcept"
160+ ]["coding" ]
161+ for item in expected_items :
162+ for key , value in item .items ():
163+ if key != "system" :
164+ assert_that (returned_items [0 ][key ]).is_equal_to (value )
165+ else :
166+ # Skip system as the mock prescription uses http vs. PFP returning https address
167+ pass
168+
169+
170+ @when ("I set the statuses I will update through" )
171+ def set_statuses_for_pfp (context ):
172+ context .statuses = [row ["Status" ] for row in context .table ]
173+
174+
175+ @then (
176+ "I process the status updates for the prescription items and verify they are returned"
177+ )
178+ def process_status_updates_and_verify (context ):
179+ # For each prescription ID in the scenario, update the status according to data table
180+ # Loop over all available statuses
181+ # DON'T COPY THIS -- It's crude for now until we come back to it.
182+ for status in context .statuses :
183+ print (f"Processing status update to { status } for all prescription IDs" )
184+ context .execute_steps (
185+ """
186+ When I am authorised to send prescription updates
187+ """
188+ )
189+ for prescription_id in context .prescription_ids :
190+ context .prescription_id = prescription_id
191+ print (f"Processing status update for prescription ID: { prescription_id } " )
192+ if (
193+ status .upper () == "COLLECTED"
194+ or status .upper () == "DISPENSED"
195+ or status .upper () == "NOT DISPENSED"
196+ ):
197+ terminal = "completed"
198+ else :
199+ terminal = "in-progress"
200+
201+ context .execute_steps (
202+ f"""
203+ When I send an { status } update with a terminal status of { terminal }
204+ """
205+ )
206+ # Call the PFP API to get the prescriptions and verify the statuses
207+ print (f"Verifying updated prescription statuses to be { status } " )
208+ context .execute_steps (
209+ f"""
210+ When I am authenticated with PFP-APIGEE app
211+ And I request my prescriptions
212+ And I check the prescription item statuses for '{ status } '
213+ """
214+ )
0 commit comments