Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/reusable-precommit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ jobs:
version=$(echo ${{ matrix.python_version }} | sed s/\\.//)
bash <(curl -s https://codecov.io/bash) -t ${{ secrets.PYBATFISH_CODECOV_TOKEN }} -F unit${version}
strategy:
fail-fast: false
matrix:
python_version:
- "3.11"
- "3.12"
- "3.13"
- "3.14"
- "3.15"
integration_tests:
runs-on: ubuntu-latest
needs:
Expand Down Expand Up @@ -149,12 +151,14 @@ jobs:
version=$(echo ${{ matrix.python_version }} | sed s/\\.//)
bash <(curl -s https://codecov.io/bash) -t ${{ secrets.PYBATFISH_CODECOV_TOKEN }} -F integration${version}
strategy:
fail-fast: false
matrix:
python_version:
- "3.11"
- "3.12"
- "3.13"
- "3.14"
- "3.15"
doc_tests:
runs-on: ubuntu-latest
needs:
Expand All @@ -167,7 +171,7 @@ jobs:
java-version: "25"
- uses: actions/setup-python@v7
with:
python-version: "3.14"
python-version: "3.15"
- name: Download bf JAR
uses: actions/download-artifact@v8
with:
Expand Down
27 changes: 18 additions & 9 deletions pybatfish/client/asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any

from deepdiff import DeepDiff
from pandas import DataFrame

from pybatfish.datamodel import PathConstraints
Expand Down Expand Up @@ -125,16 +124,26 @@ def _get_duplicate_router_ids(
return df[df.duplicated(["Router_ID"], keep=False)].sort_values(["Router_ID"])


def _is_value_match(actual: Any, expected: Any) -> bool:
if actual is expected:
return True
if type(actual) is not type(expected):
return False
if isinstance(actual, dict):
return actual.keys() == expected.keys() and all(
_is_value_match(value, expected[key]) for key, value in actual.items()
)
if isinstance(actual, (list, tuple, set, frozenset)):
# Collection order and duplicate counts do not affect matching.
return all(any(_is_value_match(a, e) for e in expected) for a in actual) and all(
any(_is_value_match(a, e) for a in actual) for e in expected
)
return bool(actual == expected)


def _is_dict_match(actual: dict[str, Any], expected: dict[str, Any]) -> bool:
"""Matches two dictionaries. `expected` can be a subset of `actual`."""
diff = DeepDiff(
_subdict(actual, expected.keys()),
expected,
ignore_order=True,
verbose_level=1,
view="text",
)
return not diff
return _is_value_match(_subdict(actual, expected.keys()), expected)


def _raise_common(err_text, soft=False):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ classifiers = [
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Programming Language :: Python :: 3.14",
"Programming Language :: Python :: 3.15",
]
keywords = ["network", "configuration", "verification"]
dependencies = [
"attrs>=18.1.0",
"deepdiff",
"pandas",
"python-dateutil",
"PyYAML",
Expand Down
48 changes: 48 additions & 0 deletions tests/client/test_assert.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
from pybatfish.client.session import Session
from pybatfish.datamodel import HeaderConstraints, PathConstraints
from pybatfish.datamodel.answer import TableAnswer
from pybatfish.datamodel.primitives import ListWrapper
from pybatfish.datamodel.route import NextHopIp
from pybatfish.exception import (
BatfishAssertException,
BatfishAssertWarning,
Expand Down Expand Up @@ -626,6 +628,52 @@ def test_is_dict_match():
# wrong value
assert not _is_dict_match({"k1": "v1", "k2": "v2"}, {"k1": "v3"})

assert _is_dict_match({}, {"k1": None})


@pytest.mark.parametrize(
"actual,expected,matched",
[
([1, 2], [2, 1], True),
([1, 1], [1], True),
([1], [1, 1], True),
([1, 2], [1, 3], False),
([], [], True),
([], [1], False),
([1], [], False),
((1, 2), (2, 1), True),
([1, 2], (1, 2), False),
({1, 2}, {2, 1}, True),
(frozenset({1, 2}), frozenset({2, 1}), True),
({1}, {1.0}, False),
({"a": [1, 2], "b": 3}, {"b": 3, "a": [2, 1]}, True),
([{"a": [1, 2]}, {"b": 3}], [{"b": 3}, {"a": [2, 1]}], True),
({"a": 1, "b": 2}, {"a": 1}, False),
(1, 1.0, False),
(True, 1, False),
(float("nan"), float("nan"), False),
(ListWrapper([1, 2]), ListWrapper([2, 1]), True),
(ListWrapper([1, 2]), [2, 1], False),
(NextHopIp("1.1.1.1"), NextHopIp("1.1.1.1"), True),
(NextHopIp("1.1.1.1"), NextHopIp("2.2.2.2"), False),
],
)
def test_is_dict_match_values(actual, expected, matched):
assert _is_dict_match({"field": actual}, {"field": expected}) is matched


def test_has_route_unordered_communities():
routes = DataFrame(
{
"Node": ["n1"],
"VRF": ["default"],
"Network": ["192.0.2.0/24"],
"Communities": [["1:1", "1:2"]],
}
)
assert_has_route(routes, {"Communities": ["1:2", "1:1"]}, "n1")
assert_has_no_route(routes, {"Communities": ["1:3"]}, "n1")


def test_has_route_unsupported_type_routes():
with pytest.raises(TypeError) as excinfo:
Expand Down
Loading