-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_btb.py
287 lines (241 loc) · 15.7 KB
/
test_btb.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# import sys
from filecmp import cmp
from pathlib import Path
import pytest
# TODO consider refactoring to simplify the import
# Modify the path because of the - in the directory
from btb import btb_wrapper
TEST_DIR = Path('test')
OUT_FILE = Path(TEST_DIR, 'output', 'output.txt')
BTB_OUT_FILE = Path(TEST_DIR, 'output', 'btb-output.txt')
DISJOINT_OUT_FILE = Path(TEST_DIR, 'output', 'disjoint-output.txt')
DISJOINT2_OUT_FILE = Path(TEST_DIR, 'output', 'disjoint2-output.txt')
SOURCE_TO_SOURCE_OUT_FILE = Path(TEST_DIR, 'output', 'source-to-source-output.txt')
SOURCE_TO_SOURCE2_OUT_FILE = Path(TEST_DIR, 'output', 'source-to-source2-output.txt')
SOURCE_TO_SOURCE_DISJOINT_OUT_FILE = Path(TEST_DIR, 'output', 'source-to-source-disjoint-output.txt')
BIDIRECTIONAL_OUT_FILE = Path(TEST_DIR, 'output', 'bidirectional-output.txt')
TARGET_TO_SOURCE_OUT_FILE = Path(TEST_DIR, 'output', 'target-to-source-output.txt')
LOOP_OUT_FILE = Path(TEST_DIR, 'output', 'loop-output.txt')
WEIGHTED_OUT_FILE = Path(TEST_DIR, 'output', 'weighted-output.txt')
NO_WEIGHT_OUT_FILE = Path(TEST_DIR, 'output', 'no-weight-output.txt')
WEIGHT_ONE_OUT_FILE = Path(TEST_DIR, 'output', 'weight-one-output.txt')
class TestBowTieBuilder:
"""
Run the BowTieBuilder algorithm on the example input files and check the output matches the expected output
"""
def test_btb(self):
BTB_OUT_FILE.unlink(missing_ok=True)
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'btb-edges.txt'),
source_file=Path(TEST_DIR, 'input', 'btb-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'btb-targets.txt'),
output_file=BTB_OUT_FILE)
assert BTB_OUT_FILE.exists(), 'Output file was not written'
expected_file = Path(TEST_DIR, 'expected_output', 'btb-output.txt')
# Read the content of the output files and expected file into sets
with open(BTB_OUT_FILE, 'r') as output_file:
output_content = set(output_file.read().splitlines())
with open(expected_file, 'r') as expected_output_file:
expected_content = set(expected_output_file.read().splitlines())
# Check if the sets are equal, regardless of the order of lines
assert output_content == expected_content, 'Output file does not match expected output file'
"""
Run the BowTieBuilder algorithm on the example disjoint input files and check the output matches the expected output
"""
def test_disjoint(self):
DISJOINT_OUT_FILE.unlink(missing_ok=True)
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'disjoint-edges.txt'),
source_file=Path(TEST_DIR, 'input', 'disjoint-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'disjoint-targets.txt'),
output_file=DISJOINT_OUT_FILE)
assert DISJOINT_OUT_FILE.exists(), 'Output file was not written'
expected_file = Path(TEST_DIR, 'expected_output', 'disjoint-output.txt')
# Read the content of the output files and expected file into sets
with open(DISJOINT_OUT_FILE, 'r') as output_file:
output_content = set(output_file.read().splitlines())
with open(expected_file, 'r') as expected_output_file:
expected_content = set(expected_output_file.read().splitlines())
# Check if the sets are equal, regardless of the order of lines
assert output_content == expected_content, 'Output file does not match expected output file'
"""
Run the BowTieBuilder algorithm on the example disjoint2 input files and check the output matches the expected output
"""
def test_disjoint2(self):
DISJOINT2_OUT_FILE.unlink(missing_ok=True)
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'disjoint2-edges.txt'),
source_file=Path(TEST_DIR, 'input', 'disjoint-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'disjoint-targets.txt'),
output_file=DISJOINT2_OUT_FILE)
assert DISJOINT2_OUT_FILE.exists(), 'Output file was not written'
expected_file = Path(TEST_DIR, 'expected_output', 'disjoint-output.txt')
# Read the content of the output files and expected file into sets
with open(DISJOINT2_OUT_FILE, 'r') as output_file:
output_content = set(output_file.read().splitlines())
with open(expected_file, 'r') as expected_output_file:
expected_content = set(expected_output_file.read().splitlines())
# Check if the sets are equal, regardless of the order of lines
assert output_content == expected_content, 'Output file does not match expected output file'
"""
Run the BowTieBuilder algorithm with a missing input file
"""
def test_missing_file(self):
with pytest.raises(OSError):
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'missing.txt'),
source_file=Path(TEST_DIR, 'input', 'btb-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'btb-targets.txt'),
output_file=OUT_FILE)
"""
Run the BowTieBuilder algorithm on the example source to source input files and check the output matches the expected output
"""
def test_source_to_source(self):
SOURCE_TO_SOURCE_OUT_FILE.unlink(missing_ok=True)
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'source-to-source-edges.txt'),
source_file=Path(TEST_DIR, 'input', 'btb-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'btb-targets.txt'),
output_file=SOURCE_TO_SOURCE_OUT_FILE)
assert SOURCE_TO_SOURCE_OUT_FILE.exists(), 'Output file was not written'
expected_file = Path(TEST_DIR, 'expected_output', 'source-to-source-output.txt')
# Read the content of the output files and expected file into sets
with open(SOURCE_TO_SOURCE_OUT_FILE, 'r') as output_file:
output_content = set(output_file.read().splitlines())
with open(expected_file, 'r') as expected_output_file:
expected_content = set(expected_output_file.read().splitlines())
# Check if the sets are equal, regardless of the order of lines
assert output_content == expected_content, 'Output file does not match expected output file'
"""
Run the BowTieBuilder algorithm on the example source to source input files and check the output matches the expected output
"""
def test_source_to_source2(self):
SOURCE_TO_SOURCE2_OUT_FILE.unlink(missing_ok=True)
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'source-to-source2-edges.txt'),
source_file=Path(TEST_DIR, 'input', 'btb-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'btb-targets.txt'),
output_file=SOURCE_TO_SOURCE2_OUT_FILE)
assert SOURCE_TO_SOURCE2_OUT_FILE.exists(), 'Output file was not written'
expected_file = Path(TEST_DIR, 'expected_output', 'source-to-source2-output.txt')
# Read the content of the output files and expected file into sets
with open(SOURCE_TO_SOURCE2_OUT_FILE, 'r') as output_file:
output_content = set(output_file.read().splitlines())
with open(expected_file, 'r') as expected_output_file:
expected_content = set(expected_output_file.read().splitlines())
# Check if the sets are equal, regardless of the order of lines
assert output_content == expected_content, 'Output file does not match expected output file'
"""
Run the BowTieBuilder algorithm on two separate source to target paths connected by sources and check the output matches the expected output
"""
def test_source_to_source_disjoint(self):
SOURCE_TO_SOURCE_DISJOINT_OUT_FILE.unlink(missing_ok=True)
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'source-to-source-disjoint-edges.txt'),
source_file=Path(TEST_DIR, 'input', 'btb-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'btb-targets.txt'),
output_file=SOURCE_TO_SOURCE_DISJOINT_OUT_FILE)
assert SOURCE_TO_SOURCE_DISJOINT_OUT_FILE.exists(), 'Output file was not written'
expected_file = Path(TEST_DIR, 'expected_output', 'source-to-source-disjoint-output.txt')
# Read the content of the output files and expected file into sets
with open(SOURCE_TO_SOURCE_DISJOINT_OUT_FILE, 'r') as output_file:
output_content = set(output_file.read().splitlines())
with open(expected_file, 'r') as expected_output_file:
expected_content = set(expected_output_file.read().splitlines())
# Check if the sets are equal, regardless of the order of lines
assert output_content == expected_content, 'Output file does not match expected output file'
"""
Run the BowTieBuilder algorithm on the example bidirectional input files and check the output matches the expected output
"""
def test_bidirectional(self):
BIDIRECTIONAL_OUT_FILE.unlink(missing_ok=True)
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'bidirectional-edges.txt'),
source_file=Path(TEST_DIR, 'input', 'btb-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'btb-targets.txt'),
output_file=BIDIRECTIONAL_OUT_FILE)
assert BIDIRECTIONAL_OUT_FILE.exists(), 'Output file was not written'
expected_file = Path(TEST_DIR, 'expected_output', 'bidirectional-output.txt')
# Read the content of the output files and expected file into sets
with open(BIDIRECTIONAL_OUT_FILE, 'r') as output_file:
output_content = set(output_file.read().splitlines())
with open(expected_file, 'r') as expected_output_file:
expected_content = set(expected_output_file.read().splitlines())
# Check if the sets are equal, regardless of the order of lines
assert output_content == expected_content, 'Output file does not match expected output file'
"""
Run the BowTieBuilder algorithm on the example target to source input files and check the output matches the expected output
"""
def test_target_to_source(self):
TARGET_TO_SOURCE_OUT_FILE.unlink(missing_ok=True)
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'target-to-source-edges.txt'),
source_file=Path(TEST_DIR, 'input', 'btb-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'btb-targets.txt'),
output_file=TARGET_TO_SOURCE_OUT_FILE)
assert TARGET_TO_SOURCE_OUT_FILE.exists(), 'Output file was not written'
expected_file = Path(TEST_DIR, 'expected_output', 'empty-output.txt')
# Read the content of the output files and expected file into sets
with open(TARGET_TO_SOURCE_OUT_FILE, 'r') as output_file:
output_content = set(output_file.read().splitlines())
with open(expected_file, 'r') as expected_output_file:
expected_content = set(expected_output_file.read().splitlines())
# Check if the sets are equal, regardless of the order of lines
assert output_content == expected_content, 'Output file does not match expected output file'
"""
Run the BowTieBuilder algorithm on the example loop network files and check the output matches the expected output
"""
def test_loop(self):
LOOP_OUT_FILE.unlink(missing_ok=True)
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'loop-edges.txt'),
source_file=Path(TEST_DIR, 'input', 'btb-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'btb-targets.txt'),
output_file=LOOP_OUT_FILE)
assert LOOP_OUT_FILE.exists(), 'Output file was not written'
expected_file = Path(TEST_DIR, 'expected_output', 'loop-output.txt')
# Read the content of the output files and expected file into sets
with open(LOOP_OUT_FILE, 'r') as output_file:
output_content = set(output_file.read().splitlines())
with open(expected_file, 'r') as expected_output_file:
expected_content = set(expected_output_file.read().splitlines())
# Check if the sets are equal, regardless of the order of lines
assert output_content == expected_content, 'Output file does not match expected output file'
"""
Run the BowTieBuilder algorithm on the weighted input files and check the output matches the expected output
"""
def test_weighted(self):
WEIGHTED_OUT_FILE.unlink(missing_ok=True)
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'weighted-edges.txt'),
source_file=Path(TEST_DIR, 'input', 'btb-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'btb-targets.txt'),
output_file=WEIGHTED_OUT_FILE)
assert WEIGHTED_OUT_FILE.exists(), 'Output file was not written'
expected_file = Path(TEST_DIR, 'expected_output', 'weighted-output.txt')
# Read the content of the output files and expected file into sets
with open(WEIGHTED_OUT_FILE, 'r') as output_file:
output_content = set(output_file.read().splitlines())
with open(expected_file, 'r') as expected_output_file:
expected_content = set(expected_output_file.read().splitlines())
# Check if the sets are equal, regardless of the order of lines
assert output_content == expected_content, 'Output file does not match expected output file'
def test_no_weight(self):
NO_WEIGHT_OUT_FILE.unlink(missing_ok=True)
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'no-weight-edges.txt'),
source_file=Path(TEST_DIR, 'input', 'btb-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'btb-targets.txt'),
output_file=NO_WEIGHT_OUT_FILE)
assert NO_WEIGHT_OUT_FILE.exists(), 'Output file was not written'
expected_file = Path(TEST_DIR, 'expected_output', 'weighted-output.txt')
# Read the content of the output files and expected file into sets
with open(NO_WEIGHT_OUT_FILE, 'r') as output_file:
output_content = set(output_file.read().splitlines())
with open(expected_file, 'r') as expected_output_file:
expected_content = set(expected_output_file.read().splitlines())
# Check if the sets are equal, regardless of the order of lines
assert output_content == expected_content, 'Output file does not match expected output file'
def test_weight_one(self):
WEIGHT_ONE_OUT_FILE.unlink(missing_ok=True)
btb_wrapper(network_file=Path(TEST_DIR, 'input', 'weight-one-edges.txt'),
source_file=Path(TEST_DIR, 'input', 'btb-sources.txt'),
target_file=Path(TEST_DIR, 'input', 'btb-targets.txt'),
output_file=WEIGHT_ONE_OUT_FILE)
assert WEIGHT_ONE_OUT_FILE.exists(), 'Output file was not written'
expected_file = Path(TEST_DIR, 'expected_output', 'weighted-output.txt')
# Read the content of the output files and expected file into sets
with open(WEIGHT_ONE_OUT_FILE, 'r') as output_file:
output_content = set(output_file.read().splitlines())
with open(expected_file, 'r') as expected_output_file:
expected_content = set(expected_output_file.read().splitlines())
# Check if the sets are equal, regardless of the order of lines
assert output_content == expected_content, 'Output file does not match expected output file'