Coverage for tests / unit / utils / native_parsers / test_json_config.py: 100%

42 statements  

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

1"""Tests for _load_json_config function.""" 

2 

3from __future__ import annotations 

4 

5from pathlib import Path 

6 

7import pytest 

8from assertpy import assert_that 

9 

10from lintro.utils.native_parsers import _load_json_config 

11 

12 

13def test_load_json_config_valid_json_file(tmp_path: Path) -> None: 

14 """Load valid JSON configuration file and return its contents. 

15 

16 Args: 

17 tmp_path: Temporary directory path for test files. 

18 """ 

19 config_file = tmp_path / "config.json" 

20 config_file.write_text('{"key": "value", "number": 42}') 

21 result = _load_json_config(config_file) 

22 assert_that(result).is_equal_to({"key": "value", "number": 42}) 

23 

24 

25@pytest.mark.parametrize( 

26 ("content", "description"), 

27 [ 

28 ("not valid json {", "invalid_json"), 

29 ('["item1", "item2"]', "non_dict_array"), 

30 ], 

31 ids=["invalid_json_syntax", "json_array_not_dict"], 

32) 

33def test_load_json_config_returns_empty_dict( 

34 tmp_path: Path, 

35 content: str, 

36 description: str, 

37) -> None: 

38 """Return empty dict for invalid JSON or non-dict JSON content. 

39 

40 Args: 

41 tmp_path: Temporary directory path for test files. 

42 content: Invalid JSON content to write to file. 

43 description: Description of the test case. 

44 """ 

45 config_file = tmp_path / "config.json" 

46 config_file.write_text(content) 

47 result = _load_json_config(config_file) 

48 assert_that(result).is_empty() 

49 

50 

51def test_load_json_config_non_existent_file(tmp_path: Path) -> None: 

52 """Return empty dict when the config file does not exist. 

53 

54 Args: 

55 tmp_path: Temporary directory path for test files. 

56 """ 

57 config_file = tmp_path / "nonexistent.json" 

58 result = _load_json_config(config_file) 

59 assert_that(result).is_empty() 

60 

61 

62def test_load_json_config_unicode_file_path(tmp_path: Path) -> None: 

63 """Load JSON config from path with Unicode characters. 

64 

65 Args: 

66 tmp_path: Temporary directory path for test files. 

67 """ 

68 config_dir = tmp_path / "配置" 

69 config_dir.mkdir() 

70 config_file = config_dir / "config.json" 

71 config_file.write_text('{"key": "value"}') 

72 result = _load_json_config(config_file) 

73 assert_that(result).is_equal_to({"key": "value"}) 

74 

75 

76def test_load_json_config_empty_file(tmp_path: Path) -> None: 

77 """Return empty dict for empty JSON file. 

78 

79 Args: 

80 tmp_path: Temporary directory path for test files. 

81 """ 

82 config_file = tmp_path / "empty.json" 

83 config_file.write_text("") 

84 result = _load_json_config(config_file) 

85 assert_that(result).is_empty() 

86 

87 

88def test_load_json_config_null_json(tmp_path: Path) -> None: 

89 """Return empty dict when JSON content is null. 

90 

91 Args: 

92 tmp_path: Temporary directory path for test files. 

93 """ 

94 config_file = tmp_path / "null.json" 

95 config_file.write_text("null") 

96 result = _load_json_config(config_file) 

97 assert_that(result).is_empty() 

98 

99 

100def test_load_json_config_nested_empty_object(tmp_path: Path) -> None: 

101 """Load JSON with nested empty objects. 

102 

103 Args: 

104 tmp_path: Temporary directory path for test files. 

105 """ 

106 config_file = tmp_path / "config.json" 

107 config_file.write_text('{"outer": {"inner": {}}}') 

108 result = _load_json_config(config_file) 

109 assert_that(result["outer"]["inner"]).is_empty()