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
|
# R overlay --
# -*- coding: utf-8 -*-
# Copyright (C) 2013 André Erdmann <dywi@mailerd.de>
# Distributed under the terms of the GNU General Public License;
# either version 2 of the License, or (at your option) any later version.
from __future__ import print_function
import random
import roverlay.interface.depres
import tests.base
import tests.interface
import tests.static.depres
from tests.static.depres import DEPRES_DATA, DEPRES_RULES, DEPRES_INCLUDE
def suite():
return tests.base.make_testsuite ( DepresTestCase )
as_iterable = lambda s : ( s, ) if isinstance ( s, str ) else s
class DepresTestCase ( tests.interface.RoverlayInterfaceTestCase ):
TESTSUITE = [
'sanity_checks',
'visualize',
'depres_static', 'depres_static_randomized',
'load_rules',
]
DEPRES_INTERFACE = None
@classmethod
def setUpClass ( cls ):
super ( DepresTestCase, cls ).setUpClass()
cls.ROOT_INTERFACE.register_interface (
"depres", roverlay.interface.depres.DepresInterface, force=True
)
cls.DEPRES_INTERFACE = cls.ROOT_INTERFACE.spawn_interface ( "depres" )
cls.DEPRES_INTERFACE.set_greedy ( False )
# for now, tuples are expected when running tests
# (AssertEquals ( <expected result>, <result> )
#
cls.DEPRES_INTERFACE.want_tuple = True
# --- end of setUpClass (...) ---
def setUp ( self ):
#ref
self.depres = self.__class__.DEPRES_INTERFACE
self.depres.get_new_pool()
# --- end of setUp (...) ---
def tearDown ( self ):
self.depres.discard_pool()
self.depres.discard_empty_pools()
self.depres.update()
# --- end of tearDown (...) ---
def do_depres_test ( self, test_identifier, rule_names, test_data ):
unpacked = lambda T: \
( "" if T[0] is None else T[0] ) if T and len ( T ) == 1 else T
self.depres.compile_rules()
self.tearDown()
self.depres.get_new_pool()
for rule_name in rule_names:
self.assertTrue (
self.depres.add_rule_list ( DEPRES_RULES [rule_name] )
)
self.assertTrue ( self.depres.compile_rules() )
for depstr, t_expected_result in test_data:
expected_result = unpacked ( t_expected_result )
result = unpacked ( self.depres.resolve ( depstr ) )
self.assertEquals (
result, expected_result,
"{!s}: {!r} should be resolved as {!r} and not {!r}".format (
test_identifier, depstr, expected_result, result
)
)
# --- end of do_depres_test (...) ---
def do_randomized_depres_test (
self, test_identifier, rule_names, test_data, allow_modify=False
):
if allow_modify and isinstance ( test_data, list ):
rand_list = test_data
else:
rand_list = list ( test_data )
random.shuffle ( rand_list )
return self.do_depres_test ( test_identifier, rule_names, rand_list )
# --- end of do_randomized_depres_test (...) ---
def get_depres_include ( self, dataset_name ):
if dataset_name in DEPRES_INCLUDE:
return as_iterable ( DEPRES_INCLUDE [dataset_name] )
else:
return ( dataset_name, )
# --- end of get_depres_include (...) ---
def test_visualize ( self ):
for name, ruleset in DEPRES_RULES.items():
self.depres.compile_rules()
self.tearDown()
self.depres.get_new_pool()
self.assertTrue ( self.depres.add_rule_list ( ruleset ) )
self.assertTrue ( self.depres.compile_rules() )
vis = self.depres.visualize_pool()
self.assertIsInstance ( vis, str )
self.assertEquals (
bool ( not self.depres.get_pool().empty() ), bool ( vis )
)
# --- end of test_visualize (...) ---
def test_sanity_checks ( self ):
for dataset_name in DEPRES_DATA.keys():
if dataset_name in DEPRES_INCLUDE:
ruleset_list = as_iterable ( DEPRES_INCLUDE [dataset_name] )
else:
ruleset_list = ( dataset_name, )
for ruleset_name in ruleset_list:
self.assertIn (
ruleset_name, DEPRES_RULES,
"missing ruleset {!r} for depres test {!r}".format (
ruleset_name, dataset_name
)
)
# --- end of test_sanity_checks (...) ---
def test_depres_static ( self ):
for name, test_data in DEPRES_DATA.items():
self.do_depres_test (
name,
self.get_depres_include ( name ),
DEPRES_DATA [test_data] if isinstance ( test_data, str )
else test_data
)
# --- end of test_depres_static (...) ---
def test_depres_static_randomized ( self ):
data_keys = list ( DEPRES_DATA.keys() )
random.shuffle ( data_keys )
for name in data_keys:
test_data = DEPRES_DATA [name]
self.do_randomized_depres_test (
name,
self.get_depres_include ( name ),
DEPRES_DATA [test_data] if isinstance ( test_data, str )
else test_data
)
# --- end of test_depres_static_randomized (...) ---
def test_load_rules ( self ):
self.depres.discard_all_pools()
if self.CONFIG.get ( "DEPRES.simple_rules.files", None ):
self.depres.load_rules_from_config ( ignore_missing=True )
self.depres.discard_all_pools()
else:
self.skipTest ( "No rule files configured." )
# --- end of test_load_rules (...) ---
|