Skip to content

Commit 169b04a

Browse files
refactor: move test_json_handler.py to proper tests directory
- Move test_json_handler.py from root to tests/ directory - Update import path to work from tests/ directory - Remove duplicate pathlib import - Add T20 (print statements) to per-file-ignores for test files - Fix line length issues and unused variables - Remove unnecessary open mode parameters - Break long strings to comply with 88 character limit - Maintain all test functionality and imports This follows proper Python project structure where test files belong in the tests/ directory and allows print statements in test files for demonstration purposes.
1 parent 09150aa commit 169b04a

File tree

2 files changed

+85
-59
lines changed

2 files changed

+85
-59
lines changed

pyproject.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ fixable = ["I"]
2525
[tool.ruff.lint.per-file-ignores]
2626
"example_usage.py" = ["T20"]
2727
".github/scripts/analyze_vulnerabilities.py" = ["T20"]
28-
"tests/**/*.py" = ["S101"] # Allow assert statements in test files
29-
"**/test_*.py" = ["S101"] # Allow assert statements in test files
30-
"**/*_test.py" = ["S101"] # Allow assert statements in test files
28+
"tests/**/*.py" = ["S101", "T20"] # Allow assert statements and print in test files
29+
"**/test_*.py" = ["S101", "T20"] # Allow assert statements and print in test files
30+
"**/*_test.py" = ["S101", "T20"] # Allow assert statements and print in test files
3131

3232
[tool.isort]
3333
profile = "black"
Lines changed: 82 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,94 @@
44
"""
55

66
import json
7+
import sys
78
import tempfile
89
from pathlib import Path
9-
from scripts.handlers.json_handler import apply_json_suggestion, validate_json_suggestion, has_duplicate_keys
10+
11+
# Add the scripts directory to the path
12+
sys.path.insert(0, str(Path(__file__).parent.parent / "scripts"))
13+
14+
from handlers.json_handler import (
15+
apply_json_suggestion,
16+
has_duplicate_keys,
17+
validate_json_suggestion,
18+
)
19+
1020

1121
def test_duplicate_key_detection():
1222
"""Test that duplicate keys are detected."""
1323
print("Testing duplicate key detection...")
1424

1525
# Test JSON string with duplicate keys (like the original problem)
16-
duplicate_json = '''
26+
duplicate_json = """
1727
{
1828
"name": "@contextforge/memory-client",
1929
"version": "0.1.0",
2030
"name": "@contextforge/memory-client",
2131
"version": "0.1.0",
2232
"type": "module"
2333
}
24-
'''
34+
"""
2535

2636
# Parse the JSON - this should fail due to duplicate keys
2737
try:
28-
duplicate_data = json.loads(duplicate_json)
38+
json.loads(duplicate_json)
2939
# If we get here, Python's JSON parser didn't catch it
3040
# (which is expected - JSON parsers typically keep the last value)
3141
print("Note: JSON parser kept last values for duplicate keys")
3242
except json.JSONDecodeError:
3343
print("Note: JSON parser rejected duplicate keys")
3444

3545
# Test data without duplicate keys
36-
clean_data = {
37-
"name": "test",
38-
"version": "1.0.0",
39-
"description": "test package"
40-
}
46+
clean_data = {"name": "test", "version": "1.0.0", "description": "test package"}
4147

42-
assert not has_duplicate_keys(clean_data), "Should not detect duplicates in clean data"
48+
assert not has_duplicate_keys(
49+
clean_data
50+
), "Should not detect duplicates in clean data"
4351
print("✓ Clean data validation works")
4452

53+
4554
def test_json_suggestion_application():
4655
"""Test applying JSON suggestions."""
4756
print("\nTesting JSON suggestion application...")
4857

4958
# Create a temporary JSON file
50-
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
59+
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
5160
original_data = {
5261
"name": "@contextforge/memory-client",
5362
"version": "0.1.0",
5463
"type": "module",
55-
"description": "TypeScript client for ContextForge Memory API"
64+
"description": "TypeScript client for ContextForge Memory API",
5665
}
5766
json.dump(original_data, f, indent=2)
5867
temp_path = f.name
5968

6069
try:
6170
# Test valid suggestion
62-
valid_suggestion = json.dumps({
63-
"name": "@contextforge/memory-client",
64-
"version": "0.1.0",
65-
"type": "module",
66-
"description": "TypeScript client for ContextForge Memory API with v0 and v1 support",
67-
"main": "dist/index.cjs",
68-
"module": "dist/index.esm.js",
69-
"types": "dist/types/index.d.ts",
70-
"sideEffects": False,
71-
"exports": {
72-
".": {
73-
"import": "./dist/index.esm.js",
74-
"require": "./dist/index.cjs",
75-
"types": "./dist/types/index.d.ts"
71+
valid_suggestion = json.dumps(
72+
{
73+
"name": "@contextforge/memory-client",
74+
"version": "0.1.0",
75+
"type": "module",
76+
"description": (
77+
"TypeScript client for ContextForge Memory API "
78+
"with v0 and v1 support"
79+
),
80+
"main": "dist/index.cjs",
81+
"module": "dist/index.esm.js",
82+
"types": "dist/types/index.d.ts",
83+
"sideEffects": False,
84+
"exports": {
85+
".": {
86+
"import": "./dist/index.esm.js",
87+
"require": "./dist/index.cjs",
88+
"types": "./dist/types/index.d.ts",
89+
},
90+
"./package.json": "./package.json",
7691
},
77-
"./package.json": "./package.json"
78-
}
79-
}, indent=2)
92+
},
93+
indent=2,
94+
)
8095

8196
# Validate suggestion
8297
is_valid, msg = validate_json_suggestion(temp_path, valid_suggestion, 1, 1)
@@ -89,7 +104,7 @@ def test_json_suggestion_application():
89104
print("✓ Valid suggestion application works")
90105

91106
# Verify the file was updated correctly
92-
with open(temp_path, 'r') as f:
107+
with open(temp_path) as f:
93108
updated_data = json.load(f)
94109

95110
assert "exports" in updated_data, "Exports field should be added"
@@ -100,64 +115,75 @@ def test_json_suggestion_application():
100115
# Clean up
101116
Path(temp_path).unlink()
102117

118+
103119
def test_duplicate_key_prevention():
104120
"""Test that duplicate keys are prevented."""
105121
print("\nTesting duplicate key prevention...")
106122

107123
# Create a temporary JSON file
108-
with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
109-
original_data = {
110-
"name": "@contextforge/memory-client",
111-
"version": "0.1.0"
112-
}
124+
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
125+
original_data = {"name": "@contextforge/memory-client", "version": "0.1.0"}
113126
json.dump(original_data, f, indent=2)
114127
temp_path = f.name
115128

116129
try:
117130
# Test suggestion that would create duplicates when merged
118131
# This simulates the original package.json problem
119-
suggestion_with_potential_duplicates = json.dumps({
120-
"name": "@contextforge/memory-client",
121-
"version": "0.1.0",
122-
"type": "module",
123-
"description": "TypeScript client for ContextForge Memory API with v0 and v1 support",
124-
"main": "dist/index.cjs",
125-
"module": "dist/index.esm.js",
126-
"types": "dist/types/index.d.ts",
127-
"sideEffects": False,
128-
"exports": {
129-
".": {
130-
"import": "./dist/index.esm.js",
131-
"require": "./dist/index.cjs",
132-
"types": "./dist/types/index.d.ts"
132+
suggestion_with_potential_duplicates = json.dumps(
133+
{
134+
"name": "@contextforge/memory-client",
135+
"version": "0.1.0",
136+
"type": "module",
137+
"description": (
138+
"TypeScript client for ContextForge Memory API "
139+
"with v0 and v1 support"
140+
),
141+
"main": "dist/index.cjs",
142+
"module": "dist/index.esm.js",
143+
"types": "dist/types/index.d.ts",
144+
"sideEffects": False,
145+
"exports": {
146+
".": {
147+
"import": "./dist/index.esm.js",
148+
"require": "./dist/index.cjs",
149+
"types": "./dist/types/index.d.ts",
150+
},
151+
"./package.json": "./package.json",
133152
},
134-
"./package.json": "./package.json"
135-
}
136-
}, indent=2)
153+
},
154+
indent=2,
155+
)
137156

138157
# This should pass validation (no duplicates in suggestion itself)
139-
is_valid, msg = validate_json_suggestion(temp_path, suggestion_with_potential_duplicates, 1, 1)
158+
is_valid, msg = validate_json_suggestion(
159+
temp_path, suggestion_with_potential_duplicates, 1, 1
160+
)
140161
assert is_valid, f"Valid suggestion should pass validation: {msg}"
141162
print("✓ Valid suggestion validation works")
142163

143164
# This should be applied successfully
144-
result = apply_json_suggestion(temp_path, suggestion_with_potential_duplicates, 1, 1)
165+
result = apply_json_suggestion(
166+
temp_path, suggestion_with_potential_duplicates, 1, 1
167+
)
145168
assert result, "Valid suggestion should be applied successfully"
146169
print("✓ Valid suggestion application works")
147170

148171
# Verify the file was updated correctly
149-
with open(temp_path, 'r') as f:
172+
with open(temp_path) as f:
150173
updated_data = json.load(f)
151174

152175
assert "exports" in updated_data, "Exports field should be added"
153176
assert updated_data["main"] == "dist/index.cjs", "Main field should be updated"
154-
assert updated_data["name"] == "@contextforge/memory-client", "Name should be preserved"
177+
assert (
178+
updated_data["name"] == "@contextforge/memory-client"
179+
), "Name should be preserved"
155180
print("✓ File was updated correctly without duplicates")
156181

157182
finally:
158183
# Clean up
159184
Path(temp_path).unlink()
160185

186+
161187
if __name__ == "__main__":
162188
print("Testing JSON Handler for CodeRabbit Suggestions")
163189
print("=" * 50)

0 commit comments

Comments
 (0)