Skip to content

Commit a115fb6

Browse files
committed
refactor test and seperate concerns
1 parent 7e772c5 commit a115fb6

File tree

13 files changed

+61
-61
lines changed

13 files changed

+61
-61
lines changed

lambdas/shared/tests/test_common/validator/data/test_data.csv renamed to lambdas/shared/tests/test_common/validator/sample_data/test_data.csv

File renamed without changes.

lambdas/shared/tests/test_common/validator/data/test_data_ok.csv renamed to lambdas/shared/tests/test_common/validator/sample_data/test_data_ok.csv

File renamed without changes.

lambdas/shared/tests/test_common/validator/data/test_small_nok.csv renamed to lambdas/shared/tests/test_common/validator/sample_data/test_small_nok.csv

File renamed without changes.

lambdas/shared/tests/test_common/validator/data/test_small_ok.csv renamed to lambdas/shared/tests/test_common/validator/sample_data/test_small_ok.csv

File renamed without changes.

lambdas/shared/tests/test_common/validator/data/vaccination.json renamed to lambdas/shared/tests/test_common/validator/sample_data/vaccination.json

File renamed without changes.

lambdas/shared/tests/test_common/validator/data/vaccination2.json renamed to lambdas/shared/tests/test_common/validator/sample_data/vaccination2.json

File renamed without changes.

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

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,47 @@
33
import unittest
44
from pathlib import Path
55

6-
from common.validator.validator import Validator
6+
from test_common.validator.testing_utils.constants import CSV_HEADER
7+
from test_common.validator.testing_utils.constants import CSV_VALUES
78

8-
CSV_HEADER = (
9-
"NHS_NUMBER,PERSON_FORENAME,PERSON_SURNAME,SITE_CODE,"
10-
"PERFORMING_PROFESSIONAL_FORENAME,PERFORMING_PROFESSIONAL_SURNAME,PRIMARY_SOURCE,"
11-
"VACCINATION_PROCEDURE_CODE,VACCINATION_PROCEDURE_TERM,DOSE_SEQUENCE,"
12-
"VACCINE_PRODUCT_CODE,VACCINE_PRODUCT_TERM,VACCINE_MANUFACTURER,BATCH_NUMBER"
13-
)
9+
from common.validator.validator import Validator
1410

15-
schema_data_folder = Path(__file__).parent / "schemas"
11+
schema_data_folder = Path(__file__).parent / "test_schemas"
1612
schemaFilePath = schema_data_folder / "test_schema.json"
1713

1814

1915
class TestValidator(unittest.TestCase):
16+
"""
17+
Unit tests for the CSV row validation logic using the Validator class.
18+
"""
19+
2020
@staticmethod
21-
def build_row(header: str, values: dict) -> str:
21+
def build_row(header: str, csv_file: dict) -> str:
22+
"""
23+
Construct a CSV row string from the provided csv_file.
24+
Any missing header columns get empty string values.
25+
"""
2226
cols = header.split(",")
23-
return ",".join(str(values.get(col, "")) for col in cols)
27+
return ",".join(str(csv_file.get(col, "")) for col in cols)
2428

2529
def setUp(self):
26-
with open(schemaFilePath) as JSON:
27-
SchemaFile = json.load(JSON)
28-
self.validator = Validator(SchemaFile)
30+
with open(schemaFilePath, encoding="utf-8") as file:
31+
schema_file = json.load(file)
32+
self.validator = Validator(schema_file)
2933

30-
def test_run_validation_csv_row_success(self):
31-
values = {
32-
"NHS_NUMBER": "9000000009",
33-
"PERSON_FORENAME": "JOHN",
34-
"PERSON_SURNAME": "DOE",
35-
"SITE_CODE": "RJ1",
36-
"PERFORMING_PROFESSIONAL_FORENAME": "ALICE",
37-
"PERFORMING_PROFESSIONAL_SURNAME": "SMITH",
38-
"PRIMARY_SOURCE": "true",
39-
"VACCINATION_PROCEDURE_CODE": "PROC123",
40-
"VACCINATION_PROCEDURE_TERM": "Procedure Term",
41-
"DOSE_SEQUENCE": 1,
42-
"VACCINE_PRODUCT_CODE": "VACC123",
43-
"VACCINE_PRODUCT_TERM": "Vaccine Term",
44-
"VACCINE_MANUFACTURER": "Manufacturer XYZ",
45-
"BATCH_NUMBER": "BATCH001",
46-
}
47-
good_row = self.build_row(CSV_HEADER, values)
48-
error_report = self.validator.validate_csv_row(good_row, CSV_HEADER, True, True, True)
34+
def test_run_validation_on_valid_csv_row(self):
35+
valid_rows = self.build_row(CSV_HEADER, CSV_VALUES)
36+
error_report = self.validator.validate_csv_row(valid_rows, CSV_HEADER, True, True, True)
4937
self.assertEqual(error_report, [])
5038

51-
def test_run_validation_csv_row_failure(self):
52-
# With fieldNameFlat used for CSV, empty NHS_NUMBER should fail the NOTEMPTY check
53-
values = {
54-
"NHS_NUMBER": "",
55-
"PERSON_FORENAME": "JOHN",
56-
"PERSON_SURNAME": "DOE",
57-
"SITE_CODE": "RJ1",
58-
"PERFORMING_PROFESSIONAL_FORENAME": "ALICE",
59-
"PERFORMING_PROFESSIONAL_SURNAME": "SMITH",
60-
"PRIMARY_SOURCE": "true",
61-
"VACCINATION_PROCEDURE_CODE": "PROC123",
62-
"VACCINATION_PROCEDURE_TERM": "Procedure Term",
63-
"DOSE_SEQUENCE": 1,
64-
"VACCINE_PRODUCT_CODE": "VACC123",
65-
"VACCINE_PRODUCT_TERM": "Vaccine Term",
66-
"VACCINE_MANUFACTURER": "Manufacturer XYZ",
67-
"BATCH_NUMBER": "BATCH001",
68-
}
69-
bad_row = self.build_row(CSV_HEADER, values)
70-
error_report = self.validator.validate_csv_row(bad_row, CSV_HEADER, True, True, True)
39+
def test_run_validation_on_invalid_csv_row(self):
40+
invalid_rows = self.build_row(CSV_HEADER, {**CSV_VALUES, "NHS_NUMBER": ""})
41+
error_report = self.validator.validate_csv_row(invalid_rows, CSV_HEADER, True, True, True)
7142
self.assertTrue(len(error_report) > 0)
72-
# Assert the NHS Number NOTEMPTY error is present
7343
messages = [(e.name, e.message, e.details) for e in error_report]
74-
self.assertIn(
75-
("NHS Number Not Empty Check", "Value not empty failure", "Value is empty, not as expected"), messages
44+
expected_error = (
45+
"NHS Number Not Empty Check",
46+
"Value not empty failure",
47+
"Value is empty, not as expected",
7648
)
49+
self.assertIn(expected_error, messages)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
class TestValidator(unittest.TestCase):
1010
def setUp(self):
1111
self.parent_folder = Path(__file__).parent
12-
schema_file_path = self.parent_folder / "schemas/test_small_schema.json"
12+
schema_file_path = self.parent_folder / "test_schemas/test_small_schema.json"
1313
with open(schema_file_path) as JSON:
1414
self.schema = json.load(JSON)
1515

1616
def test_run_validation_csv_success(self):
17-
good_file_path = self.parent_folder / "data/test_small_ok.csv"
17+
good_file_path = self.parent_folder / "sample_data/test_small_ok.csv"
1818
validator = Validator(self.schema)
1919
error_report = validator.validate_csv(good_file_path, False, True, True)
2020
self.assertTrue(error_report == [])
2121

2222
def test_run_validation_csv_fails(self):
23-
bad_file_path = self.parent_folder / "data/test_small_nok.csv"
23+
bad_file_path = self.parent_folder / "sample_data/test_small_nok.csv"
2424
validator = Validator(self.schema)
2525
error_report = validator.validate_csv(bad_file_path, False, True, True)
2626
self.assertTrue(error_report != [])

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
class TestApplication(unittest.TestCase):
1313
def setUp(self):
1414
validation_folder = Path(__file__).resolve().parent
15-
self.FHIRFilePath = validation_folder / "data/vaccination2.json"
16-
self.schemaFilePath = validation_folder / "schemas/test_schema.json"
15+
self.FHIRFilePath = validation_folder / "sample_data/vaccination2.json"
16+
self.schemaFilePath = validation_folder / "test_schemas/test_schema.json"
1717

1818
def test_validation(self):
1919
start = time.time()

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

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

88
class TestParse(unittest.TestCase):
99
def setUp(self):
10-
self.fhir_data_folder = Path(__file__).parent / "data"
10+
self.fhir_data_folder = Path(__file__).parent / "sample_data"
1111

1212
def test_parse_fhir_key_exists(self):
1313
fhirFilePath = self.fhir_data_folder / "vaccination.json"

0 commit comments

Comments
 (0)