-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
72 lines (62 loc) · 2.1 KB
/
test.py
File metadata and controls
72 lines (62 loc) · 2.1 KB
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
"""
The test uses data/test_data.txt as input table and data/test-output.txt as standard result.
The test compares result generated by algorithm with standard result.
"""
import unittest
from os import remove
from utils.reader import Reader
from utils.writer import Writer
from algorithm.tane import TANE
__author__ = 'Ma Zijun'
__date__ = '2017-04-23'
class MyTestCase(unittest.TestCase):
"""
a test case
"""
TABLE_FILE = 'data/test_data.txt'
STANDARD_RESULT_FILE = 'data/test_output.txt'
TANE_RESULT_FILE = 'data/test_output_1.txt'
def setUp(self):
"""
load table and standard result into memory
"""
print('Test begins')
print('Read table from file')
self.table = Reader.read_table_from_file(MyTestCase.TABLE_FILE, ',')
print('Read standard function dependency from file')
self.standard_result = get_result_from_file(MyTestCase.STANDARD_RESULT_FILE)
def tearDown(self):
"""
remove output file generated in test process
"""
print('Remove output file generated in test process')
remove(MyTestCase.TANE_RESULT_FILE)
print('Test ends')
def test_tane(self):
"""
testing TANE algorithm
"""
print('Apply TANE algorithm')
tane = TANE(self.table)
tane.run()
print('Write result dependencies to file')
Writer.write_dependency_to_file(tane.ans, MyTestCase.TANE_RESULT_FILE)
result = get_result_from_file(MyTestCase.TANE_RESULT_FILE)
print('Compare algorithm result with standard result')
self.assertEqual(self.standard_result, result)
def get_result_from_file(file_name):
"""
load dependency result from file
:param file_name: name of the file that stores functional dependency
:return: a sequence of each stripped line string
"""
try:
with open(file_name, 'r') as f:
raw_data = f.read()
except IOError as e:
print(e)
return []
lines = raw_data.strip().split('\n')
return [x.strip() for x in lines]
if __name__ == '__main__':
unittest.main()