Skip to content

Commit

Permalink
[MAINTENANCE] Update Expectation equality to simplify meta and note…
Browse files Browse the repository at this point in the history
  • Loading branch information
cdkini authored Sep 5, 2024
1 parent d67eafc commit e8c484f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
17 changes: 17 additions & 0 deletions great_expectations/expectations/expectation.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,23 @@ def schema_extra(schema: Dict[str, Any], model: Type[Expectation]) -> None:
default=None
)

@override
def __eq__(self, other: object) -> bool:
if not isinstance(other, Expectation):
return False

self_dict = self.dict()
other_dict = other.dict()

# Simplify notes and meta equality - falsiness is equivalent
for attr in ("notes", "meta"):
self_val = self_dict.pop(attr, None) or None
other_val = other_dict.pop(attr, None) or None
if self_val != other_val:
return False

return self_dict == other_dict

@pydantic.validator("result_format")
def _validate_result_format(cls, result_format: ResultFormat | dict) -> ResultFormat | dict:
if isinstance(result_format, dict) and "result_format" not in result_format:
Expand Down
63 changes: 63 additions & 0 deletions tests/expectations/test_expectation.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,66 @@ def test_expectation_with_duplicate_suite_parameters(self):
max_value={"$PARAMETER": self.SUITE_PARAMETER_VALUE},
)
assert expectation.suite_parameter_options == (self.SUITE_PARAMETER_VALUE,)


@pytest.mark.unit
@pytest.mark.parametrize(
"column_a,column_b,expected",
[
pytest.param("foo", "foo", True, id="equivalent_columns"),
pytest.param("foo", "bar", False, id="different_columns"),
],
)
def test_expectation_equality(column_a: str, column_b: str, expected: bool):
expectation_a = gxe.ExpectColumnValuesToBeBetween(column=column_a, min_value=0, max_value=10)
expectation_b = gxe.ExpectColumnValuesToBeBetween(column=column_b, min_value=0, max_value=10)

assert (expectation_a == expectation_b) is expected


@pytest.mark.unit
@pytest.mark.parametrize(
"notes_a,notes_b,expected",
[
pytest.param(None, None, True, id="both_none"),
pytest.param([], None, True, id="both_falsy"),
pytest.param("my_notes", None, False, id="missing_notes"),
pytest.param("my_notes", "my_other_notes", False, id="different_notes"),
pytest.param("my_notes", "my_notes", True, id="equivalent_notes"),
],
)
def test_expectation_equality_with_notes(
notes_a: str | list[str] | None, notes_b: str | list[str] | None, expected: bool
):
expectation_a = gxe.ExpectColumnValuesToBeBetween(
column="foo", min_value=0, max_value=10, notes=notes_a
)
expectation_b = gxe.ExpectColumnValuesToBeBetween(
column="foo", min_value=0, max_value=10, notes=notes_b
)

assert (expectation_a == expectation_b) is expected


@pytest.mark.unit
@pytest.mark.parametrize(
"meta_a,meta_b,expected",
[
pytest.param(None, None, True, id="both_none"),
pytest.param({}, None, True, id="both_falsy"),
pytest.param({"author": "Bob Dylan"}, None, False, id="missing_meta"),
pytest.param(
{"author": "Bob Dylan"}, {"author": "John Lennon"}, False, id="different_meta"
),
pytest.param({"author": "Bob Dylan"}, {"author": "Bob Dylan"}, True, id="equivalent_meta"),
],
)
def test_expectation_equality_with_meta(meta_a: dict | None, meta_b: dict | None, expected: bool):
expectation_a = gxe.ExpectColumnValuesToBeBetween(
column="foo", min_value=0, max_value=10, meta=meta_a
)
expectation_b = gxe.ExpectColumnValuesToBeBetween(
column="foo", min_value=0, max_value=10, meta=meta_b
)

assert (expectation_a == expectation_b) is expected

0 comments on commit e8c484f

Please sign in to comment.