22from __future__
import annotations
26from abc
import ABC, abstractmethod
27from typing
import Any, Optional, Tuple
33from lsst.sphgeom import Angle, Circle, Region, UnitVector3d
34from .data_factory
import makeObjectCatalog, makeForcedSourceCatalog, 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
58 n_obj_last_columns = 5
65 """Make config class instance used in all tests."""
66 raise NotImplementedError()
70 """Return number of columns for a specified table."""
71 raise NotImplementedError()
75 """Return type of table returned from getDiaObjects method."""
76 raise NotImplementedError()
78 def make_region(self, xyz: Tuple[float, float, float] = (1., 1., -1.)) -> Region:
79 """Make a region to use in tests"""
85 def assert_catalog(self, catalog: Any, rows: int, table: ApdbTables) ->
None:
86 """Validate catalog type and size
91 Expected type of this is ``type``.
93 Expected number of rows
in a catalog.
97 self.assertIsInstance(catalog, pandas.DataFrame)
98 self.assertEqual(catalog.shape[0], rows)
99 self.assertEqual(catalog.shape[1], self.
n_columnsn_columns(table))
102 """Test for makeing APDB schema."""
107 self.assertIsNotNone(apdb.tableDef(ApdbTables.DiaObject))
108 self.assertIsNotNone(apdb.tableDef(ApdbTables.DiaObjectLast))
109 self.assertIsNotNone(apdb.tableDef(ApdbTables.DiaSource))
110 self.assertIsNotNone(apdb.tableDef(ApdbTables.DiaForcedSource))
113 """Test for getting data from empty database.
115 All get() methods should return empty results, only useful
for
116 checking that code
is not broken.
127 res: Optional[pandas.DataFrame]
130 res = apdb.getDiaObjects(region)
134 res = apdb.getDiaSources(region,
None, visit_time)
137 res = apdb.getDiaSources(region, [], visit_time)
141 res = apdb.getDiaSources(region, [1, 2, 3], visit_time)
145 res = apdb.getDiaForcedSources(region, [], visit_time)
146 self.
assert_catalogassert_catalog(res, 0, ApdbTables.DiaForcedSource)
149 res = apdb.getDiaForcedSources(region, [1, 2, 3], visit_time)
150 self.
assert_catalogassert_catalog(res, 0, ApdbTables.DiaForcedSource)
154 with self.assertRaises(NotImplementedError):
155 apdb.getDiaForcedSources(region,
None, visit_time)
157 apdb.getDiaForcedSources(region,
None, visit_time)
158 self.
assert_catalogassert_catalog(res, 0, ApdbTables.DiaForcedSource)
161 """Test for getting data from empty database.
163 All get() methods should return empty DataFrame
or None.
167 config = self.
make_configmake_config(read_sources_months=0,
168 read_forced_sources_months=0)
175 res: Optional[pandas.DataFrame]
178 res = apdb.getDiaObjects(region)
182 res = apdb.getDiaSources(region,
None, visit_time)
183 self.assertIs(res,
None)
186 res = apdb.getDiaSources(region, [], visit_time)
187 self.assertIs(res,
None)
190 res = apdb.getDiaForcedSources(region, [], visit_time)
191 self.assertIs(res,
None)
194 """Store and retrieve DiaObjects."""
208 apdb.store(visit_time, catalog)
211 res = apdb.getDiaObjects(region)
215 """Store and retrieve DiaObject history."""
222 region1 = self.
make_regionmake_region((1., 1., -1.))
223 region2 = self.
make_regionmake_region((-1., -1., -1.))
225 DateTime(
"2021-01-01T00:01:00", DateTime.TAI),
226 DateTime(
"2021-01-01T00:02:00", DateTime.TAI),
227 DateTime(
"2021-01-01T00:03:00", DateTime.TAI),
228 DateTime(
"2021-01-01T00:04:00", DateTime.TAI),
229 DateTime(
"2021-01-01T00:05:00", DateTime.TAI),
230 DateTime(
"2021-01-01T00:06:00", DateTime.TAI),
231 DateTime(
"2021-03-01T00:01:00", DateTime.TAI),
232 DateTime(
"2021-03-01T00:02:00", DateTime.TAI),
234 end_time =
DateTime(
"2021-03-02T00:00:00", DateTime.TAI)
238 apdb.store(visit_time[0], catalog1)
239 apdb.store(visit_time[2], catalog1)
240 apdb.store(visit_time[4], catalog1)
241 apdb.store(visit_time[6], catalog1)
243 apdb.store(visit_time[1], catalog2)
244 apdb.store(visit_time[3], catalog2)
245 apdb.store(visit_time[5], catalog2)
246 apdb.store(visit_time[7], catalog2)
249 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-01-01T00:00:00", DateTime.TAI), end_time)
250 self.
assert_catalogassert_catalog(res, nobj * 8, ApdbTables.DiaObject)
252 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-01-01T00:01:00", DateTime.TAI), end_time)
253 self.
assert_catalogassert_catalog(res, nobj * 8, ApdbTables.DiaObject)
255 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-01-01T00:01:01", DateTime.TAI), end_time)
256 self.
assert_catalogassert_catalog(res, nobj * 7, ApdbTables.DiaObject)
258 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-01-01T00:02:30", DateTime.TAI), end_time)
259 self.
assert_catalogassert_catalog(res, nobj * 6, ApdbTables.DiaObject)
261 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-01-01T00:05:00", DateTime.TAI), end_time)
262 self.
assert_catalogassert_catalog(res, nobj * 4, ApdbTables.DiaObject)
264 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-01-01T00:06:30", DateTime.TAI), end_time)
265 self.
assert_catalogassert_catalog(res, nobj * 2, ApdbTables.DiaObject)
267 res = apdb.getDiaObjectsHistory(
DateTime(
"2021-03-01T00:02:00.001", DateTime.TAI), end_time)
270 res = apdb.getDiaObjectsHistory(
271 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
272 DateTime(
"2021-01-01T00:06:01", DateTime.TAI),
274 self.
assert_catalogassert_catalog(res, nobj * 6, ApdbTables.DiaObject)
276 res = apdb.getDiaObjectsHistory(
277 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
278 DateTime(
"2021-01-01T00:06:00", DateTime.TAI),
280 self.
assert_catalogassert_catalog(res, nobj * 5, ApdbTables.DiaObject)
282 res = apdb.getDiaObjectsHistory(
283 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
284 DateTime(
"2021-01-01T00:01:00", DateTime.TAI),
288 res = apdb.getDiaObjectsHistory(
289 DateTime(
"2021-01-01T00:00:00", DateTime.TAI), end_time, region=region1
291 self.
assert_catalogassert_catalog(res, nobj * 4, ApdbTables.DiaObject)
293 res = apdb.getDiaObjectsHistory(
294 DateTime(
"2021-01-01T00:03:00", DateTime.TAI), end_time, region=region2
296 self.
assert_catalogassert_catalog(res, nobj * 3, ApdbTables.DiaObject)
298 res = apdb.getDiaObjectsHistory(
299 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
300 DateTime(
"2021-01-01T00:03:30", DateTime.TAI),
303 self.
assert_catalogassert_catalog(res, nobj * 2, ApdbTables.DiaObject)
306 """Store and retrieve DiaSources."""
316 oids =
list(objects[
"diaObjectId"])
320 apdb.store(visit_time, objects, sources)
323 res = apdb.getDiaSources(region,
None, visit_time)
324 self.
assert_catalogassert_catalog(res, len(sources), ApdbTables.DiaSource)
327 res = apdb.getDiaSources(region, oids, visit_time)
328 self.
assert_catalogassert_catalog(res, len(sources), ApdbTables.DiaSource)
331 res = apdb.getDiaSources(region, [], visit_time)
335 """Store and retrieve DiaSource history."""
343 region1 = self.
make_regionmake_region((1., 1., -1.))
344 region2 = self.
make_regionmake_region((-1., -1., -1.))
350 (
DateTime(
"2021-01-01T00:01:00", DateTime.TAI), objects1),
351 (
DateTime(
"2021-01-01T00:02:00", DateTime.TAI), objects2),
352 (
DateTime(
"2021-01-01T00:03:00", DateTime.TAI), objects1),
353 (
DateTime(
"2021-01-01T00:04:00", DateTime.TAI), objects2),
354 (
DateTime(
"2021-01-01T00:05:00", DateTime.TAI), objects1),
355 (
DateTime(
"2021-01-01T00:06:00", DateTime.TAI), objects2),
356 (
DateTime(
"2021-03-01T00:01:00", DateTime.TAI), objects1),
357 (
DateTime(
"2021-03-01T00:02:00", DateTime.TAI), objects2),
359 end_time =
DateTime(
"2021-03-02T00:00:00", DateTime.TAI)
362 for visit_time, objects
in visits:
364 apdb.store(visit_time, objects, sources)
368 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-01-01T00:00:00", DateTime.TAI), end_time)
369 self.
assert_catalogassert_catalog(res, nobj * 8, ApdbTables.DiaSource)
371 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-01-01T00:01:00", DateTime.TAI), end_time)
372 self.
assert_catalogassert_catalog(res, nobj * 8, ApdbTables.DiaSource)
374 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-01-01T00:01:01", DateTime.TAI), end_time)
375 self.
assert_catalogassert_catalog(res, nobj * 7, ApdbTables.DiaSource)
377 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-01-01T00:02:30", DateTime.TAI), end_time)
378 self.
assert_catalogassert_catalog(res, nobj * 6, ApdbTables.DiaSource)
380 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-01-01T00:05:00", DateTime.TAI), end_time)
381 self.
assert_catalogassert_catalog(res, nobj * 4, ApdbTables.DiaSource)
383 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-01-01T00:06:30", DateTime.TAI), end_time)
384 self.
assert_catalogassert_catalog(res, nobj * 2, ApdbTables.DiaSource)
386 res = apdb.getDiaSourcesHistory(
DateTime(
"2021-03-01T00:02:00.001", DateTime.TAI), end_time)
389 res = apdb.getDiaSourcesHistory(
390 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
391 DateTime(
"2021-01-01T00:06:01", DateTime.TAI),
393 self.
assert_catalogassert_catalog(res, nobj * 6, ApdbTables.DiaSource)
395 res = apdb.getDiaSourcesHistory(
396 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
397 DateTime(
"2021-01-01T00:06:00", DateTime.TAI),
399 self.
assert_catalogassert_catalog(res, nobj * 5, ApdbTables.DiaSource)
401 res = apdb.getDiaSourcesHistory(
402 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
403 DateTime(
"2021-01-01T00:01:00", DateTime.TAI),
407 res = apdb.getDiaSourcesHistory(
408 DateTime(
"2021-01-01T00:00:00", DateTime.TAI), end_time, region=region1
410 self.
assert_catalogassert_catalog(res, nobj * 4, ApdbTables.DiaSource)
412 res = apdb.getDiaSourcesHistory(
413 DateTime(
"2021-01-01T00:03:00", DateTime.TAI), end_time, region=region2
415 self.
assert_catalogassert_catalog(res, nobj * 3, ApdbTables.DiaSource)
417 res = apdb.getDiaSourcesHistory(
418 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
419 DateTime(
"2021-01-01T00:03:30", DateTime.TAI),
422 self.
assert_catalogassert_catalog(res, nobj * 2, ApdbTables.DiaSource)
425 """Store and retrieve DiaForcedSources."""
436 oids =
list(objects[
"diaObjectId"])
439 apdb.store(visit_time, objects, forced_sources=catalog)
442 res = apdb.getDiaForcedSources(region, oids, visit_time)
443 self.
assert_catalogassert_catalog(res, len(catalog), ApdbTables.DiaForcedSource)
446 res = apdb.getDiaForcedSources(region, [], visit_time)
447 self.
assert_catalogassert_catalog(res, 0, ApdbTables.DiaForcedSource)
450 """Store and retrieve DiaForcedSource history."""
458 region1 = self.
make_regionmake_region((1., 1., -1.))
459 region2 = self.
make_regionmake_region((-1., -1., -1.))
465 (
DateTime(
"2021-01-01T00:01:00", DateTime.TAI), objects1),
466 (
DateTime(
"2021-01-01T00:02:00", DateTime.TAI), objects2),
467 (
DateTime(
"2021-01-01T00:03:00", DateTime.TAI), objects1),
468 (
DateTime(
"2021-01-01T00:04:00", DateTime.TAI), objects2),
469 (
DateTime(
"2021-01-01T00:05:00", DateTime.TAI), objects1),
470 (
DateTime(
"2021-01-01T00:06:00", DateTime.TAI), objects2),
471 (
DateTime(
"2021-03-01T00:01:00", DateTime.TAI), objects1),
472 (
DateTime(
"2021-03-01T00:02:00", DateTime.TAI), objects2),
474 end_time =
DateTime(
"2021-03-02T00:00:00", DateTime.TAI)
477 for visit_time, objects
in visits:
479 apdb.store(visit_time, objects, forced_sources=sources)
483 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-01-01T00:00:00", DateTime.TAI), end_time)
484 self.
assert_catalogassert_catalog(res, nobj * 8, ApdbTables.DiaForcedSource)
486 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-01-01T00:01:00", DateTime.TAI), end_time)
487 self.
assert_catalogassert_catalog(res, nobj * 8, ApdbTables.DiaForcedSource)
489 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-01-01T00:01:01", DateTime.TAI), end_time)
490 self.
assert_catalogassert_catalog(res, nobj * 7, ApdbTables.DiaForcedSource)
492 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-01-01T00:02:30", DateTime.TAI), end_time)
493 self.
assert_catalogassert_catalog(res, nobj * 6, ApdbTables.DiaForcedSource)
495 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-01-01T00:05:00", DateTime.TAI), end_time)
496 self.
assert_catalogassert_catalog(res, nobj * 4, ApdbTables.DiaForcedSource)
498 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-01-01T00:06:30", DateTime.TAI), end_time)
499 self.
assert_catalogassert_catalog(res, nobj * 2, ApdbTables.DiaForcedSource)
501 res = apdb.getDiaForcedSourcesHistory(
DateTime(
"2021-03-01T00:02:00.001", DateTime.TAI), end_time)
502 self.
assert_catalogassert_catalog(res, 0, ApdbTables.DiaForcedSource)
504 res = apdb.getDiaForcedSourcesHistory(
505 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
506 DateTime(
"2021-01-01T00:06:01", DateTime.TAI),
508 self.
assert_catalogassert_catalog(res, nobj * 6, ApdbTables.DiaForcedSource)
510 res = apdb.getDiaForcedSourcesHistory(
511 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
512 DateTime(
"2021-01-01T00:06:00", DateTime.TAI),
514 self.
assert_catalogassert_catalog(res, nobj * 5, ApdbTables.DiaForcedSource)
516 res = apdb.getDiaForcedSourcesHistory(
517 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
518 DateTime(
"2021-01-01T00:01:00", DateTime.TAI),
520 self.
assert_catalogassert_catalog(res, 0, ApdbTables.DiaForcedSource)
522 res = apdb.getDiaForcedSourcesHistory(
523 DateTime(
"2021-01-01T00:00:00", DateTime.TAI), end_time, region=region1
526 self.
assert_catalogassert_catalog(res, rows, ApdbTables.DiaForcedSource)
528 res = apdb.getDiaForcedSourcesHistory(
529 DateTime(
"2021-01-01T00:03:00", DateTime.TAI), end_time, region=region2
532 self.
assert_catalogassert_catalog(res, rows, ApdbTables.DiaForcedSource)
534 res = apdb.getDiaForcedSourcesHistory(
535 DateTime(
"2021-01-01T00:00:00", DateTime.TAI),
536 DateTime(
"2021-01-01T00:03:30", DateTime.TAI),
540 self.
assert_catalogassert_catalog(res, rows, ApdbTables.DiaForcedSource)
543 """Store and retrieve SSObjects."""
554 apdb.storeSSObjects(catalog)
557 res = apdb.getSSObjects()
558 self.
assert_catalogassert_catalog(res, len(catalog), ApdbTables.SSObject)
562 apdb.storeSSObjects(catalog)
563 res = apdb.getSSObjects()
565 self.assertEqual(len(res[res[
"flags"] == 1]), 50)
566 self.assertEqual(len(res[res[
"flags"] == 2]), 100)
569 """Reassign DiaObjects."""
579 oids =
list(objects[
"diaObjectId"])
581 apdb.store(visit_time, objects, sources)
584 apdb.storeSSObjects(catalog)
587 res = apdb.getDiaSources(region, oids, visit_time)
588 self.
assert_catalogassert_catalog(res, len(sources), ApdbTables.DiaSource)
590 apdb.reassignDiaSources({1: 1, 2: 2, 5: 5})
591 res = apdb.getDiaSources(region, oids, visit_time)
592 self.
assert_catalogassert_catalog(res, len(sources) - 3, ApdbTables.DiaSource)
594 with self.assertRaisesRegex(ValueError,
r"do not exist.*\D1000"):
595 apdb.reassignDiaSources({1000: 1, 7: 3, })
596 self.
assert_catalogassert_catalog(res, len(sources) - 3, ApdbTables.DiaSource)
599 """Test for time filtering of DiaSources.
607 src_time1 =
DateTime(
"2021-01-01T00:00:00", DateTime.TAI)
608 src_time2 =
DateTime(
"2021-01-01T00:00:02", DateTime.TAI)
609 visit_time0 =
DateTime(
"2021-12-26T23:59:59", DateTime.TAI)
610 visit_time1 =
DateTime(
"2021-12-27T00:00:01", DateTime.TAI)
611 visit_time2 =
DateTime(
"2021-12-27T00:00:03", DateTime.TAI)
614 oids =
list(objects[
"diaObjectId"])
616 apdb.store(src_time1, objects, sources)
619 apdb.store(src_time2, objects, sources)
622 res = apdb.getDiaSources(region, oids, src_time2)
626 res = apdb.getDiaSources(region, oids, visit_time0)
630 res = apdb.getDiaSources(region, oids, visit_time1)
634 res = apdb.getDiaSources(region, oids, visit_time2)
638 """Test for time filtering of DiaForcedSources.
645 src_time1 = DateTime("2021-01-01T00:00:00", DateTime.TAI)
646 src_time2 =
DateTime(
"2021-01-01T00:00:02", DateTime.TAI)
647 visit_time0 =
DateTime(
"2021-12-26T23:59:59", DateTime.TAI)
648 visit_time1 =
DateTime(
"2021-12-27T00:00:01", DateTime.TAI)
649 visit_time2 =
DateTime(
"2021-12-27T00:00:03", DateTime.TAI)
652 oids =
list(objects[
"diaObjectId"])
654 apdb.store(src_time1, objects, forced_sources=sources)
657 apdb.store(src_time2, objects, forced_sources=sources)
660 res = apdb.getDiaForcedSources(region, oids, src_time2)
661 self.
assert_catalogassert_catalog(res, 200, ApdbTables.DiaForcedSource)
664 res = apdb.getDiaForcedSources(region, oids, visit_time0)
665 self.
assert_catalogassert_catalog(res, 200, ApdbTables.DiaForcedSource)
668 res = apdb.getDiaForcedSources(region, oids, visit_time1)
669 self.
assert_catalogassert_catalog(res, 100, ApdbTables.DiaForcedSource)
672 res = apdb.getDiaForcedSources(region, oids, visit_time2)
673 self.
assert_catalogassert_catalog(res, 0, ApdbTables.DiaForcedSource)
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
Apdb make_apdb(ApdbConfig config)
pandas.DataFrame makeSourceCatalog(pandas.DataFrame objects, DateTime visit_time, int start_id=0, int ccdVisitId=1)
pandas.DataFrame makeForcedSourceCatalog(pandas.DataFrame objects, DateTime visit_time, int ccdVisitId=1)
pandas.DataFrame makeObjectCatalog(Region region, int count, DateTime visit_time, *int start_id=1)
pandas.DataFrame makeSSObjectCatalog(int count, int start_id=1, int flags=0)