DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
test_configobject.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 ConfigObject class."""
7
8import os
9import unittest
10import conffwk
11
12scriptsdir=os.path.dirname(os.path.realpath(__file__))
13
14def limit_test(obj, attrname, min, max):
15 db = conffwk.Configuration("oksconflibs")
16 db.create_db('test.data.xml', [f'{scriptsdir}/test.schema.xml'])
17
18 low = db.create_obj("Dummy", "TestDummy-Low")
19 low[attrname] = min
20 obj.assertEqual(low[attrname], min)
21
22 high = db.create_obj("Dummy", "TestDummy-High")
23 high[attrname] = max
24 obj.assertEqual(high[attrname], max)
25
26 db.commit()
27
28
29def limit_test_raise(obj, attrname, min, max):
30 db = conffwk.Configuration("oksconflibs")
31 filename = 'overflow_test.data.xml'
32 db.create_db(filename, [f'{scriptsdir}/test.schema.xml'])
33
34 low = db.create_obj("Dummy", "TestDummy-Underflow")
35
36 #obj.assertRaises(OverflowError, low.__setitem__, attrname, min-1)
37 obj.assertRaises(TypeError, low.__setitem__, attrname, min-1)
38
39 high = db.create_obj("Dummy", "TestDummy-Overflow")
40 #obj.assertRaises(OverflowError, high.__setitem__, attrname, max+1)
41 obj.assertRaises(TypeError, high.__setitem__, attrname, max+1)
42
43 db.commit()
44
45
46class ConfigObject(unittest.TestCase):
47
49 limit_test(self, 'bool', False, True)
50
52 limit_test(self, 'sint8', -2**7, (2**7)-1)
53 limit_test_raise(self, 'sint8', -2**7, (2**7)-1)
54
56 limit_test(self, 'uint8', 0, (2**8)-1)
57 limit_test_raise(self, 'uint8', 0, (2**8)-1)
58
60 limit_test(self, 'sint16', -2**15, (2**15)-1)
61 limit_test_raise(self, 'sint16', -2**15, (2**15)-1)
62
64 limit_test(self, 'uint16', 0, (2**16)-1)
65 limit_test_raise(self, 'uint16', 0, (2**16)-1)
66
68 limit_test(self, 'sint32', -2**31, (2**31)-1)
69 limit_test_raise(self, 'sint32', -2**31, (2**31)-1)
70
72 limit_test(self, 'uint32', 0, (2**32)-1)
73 # The next test actually will fail on 32-bit machines if we set the
74 # lower limit to 0. It works OK in other platforms. The bug is related
75 # to the function PyLong_AsUnsignedLongLong() and variants that convert
76 # '-1L' to the highest possible 32-bit unsigned integer not raising an
77 # negative OverflowError as in the other convertions above.
78 # limit_test_raise(self, 'uint32', 0, (2**32)-1)
79
81 limit_test(self, 'sint64', -2**63, (2**63)-1)
82 limit_test_raise(self, 'sint64', -2**63, (2**63)-1)
83
85 limit_test(self, 'uint64', 0, (2**64)-1)
86 # The next test actually will fail on 64-bit machines if we set the
87 # lower limit to 0. It works OK in other platforms. The bug is related
88 # to the function PyLong_AsUnsignedLongLong() and variants that convert
89 # '-1L' to the highest possible 64-bit unsigned integer not raising an
90 # negative OverflowError as in the other convertions above.
91 # limit_test_raise(self, 'uint64', 0, (2**64)-1)
92
94 db = conffwk.Configuration("oksconflibs")
95 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
96 t = db.create_obj("Dummy", "TestDummy-1")
97 attrname = 'uint64_vector_range'
98 min = 2**64 - 16
99 max = 2**64 - 1
100 val = [min, max]
101 t[attrname] = val
102 self.assertEqual(t[attrname], val)
103 self.assertRaises(ValueError, t.__setitem__, attrname, [0, 1, 2])
104 db.commit()
105
107 db = conffwk.Configuration("oksconflibs")
108 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
109 t = db.create_obj("Dummy", "TestDummy-1")
110 val = 'bla'
111 attrname = 'string'
112 t[attrname] = val
113 self.assertEqual(t[attrname], val)
114 db.commit()
115
117 db = conffwk.Configuration("oksconflibs")
118 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
119 t = db.create_obj("Dummy", "TestDummy-1")
120 val = 'FIRST'
121 attrname = 'enum'
122 t[attrname] = val
123 self.assertEqual(t[attrname], val)
124 val = 'DOES NOT EXIST'
125 self.assertRaises(ValueError, t.__setitem__, attrname, val)
126
128 db = conffwk.Configuration("oksconflibs")
129 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
130 t = db.create_obj("Dummy", "TestDummy-1")
131 val = ["test10", "test20"]
132 attrname = 'string_vector'
133 t[attrname] = val
134 self.assertEqual(t[attrname], val)
135
137 db = conffwk.Configuration("oksconflibs")
138 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
139 t = db.create_obj("Dummy", "TestDummy-1")
140 val = "2000-Jan-01"
141 attrname = 'date'
142 t[attrname] = val
143 self.assertEqual(t[attrname], val)
144
146 db = conffwk.Configuration("oksconflibs")
147 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
148 t = db.create_obj("Dummy", "TestDummy-1")
149 val = "2000-Jan-01 00:00:00"
150 attrname = 'time'
151 t[attrname] = val
152 self.assertEqual(t[attrname], val)
153
155 db = conffwk.Configuration("oksconflibs")
156 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
157 t = db.create_obj("Dummy", "TestDummy-1")
158 val = 'Second'
159 attrname = 'classref'
160 t[attrname] = val
161 self.assertEqual(t[attrname], val)
162 self.assertRaises(ValueError, t.__setitem__, attrname, 'DoesNotExist')
163 db.commit()
164
166 db = conffwk.Configuration("oksconflibs")
167 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
168 relation = db.create_obj("Dummy", "Single-Relation")
169 t = db.create_obj('Third', 'Originator')
170 attrname = 'Another'
171 self.assertEqual(t[attrname], None) # problem
172 t[attrname] = relation
173 self.assertEqual(t[attrname], relation)
174 db.commit()
175
177 db = conffwk.Configuration("oksconflibs")
178 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
179 relations = []
180 for i in range(10):
181 relations.append(db.create_obj("Second", "Relation-%d" % i))
182 t = db.create_obj('Third', 'Originator')
183 attrname = 'Seconds'
184 t[attrname] = relations
185 self.assertEqual(t[attrname], relations)
186 db.commit()
187
189 db = conffwk.Configuration("oksconflibs")
190 db.create_db("test.data.xml", [f'{scriptsdir}/test.schema.xml'])
191 t = db.create_obj('Dummy', 'TestDummy-1')
192 t.rename('TestDummy-2')
193 self.assertEqual(t.UID(), 'TestDummy-2')
194 t2 = db.get_obj('Dummy', 'TestDummy-2')
195 self.assertEqual(t, t2)
196 db.commit()
197
198
199if __name__ == "__main__":
200 import sys
201 sys.argv.append('-v')
202 unittest.main()
limit_test(obj, attrname, min, max)
limit_test_raise(obj, attrname, min, max)