Spaces:
Configuration error
Configuration error
Download tests/test_validator.py from jonels1988/column_matching_project: direct link, hf CLI and curl.
- Browser
- Download file 2.44 kB
-
https://huggingface.co/spaces/jonels1988/column_matching_project/resolve/main/tests/test_validator.py
- Command line
-
hf download hf://spaces/jonels1988/column_matching_project/tests/test_validator.py
-
curl -L -o test_validator.py https://huggingface.co/spaces/jonels1988/column_matching_project/resolve/main/tests/test_validator.py
2.44 kB
| import pandas as pd | |
| import pytest | |
| from column_matching.schemas import ColumnSchema, DatasetSchema | |
| from column_matching.validator import ColumnValidator | |
| from column_matching.exceptions import ( | |
| MissingColumnsError, | |
| ExtraColumnsError, | |
| ColumnOrderMismatchError, | |
| ColumnTypeMismatchError, | |
| ) | |
| def test_validate_columns_success(): | |
| schema = DatasetSchema( | |
| name="test", | |
| strict_order=True, | |
| strict_types=False, | |
| columns=[ | |
| ColumnSchema("a"), | |
| ColumnSchema("b"), | |
| ], | |
| ) | |
| df = pd.DataFrame({"a": [1], "b": [2]}) | |
| validator = ColumnValidator(schema) | |
| validator.validate_columns(df) | |
| def test_missing_columns_error(): | |
| schema = DatasetSchema( | |
| name="test", | |
| columns=[ColumnSchema("a"), ColumnSchema("b")], | |
| strict_order=False, | |
| ) | |
| df = pd.DataFrame({"a": [1]}) | |
| validator = ColumnValidator(schema) | |
| with pytest.raises(MissingColumnsError): | |
| validator.validate_columns(df) | |
| def test_extra_columns_error(): | |
| schema = DatasetSchema( | |
| name="test", | |
| columns=[ColumnSchema("a")], | |
| strict_order=False, | |
| ) | |
| df = pd.DataFrame({"a": [1], "b": [2]}) | |
| validator = ColumnValidator(schema) | |
| with pytest.raises(ExtraColumnsError): | |
| validator.validate_columns(df) | |
| def test_order_mismatch_error(): | |
| schema = DatasetSchema( | |
| name="test", | |
| strict_order=True, | |
| columns=[ColumnSchema("a"), ColumnSchema("b")], | |
| ) | |
| df = pd.DataFrame({"b": [1], "a": [2]}) | |
| validator = ColumnValidator(schema) | |
| with pytest.raises(ColumnOrderMismatchError): | |
| validator.validate_columns(df) | |
| def test_validate_types_success(): | |
| schema = DatasetSchema( | |
| name="test", | |
| strict_order=False, | |
| strict_types=True, | |
| columns=[ | |
| ColumnSchema("a", dtype="int64"), | |
| ColumnSchema("b", dtype="float64"), | |
| ], | |
| ) | |
| df = pd.DataFrame({"a": [1, 2], "b": [1.0, 2.5]}) | |
| validator = ColumnValidator(schema) | |
| validator.validate_types(df) | |
| def test_validate_types_mismatch(): | |
| schema = DatasetSchema( | |
| name="test", | |
| strict_order=False, | |
| strict_types=True, | |
| columns=[ | |
| ColumnSchema("a", dtype="int64"), | |
| ], | |
| ) | |
| df = pd.DataFrame({"a": ["x", "y"]}) | |
| validator = ColumnValidator(schema) | |
| with pytest.raises(ColumnTypeMismatchError): | |
| validator.validate(df) | |