-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_reports.py
More file actions
87 lines (71 loc) · 3.08 KB
/
Copy pathtest_reports.py
File metadata and controls
87 lines (71 loc) · 3.08 KB
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
"""Unit tests for report generation and JSON/CSV export in reports.py."""
import csv
import json
import shutil
import sys
import tempfile
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from reports import build_report, export_to_csv, export_to_json, summarize
from tool import ToolResult, ToolStatus
class TestReports(unittest.TestCase):
"""Tests covering report building, summaries, JSON export, and CSV export."""
def setUp(self) -> None:
self.results = [
ToolResult(
name="Python",
status=ToolStatus.INSTALLED,
version="3.12.4",
path="/usr/bin/python3",
),
ToolResult(name="Docker", status=ToolStatus.NOT_INSTALLED),
ToolResult(
name="Maven",
status=ToolStatus.VERSION_UNAVAILABLE,
path="/usr/bin/mvn",
error="Could not parse version from command output",
),
]
self.temp_dir = Path(tempfile.mkdtemp())
def tearDown(self) -> None:
shutil.rmtree(self.temp_dir, ignore_errors=True)
def test_build_report_structure(self) -> None:
report = build_report(self.results)
self.assertIn("timestamp", report)
self.assertIn("operating_system", report)
self.assertEqual(len(report["results"]), 3)
self.assertEqual(report["results"][0]["tool"], "Python")
self.assertTrue(report["results"][0]["installed"])
self.assertFalse(report["results"][1]["installed"])
def test_summarize_counts(self) -> None:
summary = summarize(self.results)
self.assertEqual(summary["installed"], 2)
self.assertEqual(summary["missing"], 1)
self.assertEqual(summary["total"], 3)
def test_export_to_json_creates_valid_file(self) -> None:
report = build_report(self.results)
output_path = self.temp_dir / "report.json"
export_to_json(report, output_path)
self.assertTrue(output_path.exists())
with output_path.open(encoding="utf-8") as file_handle:
loaded = json.load(file_handle)
self.assertEqual(loaded["results"][0]["tool"], "Python")
self.assertEqual(len(loaded["results"]), 3)
def test_export_to_csv_creates_valid_file(self) -> None:
report = build_report(self.results)
output_path = self.temp_dir / "report.csv"
export_to_csv(report, output_path)
self.assertTrue(output_path.exists())
with output_path.open(newline="", encoding="utf-8") as file_handle:
rows = list(csv.DictReader(file_handle))
self.assertEqual(len(rows), 3)
self.assertEqual(rows[0]["tool"], "Python")
self.assertEqual(rows[1]["tool"], "Docker")
def test_export_creates_missing_parent_directories(self) -> None:
report = build_report(self.results)
nested_path = self.temp_dir / "nested" / "dir" / "report.json"
export_to_json(report, nested_path)
self.assertTrue(nested_path.exists())
if __name__ == "__main__":
unittest.main()