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# This file is part of daf_butler. 

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 

22import unittest 

23import itertools 

24import warnings 

25 

26import astropy.time 

27import astropy.utils.exceptions 

28 

29from lsst.daf.butler import Timespan 

30 

31 

32class TimespanTestCase(unittest.TestCase): 

33 """Tests for the `Timespan` class. 

34 

35 Test coverage for the `TimespanDatabaseRepresentation` classes is handled 

36 by the tests for `Database` and its subclasses. 

37 """ 

38 

39 def setUp(self): 

40 start = astropy.time.Time('2020-01-01T00:00:00', format="isot", scale="tai") 

41 offset = astropy.time.TimeDelta(60, format="sec") 

42 self.timestamps = [start + offset*n for n in range(3)] 

43 self.timespans = [Timespan(begin=None, end=None)] 

44 self.timespans.extend(Timespan(begin=None, end=t) for t in self.timestamps) 

45 self.timespans.extend(Timespan(begin=t, end=None) for t in self.timestamps) 

46 self.timespans.extend(Timespan(begin=a, end=b) for a, b in itertools.combinations(self.timestamps, 2)) 

47 

48 def testStrings(self): 

49 """Test __str__ against expected values and __repr__ with eval 

50 round-tripping. 

51 """ 

52 for ts in self.timespans: 

53 # Uncomment the next line and run this test directly for the most 

54 # important test: human inspection. 

55 # print(str(ts), repr(ts)) 

56 self.assertIn(", ", str(ts)) 

57 self.assertTrue(str(ts).endswith(")")) 

58 if ts.begin is None: 

59 self.assertTrue(str(ts).startswith("(-∞, ")) 

60 else: 

61 self.assertTrue(str(ts).startswith(f"[{ts.begin}, ")) 

62 if ts.end is None: 

63 self.assertTrue(str(ts).endswith(", ∞)")) 

64 else: 

65 self.assertTrue(str(ts).endswith(f", {ts.end})")) 

66 self.assertEqual(eval(repr(ts)), ts) 

67 

68 def testOperationConsistency(self): 

69 """Test that overlaps, intersection, and difference are consistent. 

70 """ 

71 for a, b in itertools.combinations_with_replacement(self.timespans, 2): 

72 with self.subTest(a=str(a), b=str(b)): 

73 c1 = a.intersection(b) 

74 c2 = b.intersection(a) 

75 diffs1 = tuple(a.difference(b)) 

76 diffs2 = tuple(b.difference(a)) 

77 if a == b: 

78 self.assertFalse(diffs1) 

79 self.assertFalse(diffs2) 

80 else: 

81 for t in diffs1: 

82 self.assertTrue(a.overlaps(t)) 

83 self.assertFalse(b.overlaps(t)) 

84 for t in diffs2: 

85 self.assertTrue(b.overlaps(t)) 

86 self.assertFalse(a.overlaps(t)) 

87 self.assertEqual(c1, c2) 

88 if a.overlaps(b): 

89 self.assertTrue(b.overlaps(a)) 

90 self.assertIsNotNone(c1) 

91 else: 

92 self.assertFalse(b.overlaps(a)) 

93 self.assertIsNone(c1) 

94 self.assertEqual(diffs1, (a,)) 

95 self.assertEqual(diffs2, (b,)) 

96 

97 def testPrecision(self): 

98 """Test that we only use nanosecond precision for equality.""" 

99 ts1 = self.timespans[-1] 

100 ts2 = Timespan(begin=ts1.begin + astropy.time.TimeDelta(1e-10, format="sec"), end=ts1.end) 

101 self.assertEqual(ts1, ts2) 

102 

103 self.assertEqual(Timespan(begin=None, end=None), Timespan(begin=None, end=None)) 

104 self.assertEqual(Timespan(begin=None, end=ts1.end), Timespan(begin=None, end=ts1.end)) 

105 

106 ts2 = Timespan(begin=ts1.begin + astropy.time.TimeDelta(1e-8, format="sec"), end=ts1.end) 

107 self.assertNotEqual(ts1, ts2) 

108 

109 ts2 = Timespan(begin=None, end=ts1.end) 

110 self.assertNotEqual(ts1, ts2) 

111 

112 t1 = Timespan(begin=astropy.time.Time(2456461.0, val2=0.06580758101851847, format="jd", scale="tai"), 

113 end=astropy.time.Time(2456461.0, val2=0.06617994212962963, format="jd", scale="tai")) 

114 t2 = Timespan(begin=astropy.time.Time(2456461.0, val2=0.06580758101851858, format="jd", scale="tai"), 

115 end=astropy.time.Time(2456461.0, val2=0.06617994212962963, format="jd", scale="tai")) 

116 self.assertEqual(t1, t2) 

117 

118 # Ensure that == and != work properly 

119 self.assertTrue(t1 == t2, f"Equality of {t1} and {t2}") 

120 self.assertFalse(t1 != t2, f"Check != is false for {t1} and {t2}") 

121 

122 def testTimescales(self): 

123 """Test time scale conversion occurs on comparison.""" 

124 ts1 = Timespan(begin=astropy.time.Time('2013-06-17 13:34:45.775000', scale='tai', format='iso'), 

125 end=astropy.time.Time('2013-06-17 13:35:17.947000', scale='tai', format='iso')) 

126 ts2 = Timespan(begin=astropy.time.Time('2013-06-17T13:34:10.775', scale='utc', format='isot'), 

127 end=astropy.time.Time('2013-06-17T13:34:42.947', scale='utc', format='isot')) 

128 self.assertEqual(ts1, ts2, f"Compare {ts1} with {ts2}") 

129 

130 def testFuture(self): 

131 """Check that we do not get warnings from future dates.""" 

132 

133 # Astropy will give "dubious year" for UTC five years in the future 

134 # so hide these expected warnings from the test output 

135 with warnings.catch_warnings(): 

136 warnings.simplefilter("ignore", category=astropy.utils.exceptions.AstropyWarning) 

137 ts1 = Timespan(begin=astropy.time.Time('2213-06-17 13:34:45.775000', scale='utc', format='iso'), 

138 end=astropy.time.Time('2213-06-17 13:35:17.947000', scale='utc', format='iso')) 

139 ts2 = Timespan(begin=astropy.time.Time('2213-06-17 13:34:45.775000', scale='utc', format='iso'), 

140 end=astropy.time.Time('2213-06-17 13:35:17.947000', scale='utc', format='iso')) 

141 

142 # unittest can't test for no warnings so we run the test and 

143 # trigger our own warning and count all the warnings 

144 with self.assertWarns(Warning) as cm: 

145 self.assertEqual(ts1, ts2) 

146 warnings.warn("deliberate") 

147 self.assertEqual(str(cm.warning), "deliberate") 

148 

149 

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

151 unittest.main()