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

1"""Unit tests for JsonStyle formatter. 

2 

3Tests verify JsonStyle correctly formats tabular data as valid JSON 

4with proper structure, metadata, and column normalization. 

5""" 

6 

7from __future__ import annotations 

8 

9import json 

10 

11from assertpy import assert_that 

12 

13from lintro.formatters.styles.json import JsonStyle 

14 

15from .conftest import MULTI_ROW_DATA, SINGLE_ROW_DATA, STANDARD_COLUMNS, TWO_COLUMNS 

16 

17 

18def test_json_style_empty_rows_produces_valid_json(json_style: JsonStyle) -> None: 

19 """JsonStyle formats empty rows as valid JSON with expected structure. 

20 

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) 

26 

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() 

30 

31 

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. 

36 

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) 

46 

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") 

53 

54 

55def test_json_style_multiple_rows_counts_correctly(json_style: JsonStyle) -> None: 

56 """JsonStyle formats multiple rows with correct count. 

57 

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) 

63 

64 assert_that(data["total_issues"]).is_equal_to(2) 

65 assert_that(data["issues"]).is_length(2) 

66 

67 

68def test_json_style_column_name_normalization(json_style: JsonStyle) -> None: 

69 """JsonStyle normalizes column names to lowercase with underscores. 

70 

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) 

79 

80 assert_that(data["issues"][0]).contains_key("file_path") 

81 assert_that(data["issues"][0]).contains_key("line_number") 

82 

83 

84def test_json_style_with_metadata(json_style: JsonStyle) -> None: 

85 """JsonStyle includes provided metadata in output. 

86 

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) 

97 

98 assert_that(data["metadata"]["version"]).is_equal_to("1.0.0") 

99 

100 

101def test_json_style_with_extra_kwargs_in_metadata(json_style: JsonStyle) -> None: 

102 """JsonStyle includes extra kwargs as metadata. 

103 

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) 

114 

115 assert_that(data["metadata"]["custom_field"]).is_equal_to("custom_value") 

116 

117 

118def test_json_style_has_timestamp(json_style: JsonStyle) -> None: 

119 """JsonStyle output includes timestamp field. 

120 

121 Args: 

122 json_style: The JsonStyle formatter instance. 

123 """ 

124 result = json_style.format(["File"], [["src/main.py"]]) 

125 data = json.loads(result) 

126 

127 assert_that(data).contains_key("timestamp") 

128 

129 

130def test_json_style_row_shorter_than_columns(json_style: JsonStyle) -> None: 

131 """JsonStyle handles row with fewer elements than columns. 

132 

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) 

138 

139 assert_that(data["issues"][0]["file"]).is_equal_to("src/main.py") 

140 assert_that(data["issues"][0]).does_not_contain_key("line")