mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Auto-naming for migrations and some writer fixes
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
from django.test import TransactionTestCase
|
||||
from django.db.migrations.autodetector import MigrationAutodetector
|
||||
from django.db.migrations.state import ProjectState, ModelState
|
||||
from django.db.migrations.graph import MigrationGraph
|
||||
from django.db import models
|
||||
|
||||
|
||||
@@ -11,6 +12,8 @@ class AutodetectorTests(TransactionTestCase):
|
||||
"""
|
||||
|
||||
author_empty = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True))])
|
||||
other_pony = ModelState("otherapp", "Pony", [("id", models.AutoField(primary_key=True))])
|
||||
other_stable = ModelState("otherapp", "Stable", [("id", models.AutoField(primary_key=True))])
|
||||
|
||||
def make_project_state(self, model_states):
|
||||
"Shortcut to make ProjectStates from lists of predefined models"
|
||||
@@ -19,6 +22,28 @@ class AutodetectorTests(TransactionTestCase):
|
||||
project_state.add_model_state(model_state)
|
||||
return project_state
|
||||
|
||||
def test_arrange_for_graph(self):
|
||||
"Tests auto-naming of migrations for graph matching."
|
||||
# Make a fake graph
|
||||
graph = MigrationGraph()
|
||||
graph.add_node(("testapp", "0001_initial"), None)
|
||||
graph.add_node(("testapp", "0002_foobar"), None)
|
||||
graph.add_node(("otherapp", "0001_initial"), None)
|
||||
graph.add_dependency(("testapp", "0002_foobar"), ("testapp", "0001_initial"))
|
||||
graph.add_dependency(("testapp", "0002_foobar"), ("otherapp", "0001_initial"))
|
||||
# Use project state to make a new migration change set
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([self.author_empty, self.other_pony, self.other_stable])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector.changes()
|
||||
# Run through arrange_for_graph
|
||||
changes = autodetector.arrange_for_graph(changes, graph)
|
||||
# Make sure there's a new name, deps match, etc.
|
||||
self.assertEqual(changes["testapp"][0].name, "0003_author")
|
||||
self.assertEqual(changes["testapp"][0].dependencies, [("testapp", "0002_foobar")])
|
||||
self.assertEqual(changes["otherapp"][0].name, "0002_pony_stable")
|
||||
self.assertEqual(changes["otherapp"][0].dependencies, [("otherapp", "0001_initial")])
|
||||
|
||||
def test_new_model(self):
|
||||
"Tests autodetection of new models"
|
||||
# Make state
|
||||
@@ -27,9 +52,9 @@ class AutodetectorTests(TransactionTestCase):
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector.changes()
|
||||
# Right number of migrations?
|
||||
self.assertEqual(len(changes), 1)
|
||||
self.assertEqual(len(changes['testapp']), 1)
|
||||
# Right number of actions?
|
||||
migration = changes.pop()
|
||||
migration = changes['testapp'][0]
|
||||
self.assertEqual(len(migration.operations), 1)
|
||||
# Right action?
|
||||
action = migration.operations[0]
|
||||
@@ -44,9 +69,9 @@ class AutodetectorTests(TransactionTestCase):
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector.changes()
|
||||
# Right number of migrations?
|
||||
self.assertEqual(len(changes), 1)
|
||||
self.assertEqual(len(changes['testapp']), 1)
|
||||
# Right number of actions?
|
||||
migration = changes.pop()
|
||||
migration = changes['testapp'][0]
|
||||
self.assertEqual(len(migration.operations), 1)
|
||||
# Right action?
|
||||
action = migration.operations[0]
|
||||
|
||||
Reference in New Issue
Block a user