Skip to content

Commit 5c93771

Browse files
committed
docs: update docs
1 parent 7f79f3b commit 5c93771

File tree

1 file changed

+170
-0
lines changed

1 file changed

+170
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# Plan: Update CookieCutter Test Template to Use For Loop Instead of Parametrize
2+
3+
## Overview
4+
5+
Update the cookiecutter template for `test_solution.py` to use a for loop approach for test cases instead of the current `@pytest.mark.parametrize` decorator. This will make the test structure more explicit and easier to read.
6+
7+
## Current State Analysis
8+
9+
### Current Template Structure
10+
11+
- Uses `@pytest.mark.parametrize` decorator with `method.parametrize` and `method.test_cases`
12+
- Test cases are stored as a single string in JSON: `"[([1, 2, 3], [1, 2, 3], True), ...]"`
13+
- Generated test method signature includes all parameters: `(self, p_list: list[int | None], q_list: list[int | None], expected: bool)`
14+
15+
### Current JSON Structure
16+
17+
```json
18+
"_test_methods": {
19+
"list": [
20+
{
21+
"name": "test_is_same_tree",
22+
"signature": "(self, p_list: list[int | None], q_list: list[int | None], expected: bool)",
23+
"parametrize": "p_list, q_list, expected",
24+
"test_cases": "[([1, 2, 3], [1, 2, 3], True), ([1, 2], [1, None, 2], False), ...]",
25+
"body": "result = run_is_same_tree(Solution, p_list, q_list)\nassert_is_same_tree(result, expected)"
26+
}
27+
]
28+
}
29+
```
30+
31+
## Target State
32+
33+
### New Template Structure
34+
35+
- Keep `@pytest.mark.parametrize` decorator
36+
- Use a for loop to iterate through individual test cases from the list
37+
- Test method signature remains the same
38+
- Only change: `test_cases` becomes a list instead of a string
39+
40+
### New JSON Structure
41+
42+
```json
43+
"_test_methods": {
44+
"list": [
45+
{
46+
"name": "test_is_same_tree",
47+
"signature": "(self, p_list: list[int | None], q_list: list[int | None], expected: bool)",
48+
"parametrize": "p_list, q_list, expected",
49+
"test_cases": {
50+
"list": [
51+
"([1, 2, 3], [1, 2, 3], True)",
52+
"([1, 2], [1, None, 2], False)",
53+
"([1, 2, 1], [1, 1, 2], False)"
54+
]
55+
},
56+
"body": "result = run_is_same_tree(Solution, p_list, q_list)\nassert_is_same_tree(result, expected)"
57+
}
58+
]
59+
}
60+
```
61+
62+
## Implementation Plan
63+
64+
### Phase 1: Update CookieCutter Template
65+
66+
1. **Update `test_solution.py` template**
67+
- Change from `{{method.test_cases}}` to `{{method.test_cases.list | join(', ')}}`
68+
- This follows the existing pattern used by other list fields in the template
69+
- Template line 30: `@pytest.mark.parametrize("{{method.parametrize}}", [{{method.test_cases.list | join(', ')}}])`
70+
71+
### Phase 2: Create JSON Migration Script
72+
73+
1. **Create `migrate_test_cases.py` script**
74+
- Parse existing JSON files in `leetcode_py/cli/resources/leetcode/json/problems/`
75+
- Convert `test_cases` string to `{"list": ["test_case1", "test_case2", ...]}` format
76+
- Keep all other fields exactly the same
77+
- Update only the `test_cases` field structure
78+
79+
### Phase 3: Update JSON Files
80+
81+
1. **Run migration script on all existing problem JSON files**
82+
- Process all 107 JSON files in the problems directory
83+
- Create backup of original files
84+
- Validate migrated JSON structure
85+
86+
### Phase 4: Update Template Generation Logic
87+
88+
1. **Modify cookiecutter generation code**
89+
- Update any code that generates the `test_cases` field to output JSON arrays instead of strings
90+
- Ensure new list format is used when creating new problems
91+
- Keep all other generation logic the same
92+
93+
### Phase 5: Testing and Validation
94+
95+
1. **Test generated templates**
96+
- Generate a test problem using updated template
97+
- Verify test cases run correctly with parametrize approach
98+
- Ensure JSON list format works with pytest parametrize
99+
100+
2. **Comprehensive validation with all problems**
101+
- Copy additional LeetCode problems to `.cache/leetcode` (if not already present)
102+
- Regenerate ALL problems from new design using updated template
103+
- Copy existing solutions from cache to regenerated problems
104+
- Run tests on all regenerated problems to ensure they still pass
105+
- Verify no regressions in test functionality
106+
- Document any issues found and fix them
107+
108+
### Phase 6: Update Documentation
109+
110+
1. **Update problem creation documentation**
111+
- Update `.cursor/commands/problem-creation.md` to reflect new JSON structure
112+
- Change `test_cases` format from string to `{"list": [...]}` in examples
113+
- Update template examples to show new format
114+
- Add note about migration for existing problems
115+
2. **Update any other related documentation**
116+
- Check for other docs that reference `test_cases` format
117+
- Update examples in README or other guides if needed
118+
119+
## Benefits of New Approach
120+
121+
1. **Cleaner JSON Structure**: Test cases as `{"list": [...]}` object instead of single string
122+
2. **Better Maintainability**: Easier to edit individual test cases in JSON files
123+
3. **No Parsing Issues**: Avoids tuple conversion and complex JSON parsing
124+
4. **Consistency**: Aligns with other JSON list fields in the structure
125+
5. **Template Consistency**: Uses same pattern as other list fields (`| join(', ')`)
126+
127+
## Files to Modify
128+
129+
### Template Files
130+
131+
- `leetcode_py/cli/resources/leetcode/{{cookiecutter.problem_name}}/test_solution.py`
132+
133+
### Migration Script
134+
135+
- `migrate_test_cases.py` (new file)
136+
137+
### JSON Files
138+
139+
- All files in `leetcode_py/cli/resources/leetcode/json/problems/` (107 files)
140+
141+
### Generation Code
142+
143+
- Any code that creates the `test_cases` field (to be identified)
144+
145+
## Implementation Steps
146+
147+
1. ✅ Create this plan document
148+
2. 🔄 Update cookiecutter template
149+
3. 🔄 Create migration script
150+
4. 🔄 Run migration on all JSON files
151+
5. 🔄 Test updated template generation
152+
6. 🔄 Validate all tests still work
153+
7. 🔄 Update documentation (separate step - do not combine with implementation)
154+
155+
## Risk Mitigation
156+
157+
1. **Backup Strategy**: Create backups of all JSON files before migration
158+
2. **Incremental Testing**: Test migration on a few files first
159+
3. **Rollback Plan**: Keep original template and migration script for rollback
160+
4. **Validation**: Ensure all existing tests still pass after migration
161+
162+
## Success Criteria
163+
164+
1. All existing JSON files successfully migrated to new list format
165+
2. Generated test templates work with JSON array format for test_cases
166+
3. All existing tests continue to pass with parametrize approach
167+
4. JSON structure is cleaner and more maintainable
168+
5. Template generation works correctly for new problems
169+
6. **All regenerated problems pass their tests** (comprehensive validation)
170+
7. No regressions in test functionality across the entire problem set

0 commit comments

Comments
 (0)