22from __future__
import annotations
26from abc
import ABC, abstractmethod
27from typing
import Any, Dict, Optional, Tuple
32from lsst.sphgeom import Angle, Circle, Region, UnitVector3d
34from .data_factory
import makeForcedSourceCatalog, makeObjectCatalog, makeSourceCatalog, makeSSObjectCatalog
38 """Base class for Apdb tests that can be specialized for concrete
41 This can only be used as a mixin
class for
a unittest.TestCase and it
42 calls various
assert methods.
45 time_partition_tables = False
46 visit_time =
DateTime(
"2021-01-01T00:00:00", DateTime.TAI)
48 fsrc_requires_id_list =
False
49 """Should be set to True if getDiaForcedSources requires object IDs"""
51 fsrc_history_region_filtering =
False
52 """Should be set to True if forced sources history support region-based
56 extra_object_columns: Dict[str, Any] = {}
57 """Additional columns with values to add to DiaObject catalog."""
61 n_obj_last_columns = 5
68 """Make config class instance used in all tests."""
69 raise NotImplementedError()
73 """Return number of columns for a specified table."""
74 raise NotImplementedError()
78 """Return type of table returned from getDiaObjects method."""
79 raise NotImplementedError()
81 def make_region(self, xyz: Tuple[float, float, float] = (1., 1., -1.)) -> Region:
82 """Make a region to use in tests"""
88 def assert_catalog(self, catalog: Any, rows: int, table: ApdbTables) ->
None:
89 """Validate catalog type and size
94 Expected type of this is ``type``.
96 Expected number of rows
in a catalog.
100 self.assertIsInstance(catalog, pandas.DataFrame)
101 self.assertEqual(catalog.shape[0], rows)
102 self.assertEqual(catalog.shape[1], self.
n_columns(table))
105 """Test for makeing APDB schema."""
107 apdb = make_apdb(config)
110 self.assertIsNotNone(apdb.tableDef(ApdbTables.DiaObject))
111 self.assertIsNotNone(apdb.tableDef(ApdbTables.DiaObjectLast))
112 self.assertIsNotNone(apdb.tableDef(ApdbTables.DiaSource))
113 self.assertIsNotNone(apdb.tableDef(ApdbTables.DiaForcedSource))
116 """Test for getting data from empty database.
118 All get() methods should return empty results, only useful
for
119 checking that code
is not broken.
124 apdb = make_apdb(config)
130 res: Optional[pandas.DataFrame]
133 res = apdb.getDiaObjects(region)
137 res = apdb.getDiaSources(region,
None, visit_time)
140 res = apdb.getDiaSources(region, [], visit_time)
144 res = apdb.getDiaSources(region, [1, 2, 3], visit_time)
148 res = apdb.getDiaForcedSources(region, [], visit_time)
152 res = apdb.getDiaForcedSources(region, [1, 2, 3], visit_time)
157 with self.assertRaises(NotImplementedError):
158 apdb.getDiaForcedSources(region,
None, visit_time)
160 apdb.getDiaForcedSources(region,
None, visit_time)
164 """Test for getting data from empty database.
166 All get() methods should return empty DataFrame
or None.
171 read_forced_sources_months=0)
172 apdb = make_apdb(config)
178 res: Optional[pandas.DataFrame]
181 res = apdb.getDiaObjects(region)
185 res = apdb.getDiaSources(region,
None, visit_time)
186 self.assertIs(res,
None)
189 res = apdb.getDiaSources(region, [], visit_time)
190 self.assertIs(res,
None)
193 res = apdb.getDiaForcedSources(region, [], visit_time)
194 self.assertIs(res,
None)
197 """Store and retrieve DiaObjects."""
201 apdb = make_apdb(config)
208 catalog = makeObjectCatalog(region, 100, visit_time, **self.extra_object_columns)
211 apdb.store(visit_time, catalog)
214 res = apdb.getDiaObjects(region)
218 """Store and retrieve DiaObject history."""
222 apdb = make_apdb(config)
228 DateTime(
"2021-01-01T00:01:00", DateTime.TAI),
229 DateTime(
"2021-01-01T00:02:00", DateTime.TAI),
230 DateTime(
"2021-01-01T00:03:00", DateTime.TAI),
231 DateTime(
"2021-01-01T00:04:00", DateTime.TAI),
232 DateTime(
"2021-01-01T00:05:00", DateTime.TAI),
233 DateTime(
"2021-01-01T00:06:00", DateTime.TAI),
234 DateTime(
"2021-03-01T00:01:00", DateTime.TAI),
235 DateTime(
"2021-03-01T00:02:00", DateTime.TAI),
237 end_time =
DateTime(
"2021-03-02T00:00:00", DateTime.TAI)
240 catalog1 = makeObjectCatalog(region1, nobj, visit_time[0], **self.extra_object_columns)
241 apdb.store(visit_time[0], catalog1)
242 apdb.store(visit_time[2], catalog1)
243 apdb.store(visit_time[4], catalog1)
244 apdb.store(visit_time[6], catalog1)
245 catalog2 = makeObjectCatalog(
246 region2, nobj, visit_time[1], start_id=nobj*2, **self.extra_object_columns
248 apdb.store(visit_time[1], catalog2)
249 apdb.store(visit_time[3], catalog2)
250 apdb.store(visit_time[5], catalog2)
251 apdb.store(visit_time[7], catalog2)
254 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-01-01T00:00:00", DateTime.TAI), end_time)
257 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-01-01T00:01:00", DateTime.TAI), end_time)
260 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-01-01T00:01:01", DateTime.TAI), end_time)
263 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-01-01T00:02:30", DateTime.TAI), end_time)
266 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-01-01T00:05:00", DateTime.TAI), end_time)
269 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-01-01T00:06:30", DateTime.TAI), end_time)
272 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-03-01T00:02:00.001", DateTime.TAI), end_time)
275 res = apdb.getDiaObjectsHistory(
276 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
277 DateTime(
"2021-01-01T00:06:01", DateTime.TAI),
281 res = apdb.getDiaObjectsHistory(
282 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
283 DateTime(
"2021-01-01T00:06:00", DateTime.TAI),
287 res = apdb.getDiaObjectsHistory(
288 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
289 DateTime(
"2021-01-01T00:01:00", DateTime.TAI),
293 res = apdb.getDiaObjectsHistory(
294 DateTime(
"2021-01-01T00:00:00", DateTime.TAI), end_time, region=region1
298 res = apdb.getDiaObjectsHistory(
299 DateTime(
"2021-01-01T00:03:00", DateTime.TAI), end_time, region=region2
303 res = apdb.getDiaObjectsHistory(
304 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
305 DateTime(
"2021-01-01T00:03:30", DateTime.TAI),
311 """Store and retrieve DiaSources."""
313 apdb = make_apdb(config)
320 objects = makeObjectCatalog(region, 100, visit_time, **self.extra_object_columns)
321 oids =
list(objects[
"diaObjectId"])
322 sources = makeSourceCatalog(objects, visit_time)
325 apdb.store(visit_time, objects, sources)
328 res = apdb.getDiaSources(region,
None, visit_time)
332 res = apdb.getDiaSources(region, oids, visit_time)
336 res = apdb.getDiaSources(region, [], visit_time)
340 """Store and retrieve DiaSource history."""
344 apdb = make_apdb(config)
351 objects1 = makeObjectCatalog(region1, nobj, visit_time, **self.extra_object_columns)
352 objects2 = makeObjectCatalog(region2, nobj, visit_time, start_id=nobj*2, **self.extra_object_columns)
355 (
DateTime(
"2021-01-01T00:01:00", DateTime.TAI), objects1),
356 (
DateTime(
"2021-01-01T00:02:00", DateTime.TAI), objects2),
357 (
DateTime(
"2021-01-01T00:03:00", DateTime.TAI), objects1),
358 (
DateTime(
"2021-01-01T00:04:00", DateTime.TAI), objects2),
359 (
DateTime(
"2021-01-01T00:05:00", DateTime.TAI), objects1),
360 (
DateTime(
"2021-01-01T00:06:00", DateTime.TAI), objects2),
361 (
DateTime(
"2021-03-01T00:01:00", DateTime.TAI), objects1),
362 (
DateTime(
"2021-03-01T00:02:00", DateTime.TAI), objects2),
364 end_time =
DateTime(
"2021-03-02T00:00:00", DateTime.TAI)
367 for visit_time, objects
in visits:
368 sources = makeSourceCatalog(objects, visit_time, start_id=start_id)
369 apdb.store(visit_time, objects, sources)
373 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-01-01T00:00:00", DateTime.TAI), end_time)
376 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-01-01T00:01:00", DateTime.TAI), end_time)
379 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-01-01T00:01:01", DateTime.TAI), end_time)
382 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-01-01T00:02:30", DateTime.TAI), end_time)
385 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-01-01T00:05:00", DateTime.TAI), end_time)
388 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-01-01T00:06:30", DateTime.TAI), end_time)
391 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-03-01T00:02:00.001", DateTime.TAI), end_time)
394 res = apdb.getDiaSourcesHistory(
395 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
396 DateTime(
"2021-01-01T00:06:01", DateTime.TAI),
400 res = apdb.getDiaSourcesHistory(
401 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
402 DateTime(
"2021-01-01T00:06:00", DateTime.TAI),
406 res = apdb.getDiaSourcesHistory(
407 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
408 DateTime(
"2021-01-01T00:01:00", DateTime.TAI),
412 res = apdb.getDiaSourcesHistory(
413 DateTime(
"2021-01-01T00:00:00", DateTime.TAI), end_time, region=region1
417 res = apdb.getDiaSourcesHistory(
418 DateTime(
"2021-01-01T00:03:00", DateTime.TAI), end_time, region=region2
422 res = apdb.getDiaSourcesHistory(
423 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
424 DateTime(
"2021-01-01T00:03:30", DateTime.TAI),
430 """Store and retrieve DiaForcedSources."""
433 apdb = make_apdb(config)
440 objects = makeObjectCatalog(region, 100, visit_time, **self.extra_object_columns)
441 oids =
list(objects[
"diaObjectId"])
442 catalog = makeForcedSourceCatalog(objects, visit_time)
444 apdb.store(visit_time, objects, forced_sources=catalog)
447 res = apdb.getDiaForcedSources(region, oids, visit_time)
448 self.
assert_catalog(res, len(catalog), ApdbTables.DiaForcedSource)
451 res = apdb.getDiaForcedSources(region, [], visit_time)
455 """Store and retrieve DiaForcedSource history."""
459 apdb = make_apdb(config)
466 objects1 = makeObjectCatalog(region1, nobj, visit_time, **self.extra_object_columns)
467 objects2 = makeObjectCatalog(region2, nobj, visit_time, start_id=nobj*2, **self.extra_object_columns)
470 (
DateTime(
"2021-01-01T00:01:00", DateTime.TAI), objects1),
471 (
DateTime(
"2021-01-01T00:02:00", DateTime.TAI), objects2),
472 (
DateTime(
"2021-01-01T00:03:00", DateTime.TAI), objects1),
473 (
DateTime(
"2021-01-01T00:04:00", DateTime.TAI), objects2),
474 (
DateTime(
"2021-01-01T00:05:00", DateTime.TAI), objects1),
475 (
DateTime(
"2021-01-01T00:06:00", DateTime.TAI), objects2),
476 (
DateTime(
"2021-03-01T00:01:00", DateTime.TAI), objects1),
477 (
DateTime(
"2021-03-01T00:02:00", DateTime.TAI), objects2),
479 end_time =
DateTime(
"2021-03-02T00:00:00", DateTime.TAI)
482 for visit_time, objects
in visits:
483 sources = makeForcedSourceCatalog(objects, visit_time, ccdVisitId=start_id)
484 apdb.store(visit_time, objects, forced_sources=sources)
488 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-01-01T00:00:00", DateTime.TAI), end_time)
491 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-01-01T00:01:00", DateTime.TAI), end_time)
494 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-01-01T00:01:01", DateTime.TAI), end_time)
497 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-01-01T00:02:30", DateTime.TAI), end_time)
500 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-01-01T00:05:00", DateTime.TAI), end_time)
503 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-01-01T00:06:30", DateTime.TAI), end_time)
506 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-03-01T00:02:00.001", DateTime.TAI), end_time)
509 res = apdb.getDiaForcedSourcesHistory(
510 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
511 DateTime(
"2021-01-01T00:06:01", DateTime.TAI),
515 res = apdb.getDiaForcedSourcesHistory(
516 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
517 DateTime(
"2021-01-01T00:06:00", DateTime.TAI),
521 res = apdb.getDiaForcedSourcesHistory(
522 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
523 DateTime(
"2021-01-01T00:01:00", DateTime.TAI),
527 res = apdb.getDiaForcedSourcesHistory(
528 DateTime(
"2021-01-01T00:00:00", DateTime.TAI), end_time, region=region1
533 res = apdb.getDiaForcedSourcesHistory(
534 DateTime(
"2021-01-01T00:03:00", DateTime.TAI), end_time, region=region2
539 res = apdb.getDiaForcedSourcesHistory(
540 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
541 DateTime(
"2021-01-01T00:03:30", DateTime.TAI),
548 """Store and retrieve SSObjects."""
552 apdb = make_apdb(config)
556 catalog = makeSSObjectCatalog(100, flags=1)
559 apdb.storeSSObjects(catalog)
562 res = apdb.getSSObjects()
566 catalog = makeSSObjectCatalog(100, 51, flags=2)
567 apdb.storeSSObjects(catalog)
568 res = apdb.getSSObjects()
570 self.assertEqual(len(res[res[
"flags"] == 1]), 50)
571 self.assertEqual(len(res[res[
"flags"] == 2]), 100)
574 """Reassign DiaObjects."""
578 apdb = make_apdb(config)
583 objects = makeObjectCatalog(region, 100, visit_time, **self.extra_object_columns)
584 oids =
list(objects[
"diaObjectId"])
585 sources = makeSourceCatalog(objects, visit_time)
586 apdb.store(visit_time, objects, sources)
588 catalog = makeSSObjectCatalog(100)
589 apdb.storeSSObjects(catalog)
592 res = apdb.getDiaSources(region, oids, visit_time)
595 apdb.reassignDiaSources({1: 1, 2: 2, 5: 5})
596 res = apdb.getDiaSources(region, oids, visit_time)
599 with self.assertRaisesRegex(ValueError,
r"do not exist.*\D1000"):
600 apdb.reassignDiaSources({1000: 1, 7: 3, })
604 """Test for time filtering of DiaSources.
607 apdb = make_apdb(config)
612 src_time1 =
DateTime(
"2021-01-01T00:00:00", DateTime.TAI)
613 src_time2 =
DateTime(
"2021-01-01T00:00:02", DateTime.TAI)
614 visit_time0 =
DateTime(
"2021-12-26T23:59:59", DateTime.TAI)
615 visit_time1 =
DateTime(
"2021-12-27T00:00:01", DateTime.TAI)
616 visit_time2 =
DateTime(
"2021-12-27T00:00:03", DateTime.TAI)
618 objects = makeObjectCatalog(region, 100, visit_time0, **self.extra_object_columns)
619 oids =
list(objects[
"diaObjectId"])
620 sources = makeSourceCatalog(objects, src_time1, 0)
621 apdb.store(src_time1, objects, sources)
623 sources = makeSourceCatalog(objects, src_time2, 100)
624 apdb.store(src_time2, objects, sources)
627 res = apdb.getDiaSources(region, oids, src_time2)
631 res = apdb.getDiaSources(region, oids, visit_time0)
635 res = apdb.getDiaSources(region, oids, visit_time1)
639 res = apdb.getDiaSources(region, oids, visit_time2)
643 """Test for time filtering of DiaForcedSources.
646 apdb = make_apdb(config)
650 src_time1 = DateTime("2021-01-01T00:00:00", DateTime.TAI)
651 src_time2 =
DateTime(
"2021-01-01T00:00:02", DateTime.TAI)
652 visit_time0 =
DateTime(
"2021-12-26T23:59:59", DateTime.TAI)
653 visit_time1 =
DateTime(
"2021-12-27T00:00:01", DateTime.TAI)
654 visit_time2 =
DateTime(
"2021-12-27T00:00:03", DateTime.TAI)
656 objects = makeObjectCatalog(region, 100, visit_time0, **self.extra_object_columns)
657 oids =
list(objects[
"diaObjectId"])
658 sources = makeForcedSourceCatalog(objects, src_time1, 1)
659 apdb.store(src_time1, objects, forced_sources=sources)
661 sources = makeForcedSourceCatalog(objects, src_time2, 2)
662 apdb.store(src_time2, objects, forced_sources=sources)
665 res = apdb.getDiaForcedSources(region, oids, src_time2)
669 res = apdb.getDiaForcedSources(region, oids, visit_time0)
673 res = apdb.getDiaForcedSources(region, oids, visit_time1)
677 res = apdb.getDiaForcedSources(region, oids, visit_time2)
Class for handling dates/times, including MJD, UTC, and TAI.
None test_midPointTai_src(self)
int n_columns(self, ApdbTables table)
None test_storeSSObjects(self)
ApdbConfig make_config(self, **Any kwargs)
None test_empty_gets(self)
None assert_catalog(self, Any catalog, int rows, ApdbTables table)
None test_storeSources(self)
None test_forcedSourceHistory(self)
None test_makeSchema(self)
None test_empty_gets_0months(self)
None test_objectHistory(self)
None test_storeForcedSources(self)
Region make_region(self, Tuple[float, float, float] xyz=(1., 1., -1.))
None test_midPointTai_fsrc(self)
bool fsrc_history_region_filtering
bool fsrc_requires_id_list
None test_reassignObjects(self)
None test_sourceHistory(self)
ApdbTables getDiaObjects_table(self)
None test_storeObjects(self)
Angle represents an angle in radians.
Circle is a circular region on the unit sphere that contains its boundary.
UnitVector3d is a unit vector in ℝ³ with components stored in double precision.
daf::base::PropertyList * list