2015-03-29 00:46:10 +08:00
|
|
|
import warnings
|
2015-02-20 12:43:45 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.migrations.graph import (
|
2015-03-29 00:46:10 +08:00
|
|
|
RECURSION_DEPTH_WARNING, CircularDependencyError, MigrationGraph,
|
|
|
|
NodeNotFoundError,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2013-05-30 00:47:10 +08:00
|
|
|
from django.test import TestCase
|
2014-12-18 07:41:36 +08:00
|
|
|
from django.utils.encoding import force_text
|
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)
|
2014-08-22 15:26:06 +08:00
|
|
|
graph.add_dependency("app_a.0004", ("app_a", "0004"), ("app_a", "0003"))
|
|
|
|
graph.add_dependency("app_a.0003", ("app_a", "0003"), ("app_a", "0002"))
|
|
|
|
graph.add_dependency("app_a.0002", ("app_a", "0002"), ("app_a", "0001"))
|
|
|
|
graph.add_dependency("app_a.0003", ("app_a", "0003"), ("app_b", "0002"))
|
|
|
|
graph.add_dependency("app_b.0002", ("app_b", "0002"), ("app_b", "0001"))
|
2013-05-10 19:52:04 +08:00
|
|
|
# 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(),
|
2014-06-18 14:27:03 +08:00
|
|
|
[('app_a', '0001'), ('app_b', '0001')],
|
2013-05-10 23:09:57 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
graph.leaf_nodes(),
|
2014-06-18 14:27:03 +08:00
|
|
|
[('app_a', '0004'), ('app_b', '0002')],
|
2013-05-10 23:09:57 +08:00
|
|
|
)
|
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)
|
2014-08-22 15:26:06 +08:00
|
|
|
graph.add_dependency("app_a.0004", ("app_a", "0004"), ("app_a", "0003"))
|
|
|
|
graph.add_dependency("app_a.0003", ("app_a", "0003"), ("app_a", "0002"))
|
|
|
|
graph.add_dependency("app_a.0002", ("app_a", "0002"), ("app_a", "0001"))
|
|
|
|
graph.add_dependency("app_a.0003", ("app_a", "0003"), ("app_b", "0002"))
|
|
|
|
graph.add_dependency("app_b.0002", ("app_b", "0002"), ("app_b", "0001"))
|
|
|
|
graph.add_dependency("app_a.0004", ("app_a", "0004"), ("app_c", "0002"))
|
|
|
|
graph.add_dependency("app_c.0002", ("app_c", "0002"), ("app_c", "0001"))
|
|
|
|
graph.add_dependency("app_c.0001", ("app_c", "0001"), ("app_b", "0001"))
|
|
|
|
graph.add_dependency("app_c.0002", ("app_c", "0002"), ("app_a", "0002"))
|
2013-05-10 19:52:04 +08:00
|
|
|
# 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(),
|
2014-06-18 14:27:03 +08:00
|
|
|
[('app_a', '0001'), ('app_b', '0001'), ('app_c', '0001')],
|
2013-05-10 23:09:57 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
graph.leaf_nodes(),
|
2014-06-18 14:27:03 +08:00
|
|
|
[('app_a', '0004'), ('app_b', '0002'), ('app_c', '0002')],
|
2013-05-10 23:09:57 +08:00
|
|
|
)
|
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)
|
2014-08-22 15:26:06 +08:00
|
|
|
graph.add_dependency("app_a.0003", ("app_a", "0003"), ("app_a", "0002"))
|
|
|
|
graph.add_dependency("app_a.0002", ("app_a", "0002"), ("app_a", "0001"))
|
|
|
|
graph.add_dependency("app_a.0001", ("app_a", "0001"), ("app_b", "0002"))
|
|
|
|
graph.add_dependency("app_b.0002", ("app_b", "0002"), ("app_b", "0001"))
|
|
|
|
graph.add_dependency("app_b.0001", ("app_b", "0001"), ("app_a", "0003"))
|
2013-05-10 19:52:04 +08:00
|
|
|
# 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"),
|
|
|
|
)
|
2014-08-23 17:37:25 +08:00
|
|
|
|
2014-11-25 21:29:38 +08:00
|
|
|
def test_circular_graph_2(self):
|
|
|
|
graph = MigrationGraph()
|
|
|
|
graph.add_node(('A', '0001'), None)
|
|
|
|
graph.add_node(('C', '0001'), None)
|
|
|
|
graph.add_node(('B', '0001'), None)
|
2014-11-26 00:20:40 +08:00
|
|
|
graph.add_dependency('A.0001', ('A', '0001'), ('B', '0001'))
|
|
|
|
graph.add_dependency('B.0001', ('B', '0001'), ('A', '0001'))
|
|
|
|
graph.add_dependency('C.0001', ('C', '0001'), ('B', '0001'))
|
2014-11-25 21:29:38 +08:00
|
|
|
|
|
|
|
self.assertRaises(
|
|
|
|
CircularDependencyError,
|
|
|
|
graph.forwards_plan, ('C', '0001')
|
|
|
|
)
|
|
|
|
|
2015-02-23 21:29:28 +08:00
|
|
|
def test_graph_recursive(self):
|
2015-02-20 12:43:45 +08:00
|
|
|
graph = MigrationGraph()
|
|
|
|
root = ("app_a", "1")
|
|
|
|
graph.add_node(root, None)
|
|
|
|
expected = [root]
|
|
|
|
for i in range(2, 750):
|
|
|
|
parent = ("app_a", str(i - 1))
|
|
|
|
child = ("app_a", str(i))
|
|
|
|
graph.add_node(child, None)
|
|
|
|
graph.add_dependency(str(i), child, parent)
|
|
|
|
expected.append(child)
|
2015-03-29 00:46:10 +08:00
|
|
|
leaf = expected[-1]
|
2015-02-20 12:43:45 +08:00
|
|
|
|
2015-03-29 00:46:10 +08:00
|
|
|
forwards_plan = graph.forwards_plan(leaf)
|
|
|
|
self.assertEqual(expected, forwards_plan)
|
|
|
|
|
|
|
|
backwards_plan = graph.backwards_plan(root)
|
|
|
|
self.assertEqual(expected[::-1], backwards_plan)
|
2015-02-20 12:43:45 +08:00
|
|
|
|
2015-02-23 21:29:28 +08:00
|
|
|
def test_graph_iterative(self):
|
2014-09-06 06:26:05 +08:00
|
|
|
graph = MigrationGraph()
|
|
|
|
root = ("app_a", "1")
|
|
|
|
graph.add_node(root, None)
|
|
|
|
expected = [root]
|
2014-09-06 06:47:20 +08:00
|
|
|
for i in range(2, 1000):
|
2014-09-06 06:26:05 +08:00
|
|
|
parent = ("app_a", str(i - 1))
|
|
|
|
child = ("app_a", str(i))
|
|
|
|
graph.add_node(child, None)
|
|
|
|
graph.add_dependency(str(i), child, parent)
|
|
|
|
expected.append(child)
|
2015-03-29 00:46:10 +08:00
|
|
|
leaf = expected[-1]
|
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
forwards_plan = graph.forwards_plan(leaf)
|
|
|
|
|
|
|
|
self.assertEqual(len(w), 1)
|
|
|
|
self.assertTrue(issubclass(w[-1].category, RuntimeWarning))
|
|
|
|
self.assertEqual(str(w[-1].message), RECURSION_DEPTH_WARNING)
|
|
|
|
self.assertEqual(expected, forwards_plan)
|
|
|
|
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
backwards_plan = graph.backwards_plan(root)
|
2014-09-06 06:26:05 +08:00
|
|
|
|
2015-03-29 00:46:10 +08:00
|
|
|
self.assertEqual(len(w), 1)
|
|
|
|
self.assertTrue(issubclass(w[-1].category, RuntimeWarning))
|
|
|
|
self.assertEqual(str(w[-1].message), RECURSION_DEPTH_WARNING)
|
|
|
|
self.assertEqual(expected[::-1], backwards_plan)
|
2014-09-06 06:26:05 +08:00
|
|
|
|
2014-08-23 17:37:25 +08:00
|
|
|
def test_plan_invalid_node(self):
|
|
|
|
"""
|
|
|
|
Tests for forwards/backwards_plan of nonexistent node.
|
|
|
|
"""
|
|
|
|
graph = MigrationGraph()
|
|
|
|
message = "Node ('app_b', '0001') not a valid node"
|
|
|
|
|
2014-09-26 06:24:17 +08:00
|
|
|
with self.assertRaisesMessage(NodeNotFoundError, message):
|
2014-08-23 17:37:25 +08:00
|
|
|
graph.forwards_plan(("app_b", "0001"))
|
|
|
|
|
2014-09-26 06:24:17 +08:00
|
|
|
with self.assertRaisesMessage(NodeNotFoundError, message):
|
2014-08-23 17:37:25 +08:00
|
|
|
graph.backwards_plan(("app_b", "0001"))
|
2014-08-22 15:26:06 +08:00
|
|
|
|
|
|
|
def test_missing_parent_nodes(self):
|
|
|
|
"""
|
|
|
|
Tests for missing parent nodes.
|
|
|
|
"""
|
|
|
|
# Build graph
|
|
|
|
graph = MigrationGraph()
|
|
|
|
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_dependency("app_a.0003", ("app_a", "0003"), ("app_a", "0002"))
|
|
|
|
graph.add_dependency("app_a.0002", ("app_a", "0002"), ("app_a", "0001"))
|
2014-09-10 21:54:28 +08:00
|
|
|
msg = "Migration app_a.0001 dependencies reference nonexistent parent node ('app_b', '0002')"
|
2014-09-26 06:24:17 +08:00
|
|
|
with self.assertRaisesMessage(NodeNotFoundError, msg):
|
2014-08-22 15:26:06 +08:00
|
|
|
graph.add_dependency("app_a.0001", ("app_a", "0001"), ("app_b", "0002"))
|
|
|
|
|
|
|
|
def test_missing_child_nodes(self):
|
|
|
|
"""
|
|
|
|
Tests for missing child nodes.
|
|
|
|
"""
|
|
|
|
# Build graph
|
|
|
|
graph = MigrationGraph()
|
|
|
|
graph.add_node(("app_a", "0001"), None)
|
2014-09-10 21:54:28 +08:00
|
|
|
msg = "Migration app_a.0002 dependencies reference nonexistent child node ('app_a', '0002')"
|
2014-09-26 06:24:17 +08:00
|
|
|
with self.assertRaisesMessage(NodeNotFoundError, msg):
|
2014-08-22 15:26:06 +08:00
|
|
|
graph.add_dependency("app_a.0002", ("app_a", "0002"), ("app_a", "0001"))
|
2014-11-16 00:37:13 +08:00
|
|
|
|
|
|
|
def test_infinite_loop(self):
|
|
|
|
"""
|
|
|
|
Tests a complex dependency graph:
|
|
|
|
|
|
|
|
app_a: 0001 <-
|
|
|
|
\
|
|
|
|
app_b: 0001 <- x 0002 <-
|
|
|
|
/ \
|
|
|
|
app_c: 0001<- <------------- x 0002
|
|
|
|
|
2014-12-18 07:41:36 +08:00
|
|
|
And apply squashing on app_c.
|
2014-11-16 00:37:13 +08:00
|
|
|
"""
|
|
|
|
graph = MigrationGraph()
|
|
|
|
|
|
|
|
graph.add_node(("app_a", "0001"), None)
|
|
|
|
graph.add_node(("app_b", "0001"), None)
|
|
|
|
graph.add_node(("app_b", "0002"), None)
|
|
|
|
graph.add_node(("app_c", "0001_squashed_0002"), None)
|
|
|
|
|
|
|
|
graph.add_dependency("app_b.0001", ("app_b", "0001"), ("app_c", "0001_squashed_0002"))
|
|
|
|
graph.add_dependency("app_b.0002", ("app_b", "0002"), ("app_a", "0001"))
|
|
|
|
graph.add_dependency("app_b.0002", ("app_b", "0002"), ("app_b", "0001"))
|
|
|
|
graph.add_dependency("app_c.0001_squashed_0002", ("app_c", "0001_squashed_0002"), ("app_b", "0002"))
|
|
|
|
|
|
|
|
with self.assertRaises(CircularDependencyError):
|
|
|
|
graph.forwards_plan(("app_c", "0001_squashed_0002"))
|
2014-12-18 07:41:36 +08:00
|
|
|
|
|
|
|
def test_stringify(self):
|
|
|
|
graph = MigrationGraph()
|
|
|
|
self.assertEqual(force_text(graph), "Graph: 0 nodes, 0 edges")
|
|
|
|
|
|
|
|
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)
|
|
|
|
graph.add_dependency("app_a.0002", ("app_a", "0002"), ("app_a", "0001"))
|
|
|
|
graph.add_dependency("app_a.0003", ("app_a", "0003"), ("app_a", "0002"))
|
|
|
|
graph.add_dependency("app_a.0003", ("app_a", "0003"), ("app_b", "0002"))
|
|
|
|
|
|
|
|
self.assertEqual(force_text(graph), "Graph: 5 nodes, 3 edges")
|