Skip to content

Commit 06eda46

Browse files
authored
Docs cleanup (#26)
* Docs cleanup * Automated pre-commit update * fixes * Automated pre-commit update --------- Co-authored-by: kopekC <[email protected]>
1 parent 84f9279 commit 06eda46

File tree

18 files changed

+93
-357
lines changed

18 files changed

+93
-357
lines changed

flask_to_fastapi_migration/guide.md

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

python2_to_python3/guide.md

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

python2_to_python3/run.py

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

34
# Initialize codebase
4-
codebase = Codebase("./")
55

66
# Define the target directory
7-
TARGET_DIR = "repo-before"
7+
TARGET_DIR = "input_repo"
88

99

1010
def convert_print_statements(file):
@@ -115,7 +115,8 @@ def update_iterators(file):
115115
stmt.edit(new_stmt)
116116

117117

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,6 @@ def main():
149150

150151

151152
if __name__ == "__main__":
152-
main()
153+
codebase = Codebase("./")
154+
155+
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.
File renamed without changes.

unittest_to_pytest/repo-before/jj_classes/castle.py renamed to unittest_to_pytest/input_repo/jj_classes/castle.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# jj_classes/castle.py
22

3+
34
class Castle:
45
"""Defines the Castle class."""
56

unittest_to_pytest/repo-after/jj_classes/character.py renamed to unittest_to_pytest/input_repo/jj_classes/character.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# jj_classes/character.py
22

3+
34
class Character:
45
"""Defines the Character class."""
56

File renamed without changes.

0 commit comments

Comments
 (0)