Skip to content

Commit

Permalink
Implement __eq__ for FRRoutingPrefix & CabourotteHealthcheck (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
SRv6d authored Dec 5, 2023
1 parent 51e96a4 commit 1059f4d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/anycastd/healthcheck/_cabourotte/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/anycastd/prefix/_frrouting/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/healthcheck/cabourotte/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
23 changes: 23 additions & 0 deletions tests/prefix/frrouting/test_main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ipaddress import IPv6Network
from pathlib import Path

from anycastd._executor import LocalExecutor
Expand All @@ -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

0 comments on commit 1059f4d

Please sign in to comment.