Debian Codegraph @

tests/units/result_manager/test_models.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 """ANTA Result Manager models unit tests."""
5
6 from __future__ import annotations
7
8 from typing import TYPE_CHECKING
9
10 import pytest
11
12 from anta.result_manager.models import AntaTestStatus
13 from tests.units.conftest import DEVICE_NAME
14 from tests.units.result_manager.conftest import FAKE_TEST
15
16 if TYPE_CHECKING:
17 from _pytest.mark.structures import ParameterSet
18
19 # Import as Result to avoid pytest collection
20 from .conftest import TestResultFactoryProtocol
21
22 TEST_RESULTS: list[ParameterSet] = [
23 pytest.param(AntaTestStatus.SUCCESS, [], f"Test {FAKE_TEST.name} (on {DEVICE_NAME}): success\nMessages:\nsuccess message", id="success"),
24 pytest.param(
25 AntaTestStatus.SUCCESS,
26 [AntaTestStatus.SUCCESS, AntaTestStatus.SUCCESS],
27 (
28 f"Test {FAKE_TEST.name} (on {DEVICE_NAME}): success [success,success]\nMessages:\n"
29 f"FakeTestWithInput1AtomicTestResult0 - atomic success message\n"
30 f"FakeTestWithInput1AtomicTestResult1 - atomic success message"
31 ),
32 id="success-atomic",
33 ),
34 pytest.param(AntaTestStatus.FAILURE, [], f"Test {FAKE_TEST.name} (on {DEVICE_NAME}): failure\nMessages:\nfailure message", id="failure"),
35 pytest.param(
36 AntaTestStatus.FAILURE,
37 [AntaTestStatus.SUCCESS, AntaTestStatus.FAILURE],
38 (
39 f"Test {FAKE_TEST.name} (on {DEVICE_NAME}): failure [success,failure]\nMessages:\n"
40 f"FakeTestWithInput1AtomicTestResult0 - atomic success message\n"
41 f"FakeTestWithInput1AtomicTestResult1 - atomic failure message"
42 ),
43 id="failure-atomic",
44 ),
45 pytest.param(AntaTestStatus.SKIPPED, [], f"Test {FAKE_TEST.name} (on {DEVICE_NAME}): skipped\nMessages:\nskipped message", id="skipped"),
46 pytest.param(
47 AntaTestStatus.UNSET,
48 [AntaTestStatus.SKIPPED, AntaTestStatus.SKIPPED],
49 (
50 f"Test {FAKE_TEST.name} (on {DEVICE_NAME}): unset [skipped,skipped]\nMessages:\n"
51 f"FakeTestWithInput1AtomicTestResult0 - atomic skipped message\n"
52 f"FakeTestWithInput1AtomicTestResult1 - atomic skipped message"
53 ),
54 id="skipped-atomic",
55 ),
56 pytest.param(
57 AntaTestStatus.SUCCESS,
58 [AntaTestStatus.SKIPPED, AntaTestStatus.SUCCESS],
59 (
60 f"Test {FAKE_TEST.name} (on {DEVICE_NAME}): success [skipped,success]\nMessages:\n"
61 f"FakeTestWithInput1AtomicTestResult0 - atomic skipped message\n"
62 f"FakeTestWithInput1AtomicTestResult1 - atomic success message"
63 ),
64 id="skipped-success-atomic",
65 ),
66 pytest.param(AntaTestStatus.ERROR, [], f"Test {FAKE_TEST.name} (on {DEVICE_NAME}): error\nMessages:\nerror message", id="error"),
67 pytest.param(
68 AntaTestStatus.ERROR,
69 [AntaTestStatus.SUCCESS, AntaTestStatus.ERROR],
70 (
71 f"Test {FAKE_TEST.name} (on {DEVICE_NAME}): error [success,error]\nMessages:\n"
72 f"FakeTestWithInput1AtomicTestResult0 - atomic success message\n"
73 f"FakeTestWithInput1AtomicTestResult1 - atomic error message"
74 ),
75 id="error-atomic",
76 ),
77 # TODO: failure + error atomic
78 ]
79
80
81 class TestTestResult:
82 """Test TestResult."""
83
84 @pytest.mark.parametrize(("status", "atomic", "expected"), TEST_RESULTS)
85 def test_is_status_foo(self, test_result_factory: TestResultFactoryProtocol, status: AntaTestStatus, atomic: list[AntaTestStatus], expected: str) -> None:
86 """Test TestResult.is_foo methods."""
87 result = test_result_factory(1, atomic)
88 if len(atomic) == 0:
89 result._set_status(status, message=f"{status} message")
90
91 assert result.result == status
92
93 if atomic:
94 assert len(result.messages) == len(atomic)
95 else:
96 assert len(result.messages) == 1
97
98 @pytest.mark.parametrize(("status", "atomic", "expected"), TEST_RESULTS)
99 def test____str__(self, test_result_factory: TestResultFactoryProtocol, status: AntaTestStatus, atomic: list[AntaTestStatus], expected: str) -> None:
100 """Test TestResult.__str__()."""
101 result = test_result_factory(1, atomic)
102 if len(atomic) == 0:
103 result._set_status(status, message=f"{status} message")
104 assert str(result) == expected
105
106 def test_add(self, test_result_factory: TestResultFactoryProtocol) -> None:
107 """Test TestResult.add."""
108 result = test_result_factory(1, None)
109 assert len(result.atomic_results) == 0
110 assert len(result.messages) == 0
111
112 # Add one atomic result with default status
113 result.add("Atomic result default status")
114 assert len(result.atomic_results) == 1
115 assert result.atomic_results[0].result == AntaTestStatus.UNSET
116 assert result.result == AntaTestStatus.UNSET
117 assert result.atomic_results[0].description == "Atomic result default status"
118 assert len(result.messages) == 0
119
120 # Add one atomic result with status success
121 result.add("Atomic result status==success", status=AntaTestStatus.SUCCESS)
122 assert len(result.atomic_results) == 2
123 assert result.atomic_results[1].result == AntaTestStatus.SUCCESS
124 assert result.result == AntaTestStatus.SUCCESS # TODO: but should it be really given that [0] is unset
125 assert result.atomic_results[1].description == "Atomic result status==success"
126 assert len(result.messages) == 0
127
128 # Add one atomic result with status failure
129 result.add("Atomic result status==failure", status=AntaTestStatus.FAILURE)
130 assert len(result.atomic_results) == 3
131 assert result.atomic_results[2].result == AntaTestStatus.FAILURE
132 assert result.result == AntaTestStatus.FAILURE
133 assert result.atomic_results[2].description == "Atomic result status==failure"
134 assert len(result.messages) == 0
135
136 # Add one atomic result with multiple messages
137 result.add("Multiple messages", status=AntaTestStatus.FAILURE, messages=["message 1", "message 2"])
138 assert len(result.atomic_results) == 4
139 assert result.atomic_results[3].result == AntaTestStatus.FAILURE
140 assert result.result == AntaTestStatus.FAILURE
141 assert result.atomic_results[3].description == "Multiple messages"
142 assert len(result.messages) == 2