-
Notifications
You must be signed in to change notification settings - Fork 33
/
test_url_config.py
176 lines (138 loc) · 6.18 KB
/
test_url_config.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
import json
import strax
import straxen
import fsspec
from straxen.test_utils import nt_test_context, nt_test_run_id
import unittest
import pickle
import random
import numpy as np
@straxen.URLConfig.register('random')
def generate_random(_):
return random.random()
@straxen.URLConfig.register('unpicklable')
def return_lamba(_):
return lambda x: x
@straxen.URLConfig.register('large-array')
def large_array(_):
return np.ones(1_000_000).tolist()
class ExamplePlugin(strax.Plugin):
depends_on = ()
dtype = strax.time_fields
provides = ('test_data',)
test_config = straxen.URLConfig(default=42,)
cached_config = straxen.URLConfig(default=666, cache=1)
def compute(self):
pass
class TestURLConfig(unittest.TestCase):
def setUp(self):
st = nt_test_context()
st.register(ExamplePlugin)
self.st = st
def test_default(self):
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
self.assertEqual(p.test_config, 42)
def test_literal(self):
self.st.set_config({'test_config': 666})
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
self.assertEqual(p.test_config, 666)
@unittest.skipIf(not straxen.utilix_is_configured(), "No db access, cannot test CMT.")
def test_cmt_protocol(self):
self.st.set_config({'test_config': 'cmt://elife?version=v1&run_id=plugin.run_id'})
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
self.assertTrue(abs(p.test_config-219203.49884000001) < 1e-2)
def test_json_protocol(self):
self.st.set_config({'test_config': 'json://[1,2,3]'})
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
self.assertEqual(p.test_config, [1, 2, 3])
def test_format_protocol(self):
self.st.set_config({'test_config': 'format://{run_id}?run_id=plugin.run_id'})
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
self.assertEqual(p.test_config, nt_test_run_id)
def test_fsspec_protocol(self):
with fsspec.open('memory://test_file.json', mode='w') as f:
json.dump({"value": 999}, f)
self.st.set_config(
{'test_config': 'take://json://fsspec://memory://test_file.json?take=value'})
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
self.assertEqual(p.test_config, 999)
def test_chained(self):
self.st.set_config({'test_config': 'take://json://[1,2,3]?take=0'})
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
self.assertEqual(p.test_config, 1)
def test_take_nested(self):
self.st.set_config({'test_config': 'take://json://{"a":[1,2,3]}?take=a&take=0'})
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
self.assertEqual(p.test_config, 1)
@unittest.skipIf(not straxen.utilix_is_configured(),
"No db access, cannot test!")
def test_bodedga_get(self):
"""Just a didactic example"""
self.st.set_config({
'test_config':
'take://'
'resource://'
'XENONnT_numbers.json'
'?fmt=json'
'&take=g1'
'&take=v2'
'&take=value'})
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
# Either g1 is 0, bodega changed or someone broke URLConfigs
self.assertTrue(p.test_config)
st2 = self.st.new_context()
st2.set_config({'test_config': 'bodega://g1?bodega_version=v2'})
p2 = st2.get_single_plugin(nt_test_run_id, 'test_data')
self.assertEqual(p.test_config, p2.test_config)
def test_print_protocol_desc(self):
straxen.URLConfig.print_protocols()
def test_cache(self):
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
# sanity check that default value is not affected
self.assertEqual(p.cached_config, 666)
self.st.set_config({'cached_config': 'random://abc'})
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
# value is randomly generated when accessed so if
# its equal when we access it again, its coming from the cache
cached_value = p.cached_config
self.assertEqual(cached_value, p.cached_config)
# now change the config to which will generate a new number
self.st.set_config({'cached_config': 'random://dfg'})
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
# sanity check that the new value is still consistent i.e. cached
self.assertEqual(p.cached_config, p.cached_config)
# test if previous value is evicted, since cache size is 1
self.assertNotEqual(cached_value, p.cached_config)
# verify pickalibility of objects in cache dont affect plugin pickalibility
self.st.set_config({'cached_config': 'unpicklable://dfg'})
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
with self.assertRaises(AttributeError):
pickle.dumps(p.cached_config)
pickle.dumps(p)
def test_cache_size(self):
'''test the cache helper functions
'''
# make sure the value has a detectable size
self.st.set_config({'cached_config': 'large-array://dfg'})
p = self.st.get_single_plugin(nt_test_run_id, 'test_data')
# fetch the value so its stored in the cache
value = p.cached_config
# cache should now have finite size
self.assertGreater(straxen.config_cache_size_mb(), 0.0)
# test if clearing cache works as expected
straxen.clear_config_caches()
self.assertEqual(straxen.config_cache_size_mb(), 0.0)
def test_filter_kwargs(self):
all_kwargs = dict(a=1, b=2, c=3)
# test a function that takes only a seubset of the kwargs
def func1(a=None, b=None):
return
filtered1 = straxen.filter_kwargs(func1, all_kwargs)
self.assertEqual(filtered1, dict(a=1, b=2))
func1(**filtered1)
# test function that accepts wildcard kwargs
def func2(**kwargs):
return
filtered2 = straxen.filter_kwargs(func2, all_kwargs)
self.assertEqual(filtered2, all_kwargs)
func2(**filtered2)