Coverage for tests / test_dateTime.py: 33%

41 statements  

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

1# This file is part of summit_utils. 

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"""Test cases for utils.""" 

23 

24import datetime 

25import unittest 

26 

27import astropy 

28from astropy.time import Time 

29 

30import lsst.utils.tests 

31from lsst.summit.utils.dateTime import ( # TODO: add tests for efdTimestampToAstropy, astropyToEfdTimestamp 

32 calcDayOffset, 

33 getDayObsEndTime, 

34 getDayObsForTime, 

35 getDayObsStartTime, 

36) 

37 

38 

39class DateTimeTestCase(lsst.utils.tests.TestCase): 

40 

41 def test_getDayObsAsTimes(self): 

42 """This tests getDayObsStartTime and getDayObsEndTime explicitly, 

43 but the days we loop over are chosen to test calcNextDay() which is 

44 called by getDayObsEndTime(). 

45 """ 

46 for dayObs in ( 

47 20200228, # day before end of Feb on a leap year 

48 20200229, # end of Feb on a leap year 

49 20210227, # day before end of Feb on a non-leap year 

50 20200228, # end of Feb on a non-leap year 

51 20200430, # end of a month with 30 days 

52 20200530, # end of a month with 31 days 

53 20201231, # year rollover 

54 ): 

55 dayStart = getDayObsStartTime(dayObs) 

56 self.assertIsInstance(dayStart, astropy.time.Time) 

57 

58 dayEnd = getDayObsEndTime(dayObs) 

59 self.assertIsInstance(dayStart, astropy.time.Time) 

60 

61 self.assertGreater(dayEnd, dayStart) 

62 self.assertEqual(dayEnd.jd, dayStart.jd + 1) 

63 

64 def test_calcDayOffset(self): 

65 self.assertEqual(calcDayOffset(20230531, 20230601), 1) 

66 self.assertEqual(calcDayOffset(20230531, 20230530), -1) 

67 self.assertEqual(calcDayOffset(20230531, 20230531), 0) 

68 self.assertEqual(calcDayOffset(20231231, 20240101), 1) 

69 self.assertEqual(calcDayOffset(20240228, 20240301), 2) # leap year 

70 self.assertEqual(calcDayOffset(20240229, 20240301), 1) # leap year 

71 self.assertEqual(calcDayOffset(20240228, 20240302), 3) # leap year 

72 

73 self.assertNotEqual(calcDayOffset(20230531, 20230601), -1) 

74 

75 def test_getDayObsForTime(self): 

76 pydate = datetime.datetime(2023, 2, 5, 13, 30, 1) 

77 time = Time(pydate) 

78 dayObs = getDayObsForTime(time) 

79 self.assertEqual(dayObs, 20230205) 

80 

81 pydate = datetime.datetime(2023, 2, 5, 11, 30, 1) 

82 time = Time(pydate) 

83 dayObs = getDayObsForTime(time) 

84 self.assertEqual(dayObs, 20230204) 

85 return 

86 

87 

88class TestMemory(lsst.utils.tests.MemoryTestCase): 

89 pass 

90 

91 

92def setup_module(module): 

93 lsst.utils.tests.init() 

94 

95 

96if __name__ == "__main__": 96 ↛ 97line 96 didn't jump to line 97 because the condition on line 96 was never true

97 lsst.utils.tests.init() 

98 unittest.main()