|
1 | | -from unittest.mock import Mock, mock_open, patch |
2 | | - |
3 | 1 | import pytest |
4 | 2 | from typer.testing import CliRunner |
5 | 3 |
|
@@ -46,104 +44,68 @@ def test_invalid_test_cases_raises_error(self): |
46 | 44 | with pytest.raises((ValueError, SyntaxError)): |
47 | 45 | count_test_cases_for_problem(json_data) |
48 | 46 |
|
| 47 | + def test_python_expressions_in_test_cases(self): |
| 48 | + """Test that Python expressions like 'string' * 100 are handled correctly.""" |
| 49 | + json_data = { |
| 50 | + "test_methods": [ |
| 51 | + {"test_cases": "[('input', 'expected'), ('100[leetcode]', 'leetcode' * 100)]"} |
| 52 | + ] |
| 53 | + } |
| 54 | + # Should not raise an error and should count 2 test cases |
| 55 | + assert count_test_cases_for_problem(json_data) == 2 |
| 56 | + |
49 | 57 |
|
50 | 58 | class TestCheckTestCases: |
51 | 59 | def setup_method(self): |
52 | 60 | self.runner = CliRunner() |
53 | 61 |
|
54 | | - @patch("leetcode_py.tools.check_test_cases.get_problems_json_path") |
55 | | - def test_check_with_no_problems_found(self, mock_get_path): |
56 | | - mock_path = Mock() |
57 | | - mock_path.glob.return_value = [] |
58 | | - mock_get_path.return_value = mock_path |
| 62 | + def test_real_json_files_can_be_parsed(self): |
| 63 | + """Test that all real JSON files can be parsed without errors.""" |
| 64 | + result = self.runner.invoke(app, ["--threshold", "50", "--max", "-1"]) |
59 | 65 |
|
60 | | - result = self.runner.invoke(app, ["--threshold", "10"]) |
| 66 | + # Should not crash with parsing errors |
| 67 | + assert "Error reading problem" not in result.stderr |
| 68 | + assert "malformed node or string" not in result.stderr |
61 | 69 |
|
62 | | - assert result.exit_code == 0 |
63 | | - assert "Problems with ≤10 test cases (0 total):" in result.stdout |
| 70 | + # Should produce valid output |
| 71 | + assert "Problems with ≤50 test cases" in result.stdout |
64 | 72 |
|
65 | | - @pytest.mark.parametrize( |
66 | | - "filename, json_data, expected_exit_code, expected_output", |
67 | | - [ |
68 | | - ( |
69 | | - "test_problem.json", |
70 | | - { |
71 | | - "test_methods": [ |
72 | | - {"test_cases": "[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]"}, |
73 | | - {"test_cases": "[(11, 12), (13, 14), (15, 16), (17, 18), (19, 20)]"}, |
74 | | - {"test_cases": "[(21, 22), (23, 24), (25, 26), (27, 28), (29, 30)]"}, |
75 | | - ] |
76 | | - }, |
77 | | - 0, |
78 | | - "Problems with ≤10 test cases (0 total):", |
79 | | - ), |
80 | | - ( |
81 | | - "small_problem.json", |
82 | | - {"test_methods": [{"test_cases": "[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]"}]}, |
83 | | - 1, |
84 | | - "Problems with ≤10 test cases (1 total):", |
85 | | - ), |
86 | | - ], |
87 | | - ) |
88 | | - @patch("leetcode_py.tools.check_test_cases.get_problems_json_path") |
89 | | - @patch("builtins.open", new_callable=mock_open) |
90 | | - def test_check_with_threshold( |
91 | | - self, mock_file, mock_get_path, filename, json_data, expected_exit_code, expected_output |
92 | | - ): |
93 | | - mock_problem_file = Mock() |
94 | | - mock_problem_file.name = filename |
95 | | - mock_path = Mock() |
96 | | - mock_path.glob.return_value = [mock_problem_file] |
97 | | - mock_get_path.return_value = mock_path |
98 | | - mock_file.return_value.read.return_value = "" |
99 | | - |
100 | | - with patch("json.load", return_value=json_data): |
101 | | - result = self.runner.invoke(app, ["--threshold", "10"]) |
102 | | - |
103 | | - assert result.exit_code == expected_exit_code |
104 | | - assert expected_output in result.stdout |
105 | | - |
106 | | - @patch("leetcode_py.tools.check_test_cases.get_problems_json_path") |
107 | | - @patch("builtins.open", new_callable=mock_open) |
108 | | - def test_check_with_max_results_limit(self, mock_file, mock_get_path): |
109 | | - mock_files = [Mock(name=f"problem_{i}.json") for i in range(5)] |
110 | | - mock_path = Mock() |
111 | | - mock_path.glob.return_value = mock_files |
112 | | - mock_get_path.return_value = mock_path |
113 | | - json_data = {"test_methods": [{"test_cases": "[(1, 2), (3, 4)]"}]} |
114 | | - mock_file.return_value.read.return_value = "" |
115 | | - |
116 | | - with patch("json.load", return_value=json_data): |
117 | | - result = self.runner.invoke(app, ["--threshold", "10", "--max", "2"]) |
118 | | - |
119 | | - assert result.exit_code == 1 |
120 | | - assert "Problems with ≤10 test cases (2 total):" in result.stdout |
| 73 | + def test_decode_string_specifically(self): |
| 74 | + """Test the specific decode_string.json that was causing parsing errors.""" |
| 75 | + import json |
121 | 76 |
|
122 | | - @pytest.mark.parametrize( |
123 | | - "args, expected_exit_code, expected_output, output_stream", |
124 | | - [ |
125 | | - (["--max", "invalid"], 1, "Invalid max_results value: invalid", "stderr"), |
126 | | - ], |
127 | | - ) |
128 | | - def test_invalid_inputs(self, args, expected_exit_code, expected_output, output_stream): |
129 | | - result = self.runner.invoke(app, args) |
130 | | - assert result.exit_code == expected_exit_code |
131 | | - output = getattr(result, output_stream) |
132 | | - assert expected_output in output |
133 | | - |
134 | | - def test_real_json_integration(self): |
135 | | - """Integration test with real JSON files - should fail if any problems have ≤threshold test cases.""" |
136 | | - threshold = 10 |
137 | | - result = self.runner.invoke(app, ["--threshold", str(threshold), "--max", "-1"]) |
138 | | - |
139 | | - # Extract count from output like "Problems with ≤10 test cases (X total):" |
140 | | - import re |
141 | | - |
142 | | - match = re.search(rf"Problems with ≤{threshold} test cases \((\d+) total\):", result.stdout) |
143 | | - assert match, f"Could not parse output: {result.stdout}" |
144 | | - |
145 | | - count = int(match.group(1)) |
146 | | - if count > 0: |
147 | | - pytest.fail( |
148 | | - f"Found {count} problems with ≤{threshold} test cases. All problems should have >{threshold} test cases." |
149 | | - ) |
| 77 | + from leetcode_py.cli.utils.resources import get_problems_json_path |
| 78 | + |
| 79 | + problems_dir = get_problems_json_path() |
| 80 | + decode_string_file = problems_dir / "decode_string.json" |
| 81 | + |
| 82 | + if decode_string_file.exists(): |
| 83 | + with open(decode_string_file) as f: |
| 84 | + data = json.load(f) |
| 85 | + |
| 86 | + # Should not raise an error when counting test cases |
| 87 | + count = count_test_cases_for_problem(data) |
| 88 | + assert count > 0 # Should have test cases |
| 89 | + else: |
| 90 | + pytest.skip("decode_string.json not found") |
| 91 | + |
| 92 | + def test_all_json_files_individually(self): |
| 93 | + """Test each JSON file individually to catch parsing issues.""" |
| 94 | + import json |
| 95 | + |
| 96 | + from leetcode_py.cli.utils.resources import get_problems_json_path |
| 97 | + |
| 98 | + problems_dir = get_problems_json_path() |
| 99 | + failed_files = [] |
| 100 | + |
| 101 | + for json_file in problems_dir.glob("*.json"): |
| 102 | + try: |
| 103 | + with open(json_file) as f: |
| 104 | + data = json.load(f) |
| 105 | + count_test_cases_for_problem(data) |
| 106 | + except Exception as e: |
| 107 | + failed_files.append((json_file.name, str(e))) |
| 108 | + |
| 109 | + if failed_files: |
| 110 | + error_msg = "\n".join([f"{name}: {error}" for name, error in failed_files]) |
| 111 | + pytest.fail(f"Failed to parse {len(failed_files)} JSON files:\n{error_msg}") |
0 commit comments