Skip to content

Commit deb2342

Browse files
committed
refactor utility function
1 parent e0e89de commit deb2342

File tree

10 files changed

+89
-91
lines changed

10 files changed

+89
-91
lines changed

lambdas/shared/src/common/validator/expression_checker.py

Lines changed: 61 additions & 55 deletions
Large diffs are not rendered by default.

lambdas/shared/src/common/validator/parsers/fhir_parser.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
import json
2-
3-
41
class FHIRParser:
52
def __init__(self):
63
self.fhir_resources = {}
74

8-
# This opens a file with FHIR resource data
9-
def parse_fhir_file(self, fhir_file_name: str) -> None:
10-
with open(fhir_file_name) as json_file:
11-
self.fhir_resources = json.load(json_file)
12-
135
# This is used to parse FHIR data events passed in as a dictionary
146
def parse_fhir_data(self, fhir_data: dict) -> None:
157
self.fhir_resources = fhir_data

lambdas/shared/src/common/validator/validator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,15 @@ def run_validation(
211211
return self.error_records
212212

213213
# Build the error Report
214-
def build_error_report(self, event_id):
214+
def build_error_report(self, event_id: str) -> dict:
215215
occurrence_date_time = self.data_parser.get_fhir_value("occurrenceDateTime")
216216
dq_reporter = DQReporter()
217217
dq_report = dq_reporter.generate_error_report(event_id, occurrence_date_time, self.error_records)
218218

219219
return dq_report
220220

221221
# Check all errors to see if we have a critical error that would fail the validation
222-
def has_validation_failed(self):
222+
def has_validation_failed(self) -> bool:
223223
for error_record in self.error_records:
224224
if error_record.error_level == ErrorLevels.CRITICAL_ERROR:
225225
return True

lambdas/shared/tests/test_common/validator/test_application_fhir.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Test application file
2-
import json
32
import time
43
import unittest
54
from pathlib import Path
65

76
from common.validator.validator import Validator
7+
from tests.test_common.validator.testing_utils.csv_fhir_utils import parse_test_file
88

99
# TODO this needs success and fail cases
1010

@@ -20,11 +20,9 @@ def test_validation(self):
2020
start = time.time()
2121

2222
# get the JSON of the schema, changed to cope with elasticache
23-
with open(self.schemaFilePath) as JSON:
24-
SchemaFile = json.load(JSON)
23+
SchemaFile = parse_test_file(self.schemaFilePath)
2524

26-
with open(self.FHIRFilePath) as json_file:
27-
self.fhir_resources = json.load(json_file)
25+
self.fhir_resources = parse_test_file(self.FHIRFilePath)
2826

2927
validator = Validator(SchemaFile) # FHIR File Path not needed
3028
print(f"FHIR Resources Loaded: {len(self.fhir_resources.get('entry', []))} entries")

lambdas/shared/tests/test_common/validator/test_csv_line_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from common.validator.parsers.csv_line_parser import CSVLineParser
1010
from test_common.validator.testing_utils.constants import CSV_HEADER, CSV_VALUES
11-
from test_common.validator.testing_utils.csv_utils import build_row
11+
from tests.test_common.validator.testing_utils.csv_fhir_utils import build_row
1212

1313

1414
class TestCSVLineParser(unittest.TestCase):

lambdas/shared/tests/test_common/validator/test_expression_checker.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def tearDown(self):
193193

194194
def test_validate_datetime_valid(self):
195195
result = self.expression_checker.validate_expression(
196-
"DATETIME", rule="", field_name="timestamp", field_value="2022-01-01T12:00:00", row={}
196+
"DATETIME", expression_rule="", field_name="timestamp", field_value="2022-01-01T12:00:00", row={}
197197
)
198198
self.assertEqual(
199199
result.message,
@@ -204,13 +204,13 @@ def test_validate_datetime_valid(self):
204204

205205
def test_validate_uuid_valid(self):
206206
result = self.expression_checker.validate_expression(
207-
"UUID", rule="", field_name="id", field_value="550e8400-e29b-41d4-a716-446655440000", row={}
207+
"UUID", expression_rule="", field_name="id", field_value="550e8400-e29b-41d4-a716-446655440000", row={}
208208
)
209209
self.assertTrue(result is None)
210210

211211
def test_validate_integer_invalid(self):
212212
result = self.expression_checker.validate_expression(
213-
"INT", rule="", field_name="age", field_value="hello world", row={}
213+
"INT", expression_rule="", field_name="age", field_value="hello world", row={}
214214
)
215215
self.assertEqual(result.code, ExceptionLevels.UNEXPECTED_EXCEPTION)
216216
self.assertEqual(result.field, "age")
@@ -221,14 +221,14 @@ def test_validate_in_array(self):
221221
self.mock_data_parser.get_key_values.return_value = ["val1", "val2"]
222222

223223
result = self.expression_checker.validate_expression(
224-
"INARRAY", rule="", field_name="some_field", field_value="val2", row={}
224+
"INARRAY", expression_rule="", field_name="some_field", field_value="val2", row={}
225225
)
226226
self.assertEqual(result.message, "Value not in array check failed")
227227
self.assertEqual(result.field, "some_field")
228228

229229
def test_validate_expression_type_not_found(self):
230230
result = self.expression_checker.validate_expression(
231-
"UNKNOWN", rule="", field_name="field", field_value="value", row={}
231+
"UNKNOWN", expression_rule="", field_name="field", field_value="value", row={}
232232
)
233233
self.assertIn("Schema expression not found", result)
234234

lambdas/shared/tests/test_common/validator/test_fhir_parser.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,24 @@
33
from pathlib import Path
44

55
from common.validator.parsers.fhir_parser import FHIRParser
6+
from tests.test_common.validator.testing_utils.csv_fhir_utils import parse_test_file
67

78

89
class TestParse(unittest.TestCase):
910
def setUp(self):
1011
self.fhir_data_folder = Path(__file__).parent / "sample_data"
11-
12-
def test_parse_fhir_key_exists(self):
1312
fhirFilePath = self.fhir_data_folder / "vaccination.json"
13+
self.fhir_data = parse_test_file(fhirFilePath)
1414

15+
def test_parse_fhir_key_exists(self):
1516
fhir_parser = FHIRParser()
16-
fhir_parser.parse_fhir_file(fhirFilePath)
17+
fhir_parser.parse_fhir_data(self.fhir_data)
1718
my_value = fhir_parser.get_fhir_value_list("vaccineCode|coding|0|code")
1819
self.assertEqual(my_value, ["42223111000001107"])
1920

2021
def test_parse_fhir_key_not_exists(self):
21-
fhirFilePath = self.fhir_data_folder / "vaccination.json"
22-
2322
fhir_parser = FHIRParser()
24-
fhir_parser.parse_fhir_file(fhirFilePath)
23+
fhir_parser.parse_fhir_data(self.fhir_data)
2524
my_value = fhir_parser.get_fhir_value_list("vaccineCode|coding|1")
2625
self.assertEqual(my_value, [""])
2726
my_value = fhir_parser.get_fhir_value_list("vaccineCode|coding|1|codes")

lambdas/shared/tests/test_common/validator/test_validation_csv_file.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import json
21
import unittest
32
from pathlib import Path
43

54
from common.validator.validator import Validator
5+
from tests.test_common.validator.testing_utils.csv_fhir_utils import parse_test_file
66

77

88
class TestValidator(unittest.TestCase):
@@ -14,8 +14,7 @@ class TestValidator(unittest.TestCase):
1414
def setUp(self):
1515
self.parent_folder = Path(__file__).parent
1616
schema_file_path = self.parent_folder / "test_schemas/test_small_schema.json"
17-
with open(schema_file_path) as file:
18-
self.schema = json.load(file)
17+
self.schema = parse_test_file(schema_file_path)
1918

2019
def test_run_validation_csv_success(self):
2120
good_file_path = self.parent_folder / "sample_data/valid_csv_data.csv"

lambdas/shared/tests/test_common/validator/test_validation_csv_row.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# Test application file
2-
import json
32
import unittest
43
from pathlib import Path
54

65
from common.validator.validator import Validator
76
from test_common.validator.testing_utils.constants import CSV_HEADER, CSV_VALUES
8-
from test_common.validator.testing_utils.csv_utils import build_row
7+
from tests.test_common.validator.testing_utils.csv_fhir_utils import build_row, parse_test_file
98

109
schema_data_folder = Path(__file__).parent / "test_schemas"
1110
schemaFilePath = schema_data_folder / "test_schema.json"
@@ -17,9 +16,7 @@ class TestValidator(unittest.TestCase):
1716
"""
1817

1918
def setUp(self):
20-
with open(schemaFilePath, encoding="utf-8") as file:
21-
schema_file = json.load(file)
22-
self.validator = Validator(schema_file)
19+
self.validator = Validator(parse_test_file(schemaFilePath))
2320

2421
def test_run_validation_on_valid_csv_row(self):
2522
valid_rows = build_row(CSV_HEADER, CSV_VALUES)

lambdas/shared/tests/test_common/validator/testing_utils/csv_utils.py renamed to lambdas/shared/tests/test_common/validator/testing_utils/csv_fhir_utils.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1-
# Utility function to build CSV row strings
1+
import json
22

33

4+
# Utility function to build CSV row strings
45
def build_row(header: str, csv_file: dict) -> str:
56
"""
67
Construct a CSV row string from the provided csv_file.
78
Any missing header columns get empty string values.
89
"""
910
cols = header.split(",")
1011
return ",".join(str(csv_file.get(col, "")) for col in cols)
12+
13+
14+
# Utility function to parse test (FHIR or schema) files
15+
def parse_test_file(test_file_name: str) -> dict:
16+
with open(test_file_name) as json_file:
17+
return json.load(json_file)

0 commit comments

Comments
 (0)