Coverage for tests/test_utils.py: 100%
44 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-14 10:23 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-14 10:23 +0000
1# This file is part of ap_association.
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/>.
22import tempfile
23import unittest
25import lsst.daf.butler as dafButler
26from lsst.dax.apdb import Apdb, ApdbSql
28from lsst.ap.association.utils import getMidpointFromTimespan, readMpcOrbitsSchema
29from utils_tests import makeExposure, makeRegionTime
32class TestUtils(unittest.TestCase):
34 def test_regionTime_timespan(self):
35 """Check that the midpoint from a RegionTimeInfo matches the time from
36 the visitInfo of the exposure.
37 """
38 exposure = makeExposure()
39 regionTime = makeRegionTime(exposure)
40 visitTime = exposure.visitInfo.date.toAstropy()
41 midpoint = getMidpointFromTimespan(regionTime.timespan)
42 self.assertEqual(visitTime.jd, midpoint.jd)
44 def test_invalidTimespan(self):
45 """Test the error handling of getMidpointFromTimespan.
46 """
47 exposure = makeExposure()
48 visitTime = exposure.visitInfo.date.toAstropy()
49 timespan_no_end = dafButler.Timespan(begin=visitTime, end=dafButler.Timespan.EMPTY)
50 timespan_no_begin = dafButler.Timespan(begin=dafButler.Timespan.EMPTY, end=visitTime)
51 with self.assertRaises(ValueError):
52 getMidpointFromTimespan(timespan_no_end)
53 with self.assertRaises(ValueError):
54 getMidpointFromTimespan(timespan_no_begin)
56 timespan_none_end = dafButler.Timespan(begin=visitTime, end=None)
57 timespan_none_begin = dafButler.Timespan(begin=None, end=visitTime)
58 with self.assertRaises(ValueError):
59 getMidpointFromTimespan(timespan_none_end, allowUnbounded=False)
60 with self.assertRaises(ValueError):
61 getMidpointFromTimespan(timespan_none_begin, allowUnbounded=False)
62 self.assertEqual(getMidpointFromTimespan(timespan_none_begin).jd, visitTime.jd)
63 self.assertEqual(getMidpointFromTimespan(timespan_none_end).jd, visitTime.jd)
65 timespan_none_both = dafButler.Timespan(begin=None, end=None)
66 with self.assertRaises(ValueError):
67 getMidpointFromTimespan(timespan_none_both, allowUnbounded=True)
68 with self.assertRaises(ValueError):
69 getMidpointFromTimespan(timespan_none_both, allowUnbounded=False)
71 def test_readMpcOrbitsSchema(self):
72 """Test reading the mpc_orbits schema from the APDB schema file.
73 """
74 with tempfile.NamedTemporaryFile() as tmpFile:
75 apdb = Apdb.from_config(ApdbSql.init_database(db_url="sqlite:///" + tmpFile.name))
76 table = readMpcOrbitsSchema(apdb)
77 self.assertEqual(table.name, "mpc_orbits")
78 columns = {column.name: column for column in table.columns}
79 self.assertEqual(columns["u_param"].datatype, "int")
80 self.assertTrue(columns["u_param"].nullable)
81 self.assertFalse(columns["id"].nullable)