Coverage for tests/test_struct.py: 23%

47 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-11 09:32 +0000

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.pipe.base as pipeBase 

25import lsst.utils.tests 

26 

27 

28class StructTestCase(unittest.TestCase): 

29 """A test case for Struct""" 

30 

31 def setUp(self): 

32 self.valDict = dict( 

33 foo=1, 

34 bar=(1, 2, 3), 

35 baz="value for baz", 

36 alist=[3, 5, 7, 9], 

37 ) 

38 

39 def tearDown(self): 

40 self.valDict = None 

41 

42 def testInit(self): 

43 """Test Struct.__init__""" 

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

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

46 

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

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

49 

50 def testInitException(self): 

51 """Test that struct key names cannot start with double underscores.""" 

52 with self.assertRaises(RuntimeError): 

53 pipeBase.Struct(__foo=13) 

54 

55 def testSet(self): 

56 """Test adding values via struct.name=val""" 

57 s = pipeBase.Struct() 

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

59 setattr(s, name, val) 

60 

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

62 

63 def testCopy(self): 

64 """Test copy, which returns a shallow copy""" 

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

66 sc = s.copy() 

67 self.assertEqual(s.getDict(), sc.getDict()) 

68 

69 # shallow copy, so changing a list should propagate (not necessarily a 

70 # feature) 

71 sc.alist[0] = 97 

72 self.assertEqual(s, sc) 

73 

74 sc.foo += 1 

75 self.assertNotEqual(s, sc) 

76 

77 def testMergeItems(self): 

78 """Test mergeItems""" 

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

80 newS = pipeBase.Struct() 

81 newS.mergeItems(s) 

82 # with no names listed, should merge nothing 

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

84 self.assertNotEqual(s, newS) 

85 

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

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

88 self.assertNotEqual(s, newS) 

89 

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

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

92 

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

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

95 with self.assertRaises(RuntimeError): 

96 newS.mergeItems(s, name) 

97 

98 

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

100 """Run file leak tests.""" 

101 

102 

103def setup_module(module): 

104 """Configure pytest.""" 

105 lsst.utils.tests.init() 

106 

107 

108if __name__ == "__main__": 

109 lsst.utils.tests.init() 

110 unittest.main()