Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Schema validation to make case insensitive column name comparision #500

Merged
merged 3 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions data_validation/schema_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,26 @@ def execute(self):
def schema_validation_matching(source_fields, target_fields):
"""Compare schemas between two dictionary objects"""
results = []
# Apply the casefold() function to lowercase the keys of source and target
source_fields_casefold = dict(
latika-wadhwa marked this conversation as resolved.
Show resolved Hide resolved
{
source_field_name.casefold(): source_field_type
for source_field_name, source_field_type in source_fields.items()
}
)
target_fields_casefold = dict(
{
target_field_name.casefold(): target_field_type
for target_field_name, target_field_type in target_fields.items()
}
)

# Go through each source and check if target exists and matches
for source_field_name, source_field_type in source_fields.items():
for source_field_name, source_field_type in source_fields_casefold.items():
# target field exists
if source_field_name in target_fields:
if source_field_name in target_fields_casefold:
# target data type matches
if source_field_type == target_fields[source_field_name]:
if source_field_type == target_fields_casefold[source_field_name]:
results.append(
[
source_field_name,
Expand All @@ -111,7 +125,7 @@ def schema_validation_matching(source_fields, target_fields):
"1",
consts.VALIDATION_STATUS_SUCCESS,
"Source_type:{} Target_type:{}".format(
source_field_type, target_fields[source_field_name]
source_field_type, target_fields_casefold[source_field_name]
),
]
)
Expand All @@ -125,7 +139,7 @@ def schema_validation_matching(source_fields, target_fields):
"1",
consts.VALIDATION_STATUS_FAIL,
"Data type mismatch between source and target. Source_type:{} Target_type:{}".format(
source_field_type, target_fields[source_field_name]
source_field_type, target_fields_casefold[source_field_name]
),
]
)
Expand All @@ -143,8 +157,8 @@ def schema_validation_matching(source_fields, target_fields):
)

# source field doesn't exist
for target_field_name, target_field_type in target_fields.items():
if target_field_name not in source_fields:
for target_field_name, target_field_type in target_fields_casefold.items():
if target_field_name not in source_fields_casefold:
results.append(
[
"N/A",
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/test_schema_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_import(module_under_test):


def test_schema_validation_matching(module_under_test):
source_fields = {"field1": "string", "field2": "datetime", "field3": "string"}
source_fields = {"FIELD1": "string", "fiEld2": "datetime", "field3": "string"}
target_fields = {"field1": "string", "field2": "timestamp", "field_3": "string"}

expected_results = [
Expand Down Expand Up @@ -202,7 +202,6 @@ def test_execute(module_under_test, fs):
failures = result_df[
result_df["validation_status"].str.contains(consts.VALIDATION_STATUS_FAIL)
]

assert len(result_df) == len(source_data[0]) + 1
assert result_df["source_agg_value"].astype(float).sum() == 7
assert result_df["target_agg_value"].astype(float).sum() == 7
Expand Down