Coverage for tests / formatters / test_formatters.py: 100%

54 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Tests for formatters.""" 

2 

3import pytest 

4from assertpy import assert_that 

5 

6from lintro.formatters.styles.csv import CsvStyle 

7from lintro.formatters.styles.grid import GridStyle 

8from lintro.formatters.styles.html import HtmlStyle 

9from lintro.formatters.styles.json import JsonStyle 

10from lintro.formatters.styles.markdown import MarkdownStyle 

11from lintro.formatters.styles.plain import PlainStyle 

12 

13# Test data 

14SAMPLE_COLUMNS = ["col1", "col2"] 

15SAMPLE_ROWS = [["val1", "val2"], ["val3", "val4"]] 

16 

17 

18@pytest.mark.parametrize( 

19 "style_class,expected_contains", 

20 [ 

21 (CsvStyle, ["col1,col2", "val1,val2", "val3,val4"]), 

22 (GridStyle, ["col1", "col2", "val1", "val2"]), 

23 (HtmlStyle, ["<table>", "<th>col1</th>", "<th>col2</th>", "<td>val1</td>"]), 

24 (JsonStyle, ["col1", "col2", "val1", "val2"]), 

25 (MarkdownStyle, ["| col1 | col2 |", "| val1 | val2 |", "| val3 | val4 |"]), 

26 (PlainStyle, ["col1", "col2", "val1", "val2"]), 

27 ], 

28 ids=["csv", "grid", "html", "json", "markdown", "plain"], 

29) 

30def test_style_format(style_class: type, expected_contains: list[str]) -> None: 

31 """Test that each style formats data correctly. 

32 

33 Args: 

34 style_class: The formatter style class to test. 

35 expected_contains: Strings expected in the formatted output. 

36 """ 

37 style = style_class() 

38 result = style.format(SAMPLE_COLUMNS, SAMPLE_ROWS) 

39 assert_that(result).is_instance_of(str) 

40 assert_that(result).is_not_empty() 

41 for expected in expected_contains: 

42 assert_that(result).contains(expected) 

43 

44 

45@pytest.mark.parametrize( 

46 "style_class,allows_metadata", 

47 [ 

48 (CsvStyle, False), 

49 (GridStyle, False), 

50 (HtmlStyle, False), 

51 (JsonStyle, True), # JSON includes metadata structure even when empty 

52 (MarkdownStyle, False), 

53 (PlainStyle, False), 

54 ], 

55 ids=["csv", "grid", "html", "json", "markdown", "plain"], 

56) 

57def test_style_format_empty(style_class: type, allows_metadata: bool) -> None: 

58 """Test that all styles handle empty data gracefully. 

59 

60 Args: 

61 style_class: The formatter style class to test. 

62 allows_metadata: Whether the style includes metadata for empty data. 

63 """ 

64 style = style_class() 

65 result = style.format([], []) 

66 assert_that(result).is_instance_of(str) 

67 if allows_metadata: 

68 # JSON style produces metadata structure, verify it contains no issues 

69 assert_that(result).contains('"total_issues": 0') 

70 assert_that(result).contains('"issues": []') 

71 else: 

72 # Other styles should produce empty or minimal output 

73 assert_that(len(result)).is_less_than_or_equal_to(50) 

74 

75 

76def test_grid_style_format_fallback() -> None: 

77 """Test grid style formatting fallback when tabulate is not available.""" 

78 style = GridStyle() 

79 with pytest.MonkeyPatch().context() as m: 

80 m.setattr("lintro.formatters.styles.grid.TABULATE_AVAILABLE", False) 

81 m.setattr("lintro.formatters.styles.grid.tabulate", None) 

82 result = style.format(SAMPLE_COLUMNS, SAMPLE_ROWS) 

83 assert_that(result).contains("col1") 

84 assert_that(result).contains("col2") 

85 assert_that(result).contains("val1") 

86 assert_that(result).contains("val2") 

87 assert_that(result).contains(" | ") 

88 

89 

90def test_grid_style_format_fallback_empty() -> None: 

91 """Test grid style formatting fallback with empty data.""" 

92 style = GridStyle() 

93 with pytest.MonkeyPatch().context() as m: 

94 m.setattr("lintro.formatters.styles.grid.TABULATE_AVAILABLE", False) 

95 m.setattr("lintro.formatters.styles.grid.tabulate", None) 

96 result = style.format([], []) 

97 assert_that(result).is_equal_to("") 

98 

99 

100def test_grid_style_format_fallback_single_column() -> None: 

101 """Test grid style formatting fallback with single column.""" 

102 style = GridStyle() 

103 with pytest.MonkeyPatch().context() as m: 

104 m.setattr("lintro.formatters.styles.grid.TABULATE_AVAILABLE", False) 

105 m.setattr("lintro.formatters.styles.grid.tabulate", None) 

106 result = style.format(["col1"], [["val1"], ["val2"]]) 

107 assert_that(result).contains("col1") 

108 assert_that(result).contains("val1") 

109 assert_that(result).contains("val2")