2013-05-30 00:47:10 +08:00
|
|
|
from django.test import TestCase
|
2013-05-10 23:00:55 +08:00
|
|
|
from django.db.migrations.graph import MigrationGraph, CircularDependencyError
|
2013-05-10 19:52:04 +08:00
|
|
|
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
class GraphTests(TestCase):
|
2013-05-10 19:52:04 +08:00
|
|
|
"""
|
|
|
|
Tests the digraph structure.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def test_simple_graph(self):
|
|
|
|
"""
|
|
|
|
Tests a basic dependency graph:
|
|
|
|
|
|
|
|
app_a: 0001 <-- 0002 <--- 0003 <-- 0004
|
|
|
|
/
|
|
|
|
app_b: 0001 <-- 0002 <-/
|
|
|
|
"""
|
|
|
|
# Build graph
|
2013-05-10 23:00:55 +08:00
|
|
|
graph = MigrationGraph()
|
2013-05-31 00:56:53 +08:00
|
|
|
graph.add_node(("app_a", "0001"), None)
|
|
|
|
graph.add_node(("app_a", "0002"), None)
|
|
|
|
graph.add_node(("app_a", "0003"), None)
|
|
|
|
graph.add_node(("app_a", "0004"), None)
|
|
|
|
graph.add_node(("app_b", "0001"), None)
|
|
|
|
graph.add_node(("app_b", "0002"), None)
|
2013-05-10 19:52:04 +08:00
|
|
|
graph.add_dependency(("app_a", "0004"), ("app_a", "0003"))
|
|
|
|
graph.add_dependency(("app_a", "0003"), ("app_a", "0002"))
|
|
|
|
graph.add_dependency(("app_a", "0002"), ("app_a", "0001"))
|
|
|
|
graph.add_dependency(("app_a", "0003"), ("app_b", "0002"))
|
|
|
|
graph.add_dependency(("app_b", "0002"), ("app_b", "0001"))
|
|
|
|
# Test root migration case
|
|
|
|
self.assertEqual(
|
|
|
|
graph.forwards_plan(("app_a", "0001")),
|
|
|
|
[('app_a', '0001')],
|
|
|
|
)
|
|
|
|
# Test branch B only
|
|
|
|
self.assertEqual(
|
|
|
|
graph.forwards_plan(("app_b", "0002")),
|
|
|
|
[("app_b", "0001"), ("app_b", "0002")],
|
|
|
|
)
|
|
|
|
# Test whole graph
|
|
|
|
self.assertEqual(
|
|
|
|
graph.forwards_plan(("app_a", "0004")),
|
|
|
|
[('app_b', '0001'), ('app_b', '0002'), ('app_a', '0001'), ('app_a', '0002'), ('app_a', '0003'), ('app_a', '0004')],
|
|
|
|
)
|
|
|
|
# Test reverse to b:0002
|
|
|
|
self.assertEqual(
|
|
|
|
graph.backwards_plan(("app_b", "0002")),
|
|
|
|
[('app_a', '0004'), ('app_a', '0003'), ('app_b', '0002')],
|
|
|
|
)
|
2013-05-10 23:09:57 +08:00
|
|
|
# Test roots and leaves
|
|
|
|
self.assertEqual(
|
|
|
|
graph.root_nodes(),
|
|
|
|
set([('app_a', '0001'), ('app_b', '0001')]),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
graph.leaf_nodes(),
|
|
|
|
set([('app_a', '0004'), ('app_b', '0002')]),
|
|
|
|
)
|
2013-05-10 19:52:04 +08:00
|
|
|
|
|
|
|
def test_complex_graph(self):
|
|
|
|
"""
|
|
|
|
Tests a complex dependency graph:
|
|
|
|
|
|
|
|
app_a: 0001 <-- 0002 <--- 0003 <-- 0004
|
|
|
|
\ \ / /
|
|
|
|
app_b: 0001 <-\ 0002 <-X /
|
|
|
|
\ \ /
|
|
|
|
app_c: \ 0001 <-- 0002 <-
|
|
|
|
"""
|
|
|
|
# Build graph
|
2013-05-10 23:00:55 +08:00
|
|
|
graph = MigrationGraph()
|
2013-05-31 00:56:53 +08:00
|
|
|
graph.add_node(("app_a", "0001"), None)
|
|
|
|
graph.add_node(("app_a", "0002"), None)
|
|
|
|
graph.add_node(("app_a", "0003"), None)
|
|
|
|
graph.add_node(("app_a", "0004"), None)
|
|
|
|
graph.add_node(("app_b", "0001"), None)
|
|
|
|
graph.add_node(("app_b", "0002"), None)
|
|
|
|
graph.add_node(("app_c", "0001"), None)
|
|
|
|
graph.add_node(("app_c", "0002"), None)
|
2013-05-10 19:52:04 +08:00
|
|
|
graph.add_dependency(("app_a", "0004"), ("app_a", "0003"))
|
|
|
|
graph.add_dependency(("app_a", "0003"), ("app_a", "0002"))
|
|
|
|
graph.add_dependency(("app_a", "0002"), ("app_a", "0001"))
|
|
|
|
graph.add_dependency(("app_a", "0003"), ("app_b", "0002"))
|
|
|
|
graph.add_dependency(("app_b", "0002"), ("app_b", "0001"))
|
|
|
|
graph.add_dependency(("app_a", "0004"), ("app_c", "0002"))
|
|
|
|
graph.add_dependency(("app_c", "0002"), ("app_c", "0001"))
|
|
|
|
graph.add_dependency(("app_c", "0001"), ("app_b", "0001"))
|
|
|
|
graph.add_dependency(("app_c", "0002"), ("app_a", "0002"))
|
|
|
|
# Test branch C only
|
|
|
|
self.assertEqual(
|
|
|
|
graph.forwards_plan(("app_c", "0002")),
|
|
|
|
[('app_b', '0001'), ('app_c', '0001'), ('app_a', '0001'), ('app_a', '0002'), ('app_c', '0002')],
|
|
|
|
)
|
|
|
|
# Test whole graph
|
|
|
|
self.assertEqual(
|
|
|
|
graph.forwards_plan(("app_a", "0004")),
|
|
|
|
[('app_b', '0001'), ('app_c', '0001'), ('app_a', '0001'), ('app_a', '0002'), ('app_c', '0002'), ('app_b', '0002'), ('app_a', '0003'), ('app_a', '0004')],
|
|
|
|
)
|
|
|
|
# Test reverse to b:0001
|
|
|
|
self.assertEqual(
|
|
|
|
graph.backwards_plan(("app_b", "0001")),
|
|
|
|
[('app_a', '0004'), ('app_c', '0002'), ('app_c', '0001'), ('app_a', '0003'), ('app_b', '0002'), ('app_b', '0001')],
|
|
|
|
)
|
2013-05-10 23:09:57 +08:00
|
|
|
# Test roots and leaves
|
|
|
|
self.assertEqual(
|
|
|
|
graph.root_nodes(),
|
|
|
|
set([('app_a', '0001'), ('app_b', '0001'), ('app_c', '0001')]),
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
graph.leaf_nodes(),
|
|
|
|
set([('app_a', '0004'), ('app_b', '0002'), ('app_c', '0002')]),
|
|
|
|
)
|
2013-05-10 19:52:04 +08:00
|
|
|
|
|
|
|
def test_circular_graph(self):
|
|
|
|
"""
|
|
|
|
Tests a circular dependency graph.
|
|
|
|
"""
|
|
|
|
# Build graph
|
2013-05-10 23:00:55 +08:00
|
|
|
graph = MigrationGraph()
|
2013-05-31 00:56:53 +08:00
|
|
|
graph.add_node(("app_a", "0001"), None)
|
|
|
|
graph.add_node(("app_a", "0002"), None)
|
|
|
|
graph.add_node(("app_a", "0003"), None)
|
|
|
|
graph.add_node(("app_b", "0001"), None)
|
|
|
|
graph.add_node(("app_b", "0002"), None)
|
2013-05-10 19:52:04 +08:00
|
|
|
graph.add_dependency(("app_a", "0003"), ("app_a", "0002"))
|
|
|
|
graph.add_dependency(("app_a", "0002"), ("app_a", "0001"))
|
|
|
|
graph.add_dependency(("app_a", "0001"), ("app_b", "0002"))
|
|
|
|
graph.add_dependency(("app_b", "0002"), ("app_b", "0001"))
|
|
|
|
graph.add_dependency(("app_b", "0001"), ("app_a", "0003"))
|
|
|
|
# Test whole graph
|
|
|
|
self.assertRaises(
|
2013-05-10 23:00:55 +08:00
|
|
|
CircularDependencyError,
|
2013-05-10 19:52:04 +08:00
|
|
|
graph.forwards_plan, ("app_a", "0003"),
|
|
|
|
)
|