From 1059f4d67ccb5e34b0f3112be488581ade8882ff Mon Sep 17 00:00:00 2001 From: Marvin Vogt Date: Tue, 5 Dec 2023 15:48:40 +0100 Subject: [PATCH] Implement `__eq__` for `FRRoutingPrefix` & `CabourotteHealthcheck` (#21) --- src/anycastd/healthcheck/_cabourotte/main.py | 6 +++++ src/anycastd/prefix/_frrouting/main.py | 6 +++++ tests/healthcheck/cabourotte/test_main.py | 22 +++++++++++++++++++ tests/prefix/frrouting/test_main.py | 23 ++++++++++++++++++++ 4 files changed, 57 insertions(+) diff --git a/src/anycastd/healthcheck/_cabourotte/main.py b/src/anycastd/healthcheck/_cabourotte/main.py index 499bacf..9254178 100644 --- a/src/anycastd/healthcheck/_cabourotte/main.py +++ b/src/anycastd/healthcheck/_cabourotte/main.py @@ -25,6 +25,12 @@ def __repr__(self) -> str: f"interval={self.interval!r})" ) + def __eq__(self, other: object) -> bool: + if not isinstance(other, CabourotteHealthcheck): + return NotImplemented + + return self.__dict__ == other.__dict__ + @property def interval(self) -> datetime.timedelta: return self.__interval diff --git a/src/anycastd/prefix/_frrouting/main.py b/src/anycastd/prefix/_frrouting/main.py index d06a40b..6bf10cc 100644 --- a/src/anycastd/prefix/_frrouting/main.py +++ b/src/anycastd/prefix/_frrouting/main.py @@ -47,6 +47,12 @@ def __repr__(self) -> str: f"vtysh={self.vtysh!r}, executor={self.executor!r})" ) + def __eq__(self, other: object) -> bool: + if not isinstance(other, FRRoutingPrefix): + return NotImplemented + + return self.__dict__ == other.__dict__ + @property def prefix(self) -> IPv4Network | IPv6Network: return self.__prefix diff --git a/tests/healthcheck/cabourotte/test_main.py b/tests/healthcheck/cabourotte/test_main.py index 7b5ba1b..e655948 100644 --- a/tests/healthcheck/cabourotte/test_main.py +++ b/tests/healthcheck/cabourotte/test_main.py @@ -55,6 +55,28 @@ def test_repr(attributes: dict): ) +def test_equal(): + """Two healthchecks with the same attributes are equal.""" + healthcheck1 = CabourotteHealthcheck( + "test", url="https://example.com", interval=datetime.timedelta(seconds=10) + ) + healthcheck2 = CabourotteHealthcheck( + "test", url="https://example.com", interval=datetime.timedelta(seconds=10) + ) + assert healthcheck1 == healthcheck2 + + +def test_non_equal(): + """Two healthchecks with different attributes are not equal.""" + healthcheck1 = CabourotteHealthcheck( + "test", url="https://example.com", interval=datetime.timedelta(seconds=10) + ) + healthcheck2 = CabourotteHealthcheck( + "test", url="https://example.com", interval=datetime.timedelta(seconds=20) + ) + assert healthcheck1 != healthcheck2 + + @pytest.mark.asyncio async def test__check_awaits_get_result(mocker: MockerFixture): """The check method awaits the result of get_result.""" diff --git a/tests/prefix/frrouting/test_main.py b/tests/prefix/frrouting/test_main.py index f847b69..cf31cc2 100644 --- a/tests/prefix/frrouting/test_main.py +++ b/tests/prefix/frrouting/test_main.py @@ -1,3 +1,4 @@ +from ipaddress import IPv6Network from pathlib import Path from anycastd._executor import LocalExecutor @@ -18,3 +19,25 @@ def test_repr(example_networks, example_vrfs): f"FRRoutingPrefix(prefix={example_networks!r}, vrf={example_vrfs!r}, " f"vtysh={vtysh!r}, executor={executor!r})" ) + + +def test_equal(): + """Two prefixes with the same attributes are equal.""" + prefix1 = FRRoutingPrefix( + prefix=IPv6Network("2001:db8::/32"), vrf="42", executor=LocalExecutor() + ) + prefix2 = FRRoutingPrefix( + prefix=IPv6Network("2001:db8::/32"), vrf="42", executor=LocalExecutor() + ) + assert prefix1 == prefix2 + + +def test_non_equal(): + """Two prefixes with different attributes are not equal.""" + prefix1 = FRRoutingPrefix( + prefix=IPv6Network("2001:db8::/32"), vrf="42", executor=LocalExecutor() + ) + prefix2 = FRRoutingPrefix( + prefix=IPv6Network("2001:db8::/32"), vrf="43", executor=LocalExecutor() + ) + assert prefix1 != prefix2