Skip to content

Commit 91dfefe

Browse files
committed
NPA-4474 Fix Python Formatting
1 parent a74f89d commit 91dfefe

File tree

8 files changed

+47
-127
lines changed

8 files changed

+47
-127
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ lint:
2222
find . -name '*.py' -not -path '**/.venv/*' | xargs poetry run flake8
2323

2424
format:
25-
find . -name '*.py' -not -path '**/.venv/*' -not -path './sandbox/api/constants.py' | xargs poetry run black --check
25+
find . -name '*.py' -not -path '**/.venv/*' -not -path './sandbox/api/constants.py' | xargs poetry run black -l 120 --check
2626

2727
#Removes build/ + dist/ directories
2828
clean:

sandbox/api/constants.py

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
# Common examples
1212
INTERNAL_SERVER_ERROR_EXAMPLE = "./api/examples/errors/internal-server-error.yaml"
13-
BAD_REQUEST_INCLUDE_PARAM_INVALID = (
14-
"./api/examples/errors/invalid-include-parameter.yaml"
15-
)
13+
BAD_REQUEST_INCLUDE_PARAM_INVALID = "./api/examples/errors/invalid-include-parameter.yaml"
1614
INVALIDATED_RESOURCE = "./api/examples/errors/invalidated-resource.yaml"
1715
MISSING_IDENTIFIER = "./api/examples/errors/missing-identifier.yaml"
1816
INVALID_IDENTIFIER = "./api/examples/errors/invalid-identifier.yaml"
@@ -22,61 +20,39 @@
2220
CONSENT__FILTERED_RELATIONSHIPS_STATUS_ACTIVE = (
2321
f"{CONSENT__DIRECTORY}filtered-relationships-status-active-include-details.yaml"
2422
)
25-
CONSENT__FILTERED_RELATIONSHIPS_STATUS_INACTIVE = (
26-
f"{CONSENT__DIRECTORY}filtered-relationships-status-inactive.yaml"
27-
)
23+
CONSENT__FILTERED_RELATIONSHIPS_STATUS_INACTIVE = f"{CONSENT__DIRECTORY}filtered-relationships-status-inactive.yaml"
2824
CONSENT__FILTERED_RELATIONSHIPS_STATUS_PROPOSED_ACTIVE = (
2925
f"{CONSENT__DIRECTORY}filtered-relationships-status-proposed-active.yaml"
3026
)
3127
CONSENT__MULTIPLE_RELATIONSHIPS = f"{CONSENT__DIRECTORY}multiple-relationships.yaml"
3228
CONSENT__MULTIPLE_RELATIONSHIPS_INCLUDE_BOTH = (
3329
f"{CONSENT__DIRECTORY}multiple-relationships-include-performer-patient.yaml"
3430
)
35-
CONSENT__MULTIPLE_RELATIONSHIPS_INCLUDE_PATIENT = (
36-
f"{CONSENT__DIRECTORY}multiple-relationships-include-patient.yaml"
37-
)
38-
CONSENT__MULTIPLE_RELATIONSHIPS_INCLUDE_PERFORMER = (
39-
f"{CONSENT__DIRECTORY}multiple-relationships-include-performer.yaml"
40-
)
31+
CONSENT__MULTIPLE_RELATIONSHIPS_INCLUDE_PATIENT = f"{CONSENT__DIRECTORY}multiple-relationships-include-patient.yaml"
32+
CONSENT__MULTIPLE_RELATIONSHIPS_INCLUDE_PERFORMER = f"{CONSENT__DIRECTORY}multiple-relationships-include-performer.yaml"
4133
CONSENT__NO_RELATIONSHIPS = f"{CONSENT__DIRECTORY}no-relationships.yaml"
42-
CONSENT__SINGLE_CONSENTING_ADULT_RELATIONSHIP = (
43-
f"{CONSENT__DIRECTORY}single-consenting-adult-relationship.yaml"
44-
)
45-
CONSENT__SINGLE_CONSENTING_ADULT_RELATIONSHIP_INCLUDE_BOTH = f"{CONSENT__DIRECTORY}single-consenting-adult-relationship-include-performer-patient.yaml"
46-
CONSENT__SINGLE_MOTHER_CHILD_RELATIONSHIP = (
47-
f"{CONSENT__DIRECTORY}single-mother-child-relationship.yaml"
34+
CONSENT__SINGLE_CONSENTING_ADULT_RELATIONSHIP = f"{CONSENT__DIRECTORY}single-consenting-adult-relationship.yaml"
35+
CONSENT__SINGLE_CONSENTING_ADULT_RELATIONSHIP_INCLUDE_BOTH = (
36+
f"{CONSENT__DIRECTORY}single-consenting-adult-relationship-include-performer-patient.yaml"
4837
)
49-
CONSENT__SINGLE_MOTHER_CHILD_RELATIONSHIP_INCLUDE_BOTH = f"{CONSENT__DIRECTORY}single-mother-child-relationship-include-performer-patient.yaml"
50-
CONSENT__STATUS_PARAM_INVALID = (
51-
f"{CONSENT__DIRECTORY}errors/invalid-status-parameter.yaml"
38+
CONSENT__SINGLE_MOTHER_CHILD_RELATIONSHIP = f"{CONSENT__DIRECTORY}single-mother-child-relationship.yaml"
39+
CONSENT__SINGLE_MOTHER_CHILD_RELATIONSHIP_INCLUDE_BOTH = (
40+
f"{CONSENT__DIRECTORY}single-mother-child-relationship-include-performer-patient.yaml"
5241
)
42+
CONSENT__STATUS_PARAM_INVALID = f"{CONSENT__DIRECTORY}errors/invalid-status-parameter.yaml"
5343

5444
QR_DIRECTORY = "./api/examples/POST_QuestionnaireResponse/"
5545
QUESTIONNAIRE_RESPONSE__SUCCESS = f"{QR_DIRECTORY}success.yaml"
5646

5747
RELATED_DIRECTORY = "./api/examples/GET_RelatedPerson/"
58-
RELATED__ERROR_IDENTIFIER_MISSING = (
59-
f"{RELATED_DIRECTORY}errors/invalid-identifier-missing.yaml"
60-
)
61-
RELATED__ERROR_IDENTIFIER_SYSTEM = (
62-
f"{RELATED_DIRECTORY}errors/invalid-identifier-system.yaml"
63-
)
48+
RELATED__ERROR_IDENTIFIER_MISSING = f"{RELATED_DIRECTORY}errors/invalid-identifier-missing.yaml"
49+
RELATED__ERROR_IDENTIFIER_SYSTEM = f"{RELATED_DIRECTORY}errors/invalid-identifier-system.yaml"
6450
RELATED__ERROR_IDENTIFIER = f"{RELATED_DIRECTORY}errors/invalid-identifier.yaml"
6551
RELATED__EMPTY_RESPONSE = f"{RELATED_DIRECTORY}empty_response.yaml"
6652
RELATED__LIST_RELATIONSHIP = f"{RELATED_DIRECTORY}list_relationship_9000000017.yaml"
67-
RELATED__LIST_RELATIONSHIP_WITH_INCLUDE = (
68-
f"{RELATED_DIRECTORY}list_relationship_9000000017_include.yaml"
69-
)
70-
RELATED__VERIFY_RELATIONSHIP_09 = (
71-
f"{RELATED_DIRECTORY}verify_relationship_9000000009.yaml"
72-
)
73-
RELATED__VERIFY_RELATIONSHIP_09_WITH_INCLUDE = (
74-
f"{RELATED_DIRECTORY}verify_relationship_9000000009_include.yaml"
75-
)
76-
RELATED__VERIFY_RELATIONSHIP_25 = (
77-
f"{RELATED_DIRECTORY}verify_relationship_9000000025.yaml"
78-
)
79-
RELATED__VERIFY_RELATIONSHIP_25_WITH_INCLUDE = (
80-
f"{RELATED_DIRECTORY}verify_relationship_9000000025_include.yaml"
81-
)
53+
RELATED__LIST_RELATIONSHIP_WITH_INCLUDE = f"{RELATED_DIRECTORY}list_relationship_9000000017_include.yaml"
54+
RELATED__VERIFY_RELATIONSHIP_09 = f"{RELATED_DIRECTORY}verify_relationship_9000000009.yaml"
55+
RELATED__VERIFY_RELATIONSHIP_09_WITH_INCLUDE = f"{RELATED_DIRECTORY}verify_relationship_9000000009_include.yaml"
56+
RELATED__VERIFY_RELATIONSHIP_25 = f"{RELATED_DIRECTORY}verify_relationship_9000000025.yaml"
57+
RELATED__VERIFY_RELATIONSHIP_25_WITH_INCLUDE = f"{RELATED_DIRECTORY}verify_relationship_9000000025_include.yaml"
8258
RELATED__EMPTY_RESPONSE = f"{RELATED_DIRECTORY}empty_response_9000000033.yaml"

sandbox/api/tests/test_app.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
APP_FILE_PATH = "sandbox.api.app"
1515
CONSENT_FILE_PATH = "sandbox.api.get_consent"
1616

17+
1718
@pytest.mark.parametrize("endpoint", ["/_status", "/_ping", "/health"])
1819
def test_health_check(client: object, endpoint: str) -> None:
1920
"""Test health check endpoints."""
@@ -99,9 +100,7 @@ def test_related_person(
99100
# Act
100101
response = client.get(f"{RELATED_PERSON_API_ENDPOINT}?{request_args}")
101102
# Assert
102-
mock_generate_response_from_example.assert_called_once_with(
103-
response_file_name, status_code
104-
)
103+
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
105104
assert response.status_code == status_code
106105
assert response.json == loads(mocked_response.get_data(as_text=True))
107106

@@ -132,9 +131,7 @@ def test_questionnaire_response(
132131
# Act
133132
response = client.post(url_path, json={"data": "mocked"})
134133
# Assert
135-
mock_generate_response_from_example.assert_called_once_with(
136-
response_file_name, status_code
137-
)
134+
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
138135
assert response.status_code == status_code
139136
assert response.json == loads(mocked_response.get_data(as_text=True))
140137

@@ -169,9 +166,7 @@ def test_consent(
169166
# Act
170167
response = client.get(f"{CONSENT_API_ENDPOINT}?{request_args}")
171168
# Assert
172-
mock_generate_response_from_example.assert_called_once_with(
173-
response_file_name, status_code
174-
)
169+
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
175170
assert response.status_code == status_code
176171
assert response.json == loads(mocked_response.get_data(as_text=True))
177172

@@ -186,10 +181,6 @@ def test_consent__500_internal_server_error(
186181
"""Test Consent endpoint."""
187182
mock_remove_system.side_effect = Exception("Test exception")
188183
# Act
189-
client.get(
190-
f"{CONSENT_API_ENDPOINT}?performer:identifier=9000000015&status=active&_include=Consent:performer"
191-
)
184+
client.get(f"{CONSENT_API_ENDPOINT}?performer:identifier=9000000015&status=active&_include=Consent:performer")
192185
# Assert
193-
mock_generate_response_from_example.assert_called_once_with(
194-
"./api/examples/errors/internal-server-error.yaml", 500
195-
)
186+
mock_generate_response_from_example.assert_called_once_with("./api/examples/errors/internal-server-error.yaml", 500)

sandbox/api/tests/test_utils.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
FILE_PATH = "sandbox.api.utils"
1111

12+
1213
@pytest.mark.parametrize(
1314
"request_args,response_file_name,status_code",
1415
[
@@ -40,9 +41,7 @@ def test_related_person__not_found(
4041
# Act
4142
response = client.get(f"{RELATED_PERSON_API_ENDPOINT}?{request_args}")
4243
# Assert
43-
mock_generate_response_from_example.assert_called_once_with(
44-
response_file_name, status_code
45-
)
44+
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
4645
assert response.status_code == status_code
4746
assert response.json == loads(mocked_response.get_data(as_text=True))
4847

@@ -137,19 +136,15 @@ def test_consent(
137136
# Act
138137
response = client.get(f"{CONSENT_API_ENDPOINT}?{request_args}")
139138
# Assert
140-
mock_generate_response_from_example.assert_called_once_with(
141-
response_file_name, status_code
142-
)
139+
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
143140
assert response.status_code == status_code
144141
assert response.json == loads(mocked_response.get_data(as_text=True))
145142

146143

147144
@patch(f"{FILE_PATH}.open")
148145
def test_get_response(mock_open: MagicMock) -> None:
149146
# Arrange
150-
mock_open.return_value.__enter__.return_value.read.return_value = (
151-
'{"data": "mocked"}'
152-
)
147+
mock_open.return_value.__enter__.return_value.read.return_value = '{"data": "mocked"}'
153148
file_name = "./api/responses/GET_RelatedPerson/identifier.json"
154149
# Act
155150
response = load_json_file(file_name)
@@ -193,9 +188,7 @@ def test_check_for_related_person_errors(
193188
# Act
194189
response = client.get(f"{RELATED_PERSON_API_ENDPOINT}?{request_args}")
195190
# Assert
196-
mock_generate_response_from_example.assert_called_once_with(
197-
response_file_name, status_code
198-
)
191+
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
199192
assert response.status_code == status_code
200193
assert response.json == loads(mocked_response.get_data(as_text=True))
201194

@@ -234,7 +227,5 @@ def test_check_for_consent_errors(
234227
# Act
235228
response = client.get(f"{CONSENT_API_ENDPOINT}?{request_args}")
236229
# Assert
237-
mock_generate_response_from_example.assert_called_once_with(
238-
response_file_name, status_code
239-
)
230+
mock_generate_response_from_example.assert_called_once_with(response_file_name, status_code)
240231
assert response.status_code == status_code

sandbox/api/utils.py

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,20 @@ def check_for_consent_errors(request: Request) -> Optional[tuple]:
7575
identifier_without_system = remove_system(request.args.get(identifier_key))
7676

7777
if not identifier:
78-
return generate_response_from_example(
79-
f"{GET_CONSENT_ERRORS}/missing-identifier.yaml", 400
80-
)
78+
return generate_response_from_example(f"{GET_CONSENT_ERRORS}/missing-identifier.yaml", 400)
8179
elif identifier and len(identifier_without_system) != 10:
8280
# invalid identifier
83-
return generate_response_from_example(
84-
f"{GET_CONSENT_ERRORS}/invalid-identifier.yaml", 422
85-
)
81+
return generate_response_from_example(f"{GET_CONSENT_ERRORS}/invalid-identifier.yaml", 422)
8682
elif (
8783
isinstance(identifier, str)
8884
and "|" in identifier
8985
and "https://fhir.nhs.uk/Id/nhs-number" == identifier.split("|", maxsplit=2)[0]
9086
):
9187
# invalid identifier system
92-
return generate_response_from_example(
93-
f"{GET_CONSENT_ERRORS}/invalid-identifier-system.yaml", 422
94-
)
88+
return generate_response_from_example(f"{GET_CONSENT_ERRORS}/invalid-identifier-system.yaml", 422)
9589
elif identifier_without_system == "9000000012":
9690
# invalid status
97-
return generate_response_from_example(
98-
f"{GET_CONSENT_ERRORS}/gp-practice-not-found.yaml", 404
99-
)
91+
return generate_response_from_example(f"{GET_CONSENT_ERRORS}/gp-practice-not-found.yaml", 404)
10092

10193

10294
def check_for_empty(identifier: str, patient_identifier: str) -> Response:
@@ -149,9 +141,7 @@ def check_for_validate(
149141
return generate_response_from_example(base_file, 200)
150142

151143

152-
def check_for_list(
153-
value: str, identifier: str, include: str, base_file: str, inc_file: str
154-
) -> Response:
144+
def check_for_list(value: str, identifier: str, include: str, base_file: str, inc_file: str) -> Response:
155145
"""Check for a list relationship response for a given NHS number
156146
157147
Args:
@@ -252,11 +242,7 @@ def check_for_consent_include_params(
252242
else:
253243
logger.error("No consent:patient example provided")
254244
return generate_response_from_example(INTERNAL_SERVER_ERROR_EXAMPLE, 500)
255-
elif (
256-
len(_include) == 2
257-
and CONSENT_PATIENT in _include
258-
and CONSENT_PERFORMER in _include
259-
):
245+
elif len(_include) == 2 and CONSENT_PATIENT in _include and CONSENT_PERFORMER in _include:
260246
return generate_response_from_example(include_both_response_yaml, 200)
261247
else:
262248
return generate_response_from_example(BAD_REQUEST_INCLUDE_PARAM_INVALID, 422)
@@ -284,21 +270,13 @@ def check_for_consent_filtering(
284270
if status == [] or status is None:
285271
return generate_response_from_example(INVALIDATED_RESOURCE, 404)
286272
if status == ["active"]:
287-
if (
288-
len(_include) == 2
289-
and CONSENT_PERFORMER in _include
290-
and CONSENT_PERFORMER in _include
291-
):
292-
return generate_response_from_example(
293-
status_active_with_details_response_yaml, 200
294-
)
273+
if len(_include) == 2 and CONSENT_PERFORMER in _include and CONSENT_PERFORMER in _include:
274+
return generate_response_from_example(status_active_with_details_response_yaml, 200)
295275
else:
296276
return generate_response_from_example(INVALIDATED_RESOURCE, 404)
297277
elif status == ["inactive"]:
298278
return generate_response_from_example(status_inactive_response_yaml, 200)
299279
elif len(status) == 2 and "active" in status and "proposed" in status:
300-
return generate_response_from_example(
301-
status_proposed_and_active_response_yaml, 200
302-
)
280+
return generate_response_from_example(status_proposed_and_active_response_yaml, 200)
303281
else:
304282
return generate_response_from_example(CONSENT__STATUS_PARAM_INVALID, 422)

scripts/calculate_version.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ def get_versionable_commits(repo):
3333
commits = [c for c in repo.iter_commits() if len(c.parents) == 1]
3434

3535
# If there is a marker to start versioning from, use it. Else, start from the first commit
36-
return list(
37-
itertools.takewhile(lambda c: "+startversioning" not in c.message, commits)
38-
)
36+
return list(itertools.takewhile(lambda c: "+startversioning" not in c.message, commits))
3937

4038

4139
def is_status_set_command(commit):
@@ -83,9 +81,7 @@ def calculate_version(base_major=1, base_minor=0, base_revision=0, base_pre="alp
8381
most_recent_message = status_sets[0].message.strip()
8482

8583
if most_recent_message.startswith("+setstatus "):
86-
pre = most_recent_message.split(" ")[
87-
1
88-
] # Take the first string after the command
84+
pre = most_recent_message.split(" ")[1] # Take the first string after the command
8985

9086
if most_recent_message == "+clearstatus":
9187
pre = None

scripts/validate_schema.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ def load_yaml_file_as_json(file: str) -> dict:
6666
"""
6767
patch = path.join(path.dirname(path.realpath(__file__)), file)
6868
yamlfile = safe_load(load_file(patch))
69-
jsonstr = json.dumps(
70-
yamlfile[next(iter(yamlfile))]["value"], default=date_converter
71-
)
69+
jsonstr = json.dumps(yamlfile[next(iter(yamlfile))]["value"], default=date_converter)
7270
return json.loads(jsonstr)
7371

7472

tests/test_endpoints.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,7 @@ def test_wait_for_ping(nhsd_apim_proxy_url):
2121
resp = requests.get(f"{nhsd_apim_proxy_url}/_ping")
2222
deployed_commitId = resp.json().get("commitId")
2323

24-
while (
25-
deployed_commitId != getenv("SOURCE_COMMIT_ID")
26-
and retries <= 30
27-
and resp.status_code == 200
28-
):
24+
while deployed_commitId != getenv("SOURCE_COMMIT_ID") and retries <= 30 and resp.status_code == 200:
2925
resp = requests.get(f"{nhsd_apim_proxy_url}/_ping")
3026
deployed_commitId = resp.json().get("commitId")
3127
retries += 1
@@ -40,19 +36,15 @@ def test_wait_for_ping(nhsd_apim_proxy_url):
4036

4137
@pytest.mark.smoketest
4238
def test_status(nhsd_apim_proxy_url, status_endpoint_auth_headers):
43-
resp = requests.get(
44-
f"{nhsd_apim_proxy_url}/_status", headers=status_endpoint_auth_headers
45-
)
39+
resp = requests.get(f"{nhsd_apim_proxy_url}/_status", headers=status_endpoint_auth_headers)
4640
assert resp.status_code == 200
4741
# Make some additional assertions about your status response here!
4842

4943

5044
@pytest.mark.smoketest
5145
def test_wait_for_status(nhsd_apim_proxy_url, status_endpoint_auth_headers):
5246
retries = 0
53-
resp = requests.get(
54-
f"{nhsd_apim_proxy_url}/_status", headers=status_endpoint_auth_headers
55-
)
47+
resp = requests.get(f"{nhsd_apim_proxy_url}/_status", headers=status_endpoint_auth_headers)
5648
deployed_commitId = resp.json().get("commitId")
5749

5850
while (
@@ -61,9 +53,7 @@ def test_wait_for_status(nhsd_apim_proxy_url, status_endpoint_auth_headers):
6153
and resp.status_code == 200
6254
and resp.json().get("version")
6355
):
64-
resp = requests.get(
65-
f"{nhsd_apim_proxy_url}/_status", headers=status_endpoint_auth_headers
66-
)
56+
resp = requests.get(f"{nhsd_apim_proxy_url}/_status", headers=status_endpoint_auth_headers)
6757
deployed_commitId = resp.json().get("commitId")
6858
retries += 1
6959

0 commit comments

Comments
 (0)