Coverage for tests/test_struct.py: 27%
Shortcuts 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
Shortcuts 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# Copyright 2008, 2009, 2010 LSST Corporation.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <http://www.lsstcorp.org/LegalNotices/>.
21#
22import unittest
24import lsst.utils.tests
25import lsst.pipe.base as pipeBase
28class StructTestCase(unittest.TestCase):
29 """A test case for Struct
30 """
32 def setUp(self):
33 self.valDict = dict(
34 foo=1,
35 bar=(1, 2, 3),
36 baz="value for baz",
37 alist=[3, 5, 7, 9],
38 )
40 def tearDown(self):
41 self.valDict = None
43 def testInit(self):
44 """Test Struct.__init__
45 """
46 s = pipeBase.Struct(**self.valDict)
47 self.assertEqual(self.valDict, s.getDict())
49 for name, val in self.valDict.items():
50 self.assertEqual(getattr(s, name), val)
52 def testInitException(self):
53 """Test that struct key names cannot start with double underscores.
54 """
55 with self.assertRaises(RuntimeError):
56 pipeBase.Struct(__foo=13)
58 def testSet(self):
59 """Test adding values via struct.name=val
60 """
61 s = pipeBase.Struct()
62 for name, val in self.valDict.items():
63 setattr(s, name, val)
65 self.assertEqual(self.valDict, s.getDict())
67 def testCopy(self):
68 """Test copy, which returns a shallow copy
69 """
70 s = pipeBase.Struct(**self.valDict)
71 sc = s.copy()
72 self.assertEqual(s.getDict(), sc.getDict())
74 # shallow copy, so changing a list should propagate (not necessarily a
75 # feature)
76 sc.alist[0] = 97
77 self.assertEqual(s, sc)
79 sc.foo += 1
80 self.assertNotEqual(s, sc)
82 def testMergeItems(self):
83 """Test mergeItems
84 """
85 s = pipeBase.Struct(**self.valDict)
86 newS = pipeBase.Struct()
87 newS.mergeItems(s)
88 # with no names listed, should merge nothing
89 self.assertEqual(len(newS), 0)
90 self.assertNotEqual(s, newS)
92 newS.mergeItems(s, "foo", "bar")
93 self.assertEqual(len(newS), 2)
94 self.assertNotEqual(s, newS)
96 newS.mergeItems(s, "baz", "alist")
97 self.assertEqual(len(newS), 4)
99 for name, val in newS.getDict().items():
100 self.assertEqual(val, self.valDict[name])
101 with self.assertRaises(RuntimeError):
102 newS.mergeItems(s, name)
105class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
106 pass
109def setup_module(module):
110 lsst.utils.tests.init()
113if __name__ == "__main__": 113 ↛ 114line 113 didn't jump to line 114, because the condition on line 113 was never true
114 lsst.utils.tests.init()
115 unittest.main()