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