Coverage for tests / unit / parsers / base_parser / test_extract_fields.py: 100%

68 statements  

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

1"""Tests for field extraction functions.""" 

2 

3from __future__ import annotations 

4 

5from assertpy import assert_that 

6 

7from lintro.parsers.base_parser import ( 

8 extract_dict_field, 

9 extract_int_field, 

10 extract_str_field, 

11) 

12 

13# === Extract Int Field Tests === 

14 

15 

16def test_extract_int_field_first_candidate_found() -> None: 

17 """Return value from first matching candidate key.""" 

18 data: dict[str, object] = {"row": 10, "line": 20} 

19 result = extract_int_field(data, ["row", "line"]) 

20 assert_that(result).is_equal_to(10) 

21 

22 

23def test_extract_int_field_second_candidate_found() -> None: 

24 """Return value from second candidate when first is missing.""" 

25 data: dict[str, object] = {"line": 15} 

26 result = extract_int_field(data, ["row", "line"]) 

27 assert_that(result).is_equal_to(15) 

28 

29 

30def test_extract_int_field_no_candidates_found() -> None: 

31 """Return None when no candidate keys have integer values.""" 

32 data: dict[str, object] = {"other": "value"} 

33 result = extract_int_field(data, ["row", "line"]) 

34 assert_that(result).is_none() 

35 

36 

37def test_extract_int_field_with_default() -> None: 

38 """Return default when no candidate keys have integer values.""" 

39 data: dict[str, object] = {"other": "value"} 

40 result = extract_int_field(data, ["row", "line"], default=0) 

41 assert_that(result).is_equal_to(0) 

42 

43 

44def test_extract_int_field_ignores_non_int_values() -> None: 

45 """Skip candidate keys with non-integer values.""" 

46 data: dict[str, object] = {"row": "not_an_int", "line": 5} 

47 result = extract_int_field(data, ["row", "line"]) 

48 assert_that(result).is_equal_to(5) 

49 

50 

51def test_extract_int_field_empty_candidates() -> None: 

52 """Return default for empty candidates list.""" 

53 data: dict[str, object] = {"row": 10} 

54 result = extract_int_field(data, [], default=99) 

55 assert_that(result).is_equal_to(99) 

56 

57 

58# === Extract Str Field Tests === 

59 

60 

61def test_extract_str_field_first_candidate_found() -> None: 

62 """Return value from first matching candidate key.""" 

63 data: dict[str, object] = {"filename": "test.py", "file": "other.py"} 

64 result = extract_str_field(data, ["filename", "file"]) 

65 assert_that(result).is_equal_to("test.py") 

66 

67 

68def test_extract_str_field_second_candidate_found() -> None: 

69 """Return value from second candidate when first is missing.""" 

70 data: dict[str, object] = {"file": "test.py"} 

71 result = extract_str_field(data, ["filename", "file"]) 

72 assert_that(result).is_equal_to("test.py") 

73 

74 

75def test_extract_str_field_no_candidates_found() -> None: 

76 """Return empty string when no candidate keys have string values.""" 

77 data: dict[str, object] = {"other": 123} 

78 result = extract_str_field(data, ["filename", "file"]) 

79 assert_that(result).is_equal_to("") 

80 

81 

82def test_extract_str_field_with_default() -> None: 

83 """Return custom default when no candidate keys found.""" 

84 data: dict[str, object] = {"other": 123} 

85 result = extract_str_field(data, ["filename", "file"], default="unknown") 

86 assert_that(result).is_equal_to("unknown") 

87 

88 

89def test_extract_str_field_ignores_non_str_values() -> None: 

90 """Skip candidate keys with non-string values.""" 

91 data: dict[str, object] = {"filename": 123, "file": "valid.py"} 

92 result = extract_str_field(data, ["filename", "file"]) 

93 assert_that(result).is_equal_to("valid.py") 

94 

95 

96# === Extract Dict Field Tests === 

97 

98 

99def test_extract_dict_field_first_candidate_found() -> None: 

100 """Return dict value from first matching candidate key.""" 

101 data: dict[str, object] = {"location": {"line": 1}, "start": {"row": 2}} 

102 result = extract_dict_field(data, ["location", "start"]) 

103 assert_that(result).is_equal_to({"line": 1}) 

104 

105 

106def test_extract_dict_field_second_candidate_found() -> None: 

107 """Return dict value from second candidate when first is missing.""" 

108 data: dict[str, object] = {"start": {"row": 2}} 

109 result = extract_dict_field(data, ["location", "start"]) 

110 assert_that(result).is_equal_to({"row": 2}) 

111 

112 

113def test_extract_dict_field_no_candidates_found() -> None: 

114 """Return empty dict when no candidate keys have dict values.""" 

115 data: dict[str, object] = {"other": "value"} 

116 result = extract_dict_field(data, ["location", "start"]) 

117 assert_that(result).is_equal_to({}) 

118 

119 

120def test_extract_dict_field_with_default() -> None: 

121 """Return custom default when no candidate keys found.""" 

122 default: dict[str, object] = {"default": True} 

123 data: dict[str, object] = {"other": "value"} 

124 result = extract_dict_field(data, ["location", "start"], default=default) 

125 assert_that(result).is_equal_to(default) 

126 

127 

128def test_extract_dict_field_ignores_non_dict_values() -> None: 

129 """Skip candidate keys with non-dict values.""" 

130 data: dict[str, object] = {"location": "not_a_dict", "start": {"row": 5}} 

131 result = extract_dict_field(data, ["location", "start"]) 

132 assert_that(result).is_equal_to({"row": 5})