-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
181 lines (172 loc) · 4.56 KB
/
tests.py
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
from unittest import TestCase
from solution import solution
from exceptions import IllegalNode, IllegalDependency
class TestSolution(TestCase):
def test_example(self):
links = [
('s1','s3'),
('s1','d1'),
('s1','d2'),
('s2','d4'),
('s2','d3'),
('s3','s2'),
('s3','d1'),
('s2','s4'),
('d3','d2'),
('s4','d3')
]
expected = {
's1': {
'launch': ['d2','d1'],
'next': ['s3']
},
's2': {
'launch': ['d2', 'd3', 'd4'],
'next': ['s4']
},
's3': {
'launch': ['d1'],
'next': ['s2']
},
's4': {
'launch': ['d2', 'd3'],
'next': None
}
}
self.assertEqual(solution(links), expected)
def test_empty_list(self):
self.assertEqual(solution([]), {})
def test_single_link(self):
self.assertEqual(solution([('s1', 'd1')]), {'s1': {'launch': ['d1'], 'next': None}})
def test_nested_dependencies(self):
links = [
('s1','s2'),
('s1','d1'),
('s1','d2'),
('s1','d3'),
('s2','d3'),
('s2','d4'),
('s2','d5'),
('s2','d6'),
('s2','s3'),
('d4','d1'),
('d5','d2'),
('d6','d3'),
('s3','d7'),
('s3','d8'),
('s3','d9'),
('d7','d8'),
('d8','d9'),
('d8','d5'),
('d9','d1'),
]
expected = {
's1': {
'launch': ['d3','d2', 'd1'],
'next': ['s2']
},
's2': {
'launch': ['d3', 'd6', 'd2', 'd5', 'd1', 'd4'],
'next': ['s3']
},
's3': {
'launch': ['d1', 'd9', 'd2', 'd5', 'd8', 'd7'],
'next': None
}
}
self.assertEqual(solution(links), expected)
def test_illegal_nodes(self):
with self.assertRaises(IllegalNode):
solution([('s1','d1'), ('s1','f1')])
def test_multiple_next_states(self):
links = [
('s1','d1'),
('s1','s2'),
('s1','s3'),
('s2','d2'),
('s3','d3'),
('s2','s4'),
('s4','d4')
]
expected = {
's1': {
'launch': ['d1'],
'next': ['s2', 's3']
},
's2': {
'launch': ['d2'],
'next': ['s4']
},
's3': {
'launch': ['d3'],
'next': None
},
's4': {
'launch': ['d4'],
'next': None
}
}
self.assertEqual(solution(links), expected)
def test_illegal_dependency(self):
with self.assertRaises(IllegalDependency):
solution([('s1','d1'), ('d1','s2')])
def test_two_states_with_same_next_state(self):
links = [
('s1','d1'),
('s1','s2'),
('s1','s3'),
('s2','d2'),
('s2','s3'),
('s3','d3'),
('s2','s4'),
('s4','d4')
]
expected = {
's1': {
'launch': ['d1'],
'next': ['s2', 's3']
},
's2': {
'launch': ['d2'],
'next': ['s3', 's4']
},
's3': {
'launch': ['d3'],
'next': None
},
's4': {
'launch': ['d4'],
'next': None
}
}
self.assertEqual(solution(links), expected)
def test_state_node_loop(self):
links = [
('s1','d1'),
('s1','s2'),
('s2','d2'),
('s2','s3'),
('s3','d3'),
('s3','s1'),
('s2','s4'),
('s4','d4')
]
expected = {
's1': {
'launch': ['d1'],
'next': ['s2']
},
's2': {
'launch': ['d2'],
'next': ['s3','s4']
},
's3': {
'launch': ['d3'],
'next': ['s1']
},
's4': {
'launch': ['d4'],
'next': None
}
}
self.assertEqual(solution(links), expected)