Spaces:
Runtime error
Runtime error
Download tests/test_config.py from reforse/table-comparator: direct link, hf CLI and curl.
- Browser
- Download file 6.11 kB
-
https://huggingface.co/spaces/reforse/table-comparator/resolve/main/tests/test_config.py
- Command line
-
hf download hf://spaces/reforse/table-comparator/tests/test_config.py
-
curl -L -o test_config.py https://huggingface.co/spaces/reforse/table-comparator/resolve/main/tests/test_config.py
6.11 kB
| """Tests for config.py – validation and JSON round-trips.""" | |
| from table_comparator.config import ColumnMapping, ComparisonConfig | |
| # --------------------------------------------------------------------------- | |
| # ColumnMapping validation | |
| # --------------------------------------------------------------------------- | |
| class TestColumnMappingValidation: | |
| def test_valid_text_mapping(self): | |
| m = ColumnMapping(column_a="A", column_b="B", col_type="text") | |
| assert m.validate() == [] | |
| def test_valid_text_case_insensitive(self): | |
| m = ColumnMapping(column_a="A", column_b="B", col_type="text", case_sensitive=False) | |
| assert m.validate() == [] | |
| def test_valid_number_with_rounding(self): | |
| m = ColumnMapping(column_a="A", column_b="B", col_type="number", rounding=2) | |
| assert m.validate() == [] | |
| def test_valid_number_without_rounding(self): | |
| m = ColumnMapping(column_a="A", column_b="B", col_type="number") | |
| assert m.validate() == [] | |
| def test_valid_number_rounding_zero(self): | |
| m = ColumnMapping(column_a="A", column_b="B", col_type="number", rounding=0) | |
| assert m.validate() == [] | |
| def test_valid_date_mapping(self): | |
| m = ColumnMapping( | |
| column_a="A", column_b="B", col_type="date", | |
| date_format_a="%Y-%m-%d", date_format_b="%d/%m/%Y", | |
| ) | |
| assert m.validate() == [] | |
| # --- date: missing formats ------------------------------------------- | |
| def test_date_missing_format_a(self): | |
| m = ColumnMapping( | |
| column_a="A", column_b="B", col_type="date", | |
| date_format_b="%Y-%m-%d", | |
| ) | |
| errors = m.validate() | |
| assert len(errors) == 1 | |
| assert "date_format_a" in errors[0] | |
| def test_date_missing_format_b(self): | |
| m = ColumnMapping( | |
| column_a="A", column_b="B", col_type="date", | |
| date_format_a="%Y-%m-%d", | |
| ) | |
| errors = m.validate() | |
| assert len(errors) == 1 | |
| assert "date_format_b" in errors[0] | |
| def test_date_missing_both_formats(self): | |
| m = ColumnMapping(column_a="A", column_b="B", col_type="date") | |
| errors = m.validate() | |
| assert len(errors) == 2 | |
| # --- number: bad rounding -------------------------------------------- | |
| def test_negative_rounding_rejected(self): | |
| m = ColumnMapping(column_a="A", column_b="B", col_type="number", rounding=-1) | |
| errors = m.validate() | |
| assert len(errors) == 1 | |
| assert "rounding" in errors[0].lower() | |
| # --- unknown type ---------------------------------------------------- | |
| def test_invalid_col_type(self): | |
| m = ColumnMapping(column_a="A", column_b="B", col_type="boolean") | |
| errors = m.validate() | |
| assert len(errors) == 1 | |
| assert "boolean" in errors[0] | |
| # --------------------------------------------------------------------------- | |
| # ComparisonConfig validation | |
| # --------------------------------------------------------------------------- | |
| class TestComparisonConfigValidation: | |
| def test_empty_columns_list_is_invalid(self): | |
| cfg = ComparisonConfig(columns=[]) | |
| errors = cfg.validate() | |
| assert any("at least one" in e.lower() for e in errors) | |
| def test_column_errors_propagate(self): | |
| cfg = ComparisonConfig(columns=[ | |
| ColumnMapping(column_a="A", column_b="B", col_type="date"), # both formats missing | |
| ]) | |
| errors = cfg.validate() | |
| assert len(errors) == 2 | |
| def test_valid_config_no_errors(self): | |
| cfg = ComparisonConfig(columns=[ | |
| ColumnMapping(column_a="A", column_b="B", col_type="text"), | |
| ]) | |
| assert cfg.validate() == [] | |
| # --------------------------------------------------------------------------- | |
| # Serialisation round-trips | |
| # --------------------------------------------------------------------------- | |
| class TestSerialization: | |
| def test_column_mapping_round_trip(self): | |
| original = ColumnMapping( | |
| column_a="Price", column_b="Value", | |
| col_type="number", rounding=3, | |
| ) | |
| restored = ColumnMapping.from_dict(original.to_dict()) | |
| assert restored == original | |
| def test_date_mapping_round_trip(self): | |
| original = ColumnMapping( | |
| column_a="Created", column_b="Date", | |
| col_type="date", | |
| date_format_a="%d.%m.%Y", date_format_b="%Y-%m-%d", | |
| ) | |
| restored = ColumnMapping.from_dict(original.to_dict()) | |
| assert restored == original | |
| def test_config_round_trip(self): | |
| original = ComparisonConfig( | |
| table_a_path="a.csv", | |
| table_b_path="b.csv", | |
| columns=[ | |
| ColumnMapping(column_a="Name", column_b="Customer", col_type="text"), | |
| ColumnMapping( | |
| column_a="Date", column_b="Date", col_type="date", | |
| date_format_a="%Y-%m-%d", date_format_b="%d/%m/%Y", | |
| ), | |
| ColumnMapping(column_a="Amt", column_b="Val", col_type="number", rounding=0), | |
| ], | |
| ) | |
| restored = ComparisonConfig.from_dict(original.to_dict()) | |
| assert restored == original | |
| def test_save_and_load(self, tmp_path): | |
| cfg = ComparisonConfig( | |
| table_a_path="a.csv", | |
| table_b_path="b.csv", | |
| columns=[ColumnMapping(column_a="X", column_b="Y", col_type="text")], | |
| ) | |
| path = str(tmp_path / "cfg.json") | |
| cfg.save(path) | |
| loaded = ComparisonConfig.load(path) | |
| assert loaded == cfg | |
| def test_from_dict_ignores_unknown_keys(self): | |
| data = { | |
| "column_a": "A", "column_b": "B", | |
| "col_type": "text", "unknown_key": 42, | |
| } | |
| m = ColumnMapping.from_dict(data) | |
| assert m.column_a == "A" | |
| assert not hasattr(m, "unknown_key") | |
| def test_from_dict_fills_defaults(self): | |
| data = {"column_a": "A", "column_b": "B"} | |
| m = ColumnMapping.from_dict(data) | |
| assert m.col_type == "text" | |
| assert m.rounding is None | |
| assert m.case_sensitive is True | |