Skip to content

Commit 04a198e

Browse files
committed
Docs cleanup
1 parent 1b773d5 commit 04a198e

File tree

17 files changed

+86
-467
lines changed

17 files changed

+86
-467
lines changed

flask_to_fastapi_migration/guide.md

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,61 @@
1+
import codegen
12
from codegen import Codebase
2-
codebase = Codebase.from_repo("getmoto/moto", commit="786a8ada7ed0c7f9d8b04d49f24596865e4b7901")
3-
4-
print("🚀 Starting FreezeGun to TimeMachine conversion...")
5-
6-
for file in codebase.files:
7-
if "tests" not in file.filepath:
8-
continue
9-
print(f"📝 Processing: {file.filepath}")
10-
# Update imports
11-
for imp in file.imports:
12-
if imp.symbol_name and 'freezegun' in imp.source:
13-
if imp.name == 'freeze_time':
14-
# required due to Codegen limitations
15-
imp.edit('from time_machine import travel')
16-
else:
17-
imp.set_import_module('time_machine')
18-
# Find all function calls in the file
19-
for fcall in file.function_calls:
20-
# Skip if not a freeze_time call
21-
if 'freeze_time' not in fcall.source:
3+
4+
@codegen.function("freezegun-to-timemachine")
5+
def run(codebase: Codebase):
6+
"""Convert FreezeGun usage to TimeMachine in test files.
7+
8+
This script:
9+
1. Identifies test files using FreezeGun.
10+
2. Updates imports from FreezeGun to TimeMachine.
11+
3. Modifies function calls to include necessary parameters.
12+
"""
13+
print("🚀 Starting FreezeGun to TimeMachine conversion...")
14+
15+
for file in codebase.files:
16+
if "tests" not in file.filepath:
2217
continue
23-
# Get original source and prepare new source
24-
new_source = fcall.source
25-
# Add tick parameter if not present
26-
if not fcall.get_arg_by_parameter_name('tick'):
27-
if new_source.endswith(')'):
28-
new_source = new_source[:-1]
29-
if not new_source.endswith('('):
30-
new_source += ','
31-
new_source += ' tick=False)'
32-
# Replace freeze_time with travel
33-
if '.' in new_source:
34-
new_source = new_source.replace(
35-
'freeze_time', 'travel').replace('freezegun', 'time_machine')
36-
else:
37-
new_source = 'travel' + new_source[len('freeze_time'):]
38-
# Make single edit with complete changes
39-
fcall.edit(new_source)
40-
codebase.commit()
41-
42-
print("✅ FreezeGun to TimeMachine conversion completed successfully!")
18+
print(f"📝 Processing: {file.filepath}")
19+
20+
# Update imports
21+
for imp in file.imports:
22+
if imp.symbol_name and 'freezegun' in imp.source:
23+
if imp.name == 'freeze_time':
24+
# required due to Codegen limitations
25+
imp.edit('from time_machine import travel')
26+
else:
27+
imp.set_import_module('time_machine')
28+
29+
# Find all function calls in the file
30+
for fcall in file.function_calls:
31+
# Skip if not a freeze_time call
32+
if 'freeze_time' not in fcall.source:
33+
continue
34+
35+
# Get original source and prepare new source
36+
new_source = fcall.source
37+
38+
# Add tick parameter if not present
39+
if not fcall.get_arg_by_parameter_name('tick'):
40+
if new_source.endswith(')'):
41+
new_source = new_source[:-1]
42+
if not new_source.endswith('('):
43+
new_source += ','
44+
new_source += ' tick=False)'
45+
46+
# Replace freeze_time with travel
47+
if '.' in new_source:
48+
new_source = new_source.replace(
49+
'freeze_time', 'travel').replace('freezegun', 'time_machine')
50+
else:
51+
new_source = 'travel' + new_source[len('freeze_time'):]
52+
53+
# Make single edit with complete changes
54+
fcall.edit(new_source)
55+
56+
codebase.commit()
57+
print("✅ FreezeGun to TimeMachine conversion completed successfully!")
58+
59+
if __name__ == "__main__":
60+
codebase = Codebase.from_repo("getmoto/moto", commit="786a8ada7ed0c7f9d8b04d49f24596865e4b7901")
61+
run(codebase)

python2_to_python3/run.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import codegen
12
from codegen import Codebase
23

34
# Initialize codebase
45
codebase = Codebase("./")
56

67
# Define the target directory
7-
TARGET_DIR = "repo-before"
8+
TARGET_DIR = "input_repo"
89

910

1011
def convert_print_statements(file):
@@ -114,8 +115,8 @@ def update_iterators(file):
114115
new_stmt = new_stmt.rstrip() + ")"
115116
stmt.edit(new_stmt)
116117

117-
118-
def main():
118+
@codegen.function("python2-to-python3")
119+
def run():
119120
"""Main function to run the Python 2 to 3 conversion"""
120121
print("🚀 Starting Python 2 to 3 conversion...\n")
121122

@@ -149,4 +150,4 @@ def main():
149150

150151

151152
if __name__ == "__main__":
152-
main()
153+
run(codebase)

unittest_to_pytest/README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# unittest to pytest Migration Example
1+
# Unittest to Pytest Migration Example
22

3-
[![Documentation](https://img.shields.io/badge/docs-docs.codegen.com-blue)](https://docs.codegen.com/tutorials/unittest-to-pytest)
3+
This codemod demonstrates how to automatically migrate `unittest` test suites to `pytest` using Codegen. The migration script simplifies the process by handling all the tedious manual updates automatically.
44

5-
This example demonstrates how to use Codegen to automatically migrate unittest test suites to pytest. For a complete walkthrough, check out our [tutorial](https://docs.codegen.com/tutorials/unittest-to-pytest).
5+
## How the Migration Script Works
66

7-
## What This Example Does
8-
9-
The migration script handles four key transformations:
7+
The script automates the entire migration process in a few key steps:
108

119
1. **Convert Test Classes and Setup Methods**
1210
```python
@@ -29,6 +27,8 @@ The migration script handles four key transformations:
2927
user = db.create_user("test")
3028
assert user.name == "test"
3129
```
30+
- Converts `unittest.TestCase` classes to standalone functions
31+
- Replaces `setUp` methods with `pytest` fixtures
3232

3333
2. **Update Assertions**
3434
```python
@@ -45,6 +45,8 @@ The migration script handles four key transformations:
4545
with pytest.raises(ValueError):
4646
parse_id("invalid")
4747
```
48+
- Replaces `unittest` assertions with `pytest` assertions
49+
- Uses `pytest.raises` for exception testing
4850

4951
3. **Convert Test Discovery**
5052
```python
@@ -55,6 +57,8 @@ The migration script handles four key transformations:
5557
# To:
5658
# Remove unittest.main() and rename files to test_*.py
5759
```
60+
- Removes `unittest.main()` calls
61+
- Ensures files are named for `pytest` discovery
5862

5963
4. **Modernize Fixtures**
6064
```python
@@ -68,8 +72,9 @@ The migration script handles four key transformations:
6872
def conn():
6973
return create_db()
7074
```
75+
- Converts class-level setup to session-scoped fixtures
7176

72-
## Running the Example
77+
## Running the Migration
7378

7479
```bash
7580
# Install Codegen
@@ -84,12 +89,16 @@ The script will process all Python test files in the `repo-before` directory and
8489
## Understanding the Code
8590

8691
- `run.py` - The migration script
87-
- `repo-before/` - Sample unittest test suite to migrate
92+
- `repo-before/` - Sample `unittest` test suite to migrate
8893
- `guide.md` - Additional notes and explanations
8994

9095
## Learn More
9196

9297
- [Full Tutorial](https://docs.codegen.com/tutorials/unittest-to-pytest)
9398
- [pytest Documentation](https://docs.pytest.org/)
9499
- [unittest Documentation](https://docs.python.org/3/library/unittest.html)
95-
- [Codegen Documentation](https://docs.codegen.com)
100+
- [Codegen Documentation](https://docs.codegen.com)
101+
102+
## Contributing
103+
104+
Feel free to submit issues and enhancement requests!

unittest_to_pytest/guide.md

Lines changed: 0 additions & 41 deletions
This file was deleted.

unittest_to_pytest/repo-after/jj_classes/__init__.py

Whitespace-only changes.

unittest_to_pytest/repo-after/jj_classes/castle.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

unittest_to_pytest/repo-after/jj_classes/character.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

unittest_to_pytest/repo-after/tests/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)