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,))
96if __name__ == "__main__": 96 ↛ 97line 96 didn't jump to line 97, because the condition on line 96 was never true
97 unittest.main()