Skip to content

Commit 2b3063c

Browse files
committed
refactor to code and src to use enums
1 parent ed8f2bd commit 2b3063c

File tree

8 files changed

+181
-266
lines changed

8 files changed

+181
-266
lines changed
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
# all error Levels
2-
CRITICAL_ERROR = 0
3-
WARNING = 1
4-
NOTIFICATION = 2
2+
from enum import IntEnum
3+
4+
5+
class ErrorLevels(IntEnum):
6+
CRITICAL_ERROR = 0
7+
WARNING = 1
8+
NOTIFICATION = 2
59

610

711
MESSAGES = {
8-
CRITICAL_ERROR: "Critical Validation Error [%s]: %s",
9-
WARNING: "Non-Critical Validation Error [%s]: %s",
10-
NOTIFICATION: "Quality Issue Found [%s]: %s",
12+
ErrorLevels.CRITICAL_ERROR: "Critical Validation Error [%s]: %s",
13+
ErrorLevels.WARNING: "Non-Critical Validation Error [%s]: %s",
14+
ErrorLevels.NOTIFICATION: "Quality Issue Found [%s]: %s",
1115
}
Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
# all exceptions and messgaes
2-
UNEXPECTED_EXCEPTION = 0
3-
VALUE_CHECK_FAILED = 1
4-
HEADER_CHECK_FAILED = 2
5-
RECORD_LENGTH_CHECK_FAILED = 3
6-
VALUE_PREDICATE_FALSE = 4
7-
RECORD_CHECK_FAILED = 5
8-
RECORD_PREDICATE_FALSE = 6
9-
UNIQUE_CHECK_FAILED = 7
10-
ASSERT_CHECK_FAILED = 8
11-
FINALLY_ASSERT_CHECK_FAILED = 9
12-
PARSING_ERROR = 10
13-
PARENT_FAILED = 11
14-
KEY_CHECK_FAILED = 12
2+
from enum import IntEnum
3+
4+
5+
class ExceptionLevels(IntEnum):
6+
UNEXPECTED_EXCEPTION = 0
7+
VALUE_CHECK_FAILED = 1
8+
HEADER_CHECK_FAILED = 2
9+
RECORD_LENGTH_CHECK_FAILED = 3
10+
VALUE_PREDICATE_FALSE = 4
11+
RECORD_CHECK_FAILED = 5
12+
RECORD_PREDICATE_FALSE = 6
13+
UNIQUE_CHECK_FAILED = 7
14+
ASSERT_CHECK_FAILED = 8
15+
FINALLY_ASSERT_CHECK_FAILED = 9
16+
PARSING_ERROR = 10
17+
PARENT_FAILED = 11
18+
KEY_CHECK_FAILED = 12
19+
1520

1621
MESSAGES = {
17-
UNEXPECTED_EXCEPTION: "Unexpected exception [%s]: %s",
18-
VALUE_CHECK_FAILED: "Value check failed.",
19-
HEADER_CHECK_FAILED: "Header check failed.",
20-
RECORD_LENGTH_CHECK_FAILED: "Record length check failed.",
21-
RECORD_CHECK_FAILED: "Record check failed.",
22-
VALUE_PREDICATE_FALSE: "Value predicate returned false.",
23-
RECORD_PREDICATE_FALSE: "Record predicate returned false.",
24-
UNIQUE_CHECK_FAILED: "Unique check failed.",
25-
ASSERT_CHECK_FAILED: "Assertion check failed.",
26-
FINALLY_ASSERT_CHECK_FAILED: "Final assertion check failed.",
27-
PARSING_ERROR: "Failed to parse data correctly.",
28-
PARENT_FAILED: "The parent expression failed to validate",
29-
KEY_CHECK_FAILED: "Value could not be found in the Key list",
22+
ExceptionLevels.UNEXPECTED_EXCEPTION: "Unexpected exception [%s]: %s",
23+
ExceptionLevels.VALUE_CHECK_FAILED: "Value Check Failed [%s]: %s",
24+
ExceptionLevels.HEADER_CHECK_FAILED: "Header Check Failed [%s]: %s",
25+
ExceptionLevels.RECORD_LENGTH_CHECK_FAILED: "Record Length Check Failed [%s]: %s",
26+
ExceptionLevels.RECORD_CHECK_FAILED: "Record Check Failed [%s]: %s",
27+
ExceptionLevels.VALUE_PREDICATE_FALSE: "Value Predicate False [%s]: %s",
28+
ExceptionLevels.RECORD_PREDICATE_FALSE: "Record Predicate False [%s]: %s",
29+
ExceptionLevels.UNIQUE_CHECK_FAILED: "Unique Check Failed [%s]: %s",
30+
ExceptionLevels.ASSERT_CHECK_FAILED: "Assert Check Failed [%s]: %s",
31+
ExceptionLevels.FINALLY_ASSERT_CHECK_FAILED: "Finally Assert Check Failed [%s]: %s",
32+
ExceptionLevels.PARSING_ERROR: "Parsing Error [%s]: %s",
33+
ExceptionLevels.PARENT_FAILED: "Parent Failed [%s]: %s",
34+
ExceptionLevels.KEY_CHECK_FAILED: "Key Check Failed [%s]: %s",
3035
}

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

Lines changed: 125 additions & 221 deletions
Large diffs are not rendered by default.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""
22
A parser for navigating and extracting data from FHIR JSON resources.
3+
4+
Identifiy a path to a node using '|' to separate levels and
35
"""
46

57
import json

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

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

44
from dateutil import parser
55

6-
import common.validator.enums.error_levels as ErrorLevels
6+
from common.validator.enums.error_levels import ErrorLevels
77
from common.validator.record_error import ErrorReport
88

99

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
from enum import Enum
44

5-
import common.validator.enums.error_levels as ErrorLevels
6-
import common.validator.enums.exception_messages as ExceptionMessages
5+
from common.validator.enums.error_levels import ErrorLevels
6+
from common.validator.enums.exception_messages import MESSAGES
7+
from common.validator.enums.exception_messages import ExceptionLevels
78
from common.validator.expression_checker import ExpressionChecker
89
from common.validator.parsers.csv_line_parser import CSVLineParser
910
from common.validator.parsers.csv_parser import CSVParser
@@ -98,10 +99,8 @@ def _validate_expression(
9899
parent_expression = expression["parentExpression"]
99100
if self._check_error_record_for_fail(parent_expression):
100101
error_record = {
101-
"code": ExceptionMessages.PARENT_FAILED,
102-
"message": ExceptionMessages.MESSAGES[ExceptionMessages.PARENT_FAILED]
103-
+ ", Parent ID: "
104-
+ parent_expression,
102+
"code": ExceptionLevels.PARENT_FAILED,
103+
"message": MESSAGES[ExceptionLevels.PARENT_FAILED] + ", Parent ID: " + parent_expression,
105104
}
106105
self._add_error_record(
107106
error_record, expression_error_group, expression_name, expression_id, error_level
@@ -112,7 +111,7 @@ def _validate_expression(
112111
expression_values = self.data_parser.get_key_value(expression_fieldname)
113112
except Exception as e:
114113
message = f"Data get values Unexpected exception [{e.__class__.__name__}]: {e}"
115-
error_report = ErrorReport(code=ExceptionMessages.PARSING_ERROR, message=message)
114+
error_report = ErrorReport(code=ExceptionLevels.PARSING_ERROR, message=message)
116115
# original code had self.CriticalErrorLevel. Replaced with error_level
117116
self._add_error_record(error_report, expression_error_group, expression_name, expression_id, error_level)
118117
return error_report

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from unittest.mock import MagicMock
33
from unittest.mock import patch
44

5-
import common.validator.enums.exception_messages as ExceptionMessages
5+
from common.validator.enums.exception_messages import ExceptionLevels
66
from common.validator.expression_checker import ExpressionChecker
77

88
# TODO this needs to be expanded to cover all expression types
@@ -29,9 +29,10 @@ def test_validate_datetime_valid(self):
2929
"DATETIME", rule="", field_name="timestamp", field_value="2022-01-01T12:00:00", row={}
3030
)
3131
self.assertEqual(
32-
result.message, "Unexpected exception [ValueError]: Invalid isoformat string: '2022-01-01T12:00:00'"
32+
result.message,
33+
"Unexpected exception [ValueError]: Invalid isoformat string: '2022-01-01T12:00:00'",
3334
)
34-
self.assertEqual(result.code, ExceptionMessages.UNEXPECTED_EXCEPTION)
35+
self.assertEqual(result.code, ExceptionLevels.UNEXPECTED_EXCEPTION)
3536
self.assertEqual(result.field, "timestamp")
3637

3738
def test_validate_uuid_valid(self):
@@ -44,7 +45,7 @@ def test_validate_integer_invalid(self):
4445
result = self.expression_checker.validate_expression(
4546
"INT", rule="", field_name="age", field_value="hello world", row={}
4647
)
47-
self.assertEqual(result.code, ExceptionMessages.UNEXPECTED_EXCEPTION)
48+
self.assertEqual(result.code, ExceptionLevels.UNEXPECTED_EXCEPTION)
4849
self.assertEqual(result.field, "age")
4950
self.assertIn("invalid literal for int()", result.message)
5051

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from unittest.mock import MagicMock
33
from unittest.mock import patch
44

5-
import common.validator.enums.error_levels as ErrorLevels
5+
from common.validator.enums.error_levels import ErrorLevels
66
from common.validator.record_error import ErrorReport
77
from common.validator.validator import DataType
88
from common.validator.validator import Validator

0 commit comments

Comments
 (0)