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

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

# This file is part of afw. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

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

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

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

 

from collections.abc import MutableMapping 

import unittest 

 

import lsst.utils.tests 

 

from lsst.afw.typehandling import SimpleGenericMap 

from lsst.afw.typehandling.testUtils import MutableGenericMapTestBaseClass 

 

 

class SimpleGenericMapTestSuite(MutableGenericMapTestBaseClass): 

 

@staticmethod 

def makeMap(mapType, values): 

"""Initialize a map type using __setattr__ instead of a bulk constructor. 

""" 

result = mapType() 

for key, value in values.items(): 

result[key] = value 

return result 

 

@classmethod 

def setUpClass(cls): 

super().setUpClass() 

cls.genericConstructor = SimpleGenericMap # Must be mapping-constructible 

cls.targets = {SimpleGenericMap[str]} # Must be default-constructible 

cls.examples = { 

"SimpleGenericMap(testData(str))": (SimpleGenericMap[str], cls.getTestData(str)), 

"SimpleGenericMap(testKeys(str) : 0)": 

(SimpleGenericMap[str], {key: 0 for key in cls.getTestData(str).keys()}), 

"SimpleGenericMap(dtype=str)": (SimpleGenericMap[str], {}), 

} 

 

def tearDown(self): 

pass 

 

def testClass(self): 

for target in self.targets: 

self.assertTrue(issubclass(target, MutableMapping)) 

self.assertIsInstance(target(), MutableMapping) 

 

def testInitKeywords(self): 

for target in self.targets: 

for keyType in self.getValidKeys(target): 

self.checkInitKwargs(target, self.getTestData(keyType), msg=str(target)) 

 

def testInitPairs(self): 

for target in self.targets | {self.genericConstructor}: 

for keyType in self.getValidKeys(target): 

self.checkInitPairs(target, self.getTestData(keyType), msg=str(target)) 

 

def testInitMapping(self): 

for target in self.targets | {self.genericConstructor}: 

for keyType in self.getValidKeys(target): 

# Init from dict 

self.checkInitMapping(target, self.getTestData(keyType), msg=str(target)) 

# Init from GenericMap 

self.checkInitMapping(target, target(self.getTestData(keyType)), 

msg=str(target)) 

 

def testUnknownKeys(self): 

with self.assertRaises(TypeError): 

self.genericConstructor() 

# Should not raise 

self.genericConstructor(dtype=str) 

 

def testMixedKeys(self): 

badData = {"What do you get if you multiply six by nine?": "Ultimate Question", 

42: "Ultimate Answer", 

} 

for target in self.targets | {self.genericConstructor}: 

with self.assertRaises(TypeError): 

target(badData) 

with self.assertRaises(TypeError): 

target(badData.items()) 

for target in self.targets: 

with self.assertRaises(TypeError): 

target(**badData) 

 

def testFromKeys(self): 

for target in self.targets: 

for keyType in self.getValidKeys(target): 

keys = self.getTestData(keyType).keys() 

for value in self.getTestData(keyType).values(): 

self.checkFromKeys(target, keys, value, 

msg=" class=%s, value=%r" % (target, value)) 

self.checkFromKeysDefault(target, keys, msg=" class=%s, no value" % (target)) 

 

def testCopy(self): 

for label, (mappingType, contents) in self.examples.items(): 

for keyType in self.getValidKeys(mappingType): 

mapping1 = self.makeMap(mappingType, contents) 

mapping2 = mapping1.copy() 

self.assertEqual(mapping1, mapping2, msg="%s" % label) 

mapping1[keyType(42)] = "A random value!" 

self.assertNotEqual(mapping1, mapping2, msg="%s" % label) 

 

def testEquality(self): 

for label1, (mappingType1, contents1) in self.examples.items(): 

mapping1 = self.makeMap(mappingType1, contents1) 

for label2, (mappingType2, contents2) in self.examples.items(): 

mapping2 = self.makeMap(mappingType2, contents2) 

if contents1 == contents2: 

self.assertIsNot(mapping1, mapping2, msg="%s vs %s" % (label1, label2)) 

self.assertEqual(mapping1, mapping2, msg="%s vs %s" % (label1, label2)) 

self.assertEqual(mapping1, contents2, msg="%s vs dict(%s)" % (label1, label2)) 

self.assertEqual(contents1, mapping2, msg="dict(%s) vs %s" % (label1, label2)) 

else: 

self.assertNotEqual(mapping1, mapping2, msg="%s vs %s" % (label1, label2)) 

self.assertNotEqual(mapping1, contents2, msg="%s vs dict(%s)" % (label1, label2)) 

self.assertNotEqual(contents1, mapping2, msg="dict(%s) vs %s" % (label1, label2)) 

 

def testBool(self): 

for label, (mappingType, contents) in self.examples.items(): 

mapping = self.makeMap(mappingType, contents) 

if contents: 

self.assertTrue(mapping, msg=label) 

else: 

self.assertFalse(mapping, msg=label) 

 

def testContains(self): 

for label, (mappingType, contents) in self.examples.items(): 

mapping = self.makeMap(mappingType, contents) 

self.checkContains(mapping, contents, msg=label) 

 

def testContents(self): 

for label, (mappingType, contents) in self.examples.items(): 

mapping = self.makeMap(mappingType, contents) 

self.checkContents(mapping, contents, msg=label) 

 

def testGet(self): 

for label, (mappingType, contents) in self.examples.items(): 

mapping = self.makeMap(mappingType, contents) 

self.checkGet(mapping, contents, msg=label) 

 

def testIteration(self): 

for label, (mappingType, contents) in self.examples.items(): 

mapping = self.makeMap(mappingType, contents) 

self.checkIteration(mapping, contents, msg=label) 

 

def testViews(self): 

for label, (mappingType, contents) in self.examples.items(): 

self.checkMutableViews(mappingType, contents, msg=label) 

 

def testInsertItem(self): 

for target in self.targets: 

for keyType in self.getValidKeys(target): 

self.checkInsertItem(target, self.getTestData(keyType), msg=str(target)) 

 

def testSetdefault(self): 

for target in self.targets: 

for keyType in self.getValidKeys(target): 

self.checkSetdefault(target, self.getTestData(keyType), msg=str(target)) 

 

def testUpdateMapping(self): 

for target in self.targets: 

for keyType in self.getValidKeys(target): 

# Update from dict 

self.checkUpdateMapping(target, self.getTestData(keyType), msg=str(target)) 

# Update from GenericMap 

self.checkUpdateMapping(target, self.makeMap(target, self.getTestData(keyType)), 

msg=str(target)) 

 

def testUpdatePairs(self): 

for target in self.targets: 

for keyType in self.getValidKeys(target): 

self.checkUpdatePairs(target, self.getTestData(keyType), msg=str(target)) 

 

def testUpdateKwargs(self): 

for target in self.targets: 

for keyType in self.getValidKeys(target): 

self.checkUpdateKwargs(target, self.getTestData(keyType), msg=str(target)) 

 

def testReplaceItem(self): 

for target in self.targets: 

self.checkReplaceItem(target(), msg=str(target)) 

 

def testRemoveItem(self): 

for target in self.targets: 

for keyType in self.getValidKeys(target): 

self.checkRemoveItem(target, self.getTestData(keyType), msg=str(target)) 

 

def testPop(self): 

for target in self.targets: 

for keyType in self.getValidKeys(target): 

self.checkPop(target, self.getTestData(keyType), msg=str(target)) 

 

def testPopitem(self): 

for target in self.targets: 

for keyType in self.getValidKeys(target): 

self.checkPopitem(target, self.getTestData(keyType), msg=str(target)) 

 

def testClear(self): 

for target in self.targets: 

for keyType in self.getValidKeys(target): 

self.checkClear(target, self.getTestData(keyType), msg=str(target)) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

227 ↛ 228line 227 didn't jump to line 228, because the condition on line 227 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()