Coverage for tests/test_utils.py: 100%

46 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-13 09:35 +0000

1# This file is part of ip_diffim. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

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

21 

22 

23import requests 

24from unittest import mock 

25 

26import lsst.afw.cameraGeom.testUtils 

27import lsst.daf.butler 

28from lsst.images import obs_info_from_legacy 

29from lsst.images.tests import get_dp2_exposure_record 

30from lsst.ip.diffim.utils import populate_sattle_visit_cache, record_from_obs_info 

31import lsst.utils.tests 

32 

33from test_detectAndMeasure import makeVisitInfo, MockResponse 

34from utils import makeTestImage 

35 

36 

37class ExposureRecordTest(lsst.utils.tests.TestCase): 

38 """Tests of rebuilding an exposure record from an observation info.""" 

39 

40 def setUp(self): 

41 self.universe = lsst.daf.butler.DimensionUniverse() 

42 self.record = get_dp2_exposure_record(self.universe) 

43 # lsst.images needs per-amplifier raw geometry and a field angle 

44 # transform, which the trivial test detectors do not have. 

45 self.detector = list(lsst.afw.cameraGeom.testUtils.CameraWrapper().camera)[0] 

46 self.obsInfo = self._makeObsInfo(self.record) 

47 

48 def _makeObsInfo(self, record): 

49 """Return the observation info that a converted image built from 

50 ``record`` would carry. 

51 """ 

52 # The conversion requires the visit info and the record to agree 

53 # on the exposure id. 

54 return obs_info_from_legacy(makeVisitInfo(id=record.id), record, self.detector, 

55 detector_exposure_id=12345) 

56 

57 def _rebuild(self): 

58 """Return the record rebuilt from the observation info, with additional 

59 identifiers added. 

60 """ 

61 return record_from_obs_info(self.obsInfo, self.record.instrument, self.record.id, self.universe) 

62 

63 def test_fields(self): 

64 """Every field the conversion reads comes back with the value the 

65 original record had. 

66 """ 

67 record = self._rebuild() 

68 for field in ("instrument", "id", "obs_id", "group", "day_obs", "physical_filter", 

69 "exposure_time", "seq_num", "seq_start", "seq_end", "can_see_sky", "timespan"): 

70 self.assertEqual(getattr(record, field), getattr(self.record, field), msg=field) 

71 self.assertFloatsAlmostEqual(record.azimuth, self.record.azimuth, rtol=1e-14) 

72 self.assertFloatsAlmostEqual(record.zenith_angle, self.record.zenith_angle, rtol=1e-14) 

73 

74 def test_conversion_unchanged(self): 

75 """Converting with the rebuilt record gives the same observation 

76 info as converting with the original one. 

77 """ 

78 self.assertEqual(self._makeObsInfo(self._rebuild()), self.obsInfo) 

79 

80 def test_missing_obs_info(self): 

81 with self.assertRaises(ValueError): 

82 record_from_obs_info(None, self.record.instrument, self.record.id, self.universe) 

83 

84 

85class UtilsTest(lsst.utils.tests.TestCase): 

86 

87 def test_populate_sattle(self): 

88 response = MockResponse({}, 200, "success") 

89 visit_info = makeVisitInfo() 

90 with mock.patch('requests.put', return_value=response): 

91 populate_sattle_visit_cache(visit_info) 

92 

93 def test_populate_sattle_raises(self): 

94 response = MockResponse({}, 500, "failure") 

95 visit_info = makeVisitInfo() 

96 with mock.patch('requests.put', return_value=response): 

97 with self.assertRaises(requests.exceptions.HTTPError): 

98 populate_sattle_visit_cache(visit_info) 

99 

100 def test_raise_on_even_kernel(self): 

101 with self.assertRaises(ValueError): 

102 makeTestImage(kernelSize=32)