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

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

# 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 

import lsst.pex.exceptions as pexExcept 

 

from lsst.afw.typehandling import SimpleGenericMap, Storable 

from lsst.afw.typehandling.testUtils import MutableGenericMapTestBaseClass 

import testGenericMapLib as cppLib 

 

 

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=f" class={target}, value={value}") 

self.checkFromKeysDefault(target, keys, msg=f" class={target}, no value") 

 

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=f"{label}") 

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

self.assertNotEqual(mapping1, mapping2, msg=f"{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=f"{label1} vs {label2}") 

self.assertEqual(mapping1, mapping2, msg=f"{label1} vs {label2}") 

self.assertEqual(mapping1, contents2, msg=f"{label1} vs dict({label2})") 

self.assertEqual(contents1, mapping2, msg=f"dict({label1}) vs {label2}") 

else: 

self.assertNotEqual(mapping1, mapping2, msg=f"{label1} vs {label2}") 

self.assertNotEqual(mapping1, contents2, msg=f"{label1} vs dict({label2})") 

self.assertNotEqual(contents1, mapping2, msg=f"dict({label1}) vs {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 PyStorable(Storable): 

"""A Storable with simple, mutable state. 

 

Parameters 

---------- 

value 

A value to be stored inside the object. Affects the object's string 

representation. Two PyStorables are equal if and only if their 

internal values are the same. 

""" 

 

def __init__(self, value): 

Storable.__init__(self) # pybind11 discourages using super() 

self.value = value 

 

def __repr__(self): 

return repr(self.value) 

 

def __eq__(self, other): 

return self.value == other.value 

 

 

class SimpleGenericMapCppTestSuite(lsst.utils.tests.TestCase): 

def setUp(self): 

self.data = {'one': 1, 

'pi': 3.1415927, 

'string': 'neither a number nor NaN', 

} 

self.pymap = SimpleGenericMap(self.data) 

self.cppmap = cppLib.makeInitialMap() 

 

def testPythonValues(self): 

"""Check that built-in types added in Python are visible in C++. 

""" 

for key, value in self.data.items(): 

cppLib.assertKeyValue(self.pymap, key, value) 

# Ensure the test isn't giving false negatives 

with self.assertRaises(pexExcept.NotFoundError): 

cppLib.assertKeyValue(self.pymap, "NotAKey", 42) 

 

def testCppValues(self): 

"""Check that built-in types added in C++ are visible in Python. 

""" 

for key, value in self.data.items(): 

self.assertIn(key, self.cppmap) 

self.assertEqual(value, self.cppmap[key], msg="key=" + key) 

# Ensure the test isn't giving false negatives 

self.assertNotIn("NotAKey", self.cppmap) 

 

def _checkPythonUpdates(self, testmap, msg=''): 

for key, value in self.data.items(): 

self.assertIn(key, testmap, msg=msg) 

self.assertEqual(value, testmap[key], msg='key=' + key + ', ' + msg) 

cppLib.assertKeyValue(testmap, key, value) 

testmap['answer'] = 42 # New key-value pair 

testmap['pi'] = 3.0 # Replace `float` with `float` 

testmap['string'] = False # Replace `str` with `bool` 

 

for key, value in {'answer': 42, 'pi': 3.0, 'string': False}.items(): 

# Test both Python and C++ state 

self.assertIn(key, testmap, msg=msg) 

self.assertEqual(value, testmap[key], msg='key=' + key + ', ' + msg) 

cppLib.assertKeyValue(testmap, key, value) 

 

def testPythonUpdates(self): 

"""Check that changes to built-in types made in Python are visible in 

both languages. 

""" 

self._checkPythonUpdates(self.pymap, msg='map=pymap') 

self._checkPythonUpdates(self.cppmap, msg='map=cppmap') 

 

def _checkCppUpdates(self, testmap, msg=''): 

for key, value in self.data.items(): 

self.assertIn(key, testmap, msg=msg) 

self.assertEqual(value, testmap[key], msg='key=' + key + ', ' + msg) 

cppLib.assertKeyValue(testmap, key, value) 

cppLib.makeCppUpdates(testmap) 

 

for key, value in {'answer': 42, 'pi': 3.0, 'string': False}.items(): 

# Test both Python and C++ state 

self.assertIn(key, testmap, msg=msg) 

self.assertEqual(value, testmap[key], msg='key=' + key + ', ' + msg) 

cppLib.assertKeyValue(testmap, key, value) 

 

def testCppUpdates(self): 

"""Check that changes to built-in types made in C++ are visible in 

both languages. 

""" 

self._checkCppUpdates(self.pymap, msg='map=pymap') 

self._checkCppUpdates(self.cppmap, msg='map=cppmap') 

 

def _checkPythonStorableUpdates(self, testmap, msg=''): 

cppLib.addCppStorable(testmap) 

self.assertIn('cppValue', testmap, msg=msg) 

self.assertEqual(testmap['cppValue'], cppLib.CppStorable('value'), msg=msg) 

self.assertIn('cppPointer', testmap, msg=msg) 

self.assertEqual(testmap['cppPointer'], cppLib.CppStorable('pointer'), msg=msg) 

 

# should have no effect because pybind11 copies Storable values for safety 

testmap['cppValue'].value = 'new_value' 

testmap['cppPointer'].value = 'extra_pointy' 

 

for key, value in {'cppValue': cppLib.CppStorable('value'), 

'cppPointer': cppLib.CppStorable('extra_pointy'), 

}.items(): 

# Test both Python and C++ state 

self.assertIn(key, testmap, msg=msg) 

self.assertEqual(value, testmap[key], msg='key=' + key + ', ' + msg) 

cppLib.assertKeyValue(testmap, key, value) 

 

def testPythonStorableUpdates(self): 

"""Check that changes to Storables made in Python are visible in 

both languages. 

""" 

self._checkPythonStorableUpdates(self.pymap, msg='map=pymap') 

self._checkPythonStorableUpdates(self.cppmap, msg='map=cppmap') 

 

def _checkCppStorableRead(self, testmap, msg=''): 

# WARNING: the Python variables holding PyStorable must survive to the end of the test 

# This is a known bug in pybind11; see DM-21314 

storableData = {'answer': PyStorable(42), 

'question': PyStorable('Unknown'), 

} 

testmap.update(storableData) 

 

for key, value in storableData.items(): 

self.assertIn(key, testmap, msg=msg) 

self.assertEqual(value, testmap[key], msg='key=' + key + ', ' + msg) 

# Exercise C++ equality operator 

cppLib.assertKeyValue(testmap, key, PyStorable(value.value)) 

# Exercise C++ string representation 

cppLib.assertPythonStorable(testmap, key, repr(value)) 

 

def testCppStorableRead(self): 

"""Check that Storables made in Python are visible in both languages. 

""" 

self._checkCppStorableRead(self.pymap, msg='map=pymap') 

self._checkCppStorableRead(self.cppmap, msg='map=cppmap') 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()