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