Coverage for python / lsst / obs / lsst / testHelper.py: 56%

37 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-22 09:22 +0000

1# This file is part of obs_lsst. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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 <http://www.gnu.org/licenses/>. 

21# 

22 

23"""Test support classes for obs_lsst""" 

24__all__ = ("ObsLsstButlerTests", "ObsLsstObsBaseOverrides") 

25 

26import os.path 

27import unittest 

28from abc import abstractmethod 

29 

30import lsst.utils.tests 

31import lsst.obs.base.tests 

32import lsst.daf.butler 

33 

34 

35class ObsLsstButlerTests(lsst.utils.tests.TestCase): 

36 """Base class shared by all tests of the butler and mapper. 

37 

38 This class can not inherit from `~lsst.obs.base.tests.ObsTests` since 

39 that will trigger tests in this class directly that will fail. 

40 

41 This class defines a butler and a mapper for each test subclass. 

42 They are stored in the ``_mapper`` and ``_butler`` class attributes 

43 to distinguish them from the ``mapper`` and ``butler`` instance 

44 attributes used by `~lsst.obs.base.tests.ObsTests`. 

45 """ 

46 

47 instrumentDir = "TBD" # Override in subclass 

48 """Name of instrument directory within data/input.""" 

49 

50 _butler = None 

51 

52 DATAROOT = "UNDEFINED" # Define in subclass 

53 

54 @classmethod 

55 @abstractmethod 

56 def getInstrument(cls): 

57 """Retrieve the `lsst.obs.base.Instrument` class for this instrument. 

58 

59 Returns 

60 ------- 

61 instrument : `lsst.obs.base.Instrument` 

62 The class associated with this instrument. 

63 """ 

64 ... 

65 

66 @classmethod 

67 def tearDownClass(cls): 

68 del cls._butler 

69 

70 @classmethod 

71 def setUpClass(cls): 

72 cls.data_dir = os.path.normpath(os.path.join(cls.DATAROOT, cls.instrumentDir)) 

73 # Protection against the base class values being used 

74 if not os.path.exists(cls.data_dir): 

75 raise unittest.SkipTest(f"Data directory {cls.data_dir} does not exist.") 

76 

77 instrument = cls.getInstrument() 

78 # Assume the test repos use the defaults. 

79 collections = [instrument.makeUnboundedCalibrationRunName(), 

80 instrument.makeDefaultRawIngestRunName(), 

81 instrument.makeCalibrationCollectionName()] 

82 cls._butler = lsst.daf.butler.Butler.from_config( 

83 cls.data_dir, collections=collections, instrument=instrument.getName() 

84 ) 

85 cls.enterClassContext(cls._butler) 

86 

87 

88class ObsLsstObsBaseOverrides(lsst.obs.base.tests.ObsTests): 

89 """Specialist butler tests for obs_lsst.""" 

90 

91 def testRawVisitInfo(self): 

92 visitInfo = self.butler.get("raw.visitInfo", self.dataIds["raw"]) 

93 self.assertIsInstance(visitInfo, lsst.afw.image.VisitInfo) 

94 # We should always get a valid date and exposure time 

95 self.assertIsInstance(visitInfo.getDate(), lsst.daf.base.DateTime) 

96 self.assertTrue(visitInfo.getDate().isValid()) 

97 self.assertEqual(visitInfo.getExposureTime(), self.butler_get_data.exptimes["raw"]) 

98 

99 def testRawFilename(self): 

100 uri = self.butler.getURI("raw", dataId=self.dataIds["raw"]) 

101 self.assertEqual(uri.basename(), self.raw_filename)