This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_adcp.py
233 lines (173 loc) · 7.7 KB
/
test_adcp.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import os
import unittest
from py2neo import Graph, Node, Relationship, order, size
from adcp import Adcp
def load_test_nodes(neo4j: Graph):
nodes = dict()
tx = neo4j.begin()
with open('tests/nodes.tsv', 'r') as ifile:
for line in ifile.readlines():
item, item_type = line.strip().split('\t')
node = Node(item_type, name=item, is_test_node=True)
nodes[item] = node
tx.create(node)
tx.commit()
return nodes
def delete_test_nodes(neo4j: Graph):
neo4j.run('MATCH (n) WHERE n.is_test_node DETACH DELETE n')
return
def load_test_relations(neo4j: Graph, nodes: dict, filename: str):
relations = []
tx = neo4j.begin()
with open('tests/{}.tsv'.format(filename), 'r') as ifile:
for line in ifile.readlines():
source, dest, rel = line.strip().split('\t')
relation = Relationship(nodes[source], rel, nodes[dest], is_test_relation=True)
relations.append(relation)
tx.create(relation)
tx.commit()
return relations
class TestAdcp(object):
class BaseTest(unittest.TestCase):
_adcp = None # type: Adcp
def test_id(self):
node = self._adcp.id('TEST_1')
self.assertIsNotNone(node)
node = self._adcp.id('TEST_15')
self.assertIsNotNone(node)
node = self._adcp.id('TEST_100')
self.assertIsNone(node)
def test_search(self):
results = [x for x in self._adcp.search('TEST_1', operator='=')]
self.assertEqual(len(results), 1)
results = [x for x in self._adcp.search('TEST_1')]
# Nodes 1 and 10 to 19
self.assertEqual(len(results), 11)
class TestAdcp1(TestAdcp.BaseTest):
@classmethod
def setUpClass(cls):
neo4j_uri = os.environ.get('NEO4J_URI', 'bolt://localhost')
cls._adcp = Adcp(neo4j_uri=neo4j_uri)
nodes = load_test_nodes(cls._adcp.neo4j)
relations = load_test_relations(cls._adcp.neo4j, nodes, 'rels_1')
@classmethod
def tearDownClass(cls):
delete_test_nodes(cls._adcp.neo4j)
def test_empty_control_path(self):
base_node = [x for x in self._adcp.search('TEST_11', operator='=')][0]
self.assertIsNotNone(base_node)
graph = self._adcp.control_graph(base_node, 'to')
self.assertIsNone(graph)
def test_control_path(self):
base_node = [x for x in self._adcp.search('TEST_1', operator='=')][0]
self.assertIsNotNone(base_node)
graph = self._adcp.control_graph(base_node, 'to')
# 1 GROUP : Nodes TEST_1 and TEST_10 to TEST_19
self.assertEqual(order(graph), 11)
# 1 GROUP Links from (TEST_10 to TEST_19) to TEST_1
self.assertEqual(size(graph), 10)
class TestAdcp2(TestAdcp.BaseTest):
@classmethod
def setUpClass(cls):
neo4j_uri = os.environ.get('NEO4J_URI', 'bolt://localhost')
cls._adcp = Adcp(neo4j_uri=neo4j_uri, dump_directory='tests/2')
nodes = load_test_nodes(cls._adcp.neo4j)
relations = load_test_relations(cls._adcp.neo4j, nodes, 'rels_2')
@classmethod
def tearDownClass(cls):
delete_test_nodes(cls._adcp.neo4j)
def test_empty_control_path(self):
base_node = [x for x in self._adcp.search('TEST_11', operator='=')][0]
self.assertIsNotNone(base_node)
graph = self._adcp.control_graph(base_node, 'to')
self.assertIsNone(graph)
def test_control_path(self):
base_node = [x for x in self._adcp.search('TEST_1', operator='=')][0]
self.assertIsNotNone(base_node)
graph = self._adcp.control_graph(base_node, 'to')
# 2 * GROUPS (11 + 11)
self.assertEqual(order(graph), 22)
# 2 * GROUPS (10 + 10) + link TEST_1 -> TEST_2 + link DENY
self.assertEqual(size(graph), 22)
self.assertEqual(len([x for x in graph.nodes() if x['NO_LINKS']]), 1)
self.assertEqual(len([x for x in graph.relationships() if x['DENY']]), 1)
class TestAdcp3(TestAdcp.BaseTest):
@classmethod
def setUpClass(cls):
neo4j_uri = os.environ.get('NEO4J_URI', 'bolt://localhost')
cls._adcp = Adcp(neo4j_uri=neo4j_uri, dump_directory='tests/3')
nodes = load_test_nodes(cls._adcp.neo4j)
relations = load_test_relations(cls._adcp.neo4j, nodes, 'rels_3')
@classmethod
def tearDownClass(cls):
delete_test_nodes(cls._adcp.neo4j)
def test_empty_control_path(self):
base_node = [x for x in self._adcp.search('TEST_11', operator='=')][0]
self.assertIsNotNone(base_node)
graph = self._adcp.control_graph(base_node, 'to')
self.assertIsNone(graph)
def test_control_path(self):
base_node = [x for x in self._adcp.search('TEST_1', operator='=')][0]
self.assertIsNotNone(base_node)
graph = self._adcp.control_graph(base_node, 'to')
# 4 * GROUPS (4 * 11)
self.assertEqual(order(graph), 44)
# 4 * GROUPS (4 * 10) + 3 Links + link DENY
self.assertEqual(size(graph), 44)
self.assertEqual(len([x for x in graph.nodes() if x['NO_LINKS']]), 1)
self.assertEqual(len([x for x in graph.relationships() if x['DENY']]), 1)
class TestAdcp4(TestAdcp.BaseTest):
@classmethod
def setUpClass(cls):
neo4j_uri = os.environ.get('NEO4J_URI', 'bolt://localhost')
cls._adcp = Adcp(neo4j_uri=neo4j_uri, dump_directory='tests/4')
nodes = load_test_nodes(cls._adcp.neo4j)
relations = load_test_relations(cls._adcp.neo4j, nodes, 'rels_4')
@classmethod
def tearDownClass(cls):
delete_test_nodes(cls._adcp.neo4j)
def test_empty_control_path(self):
base_node = [x for x in self._adcp.search('TEST_11', operator='=')][0]
self.assertIsNotNone(base_node)
graph = self._adcp.control_graph(base_node, 'to')
self.assertIsNone(graph)
def test_control_path(self):
base_node = [x for x in self._adcp.search('TEST_1', operator='=')][0]
self.assertIsNotNone(base_node)
graph = self._adcp.control_graph(base_node, 'to')
# 9 * GROUPS (9 * 11)
self.assertEqual(order(graph), 99)
# 9 * GROUPS (9 * 10) + 10 Links + link DENY
self.assertEqual(size(graph), 100)
self.assertEqual(len([x for x in graph.nodes() if x['NO_LINKS']]), 1)
self.assertEqual(len([x for x in graph.relationships() if x['DENY']]), 1)
class TestAdcp5(TestAdcp.BaseTest):
@classmethod
def setUpClass(cls):
neo4j_uri = os.environ.get('NEO4J_URI', 'bolt://localhost')
cls._adcp = Adcp(neo4j_uri=neo4j_uri, dump_directory='tests/5')
nodes = load_test_nodes(cls._adcp.neo4j)
relations = load_test_relations(cls._adcp.neo4j, nodes, 'rels_5')
@classmethod
def tearDownClass(cls):
delete_test_nodes(cls._adcp.neo4j)
pass
def test_empty_control_path(self):
base_node = [x for x in self._adcp.search('TEST_11', operator='=')][0]
self.assertIsNotNone(base_node)
graph = self._adcp.control_graph(base_node, 'to')
self.assertIsNone(graph)
def test_control_path(self):
base_node = [x for x in self._adcp.search('TEST_1', operator='=')][0]
self.assertIsNotNone(base_node)
graph = self._adcp.control_graph(base_node, 'to')
# 9 * GROUPS (9 * 11)
self.assertEqual(order(graph), 99)
# 9 * GROUPS (9 * 10) + 10 Links + link DENY
self.assertEqual(size(graph), 100)
# Only 1 node with no link (TEST_28)
self.assertEqual(len([x for x in graph.nodes() if x['NO_LINKS']]), 1)
# 2 relations with DENY (TEST_27 and TEST_28)
self.assertEqual(len([x for x in graph.relationships() if x['DENY']]), 2)
if __name__ == '__main__':
unittest.main()