Debian Codegraph @

tests/units/result_manager/conftest.py

raw symbols SCIP
1 # Copyright (c) 2023-2026 Arista Networks, Inc.
2 # Use of this source code is governed by the Apache License 2.0
3 # that can be found in the LICENSE file.
4 """See https://docs.pytest.org/en/stable/reference/fixtures.html#conftest-py-sharing-fixtures-across-multiple-files."""
5
6 import json
7 from pathlib import Path
8 from typing import Protocol
9
10 import pytest
11
12 from anta.device import AntaDevice
13 from anta.result_manager import ResultManager
14 from anta.result_manager.models import AntaTestStatus, TestResult
15 from tests.units.test_models import FakeTestWithInput
16
17 TEST_RESULTS: Path = Path(__file__).parent.resolve() / "test_files" / "test_md_report_results.json"
18
19 FAKE_TEST = FakeTestWithInput
20
21
22 class TestResultFactoryProtocol(Protocol):
23 """https://mypy.readthedocs.io/en/stable/protocols.html#callback-protocols."""
24
25 # pylint: disable=R0903
26
27 def __call__( # noqa: D102
28 self, index: int, atomic_results_status: list[AntaTestStatus] | None = None, *, distinct_tests: bool = False, distinct_devices: bool = False
29 ) -> TestResult: ...
30
31
32 class ResultManagerFactoryProtocol(Protocol):
33 """https://mypy.readthedocs.io/en/stable/protocols.html#callback-protocols."""
34
35 # pylint: disable=R0903
36
37 def __call__( # noqa: D102
38 self, size: int, atomic_results_status: list[AntaTestStatus] | None = None, *, distinct_tests: bool = False, distinct_devices: bool = False
39 ) -> ResultManager: ...
40
41
42 @pytest.fixture(name="result_manager_factory")
43 def result_manager_factory_fixture(test_result_factory: TestResultFactoryProtocol) -> ResultManagerFactoryProtocol:
44 """Return a function that creates a ResultManager instance."""
45
46 def _create(
47 size: int = 0, atomic_results_status: list[AntaTestStatus] | None = None, *, distinct_tests: bool = False, distinct_devices: bool = False
48 ) -> ResultManager:
49 """ResultManager factory.
50
51 Parameters
52 ----------
53 size
54 Size of the ResultManager.
55 atomic_results_status
56 List of AntaTestStatus. For each an atomic result with this status is created for each TestResult instance.
57 distinct_tests
58 Whether or not to use the index in the test name.
59 distinct_devices
60 Whether or not to use the index in the device name.
61 """
62 result_manager = ResultManager()
63 result_manager.results = [
64 test_result_factory(i, atomic_results_status, distinct_tests=distinct_tests, distinct_devices=distinct_devices) for i in range(size)
65 ]
66 return result_manager
67
68 return _create
69
70
71 @pytest.fixture(name="result_manager")
72 def result_manager_fixture() -> ResultManager:
73 """Return a ResultManager with random tests loaded from a JSON file.
74
75 See `tests/units/result_manager/test_md_report_results.json` for details.
76 """
77 manager = ResultManager()
78
79 with TEST_RESULTS.open("r", encoding="utf-8") as f:
80 results = json.load(f)
81
82 for result in results:
83 manager.add(TestResult(**result))
84
85 return manager
86
87
88 @pytest.fixture(name="test_result_factory")
89 def test_result_factory_fixture(device: AntaDevice) -> TestResultFactoryProtocol:
90 """Return a function that creates a TestResult instance."""
91
92 def _create(
93 index: int = 0, atomic_results_status: list[AntaTestStatus] | None = None, *, distinct_tests: bool = False, distinct_devices: bool = False
94 ) -> TestResult:
95 """TestResult factory.
96
97 Parameters
98 ----------
99 index
100 Index of the TestResult instance, used to create distinct device and test names (if applicable) and a unique input for the test.
101 atomic_results_status
102 List of AntaTestStatus. For each an atomic result with this status is created for each TestResult instance.
103 distinct_tests
104 Whether or not to use the index in the test name.
105 distinct_devices
106 Whether or not to use the index in the device name.
107 """
108 test = FAKE_TEST(device=device, inputs={"string": f"Test instance {index}"})
109 res = TestResult(
110 name=device.name if not distinct_devices else f"{device.name}{index}",
111 test=test.name if not distinct_tests else f"{test.name}{index}",
112 categories=["test"],
113 description=test.description,
114 custom_field=None,
115 )
116 if atomic_results_status is not None:
117 for i, status in enumerate(atomic_results_status):
118 res.add(description=f"{test.name}{index}AtomicTestResult{i}", status=status, messages=[f"atomic {status} message"])
119 return res
120
121 return _create