MCPcopy
hub / github.com/django/django / _test_run_python

Method _test_run_python

tests/migrations/test_multidb.py:146–171  ·  view source on GitHub ↗
(self, app_label, should_run, hints=None)

Source from the content-addressed store, hash-verified

144 self._test_run_sql("test_mltdb_runsql3", should_run=True, hints={"foo": True})
145
146 def _test_run_python(self, app_label, should_run, hints=None):
147 with override_settings(DATABASE_ROUTERS=[MigrateEverythingRouter()]):
148 project_state = self.set_up_test_model(app_label)
149
150 # Create the operation
151 def inner_method(models, schema_editor):
152 Pony = models.get_model(app_label, "Pony")
153 Pony.objects.create(pink=1, weight=3.55)
154 Pony.objects.create(weight=5)
155
156 operation = migrations.RunPython(inner_method, hints=hints or {})
157 # Test the state alteration does nothing
158 new_state = project_state.clone()
159 operation.state_forwards(app_label, new_state)
160 self.assertEqual(new_state, project_state)
161 # Test the database alteration
162 self.assertEqual(
163 project_state.apps.get_model(app_label, "Pony").objects.count(), 0
164 )
165 with connection.schema_editor() as editor:
166 operation.database_forwards(app_label, editor, project_state, new_state)
167 Pony = project_state.apps.get_model(app_label, "Pony")
168 if should_run:
169 self.assertEqual(Pony.objects.count(), 2)
170 else:
171 self.assertEqual(Pony.objects.count(), 0)
172
173 @override_settings(DATABASE_ROUTERS=[MigrateNothingRouter()])
174 def test_run_python_migrate_nothing_router(self):

Calls 9

state_forwardsMethod · 0.95
database_forwardsMethod · 0.95
override_settingsClass · 0.90
schema_editorMethod · 0.80
set_up_test_modelMethod · 0.45
cloneMethod · 0.45
countMethod · 0.45
get_modelMethod · 0.45

Tested by

no test coverage detected