From 06b71a521bd084d71fbf8249b2bf178e30d89eb1 Mon Sep 17 00:00:00 2001 From: patrick-troy <58770937+patrick-troy@users.noreply.github.com> Date: Wed, 7 Feb 2024 12:18:54 +0000 Subject: [PATCH] add unidentified table error --- liiatools/common/stream_filters.py | 4 ++- tests/common/test_filters.py | 48 +++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/liiatools/common/stream_filters.py b/liiatools/common/stream_filters.py index 8b400792..64c87191 100644 --- a/liiatools/common/stream_filters.py +++ b/liiatools/common/stream_filters.py @@ -156,7 +156,9 @@ def add_table_name(event, schema: DataSchema): event, table_name=table_name, table_spec=schema.column_map[table_name] ) else: - return event + return EventErrors.add_to_event( + event, type="UnidentifiedTable", message=f"Failed to identify table based on headers" + ) @streamfilter(check=type_check(events.Cell), fail_function=pass_event) diff --git a/tests/common/test_filters.py b/tests/common/test_filters.py index b3928add..01633b07 100644 --- a/tests/common/test_filters.py +++ b/tests/common/test_filters.py @@ -73,24 +73,52 @@ def get_table_name(headers): stream = [events.StartTable(headers=headers)] stream = stream_filters.add_table_name(stream, schema=schema) event = list(stream)[0] - return getattr(event, "table_name", None) + table_name = getattr(event, "table_name", None) + errors = getattr(event, "errors", None) + return {"table_name": table_name, "errors": errors} assert ( - get_table_name(["CHILD", "SEX", "DOB", "ETHNIC", "UPN", "MOTHER", "MC_DOB"]) + get_table_name(["CHILD", "SEX", "DOB", "ETHNIC", "UPN", "MOTHER", "MC_DOB"])[ + "table_name" + ] == "Header" ) for table_name, table_data in schema.table.items(): headers = list(table_data.keys()) - assert get_table_name(headers) == table_name - - assert get_table_name(["incorrect", "header", "values"]) is None + assert get_table_name(headers)["table_name"] == table_name + + assert get_table_name(["incorrect", "header", "values"])["table_name"] is None + assert list(get_table_name(["incorrect", "header", "values"])["errors"]) == [ + { + "message": "Failed to identify table based on headers", + "type": "UnidentifiedTable", + } + ] - assert get_table_name([""]) is None + assert get_table_name([""])["table_name"] is None + assert list(get_table_name(["incorrect", "header", "values"])["errors"]) == [ + { + "message": "Failed to identify table based on headers", + "type": "UnidentifiedTable", + } + ] - assert get_table_name([]) is None + assert get_table_name([])["table_name"] is None + assert list(get_table_name(["incorrect", "header", "values"])["errors"]) == [ + { + "message": "Failed to identify table based on headers", + "type": "UnidentifiedTable", + } + ] - assert get_table_name(None) is None + assert get_table_name(None)["table_name"] is None + assert list(get_table_name(["incorrect", "header", "values"])["errors"]) == [ + { + "message": "Failed to identify table based on headers", + "type": "UnidentifiedTable", + } + ] schema = annex_a_schema() @@ -105,7 +133,7 @@ def get_table_name(headers): "Date of Contact", "Contact Source", ] - ) + )["table_name"] == "List 1" ) @@ -121,7 +149,7 @@ def get_table_name(headers): "Date of Contact", "Contact Source", ] - ) + )["table_name"] == "List 1" )