MCPcopy
hub / github.com/django/django / test_create_model

Method test_create_model

tests/migrations/test_operations.py:37–78  ·  view source on GitHub ↗

Tests the CreateModel operation. Most other tests use this operation as part of setup, so check failures here first.

(self)

Source from the content-addressed store, hash-verified

35 """
36
37 def test_create_model(self):
38 """
39 Tests the CreateModel operation.
40 Most other tests use this operation as part of setup, so check failures
41 here first.
42 """
43 operation = migrations.CreateModel(
44 "Pony",
45 [
46 ("id", models.AutoField(primary_key=True)),
47 ("pink", models.IntegerField(default=1)),
48 ],
49 )
50 self.assertEqual(operation.describe(), "Create model Pony")
51 self.assertEqual(operation.formatted_description(), "+ Create model Pony")
52 self.assertEqual(operation.migration_name_fragment, "pony")
53 # Test the state alteration
54 project_state = ProjectState()
55 new_state = project_state.clone()
56 operation.state_forwards("test_crmo", new_state)
57 self.assertEqual(new_state.models["test_crmo", "pony"].name, "Pony")
58 self.assertEqual(len(new_state.models["test_crmo", "pony"].fields), 2)
59 # Test the database alteration
60 self.assertTableNotExists("test_crmo_pony")
61 with connection.schema_editor() as editor:
62 operation.database_forwards("test_crmo", editor, project_state, new_state)
63 self.assertTableExists("test_crmo_pony")
64 # And test reversal
65 with connection.schema_editor() as editor:
66 operation.database_backwards("test_crmo", editor, new_state, project_state)
67 self.assertTableNotExists("test_crmo_pony")
68 # And deconstruction
69 definition = operation.deconstruct()
70 self.assertEqual(definition[0], "CreateModel")
71 self.assertEqual(definition[1], [])
72 self.assertEqual(sorted(definition[2]), ["fields", "name"])
73 # And default manager not in set
74 operation = migrations.CreateModel(
75 "Foo", fields=[], managers=[("objects", models.Manager())]
76 )
77 definition = operation.deconstruct()
78 self.assertNotIn("managers", definition[2])
79
80 def test_create_model_with_duplicate_field_name(self):
81 with self.assertRaisesMessage(

Callers

nothing calls this directly

Calls 11

describeMethod · 0.95
cloneMethod · 0.95
state_forwardsMethod · 0.95
database_forwardsMethod · 0.95
database_backwardsMethod · 0.95
deconstructMethod · 0.95
ProjectStateClass · 0.90
formatted_descriptionMethod · 0.80
schema_editorMethod · 0.80
assertTableNotExistsMethod · 0.45
assertTableExistsMethod · 0.45

Tested by

no test coverage detected