This repository has been archived by the owner on May 2, 2022. It is now read-only.
/
test_with_count_combination.py
167 lines (131 loc) · 4.57 KB
/
test_with_count_combination.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
# Tai Sakuma <tai.sakuma@gmail.com>
import copy
import itertools
import pytest
try:
import unittest.mock as mock
except ImportError:
import mock
from alphatwirl.selection.modules import AllwCount, AnywCount, NotwCount
##__________________________________________________________________||
class MockEventSelection(object):
def begin(self, event): pass
def __call__(self, event): pass
def end(self): pass
##__________________________________________________________________||
def mk_tree():
# all0 - all1 --- all2 --- sel1
# | +- sel2
# +- not1 --- any1 --- all3 --- sel3
# | +- sel4
# +- sel5
sel1 = mock.Mock(spec=MockEventSelection)
sel1.name ='sel1'
sel2 = mock.Mock(spec=MockEventSelection)
sel2.name ='sel2'
sel3 = mock.Mock(spec=MockEventSelection)
sel3.name ='sel3'
sel4 = mock.Mock(spec=MockEventSelection)
sel4.name ='sel4'
sel5 = mock.Mock(spec=MockEventSelection)
sel5.name ='sel5'
all0 = AllwCount(name='all0')
all1 = AllwCount(name='all1')
all2 = AllwCount(name='all2')
all3 = AllwCount(name='all3')
any1 = AnywCount(name='any1')
all3.add(sel3)
all3.add(sel4)
any1.add(all3)
any1.add(sel5)
not1 = NotwCount(any1, name='not1')
all2.add(sel1)
all2.add(sel2)
all1.add(all2)
all1.add(not1)
all0.add(all1)
return dict(
alls=(all0, all1, all2, all3),
anys=(any1, ),
nots=(not1, ),
sels=(sel1, sel2, sel3, sel4, sel5)
)
##__________________________________________________________________||
def test_combination():
tree = mk_tree()
all0 = tree['alls'][0]
sels = tree['sels']
event = mock.Mock()
all0.begin(event)
all_possible_results = itertools.product(*[[True, False]]*len(sels))
# e.g.,
# [
# (True, True, True, True, True),
# (True, True, True, True, False),
# ...
# (False, False, False, False, False)
# ]
for l in all_possible_results:
# e.g. l = (True, True, False, True, False)
for sel, ret in zip(sels, l):
sel.return_value = ret
all0(event)
all0.end()
count = all0.results()
assert [
[1, 'AllwCount', 'all1', 3, 32],
[2, 'AllwCount', 'all2', 8, 32],
[3, 'MockEventSelection', 'sel1', 16, 32],
[3, 'MockEventSelection', 'sel2', 8, 16],
[2 , 'NotwCount', 'not1', 3, 8],
[3, 'AnywCount', 'any1', 5, 8],
[4, 'AllwCount', 'all3', 2, 8],
[5, 'MockEventSelection', 'sel3', 4, 8],
[5, 'MockEventSelection', 'sel4', 2, 4],
[4, 'MockEventSelection', 'sel5', 3, 6]
] == count._results
##__________________________________________________________________||
def test_merge():
# manually call the fixture because multiple instances are needed
# deep.copy() is not used because it will be difficult to access
# to copied sels
tree0 = mk_tree()
all0 = tree0['alls'][0]
tree0_copy1 = mk_tree()
tree0_copy2 = mk_tree()
all0_copy1 = tree0_copy1['alls'][0]
all0_copy2 = tree0_copy2['alls'][0]
sels_copy1 = tree0_copy1['sels']
sels_copy2 = tree0_copy2['sels']
event = mock.Mock()
all0_copy1.begin(event)
all0_copy2.begin(event)
all_possible_results = list(itertools.product(*[[True, False]]*len(sels_copy1)))
results1 = all_possible_results[:len(all_possible_results)//2]
results2 = all_possible_results[len(all_possible_results)//2:]
for l in results1:
for sel, ret in zip(sels_copy1, l):
sel.return_value = ret
all0_copy1(event)
for l in results2:
for sel, ret in zip(sels_copy2, l):
sel.return_value = ret
all0_copy2(event)
all0_copy1.end()
all0_copy2.end()
all0.merge(all0_copy1)
all0.merge(all0_copy2)
count = all0.results()
assert [
[1, 'AllwCount', 'all1', 3, 32],
[2, 'AllwCount', 'all2', 8, 32],
[3, 'MockEventSelection', 'sel1', 16, 32],
[3, 'MockEventSelection', 'sel2', 8, 16],
[2 , 'NotwCount', 'not1', 3, 8],
[3, 'AnywCount', 'any1', 5, 8],
[4, 'AllwCount', 'all3', 2, 8],
[5, 'MockEventSelection', 'sel3', 4, 8],
[5, 'MockEventSelection', 'sel4', 2, 4],
[4, 'MockEventSelection', 'sel5', 3, 6]
] == count._results
##__________________________________________________________________||