-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(plugins): Move jinja test code for arista.avd.contains to Py…
…AVD (#4131)
- Loading branch information
Showing
6 changed files
with
116 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright (c) 2023-2024 Arista Networks, Inc. | ||
# Use of this source code is governed by the Apache License 2.0 | ||
# that can be found in the LICENSE file. | ||
"""AVD Jinja2 test contains. | ||
The test checks if a list contains any of the value(s) passed in test_value. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
from jinja2.runtime import Undefined | ||
|
||
|
||
def contains(value: list[Any], test_value: Any | list[Any] = None) -> bool: | ||
"""The test checks if a list contains any of the value(s) passed in test_value. | ||
If 'value' is Undefined, None or not a list then the test has failed. | ||
Parameters | ||
---------- | ||
value : | ||
List to test | ||
test_value : single item or list of items | ||
Value(s) to test for in value | ||
Returns | ||
------- | ||
boolean | ||
True if variable matches criteria, False in other cases. | ||
""" | ||
# TODO - this will fail miserably if test_value is not hashable ! | ||
if isinstance(value, Undefined) or value is None or not isinstance(value, list): | ||
# Invalid value - return false | ||
return False | ||
if isinstance(test_value, Undefined) or test_value is None: | ||
# Invalid value - return false | ||
return False | ||
if isinstance(test_value, list) and not set(value).isdisjoint(test_value): | ||
# test_value is list so test if value and test_value has any common items | ||
return True | ||
return test_value in value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright (c) 2023-2024 Arista Networks, Inc. | ||
# Use of this source code is governed by the Apache License 2.0 | ||
# that can be found in the LICENSE file. | ||
"""Unit tests for pyavd.j2tests.contains.""" | ||
|
||
from __future__ import annotations | ||
|
||
import pytest | ||
from jinja2.runtime import Undefined | ||
from pyavd.j2tests.contains import contains | ||
|
||
TEST_DATA = [ | ||
pytest.param(None, "dummy", False, id="value is None"), | ||
pytest.param(Undefined, "dummy", False, id="value is Undefined"), | ||
pytest.param("value_not_a_list", "dummy", False, id="value is not a list"), | ||
pytest.param(["dummy"], None, False, id="test_value is None"), | ||
pytest.param(["dummy"], Undefined, False, id="test_value is Undefined"), | ||
pytest.param(["a", "b", "c"], "b", True, id="test_value single value in value"), | ||
pytest.param(["a", "b", "c"], ["d", "b"], True, id="test_value list contained value"), | ||
pytest.param([1, 42, 666], 42, True, id="test success with int"), | ||
pytest.param(["a", "b", "c"], "d", False, id="test_value list not contained value"), | ||
pytest.param(["a", "b", "c"], ["d", "e"], False, id="test_value single value not in value"), | ||
] | ||
|
||
|
||
class TestContainsTest: | ||
"""Test Contains.""" | ||
|
||
@pytest.mark.parametrize(("value, test_value, expected_result"), TEST_DATA) | ||
def test_contains(self, value, test_value, expected_result): | ||
"""Test the contains function.""" | ||
assert contains(value, test_value) == expected_result |