summaryrefslogtreecommitdiffstats
path: root/tests/units/reporter/test__init__.py
blob: 259942f9caf246828ddf76f2a93f6ff2c889a116 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 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.
"""
Test anta.report.__init__.py
"""
from __future__ import annotations

from typing import Callable

import pytest
from rich.table import Table

from anta import RICH_COLOR_PALETTE
from anta.custom_types import TestStatus
from anta.reporter import ReportTable
from anta.result_manager import ResultManager


class Test_ReportTable:
    """
    Test ReportTable class
    """

    # not testing __init__ as nothing is going on there

    @pytest.mark.parametrize(
        "usr_list, delimiter, expected_output",
        [
            pytest.param([], None, "", id="empty list no delimiter"),
            pytest.param([], "*", "", id="empty list with delimiter"),
            pytest.param(["elem1"], None, "elem1", id="one elem list no delimiter"),
            pytest.param(["elem1"], "*", "* elem1", id="one elem list with delimiter"),
            pytest.param(["elem1", "elem2"], None, "elem1\nelem2", id="two elems list no delimiter"),
            pytest.param(["elem1", "elem2"], "&", "& elem1\n& elem2", id="two elems list with delimiter"),
        ],
    )
    def test__split_list_to_txt_list(self, usr_list: list[str], delimiter: str | None, expected_output: str) -> None:
        """
        test _split_list_to_txt_list
        """
        # pylint: disable=protected-access
        report = ReportTable()
        assert report._split_list_to_txt_list(usr_list, delimiter) == expected_output

    @pytest.mark.parametrize(
        "headers",
        [
            pytest.param([], id="empty list"),
            pytest.param(["elem1"], id="one elem list"),
            pytest.param(["elem1", "elem2"], id="two elemst"),
        ],
    )
    def test__build_headers(self, headers: list[str]) -> None:
        """
        test _build_headers
        """
        # pylint: disable=protected-access
        report = ReportTable()
        table = Table()
        table_column_before = len(table.columns)
        report._build_headers(headers, table)
        assert len(table.columns) == table_column_before + len(headers)
        if len(table.columns) > 0:
            assert table.columns[table_column_before].style == RICH_COLOR_PALETTE.HEADER

    @pytest.mark.parametrize(
        "status, expected_status",
        [
            pytest.param("unknown", "unknown", id="unknown status"),
            pytest.param("unset", "[grey74]unset", id="unset status"),
            pytest.param("skipped", "[bold orange4]skipped", id="skipped status"),
            pytest.param("failure", "[bold red]failure", id="failure status"),
            pytest.param("error", "[indian_red]error", id="error status"),
            pytest.param("success", "[green4]success", id="success status"),
        ],
    )
    def test__color_result(self, status: TestStatus, expected_status: str) -> None:
        """
        test _build_headers
        """
        # pylint: disable=protected-access
        report = ReportTable()
        assert report._color_result(status) == expected_status

    @pytest.mark.parametrize(
        "host, testcase, title, number_of_tests, expected_length",
        [
            pytest.param(None, None, None, 5, 5, id="all results"),
            pytest.param("host1", None, None, 5, 0, id="result for host1 when no host1 test"),
            pytest.param(None, "VerifyTest3", None, 5, 1, id="result for test VerifyTest3"),
            pytest.param(None, None, "Custom title", 5, 5, id="Change table title"),
        ],
    )
    def test_report_all(
        self,
        result_manager_factory: Callable[[int], ResultManager],
        host: str | None,
        testcase: str | None,
        title: str | None,
        number_of_tests: int,
        expected_length: int,
    ) -> None:
        """
        test report_all
        """
        # pylint: disable=too-many-arguments
        rm = result_manager_factory(number_of_tests)

        report = ReportTable()
        kwargs = {"host": host, "testcase": testcase, "title": title}
        kwargs = {k: v for k, v in kwargs.items() if v is not None}
        res = report.report_all(rm, **kwargs)  # type: ignore[arg-type]

        assert isinstance(res, Table)
        assert res.title == (title or "All tests results")
        assert res.row_count == expected_length

    @pytest.mark.parametrize(
        "testcase, title, number_of_tests, expected_length",
        [
            pytest.param(None, None, 5, 5, id="all results"),
            pytest.param("VerifyTest3", None, 5, 1, id="result for test VerifyTest3"),
            pytest.param(None, "Custom title", 5, 5, id="Change table title"),
        ],
    )
    def test_report_summary_tests(
        self,
        result_manager_factory: Callable[[int], ResultManager],
        testcase: str | None,
        title: str | None,
        number_of_tests: int,
        expected_length: int,
    ) -> None:
        """
        test report_summary_tests
        """
        # pylint: disable=too-many-arguments
        # TODO refactor this later... this is injecting double test results by modyfing the device name
        # should be a fixture
        rm = result_manager_factory(number_of_tests)
        new_results = [result.model_copy() for result in rm.get_results()]
        for result in new_results:
            result.name = "test_device"
            result.result = "failure"
        rm.add_test_results(new_results)

        report = ReportTable()
        kwargs = {"testcase": testcase, "title": title}
        kwargs = {k: v for k, v in kwargs.items() if v is not None}
        res = report.report_summary_tests(rm, **kwargs)  # type: ignore[arg-type]

        assert isinstance(res, Table)
        assert res.title == (title or "Summary per test case")
        assert res.row_count == expected_length

    @pytest.mark.parametrize(
        "host, title, number_of_tests, expected_length",
        [
            pytest.param(None, None, 5, 2, id="all results"),
            pytest.param("host1", None, 5, 1, id="result for host host1"),
            pytest.param(None, "Custom title", 5, 2, id="Change table title"),
        ],
    )
    def test_report_summary_hosts(
        self,
        result_manager_factory: Callable[[int], ResultManager],
        host: str | None,
        title: str | None,
        number_of_tests: int,
        expected_length: int,
    ) -> None:
        """
        test report_summary_hosts
        """
        # pylint: disable=too-many-arguments
        # TODO refactor this later... this is injecting double test results by modyfing the device name
        # should be a fixture
        rm = result_manager_factory(number_of_tests)
        new_results = [result.model_copy() for result in rm.get_results()]
        for result in new_results:
            result.name = host or "test_device"
            result.result = "failure"
        rm.add_test_results(new_results)

        report = ReportTable()
        kwargs = {"host": host, "title": title}
        kwargs = {k: v for k, v in kwargs.items() if v is not None}
        res = report.report_summary_hosts(rm, **kwargs)  # type: ignore[arg-type]

        assert isinstance(res, Table)
        assert res.title == (title or "Summary per host")
        assert res.row_count == expected_length