1+ import json
12from json import dumps
23from unittest .mock import MagicMock , patch
34
45import pytest
56from flask import Response
67
78from ..utils import load_json_file
8- from .conftest import RELATED_PERSON_API_ENDPOINT
9+ from .conftest import RELATED_PERSON_API_ENDPOINT , CONSENT_API_ENDPOINT
910
1011FILE_PATH = "sandbox.api.utils"
1112
1213
14+ @pytest .mark .parametrize (
15+ "request_args,response_file_name,status_code" ,
16+ [
17+ (
18+ "identifier=9000000041" ,
19+ "./api/examples/errors/not-found.yaml" ,
20+ 404 ,
21+ ),
22+ (
23+ "identifier=9000000017&patient:identifier=9000000041" ,
24+ "./api/examples/errors/not-found.yaml" ,
25+ 404 ,
26+ ),
27+ ],
28+ )
29+ @patch (f"{ FILE_PATH } .generate_response_from_example" )
30+ def test_related_person__not_found (
31+ mock_generate_response_from_example : MagicMock ,
32+ request_args : str ,
33+ response_file_name : str ,
34+ status_code : int ,
35+ client : object ,
36+ ) -> None :
37+ """Test related_persons endpoint."""
38+ # Arrange
39+ mock_generate_response_from_example .return_value = mocked_response = Response (
40+ dumps ({"data" : "mocked" }), status = status_code , content_type = "application/json"
41+ )
42+ # Act
43+ response = client .get (f"{ RELATED_PERSON_API_ENDPOINT } ?{ request_args } " )
44+ # Assert
45+ mock_generate_response_from_example .assert_called_once_with (
46+ response_file_name , status_code
47+ )
48+ assert response .status_code == status_code
49+ assert response .json == json .loads (mocked_response .get_data (as_text = True ))
50+
51+
52+ @pytest .mark .parametrize (
53+ ("request_args,response_file_name,status_code" ),
54+ [
55+ (
56+ "performer:identifier=9000000017&status=active" ,
57+ "./api/examples/GET_Consent/filtered-relationships-status-active.yaml" ,
58+ 200 ,
59+ ),
60+ (
61+ "performer:identifier=9000000017&status=inactive" ,
62+ "./api/examples/GET_Consent/filtered-relationships-status-inactive.yaml" ,
63+ 200 ,
64+ ),
65+ (
66+ "performer:identifier=9000000017&status=proposed,active" ,
67+ "./api/examples/GET_Consent/filtered-relationships-status-proposed-active.yaml" ,
68+ 200 ,
69+ ),
70+ (
71+ "performer:identifier=9000000022" ,
72+ "./api/examples/GET_Consent/multiple-relationships.yaml" ,
73+ 200 ,
74+ ),
75+ (
76+ "performer:identifier=9000000022&_include=Consent:patient" ,
77+ "./api/examples/GET_Consent/multiple-relationships-include-patient.yaml" ,
78+ 200 ,
79+ ),
80+ (
81+ "performer:identifier=9000000022&_include=Consent:performer" ,
82+ "./api/examples/GET_Consent/multiple-relationships-include-performer.yaml" ,
83+ 200 ,
84+ ),
85+ (
86+ "performer:identifier=9000000022&_include=Consent:performer,Consent:patient" ,
87+ "./api/examples/GET_Consent/multiple-relationships-include-performer-patient.yaml" ,
88+ 200 ,
89+ ),
90+ (
91+ "performer:identifier=9000000010" ,
92+ "./api/examples/GET_Consent/single-consenting-adult-relationship.yaml" ,
93+ 200 ,
94+ ),
95+ (
96+ "performer:identifier=9000000010&_include=Consent:performer,Consent:patient" ,
97+ "./api/examples/GET_Consent/single-consenting-adult-relationship-include-performer-patient.yaml" ,
98+ 200 ,
99+ ),
100+ (
101+ "performer:identifier=9000000019" ,
102+ "./api/examples/GET_Consent/single-mother-child-relationship.yaml" ,
103+ 200 ,
104+ ),
105+ (
106+ "performer:identifier=9000000019&_include=Consent:performer,Consent:patient" ,
107+ "./api/examples/GET_Consent/single-mother-child-relationship-include-performer-patient.yaml" ,
108+ 200 ,
109+ ),
110+ (
111+ "performer:identifier=9000000017&status=test" , #Invalid status parameter error
112+ "./api/examples/GET_Consent/errors/invalid-status-parameter.yaml" ,
113+ 400 ,
114+ ),
115+ (
116+ "performer:identifier=9000000019&_include=test" , #Invalid include parameter error
117+ "./api/examples/errors/invalid-include-parameter.yaml" ,
118+ 400 ,
119+ ),
120+ ],
121+ )
122+ @patch (f"{ FILE_PATH } .generate_response_from_example" )
123+ def test_consent (
124+ mock_generate_response_from_example : MagicMock ,
125+ request_args : str ,
126+ response_file_name : str ,
127+ status_code : int ,
128+ client : object ,
129+ ) -> None :
130+ """Test Consent endpoint."""
131+ mock_generate_response_from_example .return_value = mocked_response = Response (
132+ dumps ({"data" : "mocked" }), status = status_code , content_type = "application/json"
133+ )
134+ # Act
135+ response = client .get (f"{ CONSENT_API_ENDPOINT } ?{ request_args } " )
136+ # Assert
137+ mock_generate_response_from_example .assert_called_once_with (
138+ response_file_name , status_code
139+ )
140+ assert response .status_code == status_code
141+ assert response .json == json .loads (mocked_response .get_data (as_text = True ))
142+
143+
13144@patch (f"{ FILE_PATH } .open" )
14145def test_get_response (mock_open : MagicMock ) -> None :
15146 # Arrange
@@ -25,28 +156,50 @@ def test_get_response(mock_open: MagicMock) -> None:
25156
26157
27158@pytest .mark .parametrize (
28- "request_args,response_file_name,status_code" ,
159+ "endpoint, request_args,response_file_name,status_code" ,
29160 [
30161 (
31- "" , # identifier is missing
162+ RELATED_PERSON_API_ENDPOINT , # Related person - identifier is missing
163+ "" ,
32164 "./api/examples/errors/missing-identifier.yaml" ,
33165 400 ,
34166 ),
35167 (
36- "identifier=123456789" , # identifier length is less than 10
168+ RELATED_PERSON_API_ENDPOINT , # Related person - identifier length is less than 10
169+ "identifier=123456789" ,
37170 "./api/examples/errors/invalid-identifier.yaml" ,
38171 400 ,
39172 ),
40- # (
41- # "identifier=https://fhir.nhs.uk/ID/nhs-number|1234567890", # identifier system invalid
42- # "./api/responses/GET_RelatedPerson/bad_request_identifier_invalid_system.json",
43- # 400,
44- # ),
173+ (
174+ RELATED_PERSON_API_ENDPOINT , # Related person - identifier system invalid
175+ "identifier=https://fhir.nhs.uk/Id/nhs-number|A730675929" ,
176+ "./api/examples/errors/invalid-identifier-system.yaml" ,
177+ 400 ,
178+ ),
179+ (
180+ CONSENT_API_ENDPOINT , # Consent - Invalid performer identifier
181+ "performer:identifier=90000009990" ,
182+ "./api/examples/errors/invalid-identifier.yaml" ,
183+ 400 ,
184+ ),
185+ (
186+ CONSENT_API_ENDPOINT , # Consent - missing performer identifier
187+ "" ,
188+ "./api/examples/errors/missing-identifier.yaml" ,
189+ 400 ,
190+ ),
191+ (
192+ CONSENT_API_ENDPOINT , # Related person - identifier system invalid
193+ "performer:identifier=https://fhir.nhs.uk/Id/nhs-number|A730675929" ,
194+ "./api/examples/errors/invalid-identifier-system.yaml" ,
195+ 400 ,
196+ ),
45197 ],
46198)
47199@patch (f"{ FILE_PATH } .generate_response_from_example" )
48200def test_check_for_errors (
49201 mock_generate_response_from_example : MagicMock ,
202+ endpoint : str ,
50203 request_args : str ,
51204 response_file_name : str ,
52205 status_code : int ,
@@ -56,7 +209,7 @@ def test_check_for_errors(
56209 dumps ({"data" : "mocked" }), status = status_code , content_type = "application/json"
57210 )
58211 # Act
59- response = client .get (f"{ RELATED_PERSON_API_ENDPOINT } ?{ request_args } " )
212+ response = client .get (f"{ endpoint } ?{ request_args } " )
60213 # Assert
61214 mock_generate_response_from_example .assert_called_once_with (
62215 response_file_name , status_code
0 commit comments