File: test_toposort.py

package info (click to toggle)
toposort 1.5-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 120 kB
  • sloc: python: 213; sh: 20; makefile: 6
file content (201 lines) | stat: -rw-r--r-- 7,432 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#######################################################################
# Tests for toposort module.
#
# Copyright 2014 True Blade Systems, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Notes:
#
########################################################################

import unittest

from toposort import toposort, toposort_flatten, CircularDependencyError

class TestCase(unittest.TestCase):
    def test_simple(self):
        self.assertEqual(list(toposort({2: {11},
                                        9: {11, 8},
                                        10: {11, 3},
                                        11: {7, 5},
                                        8: {7, 3},
                                        })),
                         [{3, 5, 7},
                          {8, 11},
                          {2, 9, 10},
                          ])

        # make sure self dependencies are ignored
        self.assertEqual(list(toposort({2: {2, 11},
                                        9: {11, 8},
                                        10: {10, 11, 3},
                                        11: {7, 5},
                                        8: {7, 3},
                                        })),
                         [{3, 5, 7},
                          {8, 11},
                          {2, 9, 10},
                          ])

        self.assertEqual(list(toposort({1: set()})),
                         [{1}])
        self.assertEqual(list(toposort({1: {1}})),
                         [{1}])

    def test_no_dependencies(self):
        self.assertEqual(list(toposort({1: {2},
                                        3: {4},
                                        5: {6},
                                        })),
                         [{2, 4, 6}, {1, 3, 5}])

        self.assertEqual(list(toposort({1: set(),
                                        3: set(),
                                        5: set(),
                                        })),
                         [{1, 3, 5}])

    def test_empty(self):
        self.assertEqual(list(toposort({})),
                         [])

    def test_strings(self):
        self.assertEqual(list(toposort({'2': {'11'},
                                        '9': {'11', '8'},
                                        '10': {'11', '3'},
                                        '11': {'7', '5'},
                                        '8': {'7', '3'},
                                        })),
                         [{'3', '5', '7'},
                          {'8', '11'},
                          {'2', '9', '10'},
                          ])

    def test_objects(self):
        o2 = object()
        o3 = object()
        o5 = object()
        o7 = object()
        o8 = object()
        o9 = object()
        o10 = object()
        o11 = object()
        self.assertEqual(list(toposort({o2: {o11},
                                        o9: {o11, o8},
                                        o10: {o11, o3},
                                        o11: {o7, o5},
                                        o8: {o7, o3, o8},
                                        })),
                         [{o3, o5, o7},
                          {o8, o11},
                          {o2, o9, o10},
                          ])

    def test_cycle(self):
        # a simple, 2 element cycle
        # make sure we can catch this both as ValueError and CircularDependencyError
        self.assertRaises(ValueError, list, toposort({1: {2},
                                                      2: {1}
                                                      }))
        with self.assertRaises(CircularDependencyError) as ex:
            list(toposort({1: {2},
                           2: {1}
                           }))
        self.assertEqual(ex.exception.data, {1: {2}, 2: {1}})

        # an indirect cycle
        self.assertRaises(ValueError, list, toposort({1: {2},
                                                      2: {3},
                                                      3: {1},
                                                      }))
        with self.assertRaises(CircularDependencyError) as ex:
            list(toposort({1: {2},
                           2: {3},
                           3: {1},
                           }))
        self.assertEqual(ex.exception.data, {1: {2}, 2: {3}, 3: {1}})

        # not all elements involved in a cycle
        with self.assertRaises(CircularDependencyError) as ex:
            list(toposort({1: {2},
                           2: {3},
                           3: {1},
                           5: {4},
                           4: {6},
                           }))
        self.assertEqual(ex.exception.data, {1: set([2]), 2: set([3]), 3: set([1])})

    def test_input_not_modified(self):
        data = {2: {11},
                9: {11, 8},
                10: {11, 3},
                11: {7, 5},
                8: {7, 3, 8},  # includes something self-referential
                }
        orig = data.copy()
        results = list(toposort(data))
        self.assertEqual(data, orig)

    def test_input_not_modified_when_cycle_error(self):
        data = {1: {2},
                2: {1},
                3: {4},
                }
        orig = data.copy()
        self.assertRaises(ValueError, list, toposort(data))
        self.assertEqual(data, orig)


class TestCaseAll(unittest.TestCase):
    def test_sort_flatten(self):
        data = {2: {11},
                9: {11, 8},
                10: {11, 3},
                11: {7, 5},
                8: {7, 3, 8},  # includes something self-referential
                }
        expected = [{3, 5, 7}, {8, 11}, {2, 9, 10}]
        self.assertEqual(list(toposort(data)), expected)

        # now check the sorted results
        results = []
        for item in expected:
            results.extend(sorted(item))
        self.assertEqual(toposort_flatten(data), results)

        # and the unsorted results. break the results up into groups to compare them
        actual = toposort_flatten(data, False)
        results = [{i for i in actual[0:3]}, {i for i in actual[3:5]}, {i for i in actual[5:8]}]
        self.assertEqual(results, expected)


class TestAll(unittest.TestCase):
    def test_all(self):
        import toposort

        # check that __all__ in the module contains everything that should be
        #  public, and only those symbols
        all = set(toposort.__all__)

        # check that things in __all__ only appear once
        self.assertEqual(len(all), len(toposort.__all__),
                         'some symbols appear more than once in __all__')

        # get the list of public symbols
        found = set(name for name in dir(toposort) if not name.startswith('_'))

        # make sure it matches __all__
        self.assertEqual(all, found)

unittest.main()