Coverage for tests/test_PropertyList.py : 7%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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 pass
37class PropertyListTestCase(unittest.TestCase):
38 """A test case for PropertyList."""
40 def testConstruct(self):
41 apl = dafBase.PropertyList()
42 self.assertIsNotNone(apl)
44 def checkPickle(self, original):
45 new = pickle.loads(pickle.dumps(original, 2))
46 self.assertEqual(new, original)
47 return new
49 def testScalar(self):
50 apl = dafBase.PropertyList()
51 apl.setBool("bool", True)
52 apl.setShort("short", 42)
53 apl.setInt("int", 2008)
54 apl.setLongLong("int64_t", 0xfeeddeadbeef)
55 apl.setFloat("float", 3.14159)
56 apl.setDouble("double", 2.718281828459045)
57 apl.set("char*", "foo")
58 apl.setString("string", "bar")
59 apl.set("int2", 2009)
60 apl.set("dt", dafBase.DateTime("20090402T072639.314159265Z", dafBase.DateTime.UTC))
61 apl.set("subclass", FloatSubClass(1.23456789))
62 apl.set("undef", None)
64 self.assertTrue(apl.isUndefined("undef"))
65 self.assertFalse(apl.isUndefined("string"))
66 self.assertEqual(apl.typeOf("bool"), dafBase.PropertyList.TYPE_Bool)
67 self.assertEqual(apl.getBool("bool"), True)
68 self.assertEqual(apl.typeOf("short"), dafBase.PropertyList.TYPE_Short)
69 self.assertEqual(apl.getShort("short"), 42)
70 self.assertEqual(apl.typeOf("int"), dafBase.PropertyList.TYPE_Int)
71 self.assertEqual(apl.getInt("int"), 2008)
72 self.assertEqual(apl.typeOf("int64_t"),
73 dafBase.PropertyList.TYPE_LongLong)
74 self.assertEqual(apl.getLongLong("int64_t"), 0xfeeddeadbeef)
75 self.assertEqual(apl.typeOf("float"), dafBase.PropertyList.TYPE_Float)
76 self.assertAlmostEqual(apl.getFloat("float"), 3.14159, 6)
77 self.assertEqual(apl.typeOf("double"), dafBase.PropertyList.TYPE_Double)
78 self.assertEqual(apl.getDouble("double"), 2.718281828459045)
79 self.assertEqual(apl.typeOf("char*"), dafBase.PropertyList.TYPE_String)
80 self.assertEqual(apl.getString("char*"), "foo")
81 self.assertEqual(apl.typeOf("string"), dafBase.PropertyList.TYPE_String)
82 self.assertEqual(apl.getString("string"), "bar")
83 self.assertEqual(apl.typeOf("int2"), dafBase.PropertyList.TYPE_Int)
84 self.assertEqual(apl.getInt("int2"), 2009)
85 self.assertEqual(apl.get("int2"), 2009)
86 self.assertEqual(apl.getArray("int2"), [2009])
87 self.assertEqual(apl.getScalar("int2"), 2009)
88 self.assertEqual(apl.typeOf("dt"), dafBase.PropertyList.TYPE_DateTime)
89 self.assertEqual(apl.getDateTime("dt").nsecs(), 1238657233314159265)
90 self.assertEqual(apl.getDouble("subclass"), 1.23456789)
91 self.assertEqual(apl["int2"], 2009)
93 self.assertIsNone(apl.getScalar("undef"))
94 self.assertEqual(apl.typeOf("undef"), dafBase.PropertyList.TYPE_Undef)
95 self.assertIsNone(apl.get("undef"))
96 self.assertIsNone(apl["undef"])
97 self.checkPickle(apl)
99 # Now replace the undef value with a defined value
100 apl.set("undef", "not undefined")
101 self.assertEqual(apl.getScalar("undef"), "not undefined")
102 self.assertFalse(apl.isUndefined("undef"))
103 self.assertEqual(apl.typeOf("undef"), dafBase.PropertyList.TYPE_String)
105 def testGetDefault(self):
106 apl = dafBase.PropertyList()
107 apl.setInt("int", 42)
108 self.assertEqual(apl.getInt("int"), 42)
109 self.assertEqual(apl.getInt("int", 2008), 42)
110 self.assertEqual(apl.getInt("foo", 2008), 2008)
111 self.assertEqual(apl.get("int"), 42)
112 self.assertEqual(apl.get("int", 2008), 42)
113 self.assertEqual(apl.get("foo", 2008), 2008)
114 self.assertEqual(apl.get("foo2", default="missing"), "missing")
115 self.assertIsNone(apl.get("foo"))
117 def testExists(self):
118 apl = dafBase.PropertyList()
119 apl.setInt("int", 42)
120 self.assertEqual(apl.exists("int"), True)
121 self.assertEqual(apl.exists("foo"), False)
123 def testGetVector(self):
124 apl = dafBase.PropertyList()
125 v = [42, 2008, 1]
126 apl.setInt("ints", v)
127 apl.setInt("ints2", [10, 9, 8])
128 w = apl.getArrayInt("ints")
129 self.assertEqual(len(w), 3)
130 self.assertEqual(v, w)
131 self.assertEqual(apl.getInt("ints2"), 8)
132 self.assertEqual(apl.getArrayInt("ints2"), [10, 9, 8])
133 w = apl.get("ints")
134 self.assertIsInstance(w, int)
135 self.assertEqual(v[-1], w)
136 self.assertEqual(apl["ints"], v[-1])
137 self.assertEqual(apl.getArray("ints"), v)
138 self.assertEqual(apl.getScalar("ints"), v[-1])
139 apl.setInt("int", 999)
140 x = apl.get("int")
141 self.assertEqual(x, 999)
142 self.assertEqual(apl.getArray("int"), [999])
143 self.assertEqual(apl.getScalar("int"), 999)
144 self.assertEqual(apl["int"], 999)
146 self.checkPickle(apl)
148 def testGetVector2(self):
149 apl = dafBase.PropertyList()
150 v = [42, 2008, 1]
151 apl.setInt("ints", v)
152 apl.setInt("ints2", [10, 9, 8])
153 w = apl.getArrayInt("ints")
154 self.assertEqual(len(w), 3)
155 self.assertEqual(v[0], w[0])
156 self.assertEqual(v[1], w[1])
157 self.assertEqual(v[2], w[2])
158 self.assertEqual(apl.getInt("ints2"), 8)
159 self.assertEqual(apl.getArrayInt("ints2"), [10, 9, 8])
161 self.checkPickle(apl)
163 def testAddScalar(self):
164 apl = dafBase.PropertyList()
165 v = [42, 2008, 1]
166 apl.setInt("ints", v)
167 apl.addInt("ints", -999)
168 apl.add("other", "foo")
169 apl.add("ints", 13)
170 apl.add("subclass", FloatSubClass(1.23456789))
171 w = apl.getArrayInt("ints")
172 self.assertEqual(len(w), 5)
173 self.assertEqual(v[0], w[0])
174 self.assertEqual(v[1], w[1])
175 self.assertEqual(v[2], w[2])
176 self.assertEqual(w[3], -999)
177 self.assertEqual(w[4], 13)
178 self.assertEqual(apl.getString("other"), "foo")
179 self.assertEqual(apl.get("subclass"), 1.23456789)
180 self.assertEqual(apl.getArray("subclass"), [1.23456789])
181 self.assertEqual(apl.getScalar("subclass"), 1.23456789)
183 def testDateTimeToString(self):
184 apl = dafBase.PropertyList()
185 apl.set("dt", dafBase.DateTime("20090402T072639.314159265Z", dafBase.DateTime.UTC))
186 self.assertEqual(apl.toString(),
187 "dt = 2009-04-02T07:26:39.314159265Z\n")
189 def testGetScalarThrow(self):
190 apl = dafBase.PropertyList()
191 apl.setBool("bool", True)
192 apl.setShort("short", 42)
193 apl.setInt("int", 2008)
194 apl.setLongLong("int64_t", 0xfeeddeadbeef)
195 apl.setFloat("float", 3.14159)
196 apl.setDouble("double", 2.718281828459045)
197 apl.setString("string", "bar")
199 with self.assertRaises(KeyError):
200 apl["foo"]
201 with self.assertRaises(TypeError):
202 apl.getBool("short")
203 with self.assertRaises(TypeError):
204 apl.getBool("int")
205 with self.assertRaises(TypeError):
206 apl.getShort("int")
207 with self.assertRaises(TypeError):
208 apl.getInt("short")
209 with self.assertRaises(TypeError):
210 apl.getInt("bool")
211 with self.assertRaises(TypeError):
212 apl.getDouble("float")
213 with self.assertRaises(TypeError):
214 apl.getFloat("double")
215 with self.assertRaises(TypeError):
216 apl.getString("int")
218 def testAddVector(self):
219 apl = dafBase.PropertyList()
220 v = [42, 2008, 1]
221 apl.set("ints", v)
222 apl.add("ints", [-42, -2008, -1])
223 subclass = [FloatSubClass(1.23), FloatSubClass(4.56), FloatSubClass(7.89)]
224 apl.add("subclass", subclass)
225 self.assertEqual(apl.getArrayInt("ints"),
226 [42, 2008, 1, -42, -2008, -1])
227 self.assertEqual(apl.get("subclass"), subclass[-1])
228 self.assertEqual(apl.getArray("subclass"), subclass)
229 self.assertEqual(apl.getScalar("subclass"), subclass[-1])
231 def testComment(self):
232 apl = dafBase.PropertyList()
233 apl.set("NAXIS", 2, "two-dimensional")
234 self.assertEqual(apl.get("NAXIS"), 2)
235 self.assertEqual(apl.getArray("NAXIS"), [2])
236 self.assertEqual(apl.getScalar("NAXIS"), 2)
237 self.assertEqual(apl.getComment("NAXIS"), "two-dimensional")
238 apl.set("NAXIS", 3, "three-dimensional")
239 self.assertEqual(apl.get("NAXIS"), 3)
240 self.assertEqual(apl.getArray("NAXIS"), [3])
241 self.assertEqual(apl.getScalar("NAXIS"), 3)
242 self.assertEqual(apl.getComment("NAXIS"), "three-dimensional")
244 def testOrder(self):
245 apl = dafBase.PropertyList()
246 apl.set("SIMPLE", True)
247 apl.set("BITPIX", -32)
248 apl.set("NAXIS", 2)
249 apl.set("COMMENT", "This is a test")
250 apl.add("COMMENT", "This is a test line 2")
251 apl.set("RA", 3.14159, "decimal degrees")
252 apl.set("DEC", 2.71828, "decimal radians")
253 correct = [
254 ("SIMPLE", True, ""),
255 ("BITPIX", -32, ""),
256 ("NAXIS", 2, ""),
257 ("COMMENT", "This is a test", ""),
258 ("COMMENT", "This is a test line 2", ""),
259 ("RA", 3.14159, "decimal degrees"),
260 ("DEC", 2.71828, "decimal radians"),
261 ]
262 self.assertEqual(apl.toList(), correct)
263 apl.set("NAXIS1", 513, "length of data axis 1")
264 correct.append(("NAXIS1", 513, "length of data axis 1"))
265 self.assertEqual(apl.toList(), correct)
266 apl.set("RA", 1.414)
267 correct[5] = ("RA", 1.414, "decimal degrees")
268 self.assertEqual(apl.toList(), correct)
269 apl.set("DEC", 1.732)
270 correct[6] = ("DEC", 1.732, "decimal radians")
271 self.assertEqual(apl.toList(), correct)
272 apl.set("DEC", -6.28, "")
273 correct[6] = ("DEC", -6.28, "")
274 self.assertEqual(apl.toList(), correct)
275 apl.add("COMMENT", "This is a test line 3", "")
276 correct.insert(5, ("COMMENT", "This is a test line 3", ""))
277 self.assertEqual(apl.toList(), correct)
279 self.checkPickle(apl)
281 def testToOrderedDict(self):
282 from collections import OrderedDict
284 apl = dafBase.PropertyList()
285 apl.set("SIMPLE", True)
286 apl.set("BITPIX", -32)
287 apl.set("NAXIS", 2)
288 apl.set("RA", 3.14159)
289 apl.set("DEC", 2.71828)
290 apl.set("FILTER", None)
291 apl.set("COMMENT", "This is a test")
292 apl.add("COMMENT", "This is a test line 2")
293 correct = OrderedDict([
294 ("SIMPLE", True),
295 ("BITPIX", -32),
296 ("NAXIS", 2),
297 ("RA", 3.14159),
298 ("DEC", 2.71828),
299 ("FILTER", None),
300 ("COMMENT", ["This is a test", "This is a test line 2"])
301 ])
302 self.assertEqual(apl.toOrderedDict(), correct)
304 apl.set("NAXIS1", 513)
305 correct["NAXIS1"] = 513
306 self.assertEqual(apl.toOrderedDict(), correct)
307 apl.set("RA", 1.414)
308 correct["RA"] = 1.414
309 self.assertEqual(apl.toOrderedDict(), correct)
310 apl.set("DEC", 1.732)
311 correct["DEC"] = 1.732
312 self.assertEqual(apl.toOrderedDict(), correct)
313 apl.set("DEC", -6.28)
314 correct["DEC"] = -6.28
315 self.assertEqual(apl.toOrderedDict(), correct)
316 apl.add("COMMENT", "This is a test line 3")
317 correct["COMMENT"] = correct["COMMENT"] + ["This is a test line 3", ]
318 self.assertEqual(apl.toOrderedDict(), correct)
320 def testHierarchy(self):
321 apl = dafBase.PropertyList()
322 apl.set("CURRENT", 49.5)
323 apl.set("CURRENT.foo", -32)
324 apl.set("CURRENT.bar", 2)
325 self.assertEqual(apl.get("CURRENT"), 49.5)
326 self.assertEqual(apl.getArray("CURRENT"), [49.5])
327 self.assertEqual(apl.getScalar("CURRENT"), 49.5)
328 self.assertEqual(apl.get("CURRENT.foo"), -32)
329 self.assertEqual(apl.getArray("CURRENT.foo"), [-32])
330 self.assertEqual(apl.getScalar("CURRENT.foo"), -32)
331 self.assertEqual(apl.get("CURRENT.bar"), 2)
332 self.assertEqual(apl.getArray("CURRENT.bar"), [2])
333 self.assertEqual(apl.getScalar("CURRENT.bar"), 2)
335 aps = dafBase.PropertySet()
336 aps.set("bottom", "x")
337 aps.set("sibling", 42)
338 apl.set("top", aps)
339 self.assertEqual(apl.get("top.bottom"), "x")
340 self.assertEqual(apl.getArray("top.bottom"), ["x"])
341 self.assertEqual(apl.getScalar("top.bottom"), "x")
342 self.assertEqual(apl.get("top.sibling"), 42)
343 self.assertEqual(apl.getArray("top.sibling"), [42])
344 self.assertEqual(apl.getScalar("top.sibling"), 42)
345 with self.assertRaises(KeyError):
346 apl["top"]
347 self.assertEqual(apl.toString(),
348 'CURRENT = 49.500000000000\nCURRENT.foo = -32\nCURRENT.bar = 2\n'
349 'top.sibling = 42\ntop.bottom = "x"\n')
351 self.checkPickle(apl)
353 # Check that a PropertyList (with comment) can go in a PropertySet
354 apl.set("INT", 45, "an integer")
355 aps = dafBase.PropertySet()
356 aps.set("bottom", "x")
357 aps.set("apl", apl)
358 new = self.checkPickle(aps)
359 self.assertIsInstance(new, dafBase.PropertySet)
360 self.assertIsInstance(new.getScalar("apl"), dafBase.PropertyList)
361 self.assertEqual(new.getScalar("apl").getComment("INT"), "an integer")
363 def testCombineHierarchical(self):
364 # Test that we can perform a deep copy of a PropertyList containing a
365 # hierarchical (contains a '.') key.
366 # This was a segfault prior to addressing DM-882.
367 pl1 = dafBase.PropertyList()
368 pl1.set("a.b", 1)
369 pl2 = pl1.deepCopy() # should not segfault
370 self.assertEqual(pl1.get("a.b"), pl2.get("a.b"))
371 self.assertEqual(pl1.getArray("a.b"), pl2.getArray("a.b"))
372 self.assertEqual(pl1.getScalar("a.b"), pl2.getScalar("a.b"))
373 self.checkPickle(pl1)
375 def testCopy(self):
376 dest = dafBase.PropertyList()
377 source = dafBase.PropertyList()
378 value1 = [1.5, 3.2]
379 source.set("srcItem1", value1)
380 dest.copy("destItem1", source, "srcItem1")
381 self.assertEqual(dest.get("destItem1"), value1[-1])
382 self.assertEqual(dest.getArray("destItem1"), value1)
383 self.assertEqual(dest.getScalar("destItem1"), value1[-1])
385 # items are replaced, regardless of type
386 dest.set("destItem2", "string value")
387 value2 = [5, -4, 3]
388 source.set("srcItem2", value2)
389 dest.copy("destItem2", source, "srcItem2")
390 self.assertEqual(dest.get("destItem2"), value2[-1])
391 self.assertEqual(dest.getArray("destItem2"), value2)
392 self.assertEqual(dest.getScalar("destItem2"), value2[-1])
394 # asScalar copies only the last value
395 dest.copy("destItem2Scalar", source, "srcItem2", asScalar=True)
396 self.assertEqual(dest.get("destItem2Scalar"), value2[-1])
397 self.assertEqual(dest.getArray("destItem2Scalar"), [value2[-1]])
398 self.assertEqual(dest.getScalar("destItem2Scalar"), value2[-1])
400 def testArrayProperties(self):
401 apl = dafBase.PropertyList()
402 v = [42, 2008, 1]
403 apl.set("ints", v)
404 apl.set("int", 365)
405 apl.set("ints2", -42)
406 apl.add("ints2", -2008)
408 self.assertTrue(apl.isArray("ints"))
409 self.assertFalse(apl.isArray("int"))
410 self.assertTrue(apl.isArray("ints2"))
411 self.assertEqual(apl.valueCount("ints"), 3)
412 self.assertEqual(apl.valueCount("int"), 1)
413 self.assertEqual(apl.valueCount("ints2"), 2)
414 self.assertEqual(apl.typeOf("ints"), dafBase.PropertyList.TYPE_Int)
415 self.assertEqual(apl.typeOf("int"), dafBase.PropertyList.TYPE_Int)
416 self.assertEqual(apl.typeOf("ints2"), dafBase.PropertyList.TYPE_Int)
418 def testHierarchy2(self):
419 apl = dafBase.PropertyList()
420 aplp = dafBase.PropertyList()
422 aplp.set("pre", 1)
423 apl.set("apl1", aplp)
425 # Python will not see this, aplp is disconnected
426 aplp.set("post", 2)
427 self.assertFalse(apl.exists("apl1.post"))
429 apl.set("int", 42)
431 # Setting an empty PropertyList has no effect
432 apl.set("apl2", dafBase.PropertyList())
433 self.assertFalse(apl.exists("apl2"))
435 apl.set("apl2.plus", 10.24)
436 apl.set("apl2.minus", -10.24)
437 apl.set("apl3.sub1", "foo")
438 apl.set("apl3.sub2", "bar")
440 self.assertTrue(apl.exists("apl1.pre"))
441 self.assertTrue(apl.exists("apl2.plus"))
442 self.assertTrue(apl.exists("apl2.minus"))
443 self.assertTrue(apl.exists("apl3.sub1"))
444 self.assertTrue(apl.exists("apl3.sub2"))
446 # Make sure checking a subproperty doesn't create it.
447 self.assertFalse(apl.exists("apl2.pre"))
448 self.assertFalse(apl.exists("apl2.pre"))
449 # Make sure checking an element doesn't create it.
450 self.assertFalse(apl.exists("apl4"))
451 self.assertFalse(apl.exists("apl4"))
452 # Make sure checking a subproperty with a nonexistent parent doesn't
453 # create it.
454 self.assertFalse(apl.exists("apl4.sub"))
455 self.assertFalse(apl.exists("apl4.sub"))
456 # Make sure checking a subproperty doesn't create its parent.
457 self.assertFalse(apl.exists("apl4"))
459 def testvariousThrows(self):
460 apl = dafBase.PropertyList()
461 apl.set("int", 42)
463 # This raises an exception in C++ test but works in Python
464 apl.set("int.sub", "foo")
466 with self.assertRaises(TypeError):
467 apl.getDouble("int")
468 with self.assertRaises(LookupError):
469 apl.getDouble("double"),
470 with self.assertRaises(LookupError):
471 apl.getArrayDouble("double")
472 with self.assertRaises(LookupError):
473 apl.typeOf("double")
474 with self.assertRaises(TypeError):
475 apl.add("int", 4.2),
477 v = [3.14159, 2.71828]
478 with self.assertRaises(TypeError):
479 apl.add("int", v)
480 apl.remove("foo.bar")
481 apl.remove("int.sub")
483 def testNames(self):
484 apl = dafBase.PropertyList()
485 apl.set("apl1.pre", 1)
486 apl.set("apl1.post", 2)
487 apl.set("int", 42)
488 apl.set("double", 3.14)
489 apl.set("apl2.plus", 10.24)
490 apl.set("apl2.minus", -10.24)
492 # Hierarchy is always flat
493 self.assertEqual(apl.nameCount(), 6)
494 self.assertEqual(apl.nameCount(False), 6)
496 v = set(apl.names())
497 self.assertEqual(len(v), 6)
498 self.assertEqual(v, {"double", "int", "apl1.post",
499 "apl1.pre", "apl2.minus", "apl2.plus"})
501 def testParamNames(self):
502 apl = dafBase.PropertyList()
503 apl.set("apl1.pre", 1)
504 apl.set("apl1.post", 2)
505 apl.set("int", 42)
506 apl.set("double", 3.14)
507 apl.set("apl2.plus", 10.24)
508 apl.set("apl2.minus", -10.24)
510 v = set(apl.paramNames())
511 self.assertEqual(len(v), 6)
512 self.assertEqual(v, {"double", "int", "apl1.post", "apl1.pre",
513 "apl2.minus", "apl2.plus"})
515 def testPropertySetNames(self):
516 apl = dafBase.PropertyList()
517 apl.set("apl1.pre", 1)
518 apl.set("apl1.post", 2)
519 apl.set("int", 42)
520 apl.set("double", 3.14)
521 apl.set("apl2.plus", 10.24)
522 apl.set("apl2.minus", -10.24)
523 apl.set("apl3.sub.subsub", "foo")
525 # There are no PropertySets inside flattened PropertyList
526 v = set(apl.propertySetNames())
527 self.assertEqual(len(v), 0)
529 def testGetAs(self):
530 apl = dafBase.PropertyList()
531 apl.set("bool", True)
532 s = 42
533 apl.setShort("short", s)
534 apl.set("int", 2008)
535 apl.set("int64_t", 0xfeeddeadbeef)
536 f = 3.14159
537 apl.setFloat("float", f)
538 d = 2.718281828459045
539 apl.setDouble("double", d)
540 apl.setString("char*", "foo")
541 apl.set("char*2", "foo2")
542 apl.set("string", "bar")
543 aplp = dafBase.PropertyList()
544 aplp.set("bottom", "x")
545 apl.set("top", aplp)
547 self.assertEqual(apl.getAsBool("bool"), True)
548 self.assertEqual(apl.getAsInt("bool"), 1)
549 self.assertEqual(apl.getAsInt("short"), 42)
550 self.assertEqual(apl.getAsInt("int"), 2008)
551 with self.assertRaises(TypeError):
552 apl.getAsInt("int64_t")
553 self.assertEqual(apl.getAsInt64("bool"), 1)
554 self.assertEqual(apl.getAsInt64("short"), 42)
555 self.assertEqual(apl.getAsInt64("int"), 2008)
556 self.assertEqual(apl.getAsInt64("int64_t"), 0xfeeddeadbeef)
557 with self.assertRaises(TypeError):
558 apl.getAsInt64("float")
559 self.assertEqual(apl.getAsDouble("bool"), 1.0)
560 self.assertEqual(apl.getAsDouble("short"), 42.0)
561 self.assertEqual(apl.getAsDouble("int"), 2008.0)
562 self.assertEqual(apl.getAsDouble("int64_t"), float(0xfeeddeadbeef))
563 self.assertAlmostEqual(apl.getAsDouble("float"), 3.14159, places=5)
564 self.assertEqual(apl.getAsDouble("double"), 2.718281828459045)
565 with self.assertRaises(TypeError):
566 apl.getAsDouble("char*")
567 self.assertEqual(apl.getAsString("char*"), "foo")
568 self.assertEqual(apl.getAsString("char*2"), "foo2")
569 self.assertEqual(apl.getAsString("string"), "bar")
570 with self.assertRaises(TypeError):
571 apl.getAsString("int")
572 self.assertEqual(apl.getAsString("top.bottom"), "x")
574 def testCombine(self):
575 apl = dafBase.PropertyList()
576 apl.set("apl1.pre", 1)
577 apl.set("apl1.post", 2)
578 apl.set("int", 42)
579 apl.set("double", 3.14)
580 apl.set("apl2.plus", 10.24)
581 apl.set("apl2.minus", -10.24)
582 apl.set("apl3.sub.subsub", "foo")
584 aplp = dafBase.PropertyList()
585 aplp.set("apl1.pre", 3)
586 aplp.add("apl1.pre", 4)
587 aplp.set("int", 2008)
588 aplp.set("apl2.foo", "bar")
589 aplp.set("apl4.top", "bottom")
591 apl.combine(aplp)
593 self.assertFalse(apl.isArray("apl1"))
594 self.assertTrue(apl.isArray("apl1.pre"))
595 self.assertFalse(apl.isArray("apl1.post"))
596 self.assertFalse(apl.isArray("apl2"))
597 self.assertFalse(apl.isArray("apl2.plus"))
598 self.assertFalse(apl.isArray("apl2.minus"))
599 self.assertFalse(apl.isArray("apl2.foo"))
600 self.assertFalse(apl.isArray("apl3"))
601 self.assertFalse(apl.isArray("apl3.sub"))
602 self.assertFalse(apl.isArray("apl3.subsub"))
603 self.assertFalse(apl.isArray("apl4"))
604 self.assertFalse(apl.isArray("apl4.top"))
605 self.assertTrue(apl.isArray("int"))
606 self.assertFalse(apl.isArray("double"))
607 self.assertEqual(apl.valueCount("apl1.pre"), 3)
608 self.assertEqual(apl.valueCount("int"), 2)
609 v = apl.getArray("apl1.pre")
610 self.assertEqual(v, [1, 3, 4])
611 v = apl.getArray("int")
612 self.assertEqual(v, [42, 2008])
614 def testUpdate(self):
615 apl = dafBase.PropertyList()
616 apl.set("apl1.pre", 1)
617 apl.set("apl1.post", 2)
618 apl.set("int", 42)
619 apl.set("double", 3.14)
620 apl.set("apl2.plus", 10.24)
621 apl.set("apl2.minus", -10.24)
622 apl.set("apl3.sub.subsub", "foo")
624 aplp = dafBase.PropertyList()
625 aplp.set("apl1.pre", 3)
626 aplp.add("apl1.pre", 4)
627 aplp.set("int", 2008)
628 aplp.set("apl2.foo", "bar")
629 aplp.set("apl4.top", "bottom")
631 apl.update(aplp)
633 self.assertFalse(apl.isArray("apl1"))
634 self.assertTrue(apl.isArray("apl1.pre"))
635 self.assertFalse(apl.isArray("apl1.post"))
636 self.assertFalse(apl.isArray("apl2"))
637 self.assertFalse(apl.isArray("apl2.plus"))
638 self.assertFalse(apl.isArray("apl2.minus"))
639 self.assertFalse(apl.isArray("apl2.foo"))
640 self.assertFalse(apl.isArray("apl3"))
641 self.assertFalse(apl.isArray("apl3.sub"))
642 self.assertFalse(apl.isArray("apl3.subsub"))
643 self.assertFalse(apl.isArray("apl4"))
644 self.assertFalse(apl.isArray("apl4.top"))
645 self.assertFalse(apl.isArray("int"))
646 self.assertFalse(apl.isArray("double"))
647 self.assertEqual(apl.valueCount("apl1.pre"), 2)
648 self.assertEqual(apl.valueCount("int"), 1)
649 v = apl.getArray("apl1.pre")
650 self.assertEqual(v, [3, 4])
651 v = apl.getArray("int")
652 self.assertEqual(v, [2008])
654 apld = {"int": 100, "str": "String", "apl1.foo": 10.5}
655 apl.update(apld)
656 self.assertEqual(apl["int"], apld["int"])
657 self.assertEqual(apl["str"], apld["str"])
658 self.assertEqual(apl["apl1.foo"], apld["apl1.foo"])
659 self.assertEqual(apl["double"], 3.14)
661 def testCombineThrow(self):
662 apl = dafBase.PropertyList()
663 apl.set("int", 42)
665 aplp = dafBase.PropertyList()
666 aplp.set("int", 3.14159)
668 with self.assertRaises(TypeError):
669 apl.combine(aplp)
671 psd = {"bool": True}
672 with self.assertRaises(TypeError):
673 apl.combine(psd)
675 def testremove(self):
676 apl = dafBase.PropertyList()
677 apl.set("int", 42)
678 apl.set("double", 3.14159)
679 apl.set("apl1.plus", 1)
680 apl.set("apl1.minus", -1)
681 apl.set("apl1.zero", 0)
682 self.assertEqual(apl.nameCount(False), 5)
684 apl.remove("int")
685 self.assertFalse(apl.exists("int"))
686 self.assertEqual(apl.getAsDouble("double"), 3.14159)
687 self.assertEqual(apl.getAsInt("apl1.plus"), 1)
688 self.assertEqual(apl.getAsInt("apl1.minus"), -1)
689 self.assertEqual(apl.getAsInt("apl1.zero"), 0)
690 self.assertEqual(apl.nameCount(False), 4)
692 apl.remove("apl1.zero")
693 self.assertFalse(apl.exists("int"))
694 self.assertEqual(apl.getAsDouble("double"), 3.14159)
695 self.assertFalse(apl.exists("apl1.zero"))
696 self.assertEqual(apl.getAsInt("apl1.plus"), 1)
697 self.assertEqual(apl.getAsInt("apl1.minus"), -1)
698 self.assertEqual(apl.nameCount(False), 3)
700 # Removing a non-existent key (flattened) has no effect
701 self.assertFalse(apl.exists("apl1"))
702 apl.remove("apl1")
703 self.assertFalse(apl.exists("int"))
704 self.assertEqual(apl.getAsDouble("double"), 3.14159)
705 self.assertFalse(apl.exists("apl1"))
706 self.assertTrue(apl.exists("apl1.plus"))
707 self.assertTrue(apl.exists("apl1.minus"))
708 self.assertFalse(apl.exists("apl1.zero"))
709 self.assertEqual(apl.nameCount(False), 3)
711 apl.remove("double")
712 self.assertFalse(apl.exists("int"))
713 self.assertFalse(apl.exists("double"))
714 self.assertFalse(apl.exists("apl1"))
715 self.assertTrue(apl.exists("apl1.plus"))
716 self.assertTrue(apl.exists("apl1.minus"))
717 self.assertFalse(apl.exists("apl1.zero"))
718 self.assertEqual(apl.nameCount(False), 2)
720 apl.remove("apl1.plus")
721 apl.remove("apl1.minus")
722 self.assertEqual(apl.nameCount(False), 0)
724 def testdeepCopy(self):
725 apl = dafBase.PropertyList()
726 apl.set("int", 42)
727 aplp = dafBase.PropertyList()
728 aplp.set("bottom", "x")
729 apl.set("top", aplp)
731 aplp2 = apl.deepCopy()
732 self.assertTrue(aplp2.exists("int"))
733 self.assertTrue(aplp2.exists("top.bottom"))
734 self.assertEqual(aplp2.getAsInt("int"), 42)
735 self.assertEqual(aplp2.getAsString("top.bottom"), "x")
736 # Make sure it was indeed a deep copy.
737 apl.set("int", 2008)
738 apl.set("top.bottom", "z")
739 self.assertEqual(apl.getAsInt("int"), 2008)
740 self.assertEqual(apl.getAsString("top.bottom"), "z")
741 self.assertEqual(aplp2.getAsInt("int"), 42)
742 self.assertEqual(aplp2.getAsString("top.bottom"), "x")
744 def testToString(self):
745 apl = dafBase.PropertyList()
746 apl.set("bool", True)
747 s = 42
748 apl.setShort("short", s)
749 apl.set("int", 2008)
750 apl.set("int64_t", 0xfeeddeadbeef)
751 f = 3.14159
752 apl.setFloat("float", f)
753 d = 2.718281828459045
754 apl.setDouble("double", d)
755 apl.setString("char*", "foo")
756 apl.set("char*2", "foo2")
757 apl.set("string", "bar")
758 apl.set("apl1.pre", 1)
759 apl.set("apl1.post", 2)
760 apl.set("apl2.plus", 10.24)
761 apl.set("apl2.minus", -10.24)
762 apl.set("apl3.sub.subsub", "foo")
763 apl.add("v", 10)
764 apl.add("v", 9)
765 apl.add("v", 8)
767 # Check that the keys returned for this PropertyList match
768 # the order they were set
769 order = ['bool', 'short', 'int', 'int64_t', 'float', 'double', 'char*', 'char*2',
770 'string', 'apl1.pre', 'apl1.post', 'apl2.plus', 'apl2.minus', 'apl3.sub.subsub', 'v']
771 self.assertEqual(apl.getOrderedNames(), order)
773 # Argument to toString has no effect for flattened hierarchy
774 self.assertEqual(apl.toString(),
775 "bool = 1\n"
776 "short = 42\n"
777 "int = 2008\n"
778 "int64_t = 280297596632815\n"
779 "float = 3.141590\n"
780 "double = 2.7182818284590\n"
781 "char* = \"foo\"\n"
782 "char*2 = \"foo2\"\n"
783 "string = \"bar\"\n"
784 "apl1.pre = 1\n"
785 "apl1.post = 2\n"
786 "apl2.plus = 10.240000000000\n"
787 "apl2.minus = -10.240000000000\n"
788 "apl3.sub.subsub = \"foo\"\n"
789 "v = [ 10, 9, 8 ]\n"
790 )
791 self.assertEqual(apl.toString(True),
792 "bool = 1\n"
793 "short = 42\n"
794 "int = 2008\n"
795 "int64_t = 280297596632815\n"
796 "float = 3.141590\n"
797 "double = 2.7182818284590\n"
798 "char* = \"foo\"\n"
799 "char*2 = \"foo2\"\n"
800 "string = \"bar\"\n"
801 "apl1.pre = 1\n"
802 "apl1.post = 2\n"
803 "apl2.plus = 10.240000000000\n"
804 "apl2.minus = -10.240000000000\n"
805 "apl3.sub.subsub = \"foo\"\n"
806 "v = [ 10, 9, 8 ]\n"
807 )
810class TestMemory(lsst.utils.tests.MemoryTestCase):
811 pass
814def setup_module(module):
815 lsst.utils.tests.init()
818if __name__ == "__main__": 818 ↛ 819line 818 didn't jump to line 819, because the condition on line 818 was never true
819 lsst.utils.tests.init()
820 unittest.main()