Debian Codegraph @

tests/units/reporter/test_csv.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 """Test anta.report.csv_reporter.py."""
5
6 # pylint: disable=too-few-public-methods
7
8 import csv
9 import pathlib
10 from collections.abc import Callable
11 from typing import Any
12 from unittest.mock import patch
13
14 import pytest
15
16 from anta.reporter.csv_reporter import ReportCsv
17 from anta.result_manager import ResultManager
18 from anta.tools import convert_categories
19
20
21 class TestReportCsv:
22 """Tester for ReportCsv class."""
23
24 def compare_csv_and_result(self, rows: list[Any], index: int, result_manager: ResultManager) -> None:
25 """Compare CSV and TestResult."""
26 assert rows[index + 1][0] == result_manager.results[index].name
27 assert rows[index + 1][1] == result_manager.results[index].test
28 assert rows[index + 1][2] == result_manager.results[index].result
29 assert rows[index + 1][3] == ReportCsv().split_list_to_txt_list(result_manager.results[index].messages)
30 assert rows[index + 1][4] == result_manager.results[index].description
31 assert rows[index + 1][5] == ReportCsv().split_list_to_txt_list(convert_categories(result_manager.results[index].categories))
32
33 def test_report_csv_generate(
34 self,
35 result_manager_factory: Callable[[int], ResultManager],
36 tmp_path: pathlib.Path,
37 ) -> None:
38 """Test CSV reporter."""
39 max_test_entries = 10
40
41 # Create a temporary CSV file path
42 csv_filename = tmp_path / "test.csv"
43
44 # Create a ResultManager instance with dummy test results
45 result_manager = result_manager_factory(max_test_entries)
46 # Test usecase with list of messages
47 result_manager.results[0].messages = ["Message 1", "Message 2"]
48 # Test usecase with list of categories
49 result_manager.results[1].messages = ["Cat 1", "Cat 2"]
50
51 # Generate the CSV report
52 ReportCsv.generate(result_manager, csv_filename)
53
54 # Read the generated CSV file - newline required on Windows..
55 with pathlib.Path.open(csv_filename, encoding="utf-8", newline="") as csvfile:
56 reader = csv.reader(csvfile, delimiter=",")
57 rows = list(reader)
58
59 # Assert the headers
60 assert rows[0] == [
61 ReportCsv.Headers.device,
62 ReportCsv.Headers.test_name,
63 ReportCsv.Headers.test_status,
64 ReportCsv.Headers.messages,
65 ReportCsv.Headers.description,
66 ReportCsv.Headers.categories,
67 ]
68
69 # Assert the test result rows
70 for index in [0, max_test_entries - 1]:
71 self.compare_csv_and_result(rows, index, result_manager)
72
73 # Assert number of lines: Number of TestResults + CSV Headers
74 assert len(rows) == len(result_manager.results) + 1
75
76 def test_report_csv_generate_os_error(
77 self,
78 result_manager_factory: Callable[[int], ResultManager],
79 tmp_path: pathlib.Path,
80 caplog: pytest.LogCaptureFixture,
81 ) -> None:
82 """Test CSV reporter OSError."""
83 # Create a ResultManager instance with dummy test results
84 max_test_entries = 10
85 result_manager = result_manager_factory(max_test_entries)
86
87 csv_filename = tmp_path / "read_only.csv"
88
89 with patch("pathlib.Path.open", side_effect=OSError("Any OSError")), pytest.raises(OSError, match="Any OSError"):
90 # Generate the CSV report
91 ReportCsv.generate(result_manager, csv_filename)
92
93 assert len(caplog.record_tuples) == 1
94 assert "OSError caught while writing the CSV file" in caplog.text