6"""Unit test for the Python bindings to the DAL."""
12scriptsdir=os.path.dirname(os.path.realpath(__file__))
22 """Tests I can set good expected values to certain fields"""
24 setattr(obj, field, v)
25 test.assertEqual(getattr(obj, field), v)
29 """Tests I can not set bad values to certain fields"""
31 test.assertRaises(ValueError, setattr, obj, field, v)
37 obj = test.Dummy(
'Test-1')
38 test_good(self, obj,
'bool', [
True,
False, 0, 1])
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])
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])
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])
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])
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])
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])
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])
77 obj = test.Dummy(
'Test-1')
78 test_good(self, obj,
'uint64', [0, 2**64-1, 42, 0xabcd])
80 [-1, 2**64, 0xabcdeabcdeabcdeabcabcffabc])
83 obj = test.Dummy(
'Test-1')
85 [
'test01',
'test09'], [
'test30']])
86 test_bad(self, obj,
'string_vector', [[
'Test01',
'test111'], [
'abcd']])
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]])
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])
101 obj = test.Dummy(
'Test-1')
102 test_good(self, obj,
'enum', [
'FIRST',
'SECOND'])
103 test_bad(self, obj,
'string_vector', [
'first',
'zeroth'])
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'])
111 obj = test.Dummy(
'Test-1')
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'])
117 obj = test.Dummy(
'Test-1')
118 test_good(self, obj,
'classref', [
'Dummy',
'Third'])
119 test_bad(self, obj,
'classref', [
'DoesNotExistInDal'])
122 obj = test.Dummy(
'Test-1')
123 self.assertRaises(AttributeError, setattr, obj,
'doesNotExist', 24)
126 obj = test.Third(
'Test-1')
127 test_good(self, obj,
'classref', [
'Second',
'Third',
'Dummy'])
128 test_bad(self, obj,
'classref', [
'DoesNotExistInDal'])
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)
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])
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]])
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'],
158 self.assertEqual(obj2.Dummy, [obj])
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)
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)
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)
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)
194 for i
in range(number):
195 objs.append(test.Second(
"Object-%d" % i))
197 db.create_db(
"test.data.xml", [f
'{scriptsdir}/test.schema.xml'])
199 db.update_dal(k, recurse=
True)
203 objs = db.get_dals(
'Second')
204 self.assertEqual(len(objs), number)
208 sys.setrecursionlimit(50000)
209 depth = RECURSION_TEST
211 for i
in range(depth):
212 obj = test.Second(
"Object-%d" % i)
214 obj.Another = previous
217 db.create_db(
"test.data.xml", [f
'{scriptsdir}/test.schema.xml'])
218 db.update_dal(previous, recurse=
True)
222 top = db.get_dal(
'Second',
'Object-%d' % (depth-1))
227 self.assertEqual(counter, depth)
231 sys.setrecursionlimit(50000)
232 depth = RECURSION_TEST
234 top = db.get_dal(
'Second',
'Object-%d' % (depth-1))
237 all.append(top.Another)
239 self.assertEqual(len(all), depth)
242 all = list(reversed(all))
243 for i, k
in enumerate(all):
250 db.update_dal(all[0], recurse=
True)
256 top = db.get_dal(
'Second',
'Object-0')
261 self.assertEqual(counter, depth)
266 self.assertEqual(len(db.get_dals(
'Dummy')), RECURSION_TEST)
271 objs = db.get_dals(
'Dummy')
272 self.assertEqual(len(objs), RECURSION_TEST)
274 self.assertEqual(k.className(),
'Second')
277 obj1 = test.Second(
'Obj-1')
278 obj2 = test.Second(
'Obj-2')
282 db.create_db(
"test.data.xml", [f
'{scriptsdir}/test.schema.xml'])
283 db.update_dal(obj1, recurse=
True)
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)
294 obj1 = test.Second(
'Obj-1')
295 obj2 = test.Second(
'Obj-2')
296 obj3 = test.Second(
'Obj-3')
297 obj4 = test.Second(
'Obj-4')
303 db.create_db(
"test.data.xml", [f
'{scriptsdir}/test.schema.xml'])
304 db.update_dal(obj1, recurse=
True)
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)
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')
326 db.update_dal(obj2, recurse=
True)
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')
338 obj1 = db.get_dal(
'Second',
'Obj-1')
340 obj2 = db.get_dal(
'Second',
'Obj-2')
341 obj2.string =
'xyzabc'
342 db.update_dal(obj1, recurse=
False)
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')
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
362 db.create_db(
"test2.data.xml", [f
'{scriptsdir}/test.schema.xml'])
363 db.update_dal(obj11, recurse=
True)
368 obj1 = db.get_dal(
'Second',
'Obj-1')
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())
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)
387 ret = db.get_dal(
'Third',
'Test-2')
390 self.assertEqual(ret.get(
'Second',
'Test-1'), obj1)
391 self.assertEqual(type(ret.get(
'Second')), list)
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)
396 len(ret.get(
'Second', re.compile(
r'Test-[0-9]'),
True)), 2)
400 obj = db.get_dal(
'Dummy',
'Test-3')
409 obj1 = db.get_dal(
'Second',
'Test-1')
418 obj = db.get_dal(
'Dummy',
'Test-3')
421 obj2 = db.get_dal(
'Dummy',
'Test-4')
422 self.assertEqual(obj, obj2)
426if __name__ ==
"__main__":
428 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)