|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +import great_expectations.expectations as gxe |
| 4 | +from great_expectations.expectations.expectation_configuration import ( |
| 5 | + ExpectationConfiguration, |
| 6 | +) |
| 7 | + |
| 8 | + |
| 9 | +def test_expectation_is_registered_and_constructible(): |
| 10 | + expectation = gxe.ExpectColumnTypeToBe(column="col", type_="int64") |
| 11 | + assert expectation.column == "col" |
| 12 | + assert expectation.type_ == "int64" |
| 13 | + |
| 14 | + |
| 15 | +def test_validate_pandas_success(): |
| 16 | + expectation = gxe.ExpectColumnTypeToBe(column="col", type_="int64") |
| 17 | + result = expectation._validate_pandas( |
| 18 | + actual_column_type=np.dtype("int64"), expected_type="int64" |
| 19 | + ) |
| 20 | + assert result == {"success": True, "result": {"observed_value": "int64"}} |
| 21 | + |
| 22 | + |
| 23 | +def test_validate_pandas_failure_returns_schema_level_result(): |
| 24 | + expectation = gxe.ExpectColumnTypeToBe(column="col", type_="int64") |
| 25 | + result = expectation._validate_pandas( |
| 26 | + actual_column_type=np.dtype("object"), expected_type="int64" |
| 27 | + ) |
| 28 | + # Schema-level result shape (observed_value only) is preserved on failure, |
| 29 | + # with no row-level fields. |
| 30 | + assert result == {"success": False, "result": {"observed_value": "object_"}} |
| 31 | + |
| 32 | + |
| 33 | +def test_validate_pandas_object_column_is_schema_level(): |
| 34 | + # The core behavior: a pandas 'object' column reports its declared dtype and |
| 35 | + # is not checked row-by-row (unlike ExpectColumnValuesToBeOfType). |
| 36 | + expectation = gxe.ExpectColumnTypeToBe(column="col", type_="object") |
| 37 | + result = expectation._validate_pandas( |
| 38 | + actual_column_type=np.dtype("object"), expected_type="object" |
| 39 | + ) |
| 40 | + assert result == {"success": True, "result": {"observed_value": "object_"}} |
| 41 | + |
| 42 | + |
| 43 | +def test_prescriptive_renderer_template(): |
| 44 | + configuration = ExpectationConfiguration( |
| 45 | + type="expect_column_type_to_be", |
| 46 | + kwargs={"column": "col", "type_": "int64"}, |
| 47 | + ) |
| 48 | + rendered = gxe.ExpectColumnTypeToBe._prescriptive_renderer(configuration=configuration) |
| 49 | + string_template = rendered[0].string_template |
| 50 | + assert string_template["template"] == "$column type must be $type_." |
| 51 | + assert string_template["params"]["column"] == "col" |
| 52 | + assert string_template["params"]["type_"] == "int64" |
0 commit comments