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# This file is part of afw. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

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 GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22import copy 

23import gc 

24import unittest 

25 

26import lsst.utils.tests 

27 

28from lsst.afw.typehandling import Storable 

29import testGenericMapLib as cppLib 

30 

31 

32class DemoStorable(Storable): 

33 """Test that we can inherit from Storable in Python. 

34 """ 

35 

36 def __init__(self, state): 

37 super().__init__() 

38 self._state = state 

39 

40 def __str__(self): 

41 return f"value = {self._state}" 

42 

43 def __repr__(self): 

44 return f"DemoStorable({self._state!r})" 

45 

46 def __hash__(self): 

47 return hash(self._state) 

48 

49 def __copy__(self): 

50 return DemoStorable(self._state) 

51 

52 def __deepcopy__(self, memo=None): 

53 return DemoStorable(copy.deepcopy(self._state, memo)) 

54 

55 def __eq__(self, other): 

56 return self._state == other._state 

57 

58 

59class SpecializedStorable(cppLib.CppStorable): 

60 """Test that we can inherit from C++ subclasses of Storable that 

61 are not Storable itself. 

62 """ 

63 

64 def __repr__(self): 

65 return "Pythonic " + super().__repr__() 

66 

67 

68class PythonStorableTestSuite(lsst.utils.tests.TestCase): 

69 

70 def setUp(self): 

71 self.aList = [42] 

72 self.testbed = DemoStorable(self.aList) 

73 

74 def testCopy(self): 

75 shallow = copy.copy(self.testbed) 

76 self.assertIsNot(shallow, self.testbed) 

77 self.assertEqual(shallow, self.testbed) 

78 

79 deep = copy.deepcopy(self.testbed) 

80 self.assertIsNot(deep, self.testbed) 

81 self.assertEqual(deep, self.testbed) 

82 

83 cpp = cppLib.duplicate(self.testbed) 

84 self.assertIsInstance(cpp, Storable) 

85 self.assertIsInstance(cpp, DemoStorable) 

86 self.assertIsNot(cpp, self.testbed) 

87 self.assertEqual(cpp, self.testbed) 

88 

89 self.aList.append(43) 

90 self.assertEqual(shallow, DemoStorable([42, 43])) 

91 self.assertEqual(deep, DemoStorable([42])) 

92 self.assertEqual(cpp, DemoStorable([42])) 

93 

94 def testStr(self): 

95 self.assertEqual(str(self.testbed), "value = [42]") 

96 

97 def testRepr(self): 

98 self.assertEqual(repr(self.testbed), "DemoStorable([42])") 

99 cppLib.assertPythonStorable(self.testbed, "DemoStorable([42])") 

100 

101 def testHash(self): 

102 with self.assertRaises(TypeError): 

103 hash(self.testbed) 

104 

105 def testEq(self): 

106 self.assertEqual(self.testbed, DemoStorable([42])) 

107 self.assertNotEqual(self.testbed, DemoStorable(0)) 

108 

109 def testGarbageCollection(self): 

110 cppLib.keepStaticStorable(DemoStorable(3)) 

111 

112 gc.collect() 

113 

114 retrieved = cppLib.keepStaticStorable() 

115 self.assertIsInstance(retrieved, Storable) 

116 self.assertIsInstance(retrieved, DemoStorable) 

117 self.assertEqual(retrieved, DemoStorable(3)) 

118 

119 def testInheritedGarbageCollection(self): 

120 cppLib.keepStaticStorable(SpecializedStorable("Foo")) 

121 

122 gc.collect() 

123 

124 retrieved = cppLib.keepStaticStorable() 

125 self.assertIsInstance(retrieved, Storable) 

126 self.assertIsInstance(retrieved, cppLib.CppStorable) 

127 self.assertIsInstance(retrieved, SpecializedStorable) 

128 self.assertEqual(repr(retrieved), "Pythonic Foo") 

129 cppLib.assertPythonStorable(retrieved, "Pythonic Foo") 

130 

131 

132class CppStorableTestSuite(lsst.utils.tests.TestCase): 

133 

134 def setUp(self): 

135 self.initstr = "Just a string" 

136 self.testbed = cppLib.CppStorable(self.initstr) 

137 

138 def testNewValue(self): 

139 """Test a Python-side state change in both C++ and Python. 

140 """ 

141 self.assertEqual(self.testbed.value, self.initstr) 

142 cppLib.assertCppValue(self.testbed, self.initstr) 

143 

144 newstr = "Stringly typed" 

145 self.testbed.value = newstr 

146 

147 self.assertEqual(self.testbed.value, newstr) 

148 cppLib.assertCppValue(self.testbed, newstr) 

149 

150 

151class MemoryTester(lsst.utils.tests.MemoryTestCase): 

152 pass 

153 

154 

155def setup_module(module): 

156 lsst.utils.tests.init() 

157 

158 

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

160 lsst.utils.tests.init() 

161 unittest.main()