From 9410f2daf5c2e5a350b5d62acd406f2d0ef21349 Mon Sep 17 00:00:00 2001 From: sf-dcp Date: Wed, 8 Jan 2025 17:55:53 -0500 Subject: [PATCH] modify test_create_check_success() --- .../resources/invalid_data_checks.yml | 9 +++ .../validate/resources/valid_data_checks.yml | 16 +++++ dcpy/test/lifecycle/validate/test_utils.py | 71 +++++++++++-------- 3 files changed, 65 insertions(+), 31 deletions(-) create mode 100644 dcpy/test/lifecycle/validate/resources/invalid_data_checks.yml create mode 100644 dcpy/test/lifecycle/validate/resources/valid_data_checks.yml diff --git a/dcpy/test/lifecycle/validate/resources/invalid_data_checks.yml b/dcpy/test/lifecycle/validate/resources/invalid_data_checks.yml new file mode 100644 index 000000000..b454764b6 --- /dev/null +++ b/dcpy/test/lifecycle/validate/resources/invalid_data_checks.yml @@ -0,0 +1,9 @@ +- unregistered_func_with_args: + args: + min_value: 100 +- unregistered_func_without_args +- greater_than # missing args +- greater_than: + args: + min_value: 5 + unexpected_key: value diff --git a/dcpy/test/lifecycle/validate/resources/valid_data_checks.yml b/dcpy/test/lifecycle/validate/resources/valid_data_checks.yml new file mode 100644 index 000000000..04ebe4ea1 --- /dev/null +++ b/dcpy/test/lifecycle/validate/resources/valid_data_checks.yml @@ -0,0 +1,16 @@ +- in_range: + description: My custom check description. + args: + min_value: 100 + max_value: 200 + warn_only: true + +- greater_than: + args: + min_value: 5 + warn_only: false + +- greater_than: + args: + min_value: abc # adding this for visibility. Pandera sees this as a valid check + warn_only: false diff --git a/dcpy/test/lifecycle/validate/test_utils.py b/dcpy/test/lifecycle/validate/test_utils.py index 2310b6c99..2c2553eab 100644 --- a/dcpy/test/lifecycle/validate/test_utils.py +++ b/dcpy/test/lifecycle/validate/test_utils.py @@ -7,34 +7,52 @@ signature, ) # used for checking expected attributes in a class signuture -from dcpy.models.dataset import Column +from dcpy.models.dataset import Column, CheckAttributes from dcpy.lifecycle.validate import utils from . import RESOURCES +def get_valid_checks(): + """A list of test models.dataset.Column.checks objects.""" + with open(RESOURCES / "valid_data_checks.yml") as f: + data_checks = TypeAdapter( + list[str | dict[str, CheckAttributes]] + ).validate_python(yaml.safe_load(f)) + return data_checks + + +def get_invalid_checks(): + """A list of test models.dataset.Column.checks objects.""" + with open(RESOURCES / "invalid_data_checks.yml") as f: + data_checks = TypeAdapter( + list[str | dict[str, CheckAttributes]] + ).validate_python(yaml.safe_load(f)) + return data_checks + + +valid_data_checks = get_valid_checks() +invalid_data_checks = get_invalid_checks() + + @pytest.mark.parametrize( "input_data, expected_pa_check", [ ( - { - "in_range": { - "args": { - "min_value": 100, - "max_value": 200, - }, - "warn_only": True, - } - }, - pa.Check.in_range(min_value=100, max_value=200, raise_warning=True), + valid_data_checks[0], + pa.Check.in_range( + min_value=100, + max_value=200, + raise_warning=True, + description="My custom check description.", + ), ), ( - {"greater_than": {"args": {"min_value": "5"}}}, - pa.Check.greater_than(min_value="5", raise_warning=False), + valid_data_checks[1], + pa.Check.greater_than(min_value=5, raise_warning=False), ), - # Adding this case for visibility. You would expect pandera to fail but it doesn't ( - {"greater_than": {"args": {"min_value": "abc"}}}, + valid_data_checks[2], pa.Check.greater_than(min_value="abc"), ), # TODO: add custom registered check @@ -49,29 +67,20 @@ def test_create_check_success(input_data, expected_pa_check): "input_data, error_message_substring", [ ( - {"invalid_func": {"args": {"min_value": 100}}}, + invalid_data_checks[0], "Unregistered check name", ), - # Unexpected key in check 'args' ( - {"greater_than": {"args": {"min_value": 5, "unexpected_key": "value"}}}, - "Invalid argument keys found for check", + invalid_data_checks[1], + "Unregistered check name", ), - # Unexpected key in check's dict (top level) ( - { - "greater_than": { - "args": {"min_value": 5}, - "warn_only": True, - "invalid_key": "value", - }, - }, - "can't be parsed, please check attributes", + invalid_data_checks[2], + "couldn't be created", ), - # Missing args ( - "greater_than", - "couldn't be created", + invalid_data_checks[3], + "Invalid argument keys found for check", ), ], )