Coverage for tests / unit / formatters / styles / test_json.py: 100%
44 statements
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2026-04-03 18:53 +0000
1"""Tests for lintro.formatters.styles.json module."""
3from __future__ import annotations
5import json
7from assertpy import assert_that
9from lintro.formatters.styles.json import JsonStyle
12def test_json_style_basic_format(json_style: JsonStyle) -> None:
13 """JsonStyle formats data as valid JSON.
15 Args:
16 json_style: JsonStyle fixture.
17 """
18 result = json_style.format(
19 ["File", "Line"],
20 [["test.py", "10"]],
21 tool_name="ruff",
22 )
23 parsed = json.loads(result)
24 assert_that(parsed).contains_key("tool")
25 assert_that(parsed).contains_key("timestamp")
26 assert_that(parsed).contains_key("total_issues")
27 assert_that(parsed).contains_key("issues")
30def test_json_style_includes_tool_name(json_style: JsonStyle) -> None:
31 """JsonStyle includes tool name in output.
33 Args:
34 json_style: JsonStyle fixture.
35 """
36 result = json_style.format(
37 ["File"],
38 [["test.py"]],
39 tool_name="mypy",
40 )
41 parsed = json.loads(result)
42 assert_that(parsed["tool"]).is_equal_to("mypy")
45def test_json_style_normalizes_column_names(json_style: JsonStyle) -> None:
46 """JsonStyle normalizes column names to lowercase with underscores.
48 Args:
49 json_style: JsonStyle fixture.
50 """
51 result = json_style.format(
52 ["File Path", "Line Number"],
53 [["test.py", "10"]],
54 )
55 parsed = json.loads(result)
56 issue = parsed["issues"][0]
57 assert_that(issue).contains_key("file_path")
58 assert_that(issue).contains_key("line_number")
61def test_json_style_counts_issues(json_style: JsonStyle) -> None:
62 """JsonStyle counts total issues correctly.
64 Args:
65 json_style: JsonStyle fixture.
66 """
67 result = json_style.format(
68 ["File"],
69 [["a.py"], ["b.py"], ["c.py"]],
70 )
71 parsed = json.loads(result)
72 assert_that(parsed["total_issues"]).is_equal_to(3)
75def test_json_style_empty_rows(json_style: JsonStyle) -> None:
76 """JsonStyle handles empty rows.
78 Args:
79 json_style: JsonStyle fixture.
80 """
81 result = json_style.format(["File"], [])
82 parsed = json.loads(result)
83 assert_that(parsed["total_issues"]).is_equal_to(0)
84 assert_that(parsed["issues"]).is_empty()
87def test_json_style_includes_metadata(json_style: JsonStyle) -> None:
88 """JsonStyle includes metadata when provided.
90 Args:
91 json_style: JsonStyle fixture.
92 """
93 result = json_style.format(
94 ["File"],
95 [["test.py"]],
96 metadata={"version": "1.0"},
97 )
98 parsed = json.loads(result)
99 assert_that(parsed).contains_key("metadata")
100 assert_that(parsed["metadata"]["version"]).is_equal_to("1.0")
103def test_json_style_extra_kwargs_as_metadata(json_style: JsonStyle) -> None:
104 """JsonStyle adds extra kwargs to metadata.
106 Args:
107 json_style: JsonStyle fixture.
108 """
109 result = json_style.format(
110 ["File"],
111 [["test.py"]],
112 extra_field="value",
113 )
114 parsed = json.loads(result)
115 assert_that(parsed).contains_key("metadata")
116 assert_that(parsed["metadata"]["extra_field"]).is_equal_to("value")
119def test_json_style_has_timestamp(json_style: JsonStyle) -> None:
120 """JsonStyle includes timestamp in output.
122 Args:
123 json_style: JsonStyle fixture.
124 """
125 result = json_style.format(["File"], [["test.py"]])
126 parsed = json.loads(result)
127 assert_that(parsed["timestamp"]).is_not_empty()