Skip to content

Commit e0e89de

Browse files
committed
refactoring fhirpath to use a python dictionary
1 parent f73949b commit e0e89de

File tree

5 files changed

+48
-37
lines changed

5 files changed

+48
-37
lines changed

lambdas/shared/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ TEST_ENV := @PYTHONPATH=src:tests:src/common
33
test:
44
$(TEST_ENV) python -m unittest discover -s tests -p "test_*.py" -v
55

6-
testv:
6+
test-validator:
77
$(TEST_ENV) python -m unittest discover -s tests/test_common/validator -p "test_*.py" -v
88

99
test-list:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
("nhs_number,name,age").
88
99
Example:
10-
>>> parser = CSVRowParser()
11-
>>> parser.parse_row_to_dict("9011011,Tom,32", "nhs_number,name,age")
10+
>>> parser = CSVLineParser()
11+
>>> parser.parse_csv_line("9011011,Tom,32", "nhs_number,name,age")
1212
>>> parser.csv_file_data
1313
{'nhs_number': '9011011', 'name': 'Tom', 'age': '32'}
1414
"""

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def parse_fhir_file(self, fhir_file_name: str) -> None:
1010
with open(fhir_file_name) as json_file:
1111
self.fhir_resources = json.load(json_file)
1212

13-
# This is used for JSON FHIR Resource data events
13+
# This is used to parse FHIR data events passed in as a dictionary
1414
def parse_fhir_data(self, fhir_data: dict) -> None:
1515
self.fhir_resources = fhir_data
1616

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

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,44 @@ class Validator:
1818
def __init__(self, schema_file="", data_type: DataType = None, filepath=""):
1919
self.filepath = filepath
2020
self.json_data = {}
21+
self.fhir_data = {}
2122
self.schema_file = schema_file
2223
self.csv_row = ""
2324
self.csv_header = ""
2425
self.data_type = data_type
2526
self.data_parser = ""
2627
self.error_records: list[ErrorReport] = []
2728

28-
def _get_csv_line_parser(self, csv_row, csv_header):
29-
csv_parser = CSVLineParser()
30-
csv_parser.parse_csv_line(csv_row, csv_header)
31-
return csv_parser
32-
33-
def _get_csv_parser(self, filepath):
29+
# Retrieve all the Parsers,
30+
def _get_csv_parser(self, filepath: str) -> CSVParser:
3431
csv_parser = CSVParser()
3532
csv_parser.parse_csv_file(filepath)
3633
return csv_parser
3734

38-
def _get_fhir_parser(self, filepath):
39-
fhir_parser = FHIRParser()
40-
fhir_parser.parse_fhir_file(filepath)
41-
return fhir_parser
35+
def _get_csv_line_parser(self, csv_row, csv_header) -> CSVLineParser:
36+
csv_line_parser = CSVLineParser()
37+
csv_line_parser.parse_csv_line(csv_row, csv_header)
38+
return csv_line_parser
4239

43-
def _get_fhir_json_parser(self, fhir_data):
40+
def _get_fhir_parser(self, fhir_data: dict) -> FHIRParser:
4441
fhir_parser = FHIRParser()
4542
fhir_parser.parse_fhir_data(fhir_data)
4643
return fhir_parser
4744

48-
def _get_schema_parser(self, schemafile):
45+
def _get_schema_parser(self, schemafile: str) -> SchemaParser:
4946
schema_parser = SchemaParser()
5047
schema_parser.parse_schema(schemafile)
5148
return schema_parser
5249

50+
# Collect and add error record to the list
5351
def _add_error_record(
54-
self, error_record: ErrorReport, expression_error_group, expression_name, expression_id, error_level
55-
):
52+
self,
53+
error_record: ErrorReport,
54+
expression_error_group: str,
55+
expression_name: str,
56+
expression_id: str,
57+
error_level: ErrorLevels,
58+
) -> None:
5659
if error_record is not None:
5760
error_record.error_group = expression_error_group
5861
error_record.name = expression_name
@@ -61,15 +64,15 @@ def _add_error_record(
6164
self.error_records.append(error_record)
6265

6366
# Function to help identify a parent failure in the error list
64-
def _check_error_record_for_fail(self, expression_id):
67+
def _check_error_record_for_fail(self, expression_identifier: str) -> bool:
6568
for error_record in self.error_records:
66-
if error_record.id == expression_id:
69+
if error_record.id == expression_identifier:
6770
return True
6871
return False
6972

7073
# validate a single expression against the data file
7174
def _validate_expression(
72-
self, expression_validator: ExpressionChecker, expression, inc_header_in_row_count
75+
self, expression_validator: ExpressionChecker, expression: dict, inc_header_in_row_count: bool
7376
) -> ErrorReport | int:
7477
row = 1
7578
if inc_header_in_row_count:
@@ -123,34 +126,40 @@ def _validate_expression(
123126
return row
124127

125128
def validate_fhir(
126-
self, filepath, summarise=False, report_unexpected_exception=True, inc_header_in_row_count=True
129+
self,
130+
fhir_data: dict,
131+
summarise: bool = False,
132+
report_unexpected_exception: bool = True,
133+
inc_header_in_row_count: bool = True,
127134
) -> list[ErrorReport]:
128135
self.data_type = DataType.FHIR
129-
self.filepath = filepath
136+
self.fhir_data = fhir_data
130137
return self.run_validation(summarise, report_unexpected_exception, inc_header_in_row_count)
131138

132139
def validate_csv(
133-
self, filepath, summarise=False, report_unexpected_exception=True, inc_header_in_row_count=True
140+
self,
141+
filepath: str,
142+
summarise: bool = False,
143+
report_unexpected_exception: bool = True,
144+
inc_header_in_row_count: bool = True,
134145
) -> list[ErrorReport]:
135146
self.data_type = DataType.CSV
136147
self.filepath = filepath
137148
return self.run_validation(summarise, report_unexpected_exception, inc_header_in_row_count)
138149

139150
def validate_csv_row(
140-
self, csv_row, csv_header, summarise=False, report_unexpected_exception=True, inc_header_in_row_count=True
151+
self,
152+
csv_row: str,
153+
csv_header: list[str],
154+
summarise: bool = False,
155+
report_unexpected_exception: bool = True,
156+
inc_header_in_row_count: bool = True,
141157
) -> list[ErrorReport]:
142158
self.data_type = DataType.CSVROW
143159
self.csv_row = csv_row
144160
self.csv_header = csv_header
145161
return self.run_validation(summarise, report_unexpected_exception, inc_header_in_row_count)
146162

147-
def validate_fhir_json(
148-
self, json_data, summarise=False, report_unexpected_exception=True, inc_header_in_row_count=True
149-
) -> list[ErrorReport]:
150-
self.data_type = DataType.FHIRJSON
151-
self.json_data = json_data
152-
return self.run_validation(summarise, report_unexpected_exception, inc_header_in_row_count)
153-
154163
# run the validation against the data
155164
def run_validation(
156165
self, summarise=False, report_unexpected_exception=True, inc_header_in_row_count=True
@@ -160,10 +169,7 @@ def run_validation(
160169

161170
match self.data_type:
162171
case DataType.FHIR:
163-
self.data_parser = self._get_fhir_parser(self.filepath)
164-
self.is_csv = False
165-
case DataType.FHIRJSON:
166-
self.data_parser = self._get_fhir_json_parser(self.json_data)
172+
self.data_parser = self._get_fhir_parser(self.fhir_data)
167173
self.is_csv = False
168174
case DataType.CSV:
169175
self.data_parser = self._get_csv_parser(self.filepath)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def setUp(self):
1414
validation_folder = Path(__file__).resolve().parent
1515
self.FHIRFilePath = validation_folder / "sample_data/vaccination2.json"
1616
self.schemaFilePath = validation_folder / "test_schemas/test_schema.json"
17+
self.fhir_resources = None
1718

1819
def test_validation(self):
1920
start = time.time()
@@ -22,8 +23,12 @@ def test_validation(self):
2223
with open(self.schemaFilePath) as JSON:
2324
SchemaFile = json.load(JSON)
2425

26+
with open(self.FHIRFilePath) as json_file:
27+
self.fhir_resources = json.load(json_file)
28+
2529
validator = Validator(SchemaFile) # FHIR File Path not needed
26-
error_list = validator.validate_fhir(self.FHIRFilePath, True, True, True)
30+
print(f"FHIR Resources Loaded: {len(self.fhir_resources.get('entry', []))} entries")
31+
error_list = validator.validate_fhir(self.fhir_resources, True, True, True)
2732
error_report = validator.build_error_report("25a8cc4d-1875-4191-ac6d-2d63a0ebc64b") # include eventID if known
2833

2934
failed_validation = validator.has_validation_failed()

0 commit comments

Comments
 (0)