-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
95 lines (84 loc) · 3.23 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
from unittest import TestCase
from pathlib import Path
from graph_tools import (
Color,
ColoredNode,
ColoredGraph
)
class ColoredGraphTest(TestCase):
RED = Color('RED')
GREEN = Color('GREEN')
def test_colored_graph_shortest_path(self):
A = ColoredNode('A', None)
B = ColoredNode('B', None)
C = ColoredNode('C', None)
D = ColoredNode('D', None)
E = ColoredNode('E', None)
F = ColoredNode('F', None)
G = ColoredNode('G', self.GREEN)
H = ColoredNode('H', self.RED)
I = ColoredNode('I', self.GREEN)
colored_graph = ColoredGraph()
# add nodes
colored_graph.add_nodes([
A, B, C,
D, E, F,
G ,H, I
])
# add edges
colored_graph.add_edges([
(A, B), (B, C), (C, D),
(D, E), (E, F), (C, G),
(G, H), (H, I), (I, F)
])
shortest_path_from_A_to_F_with_no_route_color = colored_graph.get_shortest_path_to_node(
source=A, dest=F)
self.assertEqual(
shortest_path_from_A_to_F_with_no_route_color,
[A, B, C, D, E, F])
shortest_path_from_A_to_F_with_red_route = colored_graph.get_shortest_path_to_node(
source=A, dest=F, route_color=self.RED)
self.assertEqual(
shortest_path_from_A_to_F_with_red_route,
[A, B, C, H, F])
shortest_path_from_A_to_F_with_green_route = colored_graph.get_shortest_path_to_node(
source=A, dest=F, route_color=self.GREEN
)
self.assertIn(
shortest_path_from_A_to_F_with_green_route,
[
[A, B, C, D, E, F],
[A, B, C, G, I, F]
])
def test_load_graph_from_json(self):
colored_graph = ColoredGraph()
json_filename = 'test_graph.json'
path_to_json = Path().absolute() / json_filename
colored_graph.load_from_json(path_to_json)
A = colored_graph.get_node_by_label('A')
B = colored_graph.get_node_by_label('B')
C = colored_graph.get_node_by_label('C')
D = colored_graph.get_node_by_label('D')
E = colored_graph.get_node_by_label('E')
F = colored_graph.get_node_by_label('F')
G = colored_graph.get_node_by_label('G')
H = colored_graph.get_node_by_label('H')
I = colored_graph.get_node_by_label('I')
shortest_path_from_A_to_F_with_no_route_color = colored_graph.get_shortest_path_to_node(
source=A, dest=F)
self.assertEqual(
shortest_path_from_A_to_F_with_no_route_color,
[A, B, C, D, E, F])
shortest_path_from_A_to_F_with_red_route = colored_graph.get_shortest_path_to_node(
source=A, dest=F, route_color=self.RED)
self.assertEqual(
shortest_path_from_A_to_F_with_red_route,
[A, B, C, H, F])
shortest_path_from_A_to_F_with_green_route = colored_graph.get_shortest_path_to_node(
source=A, dest=F, route_color=self.GREEN)
self.assertIn(
shortest_path_from_A_to_F_with_green_route,
[
[A, B, C, D, E, F],
[A, B, C, G, I, F]
])