DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
test_dal.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# vim: set fileencoding=utf-8 :
3# DUNE DAQ modification notice:
4# This file has been modified from the original ATLAS config source for the DUNE DAQ project.
5# Fork baseline commit: 67a24e731 (2022-10-27).
6# Renamed since fork: yes (from python/tests/test_dal.py to scripts/test_dal.py).
7
8# Created by Andre Anjos <andre.dos.anjos@cern.ch>
9# Wed 24 Oct 2007 01:53:05 PM CEST
10
11"""Unit test for the Python bindings to the DAL."""
12
13import os
14import unittest
15import conffwk
16
17scriptsdir=os.path.dirname(os.path.realpath(__file__))
18
19# binds a new dal into the module named "test"
20test = conffwk.dal.module('test', f'{scriptsdir}/test.schema.xml')
21
22SIZE_TEST = 1000
23RECURSION_TEST = 500
24
25
26def test_good(test, obj, field, values):
27 """Tests I can set good expected values to certain fields"""
28 for v in values:
29 setattr(obj, field, v)
30 test.assertEqual(getattr(obj, field), v)
31
32
33def test_bad(test, obj, field, values):
34 """Tests I can not set bad values to certain fields"""
35 for v in values:
36 test.assertRaises(ValueError, setattr, obj, field, v)
37
38
39class DalTest(unittest.TestCase):
40
42 obj = test.Dummy('Test-1')
43 test_good(self, obj, 'bool', [True, False, 0, 1])
44
46 obj = test.Dummy('Test-1')
47 test_good(self, obj, 'sint8', [-2**7, 2**7-1, 0, 0xa])
48 test_bad(self, obj, 'sint8', [-2**7-1, 2**7, 0xab])
49
51 obj = test.Dummy('Test-1')
52 test_good(self, obj, 'uint8', [0, 2**8-1, 42, 0xab])
53 test_bad(self, obj, 'uint8', [-1, 2**8, 0xabc])
54
56 obj = test.Dummy('Test-1')
57 test_good(self, obj, 'sint16', [-2**15, 2**15-1, 42, 0xa])
58 test_bad(self, obj, 'sint8', [-2**15-1, 2**15, 0xabcd])
59
61 obj = test.Dummy('Test-1')
62 test_good(self, obj, 'uint16', [0, 2**16-1, 42, 0xabcd])
63 test_bad(self, obj, 'uint16', [-1, 2**16, 0xabcde])
64
66 obj = test.Dummy('Test-1')
67 test_good(self, obj, 'sint32', [-2**31, 2**31-1, 42, 0xa])
68 test_bad(self, obj, 'sint32', [-2**31-1, 2**31, 0xabcdabcdabcd])
69
71 obj = test.Dummy('Test-1')
72 test_good(self, obj, 'uint32', [0, 2**32-1, 42, 0xabcd])
73 test_bad(self, obj, 'uint32', [-1, 2**32, 0xabcdeabcdeabcde])
74
76 obj = test.Dummy('Test-1')
77 test_good(self, obj, 'sint64', [-2**63, 2**63-1, 42, 0xa])
78 test_bad(self, obj, 'sint64', [-2**63-1,
79 2**63, 0xabcdabcdabcdabcabcabc])
80
82 obj = test.Dummy('Test-1')
83 test_good(self, obj, 'uint64', [0, 2**64-1, 42, 0xabcd])
84 test_bad(self, obj, 'uint64',
85 [-1, 2**64, 0xabcdeabcdeabcdeabcabcffabc])
86
88 obj = test.Dummy('Test-1')
89 test_good(self, obj, 'string_vector', [
90 ['test01', 'test09'], ['test30']])
91 test_bad(self, obj, 'string_vector', [['Test01', 'test111'], ['abcd']])
92
94 obj = test.Dummy('Test-1')
95 test_good(self, obj, 'uint64_vector', [[0, 1], [0]])
96 test_bad(self, obj, 'uint64_vector', [[0, -1], [2**65]])
97
99 obj = test.Dummy('Test-1')
100 test_good(self, obj, 'uint64_vector_range', [[18446744073709551600,
101 18446744073709551615],
102 [18446744073709551610]])
103 test_bad(self, obj, 'uint64_vector_range', [[0, -1], 10])
104
106 obj = test.Dummy('Test-1')
107 test_good(self, obj, 'enum', ['FIRST', 'SECOND'])
108 test_bad(self, obj, 'string_vector', ['first', 'zeroth'])
109
111 obj = test.Dummy('Test-1')
112 test_good(self, obj, 'date', ['31/12/73', '01/01/70'])
113 test_bad(self, obj, 'date', ['31/00/19', '-1'])
114
116 obj = test.Dummy('Test-1')
117 test_good(self, obj, 'time', [
118 '31/12/73 02:03:04', '01/01/70 00:00:59'])
119 test_bad(self, obj, 'time', ['31/12/73 24:23:22', '-1'])
120
122 obj = test.Dummy('Test-1')
123 test_good(self, obj, 'classref', ['Dummy', 'Third'])
124 test_bad(self, obj, 'classref', ['DoesNotExistInDal'])
125
127 obj = test.Dummy('Test-1')
128 self.assertRaises(AttributeError, setattr, obj, 'doesNotExist', 24)
129
131 obj = test.Third('Test-1')
132 test_good(self, obj, 'classref', ['Second', 'Third', 'Dummy'])
133 test_bad(self, obj, 'classref', ['DoesNotExistInDal'])
134
136 obj1 = test.Dummy('Test-1')
137 obj2 = test.Dummy('Test-1')
138 self.assertEqual(obj1, obj2)
139 obj3 = test.Second('Test-1')
140 self.assertNotEqual(obj1, obj3)
141 obj4 = test.Dummy('Test-2')
142 self.assertNotEqual(obj1, obj4)
143
145 obj1 = test.Second('Test-1')
146 obj2 = test.Third('Test-2')
147 obj3 = test.Dummy('Test-3')
148 test_good(self, obj2, 'Single', [None, obj1])
149 test_bad(self, obj2, 'Single', [obj3, 4])
150
152 obj1 = test.Second('Test-1')
153 obj2 = test.Third('Test-2')
154 obj3 = test.Dummy('Test-3')
155 test_good(self, obj1, 'Dummy', [[], [obj3, obj2], [obj2]])
156 test_bad(self, obj2, 'Seconds', [[obj1, obj3]])
157
159 obj = test.Dummy('Test-1', string_vector=['test10', 'test20'])
160 self.assertEqual(obj.string_vector, ['test10', 'test20'])
161 obj2 = test.Second('Test-2', string_vector=['test20', 'test30'],
162 Dummy=[obj])
163 self.assertEqual(obj2.Dummy, [obj])
164
166 db = conffwk.Configuration("oksconflibs")
167 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
168 obj3 = test.Dummy('Test-3', string_vector=['test05', 'test06'])
169 obj1 = test.Second('Test-1', Dummy=[obj3], string_vector=['test30'])
170 obj2 = test.Third('Test-2', string_vector=['test10'], Seconds=[obj1])
171 db.update_dal(obj2, recurse=True) # should serialize everybody
172 db.commit()
173 ret = db.get_dal('Third', 'Test-2')
174 self.assertEqual(ret, obj2)
175 self.assertEqual(ret.Seconds[0], obj1)
176 self.assertEqual(ret.Seconds[0].Dummy[0], obj3)
177
179 db = conffwk.Configuration("oksconflibs")
180 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
181 obj3 = test.Dummy('Test-3', string_vector=['test05', 'test06'])
182 obj1 = test.Second('Test-1', Dummy=[obj3], string_vector=['test30'])
183 obj2 = test.Third('Test-2', string_vector=['test10'], Seconds=[obj1])
184 db.update_dal(obj2, recurse=True) # should serialize everybody
185 db.commit()
186 del db
187 db = conffwk.Configuration('oksconflibs:test.data.xml')
188 ret = db.get_dal('Third', 'Test-2')
189 self.assertEqual(ret, obj2)
190 self.assertEqual(ret.Seconds[0], obj1)
191 self.assertEqual(ret.Seconds[0].Dummy[0], obj3)
192 self.assertEqual(len(db.get_dals('Dummy')), 3)
193
195 import sys
196 number = SIZE_TEST
197 previous = None
198 objs = []
199 for i in range(number):
200 objs.append(test.Second("Object-%d" % i))
201 db = conffwk.Configuration("oksconflibs")
202 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
203 for k in objs:
204 db.update_dal(k, recurse=True)
205 db.commit()
206 del db
207 db = conffwk.Configuration('oksconflibs:test.data.xml')
208 objs = db.get_dals('Second')
209 self.assertEqual(len(objs), number)
210
212 import sys
213 sys.setrecursionlimit(50000)
214 depth = RECURSION_TEST # 10000 seems to be impossible!
215 previous = None
216 for i in range(depth):
217 obj = test.Second("Object-%d" % i)
218 if previous:
219 obj.Another = previous
220 previous = obj
221 db = conffwk.Configuration("oksconflibs")
222 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
223 db.update_dal(previous, recurse=True)
224 db.commit()
225 del db
226 db = conffwk.Configuration('oksconflibs:test.data.xml')
227 top = db.get_dal('Second', 'Object-%d' % (depth-1))
228 counter = 0
229 while top:
230 counter += 1
231 top = top.Another
232 self.assertEqual(counter, depth)
233
235 import sys
236 sys.setrecursionlimit(50000)
237 depth = RECURSION_TEST
238 db = conffwk.Configuration('oksconflibs:test.data.xml')
239 top = db.get_dal('Second', 'Object-%d' % (depth-1))
240 all = [top]
241 while top.Another:
242 all.append(top.Another)
243 top = top.Another
244 self.assertEqual(len(all), depth)
245
246 # reverse the linking order and update the database
247 all = list(reversed(all))
248 for i, k in enumerate(all):
249 if i < (len(all)-1):
250 k.Another = all[i+1]
251 else:
252 k.Another = None
253
254 # update the opened database
255 db.update_dal(all[0], recurse=True)
256 db.commit()
257 del db
258
259 # reopen and check
260 db = conffwk.Configuration('oksconflibs:test.data.xml')
261 top = db.get_dal('Second', 'Object-0') # that should be on the top now
262 counter = 0
263 while top:
264 counter += 1
265 top = top.Another
266 self.assertEqual(counter, depth)
267
269 # reopen and check
270 db = conffwk.Configuration('oksconflibs:test.data.xml')
271 self.assertEqual(len(db.get_dals('Dummy')), RECURSION_TEST)
272
274 # reopen and check
275 db = conffwk.Configuration('oksconflibs:test.data.xml')
276 objs = db.get_dals('Dummy')
277 self.assertEqual(len(objs), RECURSION_TEST)
278 for k in objs:
279 self.assertEqual(k.className(), 'Second')
280
282 obj1 = test.Second('Obj-1')
283 obj2 = test.Second('Obj-2')
284 obj1.Another = obj2
285 obj2.Another = obj1
286 db = conffwk.Configuration("oksconflibs")
287 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
288 db.update_dal(obj1, recurse=True)
289 db.commit()
290
292 db = conffwk.Configuration('oksconflibs:test.data.xml')
293 obj1 = db.get_dal('Second', 'Obj-1')
294 obj2 = db.get_dal('Second', 'Obj-2')
295 self.assertEqual(obj1.Another, obj2)
296 self.assertEqual(obj2.Another, obj1)
297
299 obj1 = test.Second('Obj-1')
300 obj2 = test.Second('Obj-2')
301 obj3 = test.Second('Obj-3')
302 obj4 = test.Second('Obj-4')
303 obj1.Another = obj2
304 obj2.Another = obj3
305 obj3.Another = obj4
306 obj4.Another = obj1
307 db = conffwk.Configuration("oksconflibs")
308 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
309 db.update_dal(obj1, recurse=True)
310 db.commit()
311
313 db = conffwk.Configuration('oksconflibs:test.data.xml')
314 obj1 = db.get_dal('Second', 'Obj-1')
315 obj2 = db.get_dal('Second', 'Obj-2')
316 obj3 = db.get_dal('Second', 'Obj-3')
317 obj4 = db.get_dal('Second', 'Obj-4')
318 self.assertEqual(obj1.Another, obj2)
319 self.assertEqual(obj2.Another, obj3)
320 self.assertEqual(obj3.Another, obj4)
321 self.assertEqual(obj4.Another, obj1)
322
324 db = conffwk.Configuration('oksconflibs:test.data.xml')
325 obj1 = db.get_dal('Second', 'Obj-1')
326 obj2 = db.get_dal('Second', 'Obj-2')
327 obj3 = db.get_dal('Second', 'Obj-3')
328 obj4 = db.get_dal('Second', 'Obj-4')
329 obj2.Another = None
330 # have to first eliminate ALL references
331 db.update_dal(obj2, recurse=True)
332 db.destroy_dal(obj3)
333 db.commit()
334 del db
335 db = conffwk.Configuration('oksconflibs:test.data.xml')
336 obj1 = db.get_dal('Second', 'Obj-1')
337 obj2 = db.get_dal('Second', 'Obj-2')
338 self.assertRaises(RuntimeError, db.get_dal, 'Second', 'Obj-3')
339 obj4 = db.get_dal('Second', 'Obj-4')
340
342 db = conffwk.Configuration('oksconflibs:test.data.xml')
343 obj1 = db.get_dal('Second', 'Obj-1')
344 obj1.Another = None
345 obj2 = db.get_dal('Second', 'Obj-2')
346 obj2.string = 'xyzabc'
347 db.update_dal(obj1, recurse=False)
348 db.commit()
349 del db
350 db = conffwk.Configuration('oksconflibs:test.data.xml')
351 obj1 = db.get_dal('Second', 'Obj-1')
352 self.assertNotEqual(obj1.Another, obj2)
353 self.assertEqual(obj1.Another, None)
354 obj2 = db.get_dal('Second', 'Obj-2')
355 self.assertNotEqual(obj2.string, 'xyzabc')
356
358 obj11 = test.Second('Obj-11')
359 obj22 = test.Second('Obj-22')
360 obj33 = test.Second('Obj-33')
361 obj44 = test.Second('Obj-44')
362 obj11.Another = obj22
363 obj22.Another = obj33
364 obj33.Another = obj44
365 obj44.Another = obj11
366 db = conffwk.Configuration("oksconflibs")
367 db.create_db("test2.data.xml", [f'{scriptsdir}/test.schema.xml'])
368 db.update_dal(obj11, recurse=True)
369 db.commit()
370 del db
371
372 db = conffwk.Configuration('oksconflibs:test.data.xml')
373 obj1 = db.get_dal('Second', 'Obj-1')
374 obj1.Another = obj22
375 self.assertRaises(RuntimeError, db.update_dal, obj1, recurse=False)
376 db.add_include('test2.data.xml')
377 db.update_dal(obj1, recurse=False)
378 self.assertEqual(True, obj22 in db.get_all_dals())
379 db.commit()
380 del db
381
383 db = conffwk.Configuration("oksconflibs")
384 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
385 obj3 = test.Dummy('Test-3', string_vector=['test05', 'test06'])
386 obj1 = test.Second('Test-1', Dummy=[obj3], string_vector=['test30'])
387 obj2 = test.Third('Test-2', string_vector=['test10'], Seconds=[obj1])
388 db.update_dal(obj2, recurse=True) # should serialize everybody
389 db.commit()
390 del db
391 db = conffwk.Configuration('oksconflibs:test.data.xml')
392 ret = db.get_dal('Third', 'Test-2')
393
394 import re
395 self.assertEqual(ret.get('Second', 'Test-1'), obj1)
396 self.assertEqual(type(ret.get('Second')), list)
397 self.assertEqual(
398 type(ret.get('Second', re.compile(r'Test-[0-9]'))), list)
399 self.assertEqual(len(ret.get('Second', re.compile(r'Test-[0-9]'))), 1)
400 self.assertEqual(
401 len(ret.get('Second', re.compile(r'Test-[0-9]'), True)), 2)
402
404 db = conffwk.Configuration('oksconflibs:test.data.xml')
405 obj = db.get_dal('Dummy', 'Test-3')
407 self.assertEqual(len(conffwk.updated_dals()), 0)
408 obj.bool = False
409 self.assertEqual(len(conffwk.updated_dals()), 1)
410 self.assertEqual(conffwk.updated_dals(), set((obj,)))
411 obj.sint8 = 0
412 self.assertEqual(len(conffwk.updated_dals()), 1)
413
414 obj1 = db.get_dal('Second', 'Test-1')
415 self.assertEqual(len(conffwk.updated_dals()), 1)
416
417 obj1.bool = False
418 self.assertEqual(len(conffwk.updated_dals()), 2)
419 self.assertEqual(conffwk.updated_dals(), set((obj, obj1)))
420
422 db = conffwk.Configuration('oksconflibs:test.data.xml')
423 obj = db.get_dal('Dummy', 'Test-3')
424 obj.rename('Test-4')
425 db.update_dal(obj)
426 obj2 = db.get_dal('Dummy', 'Test-4')
427 self.assertEqual(obj, obj2)
428 db.commit()
429
430
431if __name__ == "__main__":
432 import sys
433 sys.argv.append('-v')
434 unittest.main()
test22_CanReadWriteDalOnConfiguration(self)
Definition test_dal.py:165
test20_CanReadWriteMultiValuedRelations(self)
Definition test_dal.py:151
test04_CanReadWriteUInt16(self)
Definition test_dal.py:60
test29_CanWriteInfiniteRecursion(self)
Definition test_dal.py:281
test37_BookkeepUpdatedObjects(self)
Definition test_dal.py:403
test00_CanReadWriteBool(self)
Definition test_dal.py:41
test05_CanReadWriteInt32(self)
Definition test_dal.py:65
test15_CanReadWriteClassReference(self)
Definition test_dal.py:121
test01_CanReadWriteInt8(self)
Definition test_dal.py:45
test06_CanReadWriteUInt32(self)
Definition test_dal.py:70
test25_CanRecurseALot(self)
Definition test_dal.py:211
test28_CanHandleInheritanceAtSearch(self)
Definition test_dal.py:273
test17_AttributeInheritance(self)
Definition test_dal.py:130
test13_CanReadWriteDate(self)
Definition test_dal.py:110
test02_CanReadWriteUInt8(self)
Definition test_dal.py:50
test33_CanHandleDalDestruction(self)
Definition test_dal.py:323
test30_CanReadInfiniteRecursion(self)
Definition test_dal.py:291
test34_CanModifyNonRecursively(self)
Definition test_dal.py:341
test35_CannotMoveIfNotIncluded(self)
Definition test_dal.py:357
test12_CanReadWriteEnum(self)
Definition test_dal.py:105
test24_CanReadWriteBigDatabases(self)
Definition test_dal.py:194
test38_CanRename(self)
Definition test_dal.py:421
test03_CanReadWriteInt16(self)
Definition test_dal.py:55
test07_CanReadWriteInt64(self)
Definition test_dal.py:75
test14_CanReadWriteTime(self)
Definition test_dal.py:115
test10_CanReadWrite64bitVectors(self)
Definition test_dal.py:93
test23_CanSearchForDalsAtConfiguration(self)
Definition test_dal.py:178
test36_CanSearchForObjects(self)
Definition test_dal.py:382
test18_CanCompareDals(self)
Definition test_dal.py:135
test27_CanSearchUsingBaseClasses(self)
Definition test_dal.py:268
test19_CanReadWriteSingleValuedRelations(self)
Definition test_dal.py:144
test16_CanNotSetUnexistingAttribute(self)
Definition test_dal.py:126
test11_CanReadWrite64bitVectorsAndWithRange(self)
Definition test_dal.py:98
test26_CanModifyDatabases(self)
Definition test_dal.py:234
test08_CanReadWriteUInt64(self)
Definition test_dal.py:81
test32_CanReadBiggerRecursion(self)
Definition test_dal.py:312
test09_CanReadWriteStringsWithRanges(self)
Definition test_dal.py:87
test21_CanAttributeFromConstructor(self)
Definition test_dal.py:158
test31_CanWriteBiggerRecursion(self)
Definition test_dal.py:298
module(name, schema, other_dals=[], backend='oksconflibs', db=None)
Definition dal.py:678
reset_updated_dals()
Definition __init__.py:20
updated_dals()
Definition __init__.py:13
test_good(test, obj, field, values)
Definition test_dal.py:26
test_bad(test, obj, field, values)
Definition test_dal.py:33