-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest_cr_history_indexing.py
151 lines (131 loc) · 6.14 KB
/
test_cr_history_indexing.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
import unittest
from mltrace.db import Store
def isEqualComponentRun(crOne, crTwo):
if crOne.start_timestamp == crTwo.start_timestamp \
and crOne.end_timestamp == crTwo.end_timestamp:
return True
return False
class TestComponentRunHistory(unittest.TestCase):
def setUp(self):
self.store = Store("test")
# initialize four component run for TestHistoryComponent
self.store.create_component("mock_component", "", "")
num_runs = 4
for i in range(1, num_runs + 1):
inp = self.store.get_io_pointer("inp")
out = self.store.get_io_pointer("out")
cr = self.store.initialize_empty_component_run("mock_component")
cr.set_start_timestamp()
cr.set_end_timestamp()
cr.add_input(inp)
cr.add_output(out)
self.store.set_dependencies_from_inputs(cr)
self.store.commit_component_run(cr)
self.historyLength = self.store.get_component_runs_count(
"mock_component")
self.firstComponentRun = self.store.get_component_runs_by_index(
"mock_component", 0, 1)[0]
self.lastComponentRun = self.store.get_component_runs_by_index(
"mock_component", self.historyLength - 1, self.historyLength)[0]
self.secondAndThirdComponentRun = \
self.store.get_component_runs_by_index(
"mock_component", 1, 3)
def testPstPstIndex(self):
"""
Test all possible queries:
get_component_runs_by_index(positive idx, positive idx)
"""
# case 1: (0, 0) return zero componentRun
resCrList = self.store.get_component_runs_by_index(
"mock_component", 0, 0)
self.assertEqual(len(resCrList), 0)
# case 2: (0, 1) return the first componentRun
resCrList = self.store.get_component_runs_by_index(
"mock_component", 0, 1)
self.assertEqual(len(resCrList), 1)
self.assertTrue(isEqualComponentRun(
resCrList[0], self.firstComponentRun))
# case 3: (len(componentRun) - 1, len(componentRun))
# return the last componentRun
resCrList = self.store.get_component_runs_by_index(
"mock_component", self.historyLength - 1, self.historyLength)
self.assertEqual(len(resCrList), 1)
self.assertTrue(isEqualComponentRun(
resCrList[0], self.lastComponentRun))
# case 4: (1, 3) return the second and third componentRun
# given len(componentRun) >=3)
resCrList = self.store.get_component_runs_by_index(
"mock_component", 1, 3)
self.assertEqual(len(resCrList), 2)
for idx, cr in enumerate(resCrList):
if idx >= len(self.secondAndThirdComponentRun):
break
else:
self.assertTrue(isEqualComponentRun(
cr, self.secondAndThirdComponentRun[idx]))
def testPstNgtIndex(self):
"""
Test all possible queries:
get_component_runs_by_index(positive idx, negative idx)
"""
# case 1: (0, -len(componentRun) + 1) return first componentRun)
resCrList = self.store.get_component_runs_by_index(
"mock_component", 0, -self.historyLength + 1)
self.assertEqual(len(resCrList), 1)
self.assertTrue(isEqualComponentRun(
resCrList[0], self.firstComponentRun))
# case 2: cannot retrieve last componentRun this way
# case 3: (1, -len(componentRun) + 3) return second and
# third componentRun given len(componentRun) >=4)
resCrList = self.store.get_component_runs_by_index(
"mock_component", 1, -self.historyLength + 3)
self.assertEqual(len(resCrList), 2)
for idx, cr in enumerate(resCrList):
self.assertTrue(isEqualComponentRun(
cr, self.secondAndThirdComponentRun[idx]))
def testNgtPstIndex(self):
"""
Test all possible queries:
get_component_runs_by_index(negative idx, positive idx)
"""
# case 1: (-len(componentRun), 1) return the first componentRun)
resCrList = self.store.get_component_runs_by_index(
"mock_component", -self.historyLength, 1)
self.assertEqual(len(resCrList), 1)
self.assertTrue(isEqualComponentRun(
resCrList[0], self.firstComponentRun))
# case 2: (-1, len(componentRun)) return the last componentRun)
resCrList = self.store.get_component_runs_by_index(
"mock_component", -1, self.historyLength)
self.assertEqual(len(resCrList), 1)
self.assertTrue(isEqualComponentRun(
resCrList[0], self.lastComponentRun))
# case 3: (-len(componentRun) + 1, 3) return second
# and third componentRun given len(componentRun) >=3)
resCrList = self.store.get_component_runs_by_index(
"mock_component", -self.historyLength + 1, 3)
self.assertEqual(len(resCrList), 2)
for idx, cr in enumerate(resCrList):
self.assertTrue(isEqualComponentRun(
cr, self.secondAndThirdComponentRun[idx]))
def testNgtNgtIndex(self):
"""
Test all possible queries:
get_component_runs_by_index(negative idx, negative idx)
"""
# case 1: (-len(componentRun), -len(componentRun) + 1)
# return first componentRun)
resCrList = self.store.get_component_runs_by_index(
"mock_component", -self.historyLength, -self.historyLength + 1)
self.assertEqual(len(resCrList), 1)
self.assertTrue(isEqualComponentRun(
resCrList[0], self.firstComponentRun))
# case 2: cannot retrieve last componentRun this way
# case 3: (-len(componentRun) + 1, -len(componentRun) + 3)
# return second and third componentRun given len(componentRun) >=4)
resCrList = self.store.get_component_runs_by_index(
"mock_component", -self.historyLength + 1, -self.historyLength + 3)
self.assertEqual(len(resCrList), 2)
for idx, cr in enumerate(resCrList):
self.assertTrue(isEqualComponentRun(
cr, self.secondAndThirdComponentRun[idx]))