Coverage for tests/test_PropertyList.py: 6%
644 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-06 01:35 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-06 01:35 -0800
1#
2# LSST Data Management System
3#
4# Copyright 2008-2017 AURA/LSST.
5#
6# This product includes software developed by the
7# LSST Project (http://www.lsst.org/).
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the LSST License Statement and
20# the GNU General Public License along with this program. If not,
21# see <http://www.lsstcorp.org/LegalNotices/>.
22#
24import pickle
25import unittest
27import lsst.utils.tests
28import lsst.daf.base as dafBase
31class FloatSubClass(float):
32 """Intended to be something like numpy.float64, without introducing a
33 dependency on numpy
34 """
35 pass
38class PropertyListTestCase(unittest.TestCase):
39 """A test case for PropertyList.
40 """
42 def testConstruct(self):
43 apl = dafBase.PropertyList()
44 self.assertIsNotNone(apl)
46 def checkPickle(self, original):
47 new = pickle.loads(pickle.dumps(original, 2))
48 self.assertEqual(new, original)
49 return new
51 def testScalar(self):
52 apl = dafBase.PropertyList()
53 apl.setBool("bool", True)
54 apl.setShort("short", 42)
55 apl.setInt("int", 2008)
56 apl.setLongLong("int64_t", 0xfeeddeadbeef)
57 apl.setFloat("float", 3.14159)
58 apl.setDouble("double", 2.718281828459045)
59 apl.set("char*", "foo")
60 apl.setString("string", "bar")
61 apl.set("int2", 2009)
62 apl.set("dt", dafBase.DateTime("20090402T072639.314159265Z", dafBase.DateTime.UTC))
63 apl.set("subclass", FloatSubClass(1.23456789))
64 apl.set("undef", None)
66 self.assertTrue(apl.isUndefined("undef"))
67 self.assertFalse(apl.isUndefined("string"))
68 self.assertEqual(apl.typeOf("bool"), dafBase.PropertyList.TYPE_Bool)
69 self.assertEqual(apl.getBool("bool"), True)
70 self.assertEqual(apl.typeOf("short"), dafBase.PropertyList.TYPE_Short)
71 self.assertEqual(apl.getShort("short"), 42)
72 self.assertEqual(apl.typeOf("int"), dafBase.PropertyList.TYPE_Int)
73 self.assertEqual(apl.getInt("int"), 2008)
74 self.assertEqual(apl.typeOf("int64_t"),
75 dafBase.PropertyList.TYPE_LongLong)
76 self.assertEqual(apl.getLongLong("int64_t"), 0xfeeddeadbeef)
77 self.assertEqual(apl.typeOf("float"), dafBase.PropertyList.TYPE_Float)
78 self.assertAlmostEqual(apl.getFloat("float"), 3.14159, 6)
79 self.assertEqual(apl.typeOf("double"), dafBase.PropertyList.TYPE_Double)
80 self.assertEqual(apl.getDouble("double"), 2.718281828459045)
81 self.assertEqual(apl.typeOf("char*"), dafBase.PropertyList.TYPE_String)
82 self.assertEqual(apl.getString("char*"), "foo")
83 self.assertEqual(apl.typeOf("string"), dafBase.PropertyList.TYPE_String)
84 self.assertEqual(apl.getString("string"), "bar")
85 self.assertEqual(apl.typeOf("int2"), dafBase.PropertyList.TYPE_Int)
86 self.assertEqual(apl.getInt("int2"), 2009)
87 self.assertEqual(apl.get("int2"), 2009)
88 self.assertEqual(apl.getArray("int2"), [2009])
89 self.assertEqual(apl.getScalar("int2"), 2009)
90 self.assertEqual(apl.typeOf("dt"), dafBase.PropertyList.TYPE_DateTime)
91 self.assertEqual(apl.getDateTime("dt").nsecs(), 1238657233314159265)
92 self.assertEqual(apl.getDouble("subclass"), 1.23456789)
93 self.assertEqual(apl["int2"], 2009)
95 self.assertIsNone(apl.getScalar("undef"))
96 self.assertEqual(apl.typeOf("undef"), dafBase.PropertyList.TYPE_Undef)
97 self.assertIsNone(apl.get("undef"))
98 self.assertIsNone(apl["undef"])
99 self.assertEqual(apl.valueCount(), 12)
100 self.checkPickle(apl)
102 # Now replace the undef value with a defined value
103 apl.set("undef", "not undefined")
104 self.assertEqual(apl.getScalar("undef"), "not undefined")
105 self.assertFalse(apl.isUndefined("undef"))
106 self.assertEqual(apl.typeOf("undef"), dafBase.PropertyList.TYPE_String)
107 self.assertEqual(apl.valueCount(), 12)
109 def testGetDefault(self):
110 apl = dafBase.PropertyList()
111 apl.setInt("int", 42)
112 self.assertEqual(apl.getInt("int"), 42)
113 self.assertEqual(apl.getInt("int", 2008), 42)
114 self.assertEqual(apl.getInt("foo", 2008), 2008)
115 self.assertEqual(apl.get("int"), 42)
116 self.assertEqual(apl.get("int", 2008), 42)
117 self.assertEqual(apl.get("foo", 2008), 2008)
118 self.assertEqual(apl.get("foo2", default="missing"), "missing")
119 self.assertIsNone(apl.get("foo"))
121 def testExists(self):
122 apl = dafBase.PropertyList()
123 apl.setInt("int", 42)
124 self.assertEqual(apl.exists("int"), True)
125 self.assertEqual(apl.exists("foo"), False)
127 def testGetVector(self):
128 apl = dafBase.PropertyList()
129 v = [42, 2008, 1]
130 apl.setInt("ints", v)
131 apl.setInt("ints2", [10, 9, 8])
132 w = apl.getArrayInt("ints")
133 self.assertEqual(len(w), 3)
134 self.assertEqual(v, w)
135 self.assertEqual(apl.getInt("ints2"), 8)
136 self.assertEqual(apl.getArrayInt("ints2"), [10, 9, 8])
137 w = apl.get("ints")
138 self.assertIsInstance(w, int)
139 self.assertEqual(v[-1], w)
140 self.assertEqual(apl["ints"], v[-1])
141 self.assertEqual(apl.getArray("ints"), v)
142 self.assertEqual(apl.getScalar("ints"), v[-1])
143 self.assertEqual(apl.valueCount(), 6)
144 apl.setInt("int", 999)
145 x = apl.get("int")
146 self.assertEqual(x, 999)
147 self.assertEqual(apl.getArray("int"), [999])
148 self.assertEqual(apl.getScalar("int"), 999)
149 self.assertEqual(apl["int"], 999)
150 self.assertEqual(apl.valueCount(), 7)
152 self.checkPickle(apl)
154 def testGetVector2(self):
155 apl = dafBase.PropertyList()
156 v = [42, 2008, 1]
157 apl.setInt("ints", v)
158 apl.setInt("ints2", [10, 9, 8])
159 w = apl.getArrayInt("ints")
160 self.assertEqual(len(w), 3)
161 self.assertEqual(v[0], w[0])
162 self.assertEqual(v[1], w[1])
163 self.assertEqual(v[2], w[2])
164 self.assertEqual(apl.getInt("ints2"), 8)
165 self.assertEqual(apl.getArrayInt("ints2"), [10, 9, 8])
167 self.checkPickle(apl)
169 def testAddScalar(self):
170 apl = dafBase.PropertyList()
171 v = [42, 2008, 1]
172 apl.setInt("ints", v)
173 apl.addInt("ints", -999)
174 apl.add("other", "foo")
175 apl.add("ints", 13)
176 apl.add("subclass", FloatSubClass(1.23456789))
177 w = apl.getArrayInt("ints")
178 self.assertEqual(len(w), 5)
179 self.assertEqual(v[0], w[0])
180 self.assertEqual(v[1], w[1])
181 self.assertEqual(v[2], w[2])
182 self.assertEqual(w[3], -999)
183 self.assertEqual(w[4], 13)
184 self.assertEqual(apl.getString("other"), "foo")
185 self.assertEqual(apl.get("subclass"), 1.23456789)
186 self.assertEqual(apl.getArray("subclass"), [1.23456789])
187 self.assertEqual(apl.getScalar("subclass"), 1.23456789)
188 self.assertEqual(apl.valueCount(), 7)
190 def testDateTimeToString(self):
191 apl = dafBase.PropertyList()
192 apl.set("dt", dafBase.DateTime("20090402T072639.314159265Z", dafBase.DateTime.UTC))
193 self.assertEqual(apl.toString(),
194 "dt = 2009-04-02T07:26:39.314159265Z\n")
196 def testGetScalarThrow(self):
197 apl = dafBase.PropertyList()
198 apl.setBool("bool", True)
199 apl.setShort("short", 42)
200 apl.setInt("int", 2008)
201 apl.setLongLong("int64_t", 0xfeeddeadbeef)
202 apl.setFloat("float", 3.14159)
203 apl.setDouble("double", 2.718281828459045)
204 apl.setString("string", "bar")
206 with self.assertRaises(KeyError):
207 apl["foo"]
208 with self.assertRaises(TypeError):
209 apl.getBool("short")
210 with self.assertRaises(TypeError):
211 apl.getBool("int")
212 with self.assertRaises(TypeError):
213 apl.getShort("int")
214 with self.assertRaises(TypeError):
215 apl.getInt("short")
216 with self.assertRaises(TypeError):
217 apl.getInt("bool")
218 with self.assertRaises(TypeError):
219 apl.getDouble("float")
220 with self.assertRaises(TypeError):
221 apl.getFloat("double")
222 with self.assertRaises(TypeError):
223 apl.getString("int")
225 def testAddVector(self):
226 apl = dafBase.PropertyList()
227 v = [42, 2008, 1]
228 apl.set("ints", v)
229 apl.add("ints", [-42, -2008, -1])
230 subclass = [FloatSubClass(1.23), FloatSubClass(4.56), FloatSubClass(7.89)]
231 apl.add("subclass", subclass)
232 self.assertEqual(apl.getArrayInt("ints"),
233 [42, 2008, 1, -42, -2008, -1])
234 self.assertEqual(apl.get("subclass"), subclass[-1])
235 self.assertEqual(apl.getArray("subclass"), subclass)
236 self.assertEqual(apl.getScalar("subclass"), subclass[-1])
237 self.assertEqual(apl.valueCount(), 9)
239 def testComment(self):
240 apl = dafBase.PropertyList()
241 apl.set("NAXIS", 2, "two-dimensional")
242 self.assertEqual(apl.get("NAXIS"), 2)
243 self.assertEqual(apl.getArray("NAXIS"), [2])
244 self.assertEqual(apl.getScalar("NAXIS"), 2)
245 self.assertEqual(apl.getComment("NAXIS"), "two-dimensional")
246 apl.set("NAXIS", 3, "three-dimensional")
247 self.assertEqual(apl.get("NAXIS"), 3)
248 self.assertEqual(apl.getArray("NAXIS"), [3])
249 self.assertEqual(apl.getScalar("NAXIS"), 3)
250 self.assertEqual(apl.getComment("NAXIS"), "three-dimensional")
251 self.assertEqual(apl.valueCount(), 1)
253 def testOrder(self):
254 apl = dafBase.PropertyList()
255 apl.set("SIMPLE", True)
256 apl.set("BITPIX", -32)
257 apl.set("NAXIS", 2)
258 apl.set("COMMENT", "This is a test")
259 apl.add("COMMENT", "This is a test line 2")
260 apl.set("RA", 3.14159, "decimal degrees")
261 apl.set("DEC", 2.71828, "decimal radians")
262 correct = [
263 ("SIMPLE", True, ""),
264 ("BITPIX", -32, ""),
265 ("NAXIS", 2, ""),
266 ("COMMENT", "This is a test", ""),
267 ("COMMENT", "This is a test line 2", ""),
268 ("RA", 3.14159, "decimal degrees"),
269 ("DEC", 2.71828, "decimal radians"),
270 ]
271 self.assertEqual(apl.toList(), correct)
272 apl.set("NAXIS1", 513, "length of data axis 1")
273 correct.append(("NAXIS1", 513, "length of data axis 1"))
274 self.assertEqual(apl.toList(), correct)
275 apl.set("RA", 1.414)
276 correct[5] = ("RA", 1.414, "decimal degrees")
277 self.assertEqual(apl.toList(), correct)
278 apl.set("DEC", 1.732)
279 correct[6] = ("DEC", 1.732, "decimal radians")
280 self.assertEqual(apl.toList(), correct)
281 apl.set("DEC", -6.28, "")
282 correct[6] = ("DEC", -6.28, "")
283 self.assertEqual(apl.toList(), correct)
284 apl.add("COMMENT", "This is a test line 3", "")
285 correct.insert(5, ("COMMENT", "This is a test line 3", ""))
286 self.assertEqual(apl.toList(), correct)
288 self.checkPickle(apl)
290 def testToOrderedDict(self):
291 from collections import OrderedDict
293 apl = dafBase.PropertyList()
294 apl.set("SIMPLE", True)
295 apl.set("BITPIX", -32)
296 apl.set("NAXIS", 2)
297 apl.set("RA", 3.14159)
298 apl.set("DEC", 2.71828)
299 apl.set("FILTER", None)
300 apl.set("COMMENT", "This is a test")
301 apl.add("COMMENT", "This is a test line 2")
302 correct = OrderedDict([
303 ("SIMPLE", True),
304 ("BITPIX", -32),
305 ("NAXIS", 2),
306 ("RA", 3.14159),
307 ("DEC", 2.71828),
308 ("FILTER", None),
309 ("COMMENT", ["This is a test", "This is a test line 2"])
310 ])
311 self.assertEqual(apl.toOrderedDict(), correct)
313 apl.set("NAXIS1", 513)
314 correct["NAXIS1"] = 513
315 self.assertEqual(apl.toOrderedDict(), correct)
316 apl.set("RA", 1.414)
317 correct["RA"] = 1.414
318 self.assertEqual(apl.toOrderedDict(), correct)
319 apl.set("DEC", 1.732)
320 correct["DEC"] = 1.732
321 self.assertEqual(apl.toOrderedDict(), correct)
322 apl.set("DEC", -6.28)
323 correct["DEC"] = -6.28
324 self.assertEqual(apl.toOrderedDict(), correct)
325 apl.add("COMMENT", "This is a test line 3")
326 correct["COMMENT"] = correct["COMMENT"] + ["This is a test line 3", ]
327 self.assertEqual(apl.toOrderedDict(), correct)
329 def testHierarchy(self):
330 apl = dafBase.PropertyList()
331 apl.set("CURRENT", 49.5)
332 apl.set("CURRENT.foo", -32)
333 apl.set("CURRENT.bar", 2)
334 self.assertEqual(apl.get("CURRENT"), 49.5)
335 self.assertEqual(apl.getArray("CURRENT"), [49.5])
336 self.assertEqual(apl.getScalar("CURRENT"), 49.5)
337 self.assertEqual(apl.get("CURRENT.foo"), -32)
338 self.assertEqual(apl.getArray("CURRENT.foo"), [-32])
339 self.assertEqual(apl.getScalar("CURRENT.foo"), -32)
340 self.assertEqual(apl.get("CURRENT.bar"), 2)
341 self.assertEqual(apl.getArray("CURRENT.bar"), [2])
342 self.assertEqual(apl.getScalar("CURRENT.bar"), 2)
343 self.assertEqual(apl.valueCount(), 3)
345 aps = dafBase.PropertySet()
346 aps.set("bottom", "x")
347 aps.set("sibling", 42)
348 apl.set("top", aps)
349 self.assertEqual(apl.get("top.bottom"), "x")
350 self.assertEqual(apl.getArray("top.bottom"), ["x"])
351 self.assertEqual(apl.getScalar("top.bottom"), "x")
352 self.assertEqual(apl.get("top.sibling"), 42)
353 self.assertEqual(apl.getArray("top.sibling"), [42])
354 self.assertEqual(apl.getScalar("top.sibling"), 42)
355 self.assertEqual(apl.valueCount(), 5)
356 with self.assertRaises(KeyError):
357 apl["top"]
358 self.assertEqual(apl.toString(),
359 'CURRENT = 49.500000000000\nCURRENT.foo = -32\nCURRENT.bar = 2\n'
360 'top.sibling = 42\ntop.bottom = "x"\n')
362 self.checkPickle(apl)
364 # Check that a PropertyList (with comment) can go in a PropertySet
365 apl.set("INT", 45, "an integer")
366 aps = dafBase.PropertySet()
367 aps.set("bottom", "x")
368 aps.set("apl", apl)
369 new = self.checkPickle(aps)
370 self.assertIsInstance(new, dafBase.PropertySet)
371 self.assertIsInstance(new.getScalar("apl"), dafBase.PropertyList)
372 self.assertEqual(new.getScalar("apl").getComment("INT"), "an integer")
374 def testCombineHierarchical(self):
375 # Test that we can perform a deep copy of a PropertyList containing a
376 # hierarchical (contains a '.') key.
377 # This was a segfault prior to addressing DM-882.
378 pl1 = dafBase.PropertyList()
379 pl1.set("a.b", 1)
380 pl2 = pl1.deepCopy() # should not segfault
381 self.assertEqual(pl1.get("a.b"), pl2.get("a.b"))
382 self.assertEqual(pl1.getArray("a.b"), pl2.getArray("a.b"))
383 self.assertEqual(pl1.getScalar("a.b"), pl2.getScalar("a.b"))
384 self.checkPickle(pl1)
386 def testCopy(self):
387 dest = dafBase.PropertyList()
388 source = dafBase.PropertyList()
389 value1 = [1.5, 3.2]
390 source.set("srcItem1", value1)
391 dest.copy("destItem1", source, "srcItem1")
392 self.assertEqual(dest.get("destItem1"), value1[-1])
393 self.assertEqual(dest.getArray("destItem1"), value1)
394 self.assertEqual(dest.getScalar("destItem1"), value1[-1])
395 self.assertEqual(dest.valueCount(), 2)
397 # items are replaced, regardless of type
398 dest.set("destItem2", "string value")
399 self.assertEqual(dest.valueCount(), 3)
400 value2 = [5, -4, 3]
401 source.set("srcItem2", value2)
402 dest.copy("destItem2", source, "srcItem2")
403 self.assertEqual(dest.get("destItem2"), value2[-1])
404 self.assertEqual(dest.getArray("destItem2"), value2)
405 self.assertEqual(dest.getScalar("destItem2"), value2[-1])
406 self.assertEqual(dest.valueCount(), 5)
408 # asScalar copies only the last value
409 dest.copy("destItem2Scalar", source, "srcItem2", asScalar=True)
410 self.assertEqual(dest.get("destItem2Scalar"), value2[-1])
411 self.assertEqual(dest.getArray("destItem2Scalar"), [value2[-1]])
412 self.assertEqual(dest.getScalar("destItem2Scalar"), value2[-1])
413 self.assertEqual(dest.valueCount(), 6)
415 def testArrayProperties(self):
416 apl = dafBase.PropertyList()
417 v = [42, 2008, 1]
418 apl.set("ints", v)
419 apl.set("int", 365)
420 apl.set("ints2", -42)
421 apl.add("ints2", -2008)
423 self.assertTrue(apl.isArray("ints"))
424 self.assertFalse(apl.isArray("int"))
425 self.assertTrue(apl.isArray("ints2"))
426 self.assertEqual(apl.valueCount("ints"), 3)
427 self.assertEqual(apl.valueCount("int"), 1)
428 self.assertEqual(apl.valueCount("ints2"), 2)
429 self.assertEqual(apl.typeOf("ints"), dafBase.PropertyList.TYPE_Int)
430 self.assertEqual(apl.typeOf("int"), dafBase.PropertyList.TYPE_Int)
431 self.assertEqual(apl.typeOf("ints2"), dafBase.PropertyList.TYPE_Int)
432 self.assertEqual(apl.valueCount(), 6)
434 def testHierarchy2(self):
435 apl = dafBase.PropertyList()
436 aplp = dafBase.PropertyList()
438 aplp.set("pre", 1)
439 apl.set("apl1", aplp)
441 # Python will not see this, aplp is disconnected
442 aplp.set("post", 2)
443 self.assertFalse(apl.exists("apl1.post"))
445 apl.set("int", 42)
447 # Setting an empty PropertyList has no effect
448 apl.set("apl2", dafBase.PropertyList())
449 self.assertFalse(apl.exists("apl2"))
451 apl.set("apl2.plus", 10.24)
452 apl.set("apl2.minus", -10.24)
453 apl.set("apl3.sub1", "foo")
454 apl.set("apl3.sub2", "bar")
456 self.assertTrue(apl.exists("apl1.pre"))
457 self.assertTrue(apl.exists("apl2.plus"))
458 self.assertTrue(apl.exists("apl2.minus"))
459 self.assertTrue(apl.exists("apl3.sub1"))
460 self.assertTrue(apl.exists("apl3.sub2"))
462 # Make sure checking a subproperty doesn't create it.
463 self.assertFalse(apl.exists("apl2.pre"))
464 self.assertFalse(apl.exists("apl2.pre"))
465 # Make sure checking an element doesn't create it.
466 self.assertFalse(apl.exists("apl4"))
467 self.assertFalse(apl.exists("apl4"))
468 # Make sure checking a subproperty with a nonexistent parent doesn't
469 # create it.
470 self.assertFalse(apl.exists("apl4.sub"))
471 self.assertFalse(apl.exists("apl4.sub"))
472 # Make sure checking a subproperty doesn't create its parent.
473 self.assertFalse(apl.exists("apl4"))
475 def testvariousThrows(self):
476 apl = dafBase.PropertyList()
477 apl.set("int", 42)
479 # This raises an exception in C++ test but works in Python
480 apl.set("int.sub", "foo")
482 with self.assertRaises(TypeError):
483 apl.getDouble("int")
484 with self.assertRaises(LookupError):
485 apl.getDouble("double"),
486 with self.assertRaises(LookupError):
487 apl.getArrayDouble("double")
488 with self.assertRaises(LookupError):
489 apl.typeOf("double")
490 with self.assertRaises(TypeError):
491 apl.add("int", 4.2),
493 v = [3.14159, 2.71828]
494 with self.assertRaises(TypeError):
495 apl.add("int", v)
496 apl.remove("foo.bar")
497 apl.remove("int.sub")
499 def testNames(self):
500 apl = dafBase.PropertyList()
501 apl.set("apl1.pre", 1)
502 apl.set("apl1.post", 2)
503 apl.set("int", 42)
504 apl.set("double", 3.14)
505 apl.set("apl2.plus", 10.24)
506 apl.set("apl2.minus", -10.24)
508 # Hierarchy is always flat
509 self.assertEqual(apl.nameCount(), 6)
510 self.assertEqual(apl.nameCount(False), 6)
512 v = set(apl.names())
513 self.assertEqual(len(v), 6)
514 self.assertEqual(v, {"double", "int", "apl1.post",
515 "apl1.pre", "apl2.minus", "apl2.plus"})
517 def testParamNames(self):
518 apl = dafBase.PropertyList()
519 apl.set("apl1.pre", 1)
520 apl.set("apl1.post", 2)
521 apl.set("int", 42)
522 apl.set("double", 3.14)
523 apl.set("apl2.plus", 10.24)
524 apl.set("apl2.minus", -10.24)
526 v = set(apl.paramNames())
527 self.assertEqual(len(v), 6)
528 self.assertEqual(v, {"double", "int", "apl1.post", "apl1.pre",
529 "apl2.minus", "apl2.plus"})
531 def testPropertySetNames(self):
532 apl = dafBase.PropertyList()
533 apl.set("apl1.pre", 1)
534 apl.set("apl1.post", 2)
535 apl.set("int", 42)
536 apl.set("double", 3.14)
537 apl.set("apl2.plus", 10.24)
538 apl.set("apl2.minus", -10.24)
539 apl.set("apl3.sub.subsub", "foo")
541 # There are no PropertySets inside flattened PropertyList
542 v = set(apl.propertySetNames())
543 self.assertEqual(len(v), 0)
545 def testGetAs(self):
546 apl = dafBase.PropertyList()
547 apl.set("bool", True)
548 s = 42
549 apl.setShort("short", s)
550 apl.set("int", 2008)
551 apl.set("int64_t", 0xfeeddeadbeef)
552 f = 3.14159
553 apl.setFloat("float", f)
554 d = 2.718281828459045
555 apl.setDouble("double", d)
556 apl.setString("char*", "foo")
557 apl.set("char*2", "foo2")
558 apl.set("string", "bar")
559 aplp = dafBase.PropertyList()
560 aplp.set("bottom", "x")
561 apl.set("top", aplp)
563 self.assertEqual(apl.getAsBool("bool"), True)
564 self.assertEqual(apl.getAsInt("bool"), 1)
565 self.assertEqual(apl.getAsInt("short"), 42)
566 self.assertEqual(apl.getAsInt("int"), 2008)
567 with self.assertRaises(TypeError):
568 apl.getAsInt("int64_t")
569 self.assertEqual(apl.getAsInt64("bool"), 1)
570 self.assertEqual(apl.getAsInt64("short"), 42)
571 self.assertEqual(apl.getAsInt64("int"), 2008)
572 self.assertEqual(apl.getAsInt64("int64_t"), 0xfeeddeadbeef)
573 with self.assertRaises(TypeError):
574 apl.getAsInt64("float")
575 self.assertEqual(apl.getAsDouble("bool"), 1.0)
576 self.assertEqual(apl.getAsDouble("short"), 42.0)
577 self.assertEqual(apl.getAsDouble("int"), 2008.0)
578 self.assertEqual(apl.getAsDouble("int64_t"), float(0xfeeddeadbeef))
579 self.assertAlmostEqual(apl.getAsDouble("float"), 3.14159, places=5)
580 self.assertEqual(apl.getAsDouble("double"), 2.718281828459045)
581 with self.assertRaises(TypeError):
582 apl.getAsDouble("char*")
583 self.assertEqual(apl.getAsString("char*"), "foo")
584 self.assertEqual(apl.getAsString("char*2"), "foo2")
585 self.assertEqual(apl.getAsString("string"), "bar")
586 with self.assertRaises(TypeError):
587 apl.getAsString("int")
588 self.assertEqual(apl.getAsString("top.bottom"), "x")
590 def testCombine(self):
591 apl = dafBase.PropertyList()
592 apl.set("apl1.pre", 1)
593 apl.set("apl1.post", 2)
594 apl.set("int", 42)
595 apl.set("double", 3.14)
596 apl.set("apl2.plus", 10.24)
597 apl.set("apl2.minus", -10.24)
598 apl.set("apl3.sub.subsub", "foo")
600 aplp = dafBase.PropertyList()
601 aplp.set("apl1.pre", 3)
602 aplp.add("apl1.pre", 4)
603 aplp.set("int", 2008)
604 aplp.set("apl2.foo", "bar")
605 aplp.set("apl4.top", "bottom")
607 apl.combine(aplp)
609 self.assertFalse(apl.isArray("apl1"))
610 self.assertTrue(apl.isArray("apl1.pre"))
611 self.assertFalse(apl.isArray("apl1.post"))
612 self.assertFalse(apl.isArray("apl2"))
613 self.assertFalse(apl.isArray("apl2.plus"))
614 self.assertFalse(apl.isArray("apl2.minus"))
615 self.assertFalse(apl.isArray("apl2.foo"))
616 self.assertFalse(apl.isArray("apl3"))
617 self.assertFalse(apl.isArray("apl3.sub"))
618 self.assertFalse(apl.isArray("apl3.subsub"))
619 self.assertFalse(apl.isArray("apl4"))
620 self.assertFalse(apl.isArray("apl4.top"))
621 self.assertTrue(apl.isArray("int"))
622 self.assertFalse(apl.isArray("double"))
623 self.assertEqual(apl.valueCount("apl1.pre"), 3)
624 self.assertEqual(apl.valueCount("int"), 2)
625 v = apl.getArray("apl1.pre")
626 self.assertEqual(v, [1, 3, 4])
627 v = apl.getArray("int")
628 self.assertEqual(v, [42, 2008])
629 self.assertEqual(apl.valueCount(), 12)
631 def testUpdate(self):
632 apl = dafBase.PropertyList()
633 apl.set("apl1.pre", 1)
634 apl.set("apl1.post", 2)
635 apl.set("int", 42)
636 apl.set("double", 3.14)
637 apl.set("apl2.plus", 10.24)
638 apl.set("apl2.minus", -10.24)
639 apl.set("apl3.sub.subsub", "foo")
641 aplp = dafBase.PropertyList()
642 aplp.set("apl1.pre", 3)
643 aplp.add("apl1.pre", 4)
644 aplp.set("int", 2008)
645 aplp.set("apl2.foo", "bar")
646 aplp.set("apl4.top", "bottom")
648 apl.update(aplp)
650 self.assertFalse(apl.isArray("apl1"))
651 self.assertTrue(apl.isArray("apl1.pre"))
652 self.assertFalse(apl.isArray("apl1.post"))
653 self.assertFalse(apl.isArray("apl2"))
654 self.assertFalse(apl.isArray("apl2.plus"))
655 self.assertFalse(apl.isArray("apl2.minus"))
656 self.assertFalse(apl.isArray("apl2.foo"))
657 self.assertFalse(apl.isArray("apl3"))
658 self.assertFalse(apl.isArray("apl3.sub"))
659 self.assertFalse(apl.isArray("apl3.subsub"))
660 self.assertFalse(apl.isArray("apl4"))
661 self.assertFalse(apl.isArray("apl4.top"))
662 self.assertFalse(apl.isArray("int"))
663 self.assertFalse(apl.isArray("double"))
664 self.assertEqual(apl.valueCount("apl1.pre"), 2)
665 self.assertEqual(apl.valueCount("int"), 1)
666 v = apl.getArray("apl1.pre")
667 self.assertEqual(v, [3, 4])
668 v = apl.getArray("int")
669 self.assertEqual(v, [2008])
670 self.assertEqual(apl.valueCount(), 10)
672 apld = {"int": 100, "str": "String", "apl1.foo": 10.5}
673 apl.update(apld)
674 self.assertEqual(apl["int"], apld["int"])
675 self.assertEqual(apl["str"], apld["str"])
676 self.assertEqual(apl["apl1.foo"], apld["apl1.foo"])
677 self.assertEqual(apl["double"], 3.14)
678 self.assertEqual(apl.valueCount(), 12)
680 def testCombineThrow(self):
681 apl = dafBase.PropertyList()
682 apl.set("int", 42)
684 aplp = dafBase.PropertyList()
685 aplp.set("int", 3.14159)
687 with self.assertRaises(TypeError):
688 apl.combine(aplp)
690 psd = {"bool": True}
691 with self.assertRaises(TypeError):
692 apl.combine(psd)
694 def testremove(self):
695 apl = dafBase.PropertyList()
696 apl.set("int", 42)
697 apl.set("double", 3.14159)
698 apl.set("apl1.plus", 1)
699 apl.set("apl1.minus", -1)
700 apl.set("apl1.zero", 0)
701 self.assertEqual(apl.nameCount(False), 5)
703 apl.remove("int")
704 self.assertFalse(apl.exists("int"))
705 self.assertEqual(apl.getAsDouble("double"), 3.14159)
706 self.assertEqual(apl.getAsInt("apl1.plus"), 1)
707 self.assertEqual(apl.getAsInt("apl1.minus"), -1)
708 self.assertEqual(apl.getAsInt("apl1.zero"), 0)
709 self.assertEqual(apl.nameCount(False), 4)
711 apl.remove("apl1.zero")
712 self.assertFalse(apl.exists("int"))
713 self.assertEqual(apl.getAsDouble("double"), 3.14159)
714 self.assertFalse(apl.exists("apl1.zero"))
715 self.assertEqual(apl.getAsInt("apl1.plus"), 1)
716 self.assertEqual(apl.getAsInt("apl1.minus"), -1)
717 self.assertEqual(apl.nameCount(False), 3)
719 # Removing a non-existent key (flattened) has no effect
720 self.assertFalse(apl.exists("apl1"))
721 apl.remove("apl1")
722 self.assertFalse(apl.exists("int"))
723 self.assertEqual(apl.getAsDouble("double"), 3.14159)
724 self.assertFalse(apl.exists("apl1"))
725 self.assertTrue(apl.exists("apl1.plus"))
726 self.assertTrue(apl.exists("apl1.minus"))
727 self.assertFalse(apl.exists("apl1.zero"))
728 self.assertEqual(apl.nameCount(False), 3)
730 apl.remove("double")
731 self.assertFalse(apl.exists("int"))
732 self.assertFalse(apl.exists("double"))
733 self.assertFalse(apl.exists("apl1"))
734 self.assertTrue(apl.exists("apl1.plus"))
735 self.assertTrue(apl.exists("apl1.minus"))
736 self.assertFalse(apl.exists("apl1.zero"))
737 self.assertEqual(apl.nameCount(False), 2)
739 apl.remove("apl1.plus")
740 apl.remove("apl1.minus")
741 self.assertEqual(apl.nameCount(False), 0)
743 def testdeepCopy(self):
744 apl = dafBase.PropertyList()
745 apl.set("int", 42)
746 aplp = dafBase.PropertyList()
747 aplp.set("bottom", "x")
748 apl.set("top", aplp)
750 aplp2 = apl.deepCopy()
751 self.assertTrue(aplp2.exists("int"))
752 self.assertTrue(aplp2.exists("top.bottom"))
753 self.assertEqual(aplp2.getAsInt("int"), 42)
754 self.assertEqual(aplp2.getAsString("top.bottom"), "x")
755 # Make sure it was indeed a deep copy.
756 apl.set("int", 2008)
757 apl.set("top.bottom", "z")
758 self.assertEqual(apl.getAsInt("int"), 2008)
759 self.assertEqual(apl.getAsString("top.bottom"), "z")
760 self.assertEqual(aplp2.getAsInt("int"), 42)
761 self.assertEqual(aplp2.getAsString("top.bottom"), "x")
763 def testToString(self):
764 apl = dafBase.PropertyList()
765 apl.set("bool", True)
766 s = 42
767 apl.setShort("short", s)
768 apl.set("int", 2008)
769 apl.set("int64_t", 0xfeeddeadbeef)
770 f = 3.14159
771 apl.setFloat("float", f)
772 d = 2.718281828459045
773 apl.setDouble("double", d)
774 apl.setString("char*", "foo")
775 apl.set("char*2", "foo2")
776 apl.set("string", "bar")
777 apl.set("apl1.pre", 1)
778 apl.set("apl1.post", 2)
779 apl.set("apl2.plus", 10.24)
780 apl.set("apl2.minus", -10.24)
781 apl.set("apl3.sub.subsub", "foo")
782 apl.add("v", 10)
783 apl.add("v", 9)
784 apl.add("v", 8)
786 # Check that the keys returned for this PropertyList match
787 # the order they were set
788 order = ['bool', 'short', 'int', 'int64_t', 'float', 'double', 'char*', 'char*2',
789 'string', 'apl1.pre', 'apl1.post', 'apl2.plus', 'apl2.minus', 'apl3.sub.subsub', 'v']
790 self.assertEqual(apl.getOrderedNames(), order)
792 # Argument to toString has no effect for flattened hierarchy
793 self.assertEqual(apl.toString(),
794 "bool = 1\n"
795 "short = 42\n"
796 "int = 2008\n"
797 "int64_t = 280297596632815\n"
798 "float = 3.141590\n"
799 "double = 2.7182818284590\n"
800 "char* = \"foo\"\n"
801 "char*2 = \"foo2\"\n"
802 "string = \"bar\"\n"
803 "apl1.pre = 1\n"
804 "apl1.post = 2\n"
805 "apl2.plus = 10.240000000000\n"
806 "apl2.minus = -10.240000000000\n"
807 "apl3.sub.subsub = \"foo\"\n"
808 "v = [ 10, 9, 8 ]\n"
809 )
810 self.assertEqual(apl.toString(True),
811 "bool = 1\n"
812 "short = 42\n"
813 "int = 2008\n"
814 "int64_t = 280297596632815\n"
815 "float = 3.141590\n"
816 "double = 2.7182818284590\n"
817 "char* = \"foo\"\n"
818 "char*2 = \"foo2\"\n"
819 "string = \"bar\"\n"
820 "apl1.pre = 1\n"
821 "apl1.post = 2\n"
822 "apl2.plus = 10.240000000000\n"
823 "apl2.minus = -10.240000000000\n"
824 "apl3.sub.subsub = \"foo\"\n"
825 "v = [ 10, 9, 8 ]\n"
826 )
829class TestMemory(lsst.utils.tests.MemoryTestCase):
830 pass
833def setup_module(module):
834 lsst.utils.tests.init()
837if __name__ == "__main__": 837 ↛ 838line 837 didn't jump to line 838, because the condition on line 837 was never true
838 lsst.utils.tests.init()
839 unittest.main()