LSST Applications g02d81e74bb+86cf3d8bc9,g180d380827+7a4e862ed4,g2079a07aa2+86d27d4dc4,g2305ad1205+e1ca1c66fa,g29320951ab+012e1474a1,g295015adf3+341ea1ce94,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g33d1c0ed96+0e5473021a,g3a166c0a6a+0e5473021a,g3ddfee87b4+c429d67c83,g48712c4677+f88676dd22,g487adcacf7+27e1e21933,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+b41db86c35,g5a732f18d5+53520f316c,g64a986408d+86cf3d8bc9,g858d7b2824+86cf3d8bc9,g8a8a8dda67+585e252eca,g99cad8db69+84912a7fdc,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,ga8c6da7877+a2b54eae19,gb0e22166c9+60f28cb32d,gba4ed39666+c2a2e4ac27,gbb8dafda3b+6681f309db,gc120e1dc64+f0fcc2f6d8,gc28159a63d+0e5473021a,gcf0d15dbbd+c429d67c83,gdaeeff99f8+f9a426f77a,ge6526c86ff+0433e6603d,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gff1a9f87cc+86cf3d8bc9,w.2024.17
LSST Data Management Base Package
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | List of all members
lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass Class Reference
Inheritance diagram for lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass:
lsst.afw.typehandling.testUtils.GenericMapTestBaseClass

Public Member Functions

 checkInsertItem (self, mapFactory, contents, msg="")
 
 checkSetdefault (self, mapFactory, contents, msg="")
 
 checkUpdateMapping (self, mapFactory, contents, msg="")
 
 checkUpdatePairs (self, mapFactory, contents, msg="")
 
 checkUpdateKwargs (self, mapFactory, contents, msg="")
 
 checkReplaceItem (self, genericMap, msg="")
 
 checkRemoveItem (self, mapFactory, contents, msg="")
 
 checkPop (self, mapFactory, contents, msg="")
 
 checkPopitem (self, mapFactory, contents, msg="")
 
 checkClear (self, mapFactory, contents, msg="")
 
 checkMutableViews (self, mapFactory, contents, msg="")
 

Protected Member Functions

 _fillMap (cls, mapFactory, contents)
 
 _fillPartialMap (cls, mapFactory, contents, numElements)
 

Detailed Description

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

Subclasses must call `MutableGenericMapTestBaseClass.setUp(self)`
if they provide their own version.

Definition at line 367 of file testUtils.py.

Member Function Documentation

◆ _fillMap()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass._fillMap ( cls,
mapFactory,
contents )
protected
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``

Definition at line 375 of file testUtils.py.

375 def _fillMap(cls, mapFactory, contents):
376 """Create a new GenericMap with particular contents.
377
378 Parameters
379 ----------
380 mapFactory : callable
381 A zero-argument callable that creates an empty
382 `lsst.afw.typehandling.GenericMap` object
383 contents : `Mapping`
384 The key-value pairs that should be present in the new map.
385
386 Returns
387 -------
388 map : `lsst.afw.typehandling.GenericMap`
389 a GenericMap equivalent to ``contents``
390 """
391 return cls._fillPartialMap(mapFactory, contents, len(contents))
392

◆ _fillPartialMap()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass._fillPartialMap ( cls,
mapFactory,
contents,
numElements )
protected
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

Definition at line 394 of file testUtils.py.

394 def _fillPartialMap(cls, mapFactory, contents, numElements):
395 """Create a new GenericMap with particular contents.
396
397 Parameters
398 ----------
399 mapFactory : callable
400 A zero-argument callable that creates an empty
401 `lsst.afw.typehandling.GenericMap` object
402 contents : `Mapping`
403 The key-value pairs that should be present in the new map.
404 numElements : `int`
405 The number of elements from ``contents`` to be inserted.
406
407 Returns
408 -------
409 map : `lsst.afw.typehandling.GenericMap`
410 a GenericMap containing ``numElements`` of ``contents`` or all of
411 ``contents``, whichever is smaller
412 """
413 newMap = mapFactory()
414 for i, (key, value) in enumerate(contents.items()):
415 if i < numElements:
416 newMap[key] = value
417 else:
418 break
419 return newMap
420

◆ checkClear()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass.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

Definition at line 705 of file testUtils.py.

705 def checkClear(self, mapFactory, contents, msg=""):
706 """Check erasing a GenericMap.
707
708 Parameters
709 ----------
710 mapFactory : callable
711 A zero-argument callable that creates an empty
712 `lsst.afw.typehandling.GenericMap` object of the type to be tested
713 contents : `Mapping`
714 The key-value pairs initially occupying the map
715 msg : `str`
716 Error message suffix describing test parameters
717 """
718 genericMap = self._fillMap(mapFactory, contents)
719 self.assertTrue(genericMap, msg=msg)
720
721 genericMap.clear()
722 self.assertFalse(genericMap, msg=msg)
723 self.assertEqual(len(genericMap), 0, msg=msg)
724 for key in genericMap:
725 self.fail(f"Unexpected key: {key}", msg=msg)
726

◆ checkInsertItem()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass.checkInsertItem ( self,
mapFactory,
contents,
msg = "" )
Check element insertion in 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 to insert into the map
msg : `str`
    Error message suffix describing test parameters

Definition at line 430 of file testUtils.py.

430 def checkInsertItem(self, mapFactory, contents, msg=""):
431 """Check element insertion in a GenericMap.
432
433 Parameters
434 ----------
435 mapFactory : callable
436 A zero-argument callable that creates an empty
437 `lsst.afw.typehandling.GenericMap` object of the type to be tested
438 contents : `Mapping`
439 The key-value pairs to insert into the map
440 msg : `str`
441 Error message suffix describing test parameters
442 """
443 genericMap = mapFactory()
444
445 for length, (key, value) in enumerate(contents.items()):
446 loopMsg = f"{msg} Inserting {key!r}={value!r}"
447 genericMap[key] = value
448 self.assertEqual(len(genericMap), length+1, msg=loopMsg)
449 self.assertEqual(genericMap[key], value, msg=loopMsg)
450
451 self.assertEqual(dict(genericMap), dict(contents), msg=msg)
452
453 keyType = genericMap.dtype
454 with self.assertRaises(TypeError, msg=msg):
455 genericMap[keyType(0)] = GenericMapTestBaseClass.NotAStorable()
456
457 wrongType = float if keyType is not float else int
458 with self.assertRaises(TypeError):
459 genericMap[wrongType(0)] = 0
460

◆ checkMutableViews()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass.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

Definition at line 727 of file testUtils.py.

727 def checkMutableViews(self, mapFactory, contents, msg=""):
728 """Check that the views of a GenericMap update automatically.
729
730 This test supersedes `GenericMapTestBaseClass.checkViews`.
731
732 Parameters
733 ----------
734 genericMap : `lsst.afw.typehandling.GenericMap`
735 The map to test.
736 contents : `Mapping`
737 The key-value pairs that should be present in ``genericMap``
738 msg : `str`
739 Error message suffix describing test parameters
740 """
741 genericMap = mapFactory()
742 keyView = genericMap.keys()
743 valueView = genericMap.values()
744 itemView = genericMap.items()
745
746 for view in (keyView, valueView, itemView):
747 self.assertEqual(len(view), len(genericMap), msg=msg)
748 # is initial state correct?
749 self.checkViews(genericMap, {}, msg)
750
751 for key, value in contents.items():
752 genericMap[key] = value
753
754 for view in (keyView, valueView, itemView):
755 self.assertEqual(len(view), len(genericMap), msg=msg)
756 # is final state correct?
757 self.checkViews(genericMap, contents, msg)

◆ checkPop()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass.checkPop ( self,
mapFactory,
contents,
msg = "" )
Check that GenericMap.pop 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

Definition at line 642 of file testUtils.py.

642 def checkPop(self, mapFactory, contents, msg=""):
643 """Check that GenericMap.pop works correctly.
644
645 Parameters
646 ----------
647 mapFactory : callable
648 A zero-argument callable that creates an empty
649 `lsst.afw.typehandling.GenericMap` object of the type to be tested
650 contents : `Mapping`
651 The key-value pairs initially occupying the map
652 msg : `str`
653 Error message suffix describing test parameters
654 """
655 genericMap = self._fillMap(mapFactory, contents)
656
657 keyType = genericMap.dtype
658 with self.assertRaises(KeyError, msg=msg):
659 genericMap.pop(keyType(2019))
660
661 default = "This is a default"
662 result = genericMap.pop(keyType(2019), default)
663 self.assertEqual(dict(genericMap), dict(contents), msg=msg)
664 self.assertEqual(result, default)
665
666 wrongType = float if keyType is not float else int
667 with self.assertRaises(TypeError):
668 genericMap.pop(wrongType(0))
669
670 itemsToRemove = list(contents.items())
671 np.random.shuffle(itemsToRemove)
672 for numPrevious, (rawKey, value) in enumerate(itemsToRemove):
673 key = keyType(rawKey)
674 loopMsg = f"{msg} Popping {key}={value}"
675 result = genericMap.pop(key)
676 self.assertEqual(len(genericMap), len(contents)-numPrevious-1, msg=loopMsg)
677 self.assertNotIn(key, genericMap, msg=loopMsg)
678 self.assertEqual(result, value, msg=loopMsg)
679

◆ checkPopitem()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass.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

Definition at line 680 of file testUtils.py.

680 def checkPopitem(self, mapFactory, contents, msg=""):
681 """Check that GenericMap.popitem works correctly.
682
683 Parameters
684 ----------
685 mapFactory : callable
686 A zero-argument callable that creates an empty
687 `lsst.afw.typehandling.GenericMap` object of the type to be tested
688 contents : `Mapping`
689 The key-value pairs initially occupying the map
690 msg : `str`
691 Error message suffix describing test parameters
692 """
693 genericMap = self._fillMap(mapFactory, contents)
694
695 for numPrevious in range(len(genericMap)):
696 key, value = genericMap.popitem()
697 loopMsg = f"{msg} Popping {key}={value}"
698 self.assertIn((key, value), contents.items(), msg=loopMsg)
699 self.assertEqual(len(genericMap), len(contents)-numPrevious-1, msg=loopMsg)
700 self.assertNotIn(key, genericMap, msg=loopMsg)
701
702 with self.assertRaises(KeyError, msg=msg):
703 genericMap.popitem()
704

◆ checkRemoveItem()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass.checkRemoveItem ( self,
mapFactory,
contents,
msg = "" )
Check element removal from 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

Definition at line 610 of file testUtils.py.

610 def checkRemoveItem(self, mapFactory, contents, msg=""):
611 """Check element removal from a GenericMap.
612
613 Parameters
614 ----------
615 mapFactory : callable
616 A zero-argument callable that creates an empty
617 `lsst.afw.typehandling.GenericMap` object of the type to be tested
618 contents : `Mapping`
619 The key-value pairs initially occupying the map
620 msg : `str`
621 Error message suffix describing test parameters
622 """
623 genericMap = self._fillMap(mapFactory, contents)
624
625 keyType = genericMap.dtype
626 with self.assertRaises(KeyError, msg=msg):
627 del genericMap[keyType(2019)]
628
629 keysToRemove = list(contents.keys())
630 np.random.shuffle(keysToRemove)
631 for numPrevious, rawKey in enumerate(keysToRemove):
632 key = keyType(rawKey)
633 loopMsg = f"{msg} Deleting {key!r}"
634 del genericMap[key]
635 self.assertEqual(len(genericMap), len(contents)-numPrevious-1, msg=loopMsg)
636 self.assertNotIn(key, genericMap, msg=loopMsg)
637
638 wrongType = float if keyType is not float else int
639 with self.assertRaises(TypeError):
640 del genericMap[wrongType(0)]
641

◆ checkReplaceItem()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass.checkReplaceItem ( self,
genericMap,
msg = "" )
Check element replacement in a GenericMap.

Parameters
----------
genericMap : `lsst.afw.typehandling.GenericMap`
    The map to test. Must be empty.
msg : `str`
    Error message suffix describing test parameters

Definition at line 585 of file testUtils.py.

585 def checkReplaceItem(self, genericMap, msg=""):
586 """Check element replacement in a GenericMap.
587
588 Parameters
589 ----------
590 genericMap : `lsst.afw.typehandling.GenericMap`
591 The map to test. Must be empty.
592 msg : `str`
593 Error message suffix describing test parameters
594 """
595 self.assertFalse(genericMap, msg=msg)
596 keyType = genericMap.dtype
597 key = keyType(42)
598
599 for value in self.getTestData(keyType).values():
600 loopMsg = f"{msg} Inserting {key!r}={value!r}"
601 genericMap[key] = value # value may be of a different type
602 self.assertEqual(len(genericMap), 1, msg=loopMsg)
603 self.assertEqual(genericMap[key], value, msg=loopMsg)
604
605 self.assertEqual(dict(genericMap), {key: value}, msg=msg)
606
607 with self.assertRaises(TypeError, msg=msg):
608 genericMap[key] = GenericMapTestBaseClass.NotAStorable()
609

◆ checkSetdefault()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass.checkSetdefault ( self,
mapFactory,
contents,
msg = "" )
Check that GenericMap.setdefault 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 to insert into the map
msg : `str`
    Error message suffix describing test parameters

Definition at line 461 of file testUtils.py.

461 def checkSetdefault(self, mapFactory, contents, msg=""):
462 """Check that GenericMap.setdefault works correctly.
463
464 Parameters
465 ----------
466 mapFactory : callable
467 A zero-argument callable that creates an empty
468 `lsst.afw.typehandling.GenericMap` object of the type to be tested
469 contents : `Mapping`
470 The key-value pairs to insert into the map
471 msg : `str`
472 Error message suffix describing test parameters
473 """
474 genericMap = mapFactory()
475
476 keyType = genericMap.dtype
477 result = genericMap.setdefault(keyType(0))
478 self.assertEqual(len(genericMap), 1, msg=msg)
479 self.assertIsNone(result, msg=msg)
480 self.assertIsNone(genericMap[keyType(0)], msg=msg)
481 del genericMap[keyType(0)]
482
483 default = "This is a default"
484 for length, (key, _) in enumerate(contents.items()):
485 loopMsg = f"{msg} Defaulting {key!r}"
486 result = genericMap.setdefault(key, default)
487 self.assertEqual(len(genericMap), length+1, msg=loopMsg)
488 self.assertEqual(result, default, msg=loopMsg)
489 self.assertEqual(genericMap[key], default, msg=loopMsg)
490
491 self.assertEqual(genericMap.keys(), contents.keys(), msg=msg)
492
493 with self.assertRaises(TypeError, msg=msg):
494 genericMap.setdefault(keyType(100), GenericMapTestBaseClass.NotAStorable())
495
496 wrongType = float if keyType is not float else int
497 with self.assertRaises(TypeError):
498 genericMap.setdefault(wrongType(0), default)
499
500 genericMap = self._fillMap(mapFactory, contents)
501 for length, (key, value) in enumerate(contents.items()):
502 loopMsg = f"{msg} Defaulting existing {key!r}={value!r}"
503 result = genericMap.setdefault(key, default)
504 self.assertEqual(len(genericMap), len(contents), msg=loopMsg)
505 self.assertEqual(result, contents[key], msg=loopMsg)
506 self.assertEqual(genericMap[key], contents[key], msg=loopMsg)
507

◆ checkUpdateKwargs()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass.checkUpdateKwargs ( self,
mapFactory,
contents,
msg = "" )
Check bulk insertion from keywords into a GenericMap.

Parameters
----------
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

Definition at line 562 of file testUtils.py.

562 def checkUpdateKwargs(self, mapFactory, contents, msg=""):
563 """Check bulk insertion from keywords into a GenericMap.
564
565 Parameters
566 ----------
567 mapFactory : callable
568 A zero-argument callable that creates an empty
569 `lsst.afw.typehandling.GenericMap` object of the type to be tested
570 Must allow string keys.
571 contents : `Mapping`
572 The key-value pairs to insert into the map
573 msg : `str`
574 Error message suffix describing test parameters
575 """
576 genericMap = self._fillPartialMap(mapFactory, dict.fromkeys(contents, 0), len(contents)/2)
577 self.assertLess(len(genericMap), len(contents), msg=msg)
578
579 genericMap.update(**contents)
580 self.assertEqual(dict(genericMap), dict(contents), msg=msg)
581
582 with self.assertRaises(TypeError, msg=msg):
583 genericMap.update(notAKey=GenericMapTestBaseClass.NotAStorable())
584

◆ checkUpdateMapping()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass.checkUpdateMapping ( self,
mapFactory,
contents,
msg = "" )
Check bulk insertion from a mapping into 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 to insert into the map
msg : `str`
    Error message suffix describing test parameters

Definition at line 508 of file testUtils.py.

508 def checkUpdateMapping(self, mapFactory, contents, msg=""):
509 """Check bulk insertion from a mapping into a GenericMap.
510
511 Parameters
512 ----------
513 mapFactory : callable
514 A zero-argument callable that creates an empty
515 `lsst.afw.typehandling.GenericMap` object of the type to be tested
516 contents : `Mapping`
517 The key-value pairs to insert into the map
518 msg : `str`
519 Error message suffix describing test parameters
520 """
521 genericMap = self._fillPartialMap(mapFactory, dict.fromkeys(contents, 0), len(contents)/2)
522 self.assertLess(len(genericMap), len(contents), msg=msg)
523
524 genericMap.update(contents)
525 self.assertEqual(dict(genericMap), dict(contents), msg=msg)
526
527 keyType = genericMap.dtype
528 with self.assertRaises(TypeError, msg=msg):
529 genericMap.update({keyType(0): GenericMapTestBaseClass.NotAStorable()})
530
531 wrongType = float if keyType is not float else int
532 with self.assertRaises(TypeError, msg=msg):
533 genericMap.update({wrongType(0): 0})
534

◆ checkUpdatePairs()

lsst.afw.typehandling.testUtils.MutableGenericMapTestBaseClass.checkUpdatePairs ( self,
mapFactory,
contents,
msg = "" )
Check bulk insertion from an iterable of pairs into 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 to insert into the map
msg : `str`
    Error message suffix describing test parameters

Definition at line 535 of file testUtils.py.

535 def checkUpdatePairs(self, mapFactory, contents, msg=""):
536 """Check bulk insertion from an iterable of pairs into a GenericMap.
537
538 Parameters
539 ----------
540 mapFactory : callable
541 A zero-argument callable that creates an empty
542 `lsst.afw.typehandling.GenericMap` object of the type to be tested
543 contents : `Mapping`
544 The key-value pairs to insert into the map
545 msg : `str`
546 Error message suffix describing test parameters
547 """
548 genericMap = self._fillPartialMap(mapFactory, dict.fromkeys(contents, 0), len(contents)/2)
549 self.assertLess(len(genericMap), len(contents), msg=msg)
550
551 genericMap.update(contents.items())
552 self.assertEqual(dict(genericMap), dict(contents), msg=msg)
553
554 keyType = genericMap.dtype
555 with self.assertRaises(TypeError, msg=msg):
556 genericMap.update([(keyType(0), GenericMapTestBaseClass.NotAStorable())])
557
558 wrongType = float if keyType is not float else int
559 with self.assertRaises(TypeError, msg=msg):
560 genericMap.update([(wrongType(0), 0)])
561

The documentation for this class was generated from the following file: