Coverage for tests/test_timespan.py : 13%

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/>.
22import unittest
23import itertools
25import astropy.time
27from lsst.daf.butler import Timespan
30class TimespanTestCase(unittest.TestCase):
31 """Tests for the `Timespan` class.
33 Test coverage for the `DatabaseTimespanRepresentation` classes is handled
34 by the tests for `Database` and its subclasses.
35 """
37 def setUp(self):
38 start = astropy.time.Time('2020-01-01T00:00:00', format="isot", scale="tai")
39 offset = astropy.time.TimeDelta(60, format="sec")
40 self.timestamps = [start + offset*n for n in range(3)]
41 self.timespans = [Timespan(begin=None, end=None)]
42 self.timespans.extend(Timespan(begin=None, end=t) for t in self.timestamps)
43 self.timespans.extend(Timespan(begin=t, end=None) for t in self.timestamps)
44 self.timespans.extend(Timespan(begin=a, end=b) for a, b in itertools.combinations(self.timestamps, 2))
46 def testStrings(self):
47 """Test __str__ against expected values and __repr__ with eval
48 round-tripping.
49 """
50 for ts in self.timespans:
51 # Uncomment the next line and run this test directly for the most
52 # important test: human inspection.
53 # print(str(ts), repr(ts))
54 self.assertIn(", ", str(ts))
55 self.assertTrue(str(ts).endswith(")"))
56 if ts.begin is None:
57 self.assertTrue(str(ts).startswith("(-∞, "))
58 else:
59 self.assertTrue(str(ts).startswith(f"[{ts.begin}, "))
60 if ts.end is None:
61 self.assertTrue(str(ts).endswith(", ∞)"))
62 else:
63 self.assertTrue(str(ts).endswith(f", {ts.end})"))
64 self.assertEqual(eval(repr(ts)), ts)
66 def testOperationConsistency(self):
67 """Test that overlaps, intersection, and difference are consistent.
68 """
69 for a, b in itertools.combinations_with_replacement(self.timespans, 2):
70 with self.subTest(a=str(a), b=str(b)):
71 c1 = a.intersection(b)
72 c2 = b.intersection(a)
73 diffs1 = tuple(a.difference(b))
74 diffs2 = tuple(b.difference(a))
75 if a == b:
76 self.assertFalse(diffs1)
77 self.assertFalse(diffs2)
78 else:
79 for t in diffs1:
80 self.assertTrue(a.overlaps(t))
81 self.assertFalse(b.overlaps(t))
82 for t in diffs2:
83 self.assertTrue(b.overlaps(t))
84 self.assertFalse(a.overlaps(t))
85 self.assertEqual(c1, c2)
86 if a.overlaps(b):
87 self.assertTrue(b.overlaps(a))
88 self.assertIsNotNone(c1)
89 else:
90 self.assertFalse(b.overlaps(a))
91 self.assertIsNone(c1)
92 self.assertEqual(diffs1, (a,))
93 self.assertEqual(diffs2, (b,))
95 def testPrecision(self):
96 """Test that we only use nanosecond precision for equality."""
97 ts1 = self.timespans[-1]
98 ts2 = Timespan(begin=ts1.begin + astropy.time.TimeDelta(1e-10, format="sec"), end=ts1.end)
99 self.assertEqual(ts1, ts2)
101 self.assertEqual(Timespan(begin=None, end=None), Timespan(begin=None, end=None))
102 self.assertEqual(Timespan(begin=None, end=ts1.end), Timespan(begin=None, end=ts1.end))
104 ts2 = Timespan(begin=ts1.begin + astropy.time.TimeDelta(1e-8, format="sec"), end=ts1.end)
105 self.assertNotEqual(ts1, ts2)
107 ts2 = Timespan(begin=None, end=ts1.end)
108 self.assertNotEqual(ts1, ts2)
110 t1 = Timespan(begin=astropy.time.Time(2456461.0, val2=0.06580758101851847, format="jd", scale="tai"),
111 end=astropy.time.Time(2456461.0, val2=0.06617994212962963, format="jd", scale="tai"))
112 t2 = Timespan(begin=astropy.time.Time(2456461.0, val2=0.06580758101851858, format="jd", scale="tai"),
113 end=astropy.time.Time(2456461.0, val2=0.06617994212962963, format="jd", scale="tai"))
114 self.assertEqual(t1, t2)
116 # Ensure that == and != work properly
117 self.assertTrue(t1 == t2, f"Equality of {t1} and {t2}")
118 self.assertFalse(t1 != t2, f"Check != is false for {t1} and {t2}")
120 def testTimescales(self):
121 """Test time scale conversion occurs on comparison."""
122 ts1 = Timespan(begin=astropy.time.Time('2013-06-17 13:34:45.775000', scale='tai', format='iso'),
123 end=astropy.time.Time('2013-06-17 13:35:17.947000', scale='tai', format='iso'))
124 ts2 = Timespan(begin=astropy.time.Time('2013-06-17T13:34:10.775', scale='utc', format='isot'),
125 end=astropy.time.Time('2013-06-17T13:34:42.947', scale='utc', format='isot'))
126 self.assertEqual(ts1, ts2, f"Compare {ts1} with {ts2}")
129if __name__ == "__main__": 129 ↛ 130line 129 didn't jump to line 130, because the condition on line 129 was never true
130 unittest.main()