Initial commit
[ta/config-manager.git] / cmframework / test / cmdependencysort_test.py
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import unittest
16 import mock
17 from mock import call
18
19 from cmframework.utils.cmdependencysort import CMDependencySort as Sort
20 from cmframework.apis.cmerror import CMError
21
22
23 class CMDependencySortTest(unittest.TestCase):
24     def test_empty(self):
25         sort = Sort()
26         sorted_list = sort.sort()
27         assert sorted_list == []
28
29     def test_one_no_deps(self):
30         sort = Sort({'a': []})
31         sorted_list = sort.sort()
32         assert sorted_list == ['a']
33
34         sort = Sort(None, {'a': []})
35         sorted_list = sort.sort()
36         assert sorted_list == ['a']
37
38     def test_both_no_deps(self):
39         sort = Sort({'a': []}, {'a': []})
40         sorted_list = sort.sort()
41         assert sorted_list == ['a']
42
43         sort = Sort({'a': [], 'b': []}, {'a': [], 'b': []})
44         sorted_list = sort.sort()
45
46         assert len(sorted_list) == 2
47         assert 'a' in sorted_list
48         assert 'b' in sorted_list
49
50     @staticmethod
51     def _assert_orderings(after, before, sorted_list):
52         for entry, deps in after.iteritems():
53             for dep in deps:
54                 assert sorted_list.index(entry) > sorted_list.index(dep)
55
56         for entry, deps in before.iteritems():
57             for dep in deps:
58                 assert sorted_list.index(entry) < sorted_list.index(dep)
59
60     def test_mandbc(self):
61         after = {'a': ['m'], 'b': ['d']}
62         before = {'a': ['b', 'c', 'd', 'n'], 'b': ['c']}
63         sort = Sort(after, before)
64         sorted_list = sort.sort()
65
66         CMDependencySortTest._assert_orderings(after, before, sorted_list)
67
68     def test_simple_only_after(self):
69         after = {'a': ['b'], 'b': ['c']}
70         before = {}
71         sort = Sort(after)
72         sorted_list = sort.sort()
73
74         CMDependencySortTest._assert_orderings(after, before, sorted_list)
75
76     def test_simple_only_before(self):
77         after = {}
78         before = {'a': ['b'], 'b': ['c']}
79         sort = Sort(None, before)
80         sorted_list = sort.sort()
81
82         CMDependencySortTest._assert_orderings(after, before, sorted_list)
83
84     def test_simple(self):
85         after = {'b': ['a'], 'c': ['b']}
86         before = {'a': ['b'], 'b': ['c']}
87         sort = Sort(after, before)
88         sorted_list = sort.sort()
89
90         CMDependencySortTest._assert_orderings(after, before, sorted_list)
91
92     def test_cycle_before_after(self):
93         after = {'b': ['a']}
94         before = {'b': ['a']}
95         sort = Sort(after, before)
96
97         with self.assertRaises(CMError) as context:
98             sorted_list = sort.sort()
99
100     def test_cycle_only_before(self):
101         after = {}
102         before = {'b': ['a'], 'a': ['b']}
103         sort = Sort(after, before)
104
105         with self.assertRaises(CMError) as context:
106             sorted_list = sort.sort()
107
108     def test_cycle_only_after(self):
109         after = {'b': ['a'], 'a': ['b']}
110         before = {}
111         sort = Sort(after, before)
112
113         with self.assertRaises(CMError) as context:
114             sorted_list = sort.sort()
115
116 if __name__ == '__main__':
117     unittest.main()