MCPcopy
hub / github.com/django/django / test_create_model_m2m

Method test_create_model_m2m

tests/migrations/test_operations.py:234–273  ·  view source on GitHub ↗

Test the creation of a model with a ManyToMany field and the auto-created "through" model.

(self)

Source from the content-addressed store, hash-verified

232 self.assertTableExists("test_crmoua_rider")
233
234 def test_create_model_m2m(self):
235 """
236 Test the creation of a model with a ManyToMany field and the
237 auto-created "through" model.
238 """
239 project_state = self.set_up_test_model("test_crmomm")
240 operation = migrations.CreateModel(
241 "Stable",
242 [
243 ("id", models.AutoField(primary_key=True)),
244 ("ponies", models.ManyToManyField("Pony", related_name="stables")),
245 ],
246 )
247 # Test the state alteration
248 new_state = project_state.clone()
249 operation.state_forwards("test_crmomm", new_state)
250 # Test the database alteration
251 self.assertTableNotExists("test_crmomm_stable_ponies")
252 with connection.schema_editor() as editor:
253 operation.database_forwards("test_crmomm", editor, project_state, new_state)
254 self.assertTableExists("test_crmomm_stable")
255 self.assertTableExists("test_crmomm_stable_ponies")
256 self.assertColumnNotExists("test_crmomm_stable", "ponies")
257 # Make sure the M2M field actually works
258 with atomic():
259 Pony = new_state.apps.get_model("test_crmomm", "Pony")
260 Stable = new_state.apps.get_model("test_crmomm", "Stable")
261 stable = Stable.objects.create()
262 p1 = Pony.objects.create(pink=False, weight=4.55)
263 p2 = Pony.objects.create(pink=True, weight=5.43)
264 stable.ponies.add(p1, p2)
265 self.assertEqual(stable.ponies.count(), 2)
266 stable.ponies.all().delete()
267 # And test reversal
268 with connection.schema_editor() as editor:
269 operation.database_backwards(
270 "test_crmomm", editor, new_state, project_state
271 )
272 self.assertTableNotExists("test_crmomm_stable")
273 self.assertTableNotExists("test_crmomm_stable_ponies")
274
275 @skipUnlessDBFeature("supports_collation_on_charfield", "supports_foreign_keys")
276 def test_create_fk_models_to_pk_field_db_collation(self):

Callers

nothing calls this directly

Calls 15

state_forwardsMethod · 0.95
database_forwardsMethod · 0.95
database_backwardsMethod · 0.95
atomicFunction · 0.90
schema_editorMethod · 0.80
set_up_test_modelMethod · 0.45
cloneMethod · 0.45
assertTableNotExistsMethod · 0.45
assertTableExistsMethod · 0.45
assertColumnNotExistsMethod · 0.45
get_modelMethod · 0.45
createMethod · 0.45

Tested by

no test coverage detected