This example demonstrates how to use Codegen to automatically migrate test code from FreezeGun to TimeMachine for time mocking. The migration script makes this process simple by handling all the tedious manual updates automatically.
The script (run.py) automates the entire migration process in a few key steps:
-
Codebase Loading
codebase = Codebase.from_repo("getmoto/moto", commit="786a8ada7ed0c7f9d8b04d49f24596865e4b7901")
- Loads your codebase into Codegen's intelligent code analysis engine
- Provides a simple SDK for making codebase-wide changes
- Supports specific commit targeting for version control
-
Test File Detection
if "tests" not in file.filepath: continue
- Automatically identifies test files using Codegen's file APIs
- Skips non-test files to avoid unnecessary processing
- Focuses changes where time mocking is most commonly used
-
Import Updates
for imp in file.imports: if imp.symbol_name and "freezegun" in imp.source: if imp.name == "freeze_time": imp.edit("from time_machine import travel")
- Uses Codegen's import analysis to find and update imports
- Handles both direct and aliased imports
- Preserves import structure and formatting
-
Function Call Transformation
for fcall in file.function_calls: if "freeze_time" not in fcall.source: continue # Transform freeze_time to travel with tick=False
- Uses Codegen's function call analysis to find all usages
- Adds required TimeMachine parameters
- Maintains existing arguments and formatting
-
Zero Manual Updates
- Codegen SDK handles all the file searching and updating
- No tedious copy-paste work
-
Consistent Changes
- Codegen ensures all transformations follow the same patterns
- Maintains code style consistency
-
Safe Transformations
- Codegen validates changes before applying them
- Easy to review and revert if needed
# FreezeGun
@freeze_time("2023-01-01")
def test_function():
pass
# Automatically converted to:
@travel("2023-01-01", tick=False)
def test_function():
pass# FreezeGun
with freeze_time("2023-01-01"):
# test code
# Automatically converted to:
with travel("2023-01-01", tick=False):
# test code# FreezeGun
freezer = freeze_time("2023-01-01")
freezer.start()
freezer.move_to("2023-01-02")
freezer.stop()
# Automatically converted to:
traveller = travel("2023-01-01", tick=False)
traveller.start()
traveller.shift(datetime.timedelta(days=1))
traveller.stop()-
Tick Parameter
- TimeMachine requires explicit tick behavior configuration
- Script automatically adds
tick=Falseto match FreezeGun's default behavior
-
Time Movement
- FreezeGun uses
move_to()with datetime strings - TimeMachine uses
shift()with timedelta objects
- FreezeGun uses
-
Return Values
- FreezeGun's decorator returns the freezer object
- TimeMachine's decorator returns a traveller object
# Install Codegen
pip install codegen
# Run the migration
python run.pyFeel free to submit issues and enhancement requests!