Coverage for tests / unit / formatters / styles / test_style_json.py: 100%
47 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"""Unit tests for JsonStyle formatter.
3Tests verify JsonStyle correctly formats tabular data as valid JSON
4with proper structure, metadata, and column normalization.
5"""
7from __future__ import annotations
9import json
11from assertpy import assert_that
13from lintro.formatters.styles.json import JsonStyle
15from .conftest import MULTI_ROW_DATA, SINGLE_ROW_DATA, STANDARD_COLUMNS, TWO_COLUMNS
18def test_json_style_empty_rows_produces_valid_json(json_style: JsonStyle) -> None:
19 """JsonStyle formats empty rows as valid JSON with expected structure.
21 Args:
22 json_style: The JsonStyle formatter instance.
23 """
24 result = json_style.format(TWO_COLUMNS, [], tool_name="ruff")
25 data = json.loads(result)
27 assert_that(data["tool"]).is_equal_to("ruff")
28 assert_that(data["total_issues"]).is_equal_to(0)
29 assert_that(data["issues"]).is_empty()
32def test_json_style_single_row_produces_correct_structure(
33 json_style: JsonStyle,
34) -> None:
35 """JsonStyle formats single row as JSON with correct issue structure.
37 Args:
38 json_style: The JsonStyle formatter instance.
39 """
40 result = json_style.format(
41 STANDARD_COLUMNS,
42 SINGLE_ROW_DATA,
43 tool_name="mypy",
44 )
45 data = json.loads(result)
47 assert_that(data["tool"]).is_equal_to("mypy")
48 assert_that(data["total_issues"]).is_equal_to(1)
49 assert_that(data["issues"]).is_length(1)
50 assert_that(data["issues"][0]["file"]).is_equal_to("src/main.py")
51 assert_that(data["issues"][0]["line"]).is_equal_to("10")
52 assert_that(data["issues"][0]["message"]).is_equal_to("Error found")
55def test_json_style_multiple_rows_counts_correctly(json_style: JsonStyle) -> None:
56 """JsonStyle formats multiple rows with correct count.
58 Args:
59 json_style: The JsonStyle formatter instance.
60 """
61 result = json_style.format(TWO_COLUMNS, MULTI_ROW_DATA, tool_name="ruff")
62 data = json.loads(result)
64 assert_that(data["total_issues"]).is_equal_to(2)
65 assert_that(data["issues"]).is_length(2)
68def test_json_style_column_name_normalization(json_style: JsonStyle) -> None:
69 """JsonStyle normalizes column names to lowercase with underscores.
71 Args:
72 json_style: The JsonStyle formatter instance.
73 """
74 result = json_style.format(
75 ["File Path", "Line Number"],
76 [["src/main.py", "10"]],
77 )
78 data = json.loads(result)
80 assert_that(data["issues"][0]).contains_key("file_path")
81 assert_that(data["issues"][0]).contains_key("line_number")
84def test_json_style_with_metadata(json_style: JsonStyle) -> None:
85 """JsonStyle includes provided metadata in output.
87 Args:
88 json_style: The JsonStyle formatter instance.
89 """
90 result = json_style.format(
91 TWO_COLUMNS,
92 [["src/main.py", "10"]],
93 tool_name="ruff",
94 metadata={"version": "1.0.0"},
95 )
96 data = json.loads(result)
98 assert_that(data["metadata"]["version"]).is_equal_to("1.0.0")
101def test_json_style_with_extra_kwargs_in_metadata(json_style: JsonStyle) -> None:
102 """JsonStyle includes extra kwargs as metadata.
104 Args:
105 json_style: The JsonStyle formatter instance.
106 """
107 result = json_style.format(
108 TWO_COLUMNS,
109 [["src/main.py", "10"]],
110 tool_name="ruff",
111 custom_field="custom_value",
112 )
113 data = json.loads(result)
115 assert_that(data["metadata"]["custom_field"]).is_equal_to("custom_value")
118def test_json_style_has_timestamp(json_style: JsonStyle) -> None:
119 """JsonStyle output includes timestamp field.
121 Args:
122 json_style: The JsonStyle formatter instance.
123 """
124 result = json_style.format(["File"], [["src/main.py"]])
125 data = json.loads(result)
127 assert_that(data).contains_key("timestamp")
130def test_json_style_row_shorter_than_columns(json_style: JsonStyle) -> None:
131 """JsonStyle handles row with fewer elements than columns.
133 Args:
134 json_style: The JsonStyle formatter instance.
135 """
136 result = json_style.format(STANDARD_COLUMNS, [["src/main.py"]])
137 data = json.loads(result)
139 assert_that(data["issues"][0]["file"]).is_equal_to("src/main.py")
140 assert_that(data["issues"][0]).does_not_contain_key("line")