Skip to content

Commit

Permalink
Add kwarg for model subset testing
Browse files Browse the repository at this point in the history
Since we only have a limited set of models that we are testing
subsets of keys for, we should explicitly only allow subsets when
testing those getters.
  • Loading branch information
bewing committed Jan 23, 2025
1 parent c02a749 commit 053e899
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 6 additions & 2 deletions napalm/base/test/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ def test_get_ntp_peers(self, test_case):

for peer, peer_details in get_ntp_peers.items():
assert isinstance(peer, str)
assert helpers.test_model(models.NTPPeerDict, peer_details)
assert helpers.test_model(
models.NTPPeerDict, peer_details, allow_subset=True
)

return get_ntp_peers

Expand All @@ -323,7 +325,9 @@ def test_get_ntp_servers(self, test_case):

for server, server_details in get_ntp_servers.items():
assert isinstance(server, str)
assert helpers.test_model(models.NTPServerDict, server_details)
assert helpers.test_model(
models.NTPServerDict, server_details, allow_subset=True
)

return get_ntp_servers

Expand Down
12 changes: 7 additions & 5 deletions napalm/base/test/helpers.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""Several methods to help with the tests."""


def test_model(model, data):
def test_model(model, data, allow_subset=False):
"""Return if the dictionary `data` complies with the `model`."""
# Access the underlying schema for a TypedDict directly
annotations = model.__annotations__
if model.__total__:
same_keys = set(annotations.keys()) == set(data.keys())
else:
if allow_subset:
same_keys = set(data.keys()) <= set(annotations.keys())
source = data
else:
same_keys = set(annotations.keys()) == set(data.keys())
source = annotations

if not same_keys:
print(
Expand All @@ -18,7 +20,7 @@ def test_model(model, data):
)

correct_class = True
for key in data.keys():
for key in source.keys():
correct_class = isinstance(data[key], annotations[key]) and correct_class
if not correct_class:
print(
Expand Down

0 comments on commit 053e899

Please sign in to comment.