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

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

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

 

__all__ = [] 

 

from collections import Counter 

 

import numpy as np 

 

import lsst.utils.tests 

 

from lsst.afw.typehandling import Storable 

 

 

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

"""Base class for unit tests of GenericMap. 

 

Subclasses must call `GenericMapTestBaseClass.setUp(self)` 

if they provide their own version. 

 

This class is not *quite* a generic Mapping testbed, because it assumes 

that the map being tested only accepts keys of a particular type. 

""" 

 

class SimpleStorable(Storable): 

"""A subclass of Storable for testing purposes. 

""" 

def __repr__(self): 

return "Simplest possible representation" 

 

def __hash__(self): 

return 100 

 

def __eq__(self, other): 

"""Warning: violates both substitution and equality symmetry! 

""" 

return self.__class__ == other.__class__ 

 

class ComplexStorable(SimpleStorable): 

"""A subclass of Storable for testing purposes. 

""" 

def __init__(self, storage): 

super().__init__() 

self._storage = storage 

 

def __repr__(self): 

return "ComplexStorable(%r)" % self._storage 

 

def __hash__(self): 

return hash(self._storage) 

 

def __eq__(self, other): 

"""Warning: violates both substitution and equality symmetry! 

""" 

if self.__class__ == other.__class__: 

return self._storage == other._storage 

else: 

return False 

 

class NotAStorable: 

"""A class that should not be a legal value in a GenericMap. 

""" 

def __str__(self): 

return "Non-Storable" 

 

_testData = { 

0: True, 

1: 42, 

2: 42.0, 

3: "How many roads must a man walk down?", 

4: SimpleStorable(), 

5: ComplexStorable(-100.0), 

6: None, 

} 

 

@classmethod 

def getTestData(cls, keyClass): 

"""Generic dataset for testing GenericMap classes that can handle it. 

 

Parameters 

---------- 

keyClass : `type` 

The type of key expected by the GenericMap. 

""" 

return {keyClass(key): value for key, value in cls._testData.items()} 

 

def setUp(self): 

"""Set up a test 

 

Subclasses must call this method if they override setUp. 

""" 

super().setUp() 

# tell unittest to use the msg argument of asserts as a supplement 

# to the error message, rather than as the whole error message 

self.longMessage = True 

 

# Mapping must have: 

# __str__ 

# __repr__ 

# __eq__ 

# __ne__ 

# __contains__ 

# __getitem__ 

# get 

# __iter__ 

# __len__ 

# __bool__ 

# keys 

# items 

# values 

 

def checkInitMapping(self, keyType, mapClass, contents, msg=""): 

"""Check initialization from a mapping. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

mapClass : `lsst.afw.typehandling.GenericMap`-type 

The class whose ``__init__`` method will be tested. 

contents : `Mapping` 

The key-value pairs to insert into the map 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = mapClass(contents) 

self.checkContents(keyType, genericMap, contents, msg=msg) 

 

extraContents = {key: value for key, value in contents.items()} # contents need not define copy() 

extraKey = keyType(101) 

extraValue = 'Extra value' 

extraContents[extraKey] = extraValue 

genericMap = mapClass(contents, **{keyType(101): extraValue}) 

self.checkContents(keyType, genericMap, extraContents, msg=msg) 

 

with self.assertRaises(TypeError, msg=msg): 

mapClass({keyType(0): GenericMapTestBaseClass.NotAStorable()}) 

 

def checkInitPairs(self, keyType, mapClass, contents, msg=""): 

"""Check initialization from an iterable of pairs. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

mapClass: `lsst.afw.typehandling.GenericMap`-type 

The class whose ``__init__`` method will be tested. 

contents : `Mapping` 

The key-value pairs to insert into the map 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = mapClass(contents.items()) 

self.checkContents(keyType, genericMap, contents, msg=msg) 

 

extraContents = {key: value for key, value in contents.items()} # contents need not define copy() 

extraKey = keyType(101) 

extraValue = 'Extra value' 

extraContents[extraKey] = extraValue 

genericMap = mapClass(contents.items(), **{keyType(101): extraValue}) 

self.checkContents(keyType, genericMap, extraContents, msg=msg) 

 

with self.assertRaises(TypeError, msg=msg): 

mapClass([(keyType(0), GenericMapTestBaseClass.NotAStorable())]) 

 

def checkInitKwargs(self, mapClass, contents, msg=""): 

"""Check bulk insertion from keywords into a GenericMap. 

 

Parameters 

---------- 

mapClass: `lsst.afw.typehandling.GenericMapS`-type 

The class whose ``__init__`` method will be tested. 

Must allow string keys. 

contents : `Mapping` 

The key-value pairs to insert into the map 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = mapClass(**contents) 

self.checkContents(str, genericMap, contents, msg=msg) 

 

with self.assertRaises(TypeError, msg=msg): 

mapClass(notAKey=GenericMapTestBaseClass.NotAStorable()) 

 

def checkFromKeysDefault(self, keyType, mapClass, keys, msg=""): 

"""Check initialization using the ``fromkeys`` factory. 

 

Unlike `checkFromKeys`, this method lets ``fromkeys`` use its default 

value (which may give different behavior, in nonconforming 

implementations, from explicitly passing `None`). 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

mapClass: `lsst.afw.typehandling.GenericMap`-type 

The class whose ``fromkeys`` method will be tested. 

keys : `iterable` 

The keys to insert into the map. 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = mapClass.fromkeys(keys) 

self.assertIsInstance(genericMap, mapClass, msg=msg) 

self.checkContents(keyType, genericMap, dict.fromkeys(keys), msg=msg) 

 

def checkFromKeys(self, keyType, mapClass, keys, value, msg=""): 

"""Check initialization using the ``fromkeys`` factory. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

mapClass: `lsst.afw.typehandling.GenericMap`-type 

The class whose ``fromkeys`` method will be tested. 

keys : `iterable` 

The keys to insert into the map. 

value 

A legal value for a GenericMap. 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = mapClass.fromkeys(keys, value) 

self.assertIsInstance(genericMap, mapClass, msg=msg) 

self.checkContents(keyType, genericMap, dict.fromkeys(keys, value), msg=msg) 

 

def checkContains(self, keyType, genericMap, contents, msg=""): 

"""Check the contents of a GenericMap. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

genericMap : `lsst.afw.typehandling.GenericMap` 

The map to test. 

contents : `Mapping` 

The key-value pairs that should be present in ``genericMap`` 

msg : `str` 

Error message suffix describing test parameters 

""" 

for key in contents: 

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

 

for key in range(30): 

if keyType(key) not in contents: 

self.assertNotIn(keyType(key), genericMap, msg=msg) 

 

wrongType = float if keyType is not float else int 

with self.assertRaises(TypeError): 

wrongType(0) in genericMap 

 

def checkContents(self, keyType, genericMap, contents, msg=""): 

"""Check the contents of a GenericMap. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

genericMap : `lsst.afw.typehandling.GenericMap` 

The map to test. 

contents : `Mapping` 

The key-value pairs that should be present in ``genericMap`` 

msg : `str` 

Error message suffix describing test parameters 

""" 

for key, value in contents.items(): 

self.assertEqual(genericMap[key], value, msg=msg) 

 

for key in (keyType(key) for key in range(30) if keyType(key) not in contents): 

with self.assertRaises(KeyError, msg=msg): 

genericMap[key] 

 

wrongType = float if keyType is not float else int 

with self.assertRaises(TypeError): 

genericMap[wrongType(0)] 

 

def checkGet(self, keyType, genericMap, contents, msg=""): 

"""Check that GenericMap.get works correctly. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

genericMap : `lsst.afw.typehandling.GenericMap` 

The map to test. 

contents : `Mapping` 

The key-value pairs that should be present in ``genericMap`` 

msg : `str` 

Error message suffix describing test parameters 

""" 

default = "Not a default value" 

for key, value in contents.items(): 

self.assertEqual(genericMap.get(key), value, msg=msg) 

self.assertEqual(genericMap.get(key, default), value, msg=msg) 

 

for key in (keyType(key) for key in range(30) if keyType(key) not in contents): 

self.assertEqual(genericMap.get(key), None, msg=msg) 

self.assertEqual(genericMap.get(key, default), default, msg=msg) 

 

wrongType = float if keyType is not float else int 

with self.assertRaises(TypeError): 

genericMap.get(wrongType(0)) 

 

def checkIteration(self, genericMap, contents, msg=""): 

"""Check the result of iterating over a GenericMap. 

 

Parameters 

---------- 

genericMap : `lsst.afw.typehandling.GenericMap` 

The map to test. 

contents : `Mapping` 

The key-value pairs that should be present in ``genericMap`` 

msg : `str` 

Error message suffix describing test parameters 

""" 

self.assertEqual({key: genericMap[key] for key in genericMap}, dict(contents), msg=msg) 

 

def checkViews(self, genericMap, contents, msg=""): 

"""Check the views provided by a GenericMap. 

 

Parameters 

---------- 

genericMap : `lsst.afw.typehandling.GenericMap` 

The map to test. 

contents : `Mapping` 

The key-value pairs that should be present in ``genericMap`` 

msg : `str` 

Error message suffix describing test parameters 

""" 

self.assertEqual(set(genericMap.keys()), set(contents.keys()), msg=msg) 

self.assertEqual(Counter(genericMap.values()), Counter(contents.values()), msg=msg) 

self.assertEqual(Counter(genericMap.items()), Counter(contents.items()), msg=msg) 

 

 

class MutableGenericMapTestBaseClass(GenericMapTestBaseClass): 

"""Base class for unit tests of GenericMap that allow insertion/deletion. 

 

Subclasses must call `MutableGenericMapTestBaseClass.setUp(self)` 

if they provide their own version. 

""" 

 

@classmethod 

def _fillMap(cls, mapFactory, contents): 

"""Create a new GenericMap with particular contents. 

 

Parameters 

---------- 

mapFactory : callable 

A zero-argument callable that creates an empty 

`lsst.afw.typehandling.GenericMap` object 

contents : `Mapping` 

The key-value pairs that should be present in the new map. 

 

Returns 

------- 

map : `lsst.afw.typehandling.GenericMap` 

a GenericMap equivalent to ``contents`` 

""" 

return cls._fillPartialMap(mapFactory, contents, len(contents)) 

 

@classmethod 

def _fillPartialMap(cls, mapFactory, contents, numElements): 

"""Create a new GenericMap with particular contents. 

 

Parameters 

---------- 

mapFactory : callable 

A zero-argument callable that creates an empty 

`lsst.afw.typehandling.GenericMap` object 

contents : `Mapping` 

The key-value pairs that should be present in the new map. 

numElements : `int` 

The number of elements from ``contents`` to be inserted. 

 

Returns 

------- 

map : `lsst.afw.typehandling.GenericMap` 

a GenericMap containing ``numElements`` of ``contents`` or all of 

``contents``, whichever is smaller 

""" 

newMap = mapFactory() 

for i, (key, value) in enumerate(contents.items()): 

if i < numElements: 

newMap[key] = value 

else: 

break 

return newMap 

 

# MutableMapping must have: 

# __setitem__ 

# setdefault 

# __delitem__ 

# pop 

# popitem 

# clear 

# update 

 

def checkInsertItem(self, keyType, mapFactory, contents, msg=""): 

"""Check element insertion in a GenericMap. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

mapFactory : callable 

A zero-argument callable that creates an empty 

`lsst.afw.typehandling.GenericMap` object of the type to be tested 

contents : `Mapping` 

The key-value pairs to insert into the map 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = mapFactory() 

 

for length, (key, value) in enumerate(contents.items()): 

loopMsg = msg + " Inserting %r=%r" % (key, value) 

genericMap[key] = value 

self.assertEqual(len(genericMap), length+1, msg=loopMsg) 

self.assertEqual(genericMap[key], value, msg=loopMsg) 

 

self.assertEqual(dict(genericMap), dict(contents), msg=msg) 

 

with self.assertRaises(TypeError, msg=msg): 

genericMap[keyType(0)] = GenericMapTestBaseClass.NotAStorable() 

 

wrongType = float if keyType is not float else int 

with self.assertRaises(TypeError): 

genericMap[wrongType(0)] = 0 

 

def checkSetdefault(self, keyType, mapFactory, contents, msg=""): 

"""Check that GenericMap.setdefault works correctly. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

mapFactory : callable 

A zero-argument callable that creates an empty 

`lsst.afw.typehandling.GenericMap` object of the type to be tested 

contents : `Mapping` 

The key-value pairs to insert into the map 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = mapFactory() 

 

result = genericMap.setdefault(keyType(0)) 

self.assertEqual(len(genericMap), 1, msg=msg) 

self.assertIsNone(result, msg=msg) 

self.assertIsNone(genericMap[keyType(0)], msg=msg) 

del genericMap[keyType(0)] 

 

default = "This is a default" 

for length, (key, _) in enumerate(contents.items()): 

loopMsg = msg + " Defaulting %r" % (key) 

result = genericMap.setdefault(key, default) 

self.assertEqual(len(genericMap), length+1, msg=loopMsg) 

self.assertEqual(result, default, msg=loopMsg) 

self.assertEqual(genericMap[key], default, msg=loopMsg) 

 

self.assertEqual(genericMap.keys(), contents.keys(), msg=msg) 

 

with self.assertRaises(TypeError, msg=msg): 

genericMap.setdefault(keyType(100), GenericMapTestBaseClass.NotAStorable()) 

 

wrongType = float if keyType is not float else int 

with self.assertRaises(TypeError): 

genericMap.setdefault(wrongType(0), default) 

 

genericMap = self._fillMap(mapFactory, contents) 

for length, (key, value) in enumerate(contents.items()): 

loopMsg = msg + " Defaulting existing %r=%r" % (key, value) 

result = genericMap.setdefault(key, default) 

self.assertEqual(len(genericMap), len(contents), msg=loopMsg) 

self.assertEqual(result, contents[key], msg=loopMsg) 

self.assertEqual(genericMap[key], contents[key], msg=loopMsg) 

 

def checkUpdateMapping(self, keyType, mapFactory, contents, msg=""): 

"""Check bulk insertion from a mapping into a GenericMap. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

mapFactory : callable 

A zero-argument callable that creates an empty 

`lsst.afw.typehandling.GenericMap` object of the type to be tested 

contents : `Mapping` 

The key-value pairs to insert into the map 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = self._fillPartialMap(mapFactory, dict.fromkeys(contents, 0), len(contents)/2) 

self.assertLess(len(genericMap), len(contents), msg=msg) 

 

genericMap.update(contents) 

self.assertEqual(dict(genericMap), dict(contents), msg=msg) 

 

with self.assertRaises(TypeError, msg=msg): 

genericMap.update({keyType(0): GenericMapTestBaseClass.NotAStorable()}) 

 

wrongType = float if keyType is not float else int 

with self.assertRaises(TypeError, msg=msg): 

genericMap.update({wrongType(0): 0}) 

 

def checkUpdatePairs(self, keyType, mapFactory, contents, msg=""): 

"""Check bulk insertion from an iterable of pairs into a GenericMap. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

mapFactory : callable 

A zero-argument callable that creates an empty 

`lsst.afw.typehandling.GenericMap` object of the type to be tested 

contents : `Mapping` 

The key-value pairs to insert into the map 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = self._fillPartialMap(mapFactory, dict.fromkeys(contents, 0), len(contents)/2) 

self.assertLess(len(genericMap), len(contents), msg=msg) 

 

genericMap.update(contents.items()) 

self.assertEqual(dict(genericMap), dict(contents), msg=msg) 

 

with self.assertRaises(TypeError, msg=msg): 

genericMap.update([(keyType(0), GenericMapTestBaseClass.NotAStorable())]) 

 

wrongType = float if keyType is not float else int 

with self.assertRaises(TypeError, msg=msg): 

genericMap.update([(wrongType(0), 0)]) 

 

def checkUpdateKwargs(self, mapFactory, contents, msg=""): 

"""Check bulk insertion from keywords into a GenericMap. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

mapFactory : callable 

A zero-argument callable that creates an empty 

`lsst.afw.typehandling.GenericMap` object of the type to be tested 

Must allow string keys. 

contents : `Mapping` 

The key-value pairs to insert into the map 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = self._fillPartialMap(mapFactory, dict.fromkeys(contents, 0), len(contents)/2) 

self.assertLess(len(genericMap), len(contents), msg=msg) 

 

genericMap.update(**contents) 

self.assertEqual(dict(genericMap), dict(contents), msg=msg) 

 

with self.assertRaises(TypeError, msg=msg): 

genericMap.update(notAKey=GenericMapTestBaseClass.NotAStorable()) 

 

def checkReplaceItem(self, keyType, genericMap, msg=""): 

"""Check element replacement in a GenericMap. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

genericMap : `lsst.afw.typehandling.GenericMap` 

The map to test. Must be empty. 

msg : `str` 

Error message suffix describing test parameters 

""" 

self.assertFalse(genericMap, msg=msg) 

key = keyType(42) 

 

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

loopMsg = msg + " Inserting %r=%r" % (key, value) 

genericMap[key] = value # value may be of a different type 

self.assertEqual(len(genericMap), 1, msg=loopMsg) 

self.assertEqual(genericMap[key], value, msg=loopMsg) 

 

self.assertEqual(dict(genericMap), {key: value}, msg=msg) 

 

with self.assertRaises(TypeError, msg=msg): 

genericMap[key] = GenericMapTestBaseClass.NotAStorable() 

 

def checkRemoveItem(self, keyType, mapFactory, contents, msg=""): 

"""Check element removal from a GenericMap. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

mapFactory : callable 

A zero-argument callable that creates an empty 

`lsst.afw.typehandling.GenericMap` object of the type to be tested 

contents : `Mapping` 

The key-value pairs initially occupying the map 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = self._fillMap(mapFactory, contents) 

 

with self.assertRaises(KeyError, msg=msg): 

del genericMap[keyType(2019)] 

 

keysToRemove = list(contents.keys()) 

np.random.shuffle(keysToRemove) 

for numPrevious, rawKey in enumerate(keysToRemove): 

key = keyType(rawKey) 

loopMsg = msg + " Deleting %r" % (key) 

del genericMap[key] 

self.assertEqual(len(genericMap), len(contents)-numPrevious-1, msg=loopMsg) 

self.assertNotIn(key, genericMap, msg=loopMsg) 

 

wrongType = float if keyType is not float else int 

with self.assertRaises(TypeError): 

del genericMap[wrongType(0)] 

 

def checkPop(self, keyType, mapFactory, contents, msg=""): 

"""Check that GenericMap.pop works correctly. 

 

Parameters 

---------- 

keyType : `type` 

The type of key allowed by ``mapClass``. 

mapFactory : callable 

A zero-argument callable that creates an empty 

`lsst.afw.typehandling.GenericMap` object of the type to be tested 

contents : `Mapping` 

The key-value pairs initially occupying the map 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = self._fillMap(mapFactory, contents) 

 

with self.assertRaises(KeyError, msg=msg): 

genericMap.pop(keyType(2019)) 

 

default = "This is a default" 

result = genericMap.pop(keyType(2019), default) 

self.assertEqual(dict(genericMap), dict(contents), msg=msg) 

self.assertEqual(result, default) 

 

wrongType = float if keyType is not float else int 

with self.assertRaises(TypeError): 

genericMap.pop(wrongType(0)) 

 

itemsToRemove = list(contents.items()) 

np.random.shuffle(itemsToRemove) 

for numPrevious, (rawKey, value) in enumerate(itemsToRemove): 

key = keyType(rawKey) 

loopMsg = msg + " Popping %r=%r" % (key, value) 

result = genericMap.pop(key) 

self.assertEqual(len(genericMap), len(contents)-numPrevious-1, msg=loopMsg) 

self.assertNotIn(key, genericMap, msg=loopMsg) 

self.assertEqual(result, value, msg=loopMsg) 

 

def checkPopitem(self, mapFactory, contents, msg=""): 

"""Check that GenericMap.popitem works correctly. 

 

Parameters 

---------- 

mapFactory : callable 

A zero-argument callable that creates an empty 

`lsst.afw.typehandling.GenericMap` object of the type to be tested 

contents : `Mapping` 

The key-value pairs initially occupying the map 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = self._fillMap(mapFactory, contents) 

 

for numPrevious in range(len(genericMap)): 

key, value = genericMap.popitem() 

loopMsg = msg + " Popping %r=%r" % (key, value) 

self.assertIn((key, value), contents.items(), msg=loopMsg) 

self.assertEqual(len(genericMap), len(contents)-numPrevious-1, msg=loopMsg) 

self.assertNotIn(key, genericMap, msg=loopMsg) 

 

with self.assertRaises(KeyError, msg=msg): 

genericMap.popitem() 

 

def checkClear(self, mapFactory, contents, msg=""): 

"""Check erasing a GenericMap. 

 

Parameters 

---------- 

mapFactory : callable 

A zero-argument callable that creates an empty 

`lsst.afw.typehandling.GenericMap` object of the type to be tested 

contents : `Mapping` 

The key-value pairs initially occupying the map 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = self._fillMap(mapFactory, contents) 

self.assertTrue(genericMap, msg=msg) 

 

genericMap.clear() 

self.assertFalse(genericMap, msg=msg) 

self.assertEqual(len(genericMap), 0, msg=msg) 

for key in genericMap: 

self.fail("Unexpected key: %s" % key, msg=msg) 

 

def checkMutableViews(self, mapFactory, contents, msg=""): 

"""Check that the views of a GenericMap update automatically. 

 

This test supersedes `GenericMapTestBaseClass.checkViews`. 

 

Parameters 

---------- 

genericMap : `lsst.afw.typehandling.GenericMap` 

The map to test. 

contents : `Mapping` 

The key-value pairs that should be present in ``genericMap`` 

msg : `str` 

Error message suffix describing test parameters 

""" 

genericMap = mapFactory() 

keyView = genericMap.keys() 

valueView = genericMap.values() 

itemView = genericMap.items() 

 

for view in (keyView, valueView, itemView): 

self.assertEqual(len(view), len(genericMap), msg=msg) 

# is initial state correct? 

self.checkViews(genericMap, {}, msg) 

 

for key, value in contents.items(): 

genericMap[key] = value 

 

for view in (keyView, valueView, itemView): 

self.assertEqual(len(view), len(genericMap), msg=msg) 

# is final state correct? 

self.checkViews(genericMap, contents, msg)