-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_graph.py
208 lines (189 loc) · 4.91 KB
/
test_graph.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import pytest
from graph import Node
from graph import Graph
def test_empty_node():
"""
tests creation of empty node
"""
a = Node({'A':[]})
assert isinstance(a, Node)
assert len(a.connections) == 0
def test_error_node():
"""
tests creation of bad node
Node({'a':'a'})
have to pass since the test is written to test error
"""
with pytest.raises(Exception):
a = Node({'a':'a'})
def test_good_node():
"""
tests creation of good node
Node({'A':['B','C']})
"""
a = Node({'A':['B', 'C']})
assert isinstance(a, Node)
assert isinstance(a.connections, list)
def test_node_str():
"""
test that node representation of string is
"{'name':['list','of','vertex']}"
"""
a = Node({'A':['B','C']})
assert str(a) == "{'A':['B', 'C']}"
def test_empty_graph():
"""
tests creation of empty graph
"""
a = Graph([])
assert isinstance(a, Graph)
assert len(a) == 0
def test_graph_with_list():
"""
tests creation of the graph with a list of nodes
"""
node_list = []
node_list.append(Node({'A':['B','C']}))
node_list.append(Node({'B':['C','D']}))
node_list.append(Node({'C':['D']}))
node_list.append(Node({'D':['C']}))
a = Graph(node_list)
assert isinstance(a, Graph)
assert len(a) == 4
def test_graph_with_list_fail():
"""
tests creation of the graph with a list
this is an improper list and throws an error
error should be raised
"""
with pytest.raises(Exception) as e_info:
node_list = ["slippery list"]
node_list.append(Node({'A':"['B','C']"}))
node_list.append(Node({'B':['C','D']}))
node_list.append(Node({'C':['D']}))
node_list.append(Node({'D':['C']}))
Graph(node_list)
def test_graph_with_list_of_not_nodes():
"""
extra test added by me
tests creation of graph by a proper list but not of type node
error should be raised
"""
with pytest.raises(Exception):
node_list = [1, 2, 3, 4]
Graph(node_list)
def test_add_to_graph():
"""
create graph and add nodes in a loop
"""
a = Graph([])
a.add(Node({'A':['B','C']}))
a.add(Node({'B':['A','C']}))
a.add(Node({'C':['B','A']}))
assert isinstance(a, Graph)
assert len(a) == 3
def test_add_not_type_node():
"""
test added by me
tests type checking of what you are adding to the Graph
error should be raised
"""
with pytest.raises(Exception):
a = Graph([])
a.add(1)
def test_graph_str():
"""
test string representation of the graphs
should be
"[{'A':"['B','C']"}, {'B':['C','D']}, {'C':['D']}]"
"""
node_list = []
node_list.append(Node({'A':['B','C']}))
node_list.append(Node({'B':['C','D']}))
node_list.append(Node({'C':['D']}))
a = Graph(node_list)
assert str(a) == "[{'A':['B', 'C']}, {'B':['C', 'D']}, {'C':['D']}]"
def test_find_path_dfs():
"""
you should test with graphs that have 0, 1, 3 paths
"""
node_list = []
a = Node({'A':['B','C']})
b = Node({'B':['C','D']})
c = Node({'C':['D']})
d = Node({'D':[]})
e = Node({'F':[]})
node_list.append(a)
node_list.append(b)
node_list.append(c)
node_list.append(d)
node_list.append(e)
g = Graph(node_list)
assert g.find_path_dfs(a,e) == None
assert g.print_path(g.find_path_dfs(a,b)) == 'A, C, D, B'
assert g.print_path(g.find_path_dfs(a,c)) == 'A, C'
assert g.print_path(g.find_path_dfs(a,a)) == 'A'
# def test_find_path_bi():
# """
# you should test with graphs that have 0, 1, 3 paths
# """
# pass
def test_find_all_paths():
"""
you should test with graphs that have 0, 1, 3 paths
"""
node_list = []
a = Node({'A':['B','C']})
b = Node({'B':['C','D']})
c = Node({'C':['D']})
d = Node({'D':[]})
e = Node({'F':[]})
node_list.append(a)
node_list.append(b)
node_list.append(c)
node_list.append(d)
node_list.append(e)
g = Graph(node_list)
assert [str(obj.name) for path in g.find_all_paths(a,a) for obj in path] == ['A']
assert [str(obj.name) for path in g.find_all_paths(a,b) for obj in path] == ['A','B']
assert [str(obj.name) for path in g.find_all_paths(a,c) for obj in path] == ['A','B','C','A','C']
def test_find_shortest_path():
"""
you should test with graphs that have 0, 1, 3 paths
"""
node_list = []
a = Node({'A':['B','C']})
b = Node({'B':['C','D']})
c = Node({'C':['D']})
d = Node({'D':[]})
e = Node({'F':[]})
node_list.append(a)
node_list.append(b)
node_list.append(c)
node_list.append(d)
node_list.append(e)
g = Graph(node_list)
assert g.find_shortest_path(a,e) == None
assert g.print_path(g.find_shortest_path(a,b)) == 'A, B'
assert g.print_path(g.find_shortest_path(a,d)) == 'A, B, C, D'
assert g.print_path(g.find_shortest_path(a,a)) == 'A'
def test_has_route():
"""
you should test with graphs that have 0, 1, 3 paths
"""
node_list = []
a = Node({'A':['B','C']})
b = Node({'B':['C','D']})
c = Node({'C':['D']})
d = Node({'D':[]})
e = Node({'F':[]})
node_list.append(a)
node_list.append(b)
node_list.append(c)
node_list.append(d)
node_list.append(e)
g = Graph(node_list)
assert g.has_route(a,e) == False
assert g.has_route(a,b) == True
assert g.has_route(a,d) == True
assert g.has_route(a,a) == True