diff options
| author | Matthias Kestenholz <mk@feinheit.ch> | 2019-02-24 13:08:59 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2019-02-27 17:16:48 -0500 |
| commit | 77e53da127cfb5d4f0c9a3540a02ff24f04fe9e2 (patch) | |
| tree | c5232cbd37b89992db48755f30d2106e683d0ef8 /tests/utils_tests | |
| parent | d29c8ea124d4c19e23f73b8a307dd177d8a1fe7b (diff) | |
[2.2.x] Refs #30179 -- Moved topological sort functions to django.utils.
Backport of e04209e181c99ac16ca769d115ac640015a83757 from master.
Diffstat (limited to 'tests/utils_tests')
| -rw-r--r-- | tests/utils_tests/test_topological_sort.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/tests/utils_tests/test_topological_sort.py b/tests/utils_tests/test_topological_sort.py new file mode 100644 index 0000000000..ed8f9fd5a6 --- /dev/null +++ b/tests/utils_tests/test_topological_sort.py @@ -0,0 +1,24 @@ +from django.test import SimpleTestCase +from django.utils.topological_sort import ( + CyclicDependencyError, stable_topological_sort, topological_sort_as_sets, +) + + +class TopologicalSortTests(SimpleTestCase): + + def test_basic(self): + dependency_graph = { + 1: {2, 3}, + 2: set(), + 3: set(), + 4: {5, 6}, + 5: set(), + 6: {5}, + } + self.assertEqual(list(topological_sort_as_sets(dependency_graph)), [{2, 3, 5}, {1, 6}, {4}]) + self.assertEqual(stable_topological_sort([1, 2, 3, 4, 5, 6], dependency_graph), [2, 3, 5, 1, 6, 4]) + + def test_cyclic_dependency(self): + msg = 'Cyclic dependency in graph: ' + with self.assertRaisesMessage(CyclicDependencyError, msg): + list(topological_sort_as_sets({1: {2}, 2: {1}})) |
