11"""Unit test for the Python bindings to the DAL."""
17scriptsdir=os.path.dirname(os.path.realpath(__file__))
27 """Tests I can set good expected values to certain fields"""
29 setattr(obj, field, v)
30 test.assertEqual(getattr(obj, field), v)
34 """Tests I can not set bad values to certain fields"""
36 test.assertRaises(ValueError, setattr, obj, field, v)
42 obj = test.Dummy(
'Test-1')
43 test_good(self, obj,
'bool', [
True,
False, 0, 1])
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])
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])
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])
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])
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])
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])
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])
82 obj = test.Dummy(
'Test-1')
83 test_good(self, obj,
'uint64', [0, 2**64-1, 42, 0xabcd])
85 [-1, 2**64, 0xabcdeabcdeabcdeabcabcffabc])
88 obj = test.Dummy(
'Test-1')
90 [
'test01',
'test09'], [
'test30']])
91 test_bad(self, obj,
'string_vector', [[
'Test01',
'test111'], [
'abcd']])
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]])
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])
106 obj = test.Dummy(
'Test-1')
107 test_good(self, obj,
'enum', [
'FIRST',
'SECOND'])
108 test_bad(self, obj,
'string_vector', [
'first',
'zeroth'])
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'])
116 obj = test.Dummy(
'Test-1')
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'])
122 obj = test.Dummy(
'Test-1')
123 test_good(self, obj,
'classref', [
'Dummy',
'Third'])
124 test_bad(self, obj,
'classref', [
'DoesNotExistInDal'])
127 obj = test.Dummy(
'Test-1')
128 self.assertRaises(AttributeError, setattr, obj,
'doesNotExist', 24)
131 obj = test.Third(
'Test-1')
132 test_good(self, obj,
'classref', [
'Second',
'Third',
'Dummy'])
133 test_bad(self, obj,
'classref', [
'DoesNotExistInDal'])
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)
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])
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]])
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'],
163 self.assertEqual(obj2.Dummy, [obj])
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)
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)
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)
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)
199 for i
in range(number):
200 objs.append(test.Second(
"Object-%d" % i))
202 db.create_db(
"test.data.xml", [f
'{scriptsdir}/test.schema.xml'])
204 db.update_dal(k, recurse=
True)
208 objs = db.get_dals(
'Second')
209 self.assertEqual(len(objs), number)
213 sys.setrecursionlimit(50000)
214 depth = RECURSION_TEST
216 for i
in range(depth):
217 obj = test.Second(
"Object-%d" % i)
219 obj.Another = previous
222 db.create_db(
"test.data.xml", [f
'{scriptsdir}/test.schema.xml'])
223 db.update_dal(previous, recurse=
True)
227 top = db.get_dal(
'Second',
'Object-%d' % (depth-1))
232 self.assertEqual(counter, depth)
236 sys.setrecursionlimit(50000)
237 depth = RECURSION_TEST
239 top = db.get_dal(
'Second',
'Object-%d' % (depth-1))
242 all.append(top.Another)
244 self.assertEqual(len(all), depth)
247 all = list(reversed(all))
248 for i, k
in enumerate(all):
255 db.update_dal(all[0], recurse=
True)
261 top = db.get_dal(
'Second',
'Object-0')
266 self.assertEqual(counter, depth)
271 self.assertEqual(len(db.get_dals(
'Dummy')), RECURSION_TEST)
276 objs = db.get_dals(
'Dummy')
277 self.assertEqual(len(objs), RECURSION_TEST)
279 self.assertEqual(k.className(),
'Second')
282 obj1 = test.Second(
'Obj-1')
283 obj2 = test.Second(
'Obj-2')
287 db.create_db(
"test.data.xml", [f
'{scriptsdir}/test.schema.xml'])
288 db.update_dal(obj1, recurse=
True)
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)
299 obj1 = test.Second(
'Obj-1')
300 obj2 = test.Second(
'Obj-2')
301 obj3 = test.Second(
'Obj-3')
302 obj4 = test.Second(
'Obj-4')
308 db.create_db(
"test.data.xml", [f
'{scriptsdir}/test.schema.xml'])
309 db.update_dal(obj1, recurse=
True)
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)
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')
331 db.update_dal(obj2, recurse=
True)
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')
343 obj1 = db.get_dal(
'Second',
'Obj-1')
345 obj2 = db.get_dal(
'Second',
'Obj-2')
346 obj2.string =
'xyzabc'
347 db.update_dal(obj1, recurse=
False)
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')
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
367 db.create_db(
"test2.data.xml", [f
'{scriptsdir}/test.schema.xml'])
368 db.update_dal(obj11, recurse=
True)
373 obj1 = db.get_dal(
'Second',
'Obj-1')
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())
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)
392 ret = db.get_dal(
'Third',
'Test-2')
395 self.assertEqual(ret.get(
'Second',
'Test-1'), obj1)
396 self.assertEqual(type(ret.get(
'Second')), list)
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)
401 len(ret.get(
'Second', re.compile(
r'Test-[0-9]'),
True)), 2)
405 obj = db.get_dal(
'Dummy',
'Test-3')
414 obj1 = db.get_dal(
'Second',
'Test-1')
423 obj = db.get_dal(
'Dummy',
'Test-3')
426 obj2 = db.get_dal(
'Dummy',
'Test-4')
427 self.assertEqual(obj, obj2)
431if __name__ ==
"__main__":
433 sys.argv.append(
'-v')
test22_CanReadWriteDalOnConfiguration(self)
test20_CanReadWriteMultiValuedRelations(self)
test04_CanReadWriteUInt16(self)
test29_CanWriteInfiniteRecursion(self)
test37_BookkeepUpdatedObjects(self)
test00_CanReadWriteBool(self)
test05_CanReadWriteInt32(self)
test15_CanReadWriteClassReference(self)
test01_CanReadWriteInt8(self)
test06_CanReadWriteUInt32(self)
test25_CanRecurseALot(self)
test28_CanHandleInheritanceAtSearch(self)
test17_AttributeInheritance(self)
test13_CanReadWriteDate(self)
test02_CanReadWriteUInt8(self)
test33_CanHandleDalDestruction(self)
test30_CanReadInfiniteRecursion(self)
test34_CanModifyNonRecursively(self)
test35_CannotMoveIfNotIncluded(self)
test12_CanReadWriteEnum(self)
test24_CanReadWriteBigDatabases(self)
test03_CanReadWriteInt16(self)
test07_CanReadWriteInt64(self)
test14_CanReadWriteTime(self)
test10_CanReadWrite64bitVectors(self)
test23_CanSearchForDalsAtConfiguration(self)
test36_CanSearchForObjects(self)
test18_CanCompareDals(self)
test27_CanSearchUsingBaseClasses(self)
test19_CanReadWriteSingleValuedRelations(self)
test16_CanNotSetUnexistingAttribute(self)
test11_CanReadWrite64bitVectorsAndWithRange(self)
test26_CanModifyDatabases(self)
test08_CanReadWriteUInt64(self)
test32_CanReadBiggerRecursion(self)
test09_CanReadWriteStringsWithRanges(self)
test21_CanAttributeFromConstructor(self)
test31_CanWriteBiggerRecursion(self)
module(name, schema, other_dals=[], backend='oksconflibs', db=None)
test_good(test, obj, field, values)
test_bad(test, obj, field, values)