Hide keyboard shortcuts

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# 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 

23 

24import lsst.utils.tests 

25import lsst.pipe.base as pipeBase 

26 

27 

28class StructTestCase(unittest.TestCase): 

29 """A test case for Struct 

30 """ 

31 

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 ) 

39 

40 def tearDown(self): 

41 self.valDict = None 

42 

43 def testInit(self): 

44 """Test Struct.__init__ 

45 """ 

46 s = pipeBase.Struct(**self.valDict) 

47 self.assertEqual(self.valDict, s.getDict()) 

48 

49 for name, val in self.valDict.items(): 

50 self.assertEqual(getattr(s, name), val) 

51 

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) 

57 

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) 

64 

65 self.assertEqual(self.valDict, s.getDict()) 

66 

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()) 

73 

74 # shallow copy, so changing a list should propagate (not necessarily a feature) 

75 sc.alist[0] = 97 

76 self.assertEqual(s, sc) 

77 

78 sc.foo += 1 

79 self.assertNotEqual(s, sc) 

80 

81 def testMergeItems(self): 

82 """Test mergeItems 

83 """ 

84 s = pipeBase.Struct(**self.valDict) 

85 newS = pipeBase.Struct() 

86 newS.mergeItems(s) 

87 # with no names listed, should merge nothing 

88 self.assertEqual(len(newS), 0) 

89 self.assertNotEqual(s, newS) 

90 

91 newS.mergeItems(s, "foo", "bar") 

92 self.assertEqual(len(newS), 2) 

93 self.assertNotEqual(s, newS) 

94 

95 newS.mergeItems(s, "baz", "alist") 

96 self.assertEqual(len(newS), 4) 

97 

98 for name, val in newS.getDict().items(): 

99 self.assertEqual(val, self.valDict[name]) 

100 with self.assertRaises(RuntimeError): 

101 newS.mergeItems(s, name) 

102 

103 

104class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase): 

105 pass 

106 

107 

108def setup_module(module): 

109 lsst.utils.tests.init() 

110 

111 

112if __name__ == "__main__": 112 ↛ 113line 112 didn't jump to line 113, because the condition on line 112 was never true

113 lsst.utils.tests.init() 

114 unittest.main()