Coverage for python/lsst/dax/apdb/tests/_apdb.py: 12%

340 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-25 08:49 +0000

1# This file is part of dax_apdb. 

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/>. 

21 

22from __future__ import annotations 

23 

24__all__ = ["ApdbTest"] 

25 

26from abc import ABC, abstractmethod 

27from typing import Any, Optional, Tuple 

28 

29import pandas 

30 

31from lsst.daf.base import DateTime 

32from lsst.dax.apdb import ApdbConfig, ApdbTables, make_apdb 

33from lsst.sphgeom import Angle, Circle, Region, UnitVector3d 

34from .data_factory import makeObjectCatalog, makeForcedSourceCatalog, makeSourceCatalog, makeSSObjectCatalog 

35 

36 

37class ApdbTest(ABC): 

38 """Base class for Apdb tests that can be specialized for concrete 

39 implementation. 

40 

41 This can only be used as a mixin class for a unittest.TestCase and it 

42 calls various assert methods. 

43 """ 

44 

45 time_partition_tables = False 

46 visit_time = DateTime("2021-01-01T00:00:00", DateTime.TAI) 

47 

48 fsrc_requires_id_list = False 

49 """Should be set to True if getDiaForcedSources requires object IDs""" 

50 

51 fsrc_history_region_filtering = False 

52 """Should be set to True if forced sources history support region-based 

53 filtering. 

54 """ 

55 

56 # number of columns as defined in tests/config/schema.yaml 

57 n_obj_columns = 7 

58 n_obj_last_columns = 5 

59 n_src_columns = 9 

60 n_fsrc_columns = 4 

61 n_ssobj_columns = 3 

62 

63 @abstractmethod 

64 def make_config(self, **kwargs: Any) -> ApdbConfig: 

65 """Make config class instance used in all tests.""" 

66 raise NotImplementedError() 

67 

68 @abstractmethod 

69 def n_columns(self, table: ApdbTables) -> int: 

70 """Return number of columns for a specified table.""" 

71 raise NotImplementedError() 

72 

73 @abstractmethod 

74 def getDiaObjects_table(self) -> ApdbTables: 

75 """Return type of table returned from getDiaObjects method.""" 

76 raise NotImplementedError() 

77 

78 def make_region(self, xyz: Tuple[float, float, float] = (1., 1., -1.)) -> Region: 

79 """Make a region to use in tests""" 

80 pointing_v = UnitVector3d(*xyz) 

81 fov = 0.05 # radians 

82 region = Circle(pointing_v, Angle(fov/2)) 

83 return region 

84 

85 def assert_catalog(self, catalog: Any, rows: int, table: ApdbTables) -> None: 

86 """Validate catalog type and size 

87 

88 Parameters 

89 ---------- 

90 catalog : `object` 

91 Expected type of this is ``type``. 

92 rows : int 

93 Expected number of rows in a catalog. 

94 table : `ApdbTables` 

95 APDB table type. 

96 """ 

97 self.assertIsInstance(catalog, pandas.DataFrame) # type: ignore[attr-defined] 

98 self.assertEqual(catalog.shape[0], rows) # type: ignore[attr-defined] 

99 self.assertEqual(catalog.shape[1], self.n_columns(table)) # type: ignore[attr-defined] 

100 

101 def test_makeSchema(self) -> None: 

102 """Test for makeing APDB schema.""" 

103 config = self.make_config() 

104 apdb = make_apdb(config) 

105 

106 apdb.makeSchema() 

107 self.assertIsNotNone(apdb.tableDef(ApdbTables.DiaObject)) # type: ignore[attr-defined] 

108 self.assertIsNotNone(apdb.tableDef(ApdbTables.DiaObjectLast)) # type: ignore[attr-defined] 

109 self.assertIsNotNone(apdb.tableDef(ApdbTables.DiaSource)) # type: ignore[attr-defined] 

110 self.assertIsNotNone(apdb.tableDef(ApdbTables.DiaForcedSource)) # type: ignore[attr-defined] 

111 

112 def test_empty_gets(self) -> None: 

113 """Test for getting data from empty database. 

114 

115 All get() methods should return empty results, only useful for 

116 checking that code is not broken. 

117 """ 

118 

119 # use non-zero months for Forced/Source fetching 

120 config = self.make_config() 

121 apdb = make_apdb(config) 

122 apdb.makeSchema() 

123 

124 region = self.make_region() 

125 visit_time = self.visit_time 

126 

127 res: Optional[pandas.DataFrame] 

128 

129 # get objects by region 

130 res = apdb.getDiaObjects(region) 

131 self.assert_catalog(res, 0, self.getDiaObjects_table()) 

132 

133 # get sources by region 

134 res = apdb.getDiaSources(region, None, visit_time) 

135 self.assert_catalog(res, 0, ApdbTables.DiaSource) 

136 

137 res = apdb.getDiaSources(region, [], visit_time) 

138 self.assert_catalog(res, 0, ApdbTables.DiaSource) 

139 

140 # get sources by object ID, non-empty object list 

141 res = apdb.getDiaSources(region, [1, 2, 3], visit_time) 

142 self.assert_catalog(res, 0, ApdbTables.DiaSource) 

143 

144 # get forced sources by object ID, empty object list 

145 res = apdb.getDiaForcedSources(region, [], visit_time) 

146 self.assert_catalog(res, 0, ApdbTables.DiaForcedSource) 

147 

148 # get sources by object ID, non-empty object list 

149 res = apdb.getDiaForcedSources(region, [1, 2, 3], visit_time) 

150 self.assert_catalog(res, 0, ApdbTables.DiaForcedSource) 

151 

152 # get sources by region 

153 if self.fsrc_requires_id_list: 

154 with self.assertRaises(NotImplementedError): # type: ignore[attr-defined] 

155 apdb.getDiaForcedSources(region, None, visit_time) 

156 else: 

157 apdb.getDiaForcedSources(region, None, visit_time) 

158 self.assert_catalog(res, 0, ApdbTables.DiaForcedSource) 

159 

160 def test_empty_gets_0months(self) -> None: 

161 """Test for getting data from empty database. 

162 

163 All get() methods should return empty DataFrame or None. 

164 """ 

165 

166 # set read_sources_months to 0 so that Forced/Sources are None 

167 config = self.make_config(read_sources_months=0, 

168 read_forced_sources_months=0) 

169 apdb = make_apdb(config) 

170 apdb.makeSchema() 

171 

172 region = self.make_region() 

173 visit_time = self.visit_time 

174 

175 res: Optional[pandas.DataFrame] 

176 

177 # get objects by region 

178 res = apdb.getDiaObjects(region) 

179 self.assert_catalog(res, 0, self.getDiaObjects_table()) 

180 

181 # get sources by region 

182 res = apdb.getDiaSources(region, None, visit_time) 

183 self.assertIs(res, None) # type: ignore[attr-defined] 

184 

185 # get sources by object ID, empty object list 

186 res = apdb.getDiaSources(region, [], visit_time) 

187 self.assertIs(res, None) # type: ignore[attr-defined] 

188 

189 # get forced sources by object ID, empty object list 

190 res = apdb.getDiaForcedSources(region, [], visit_time) 

191 self.assertIs(res, None) # type: ignore[attr-defined] 

192 

193 def test_storeObjects(self) -> None: 

194 """Store and retrieve DiaObjects.""" 

195 

196 # don't care about sources. 

197 config = self.make_config() 

198 apdb = make_apdb(config) 

199 apdb.makeSchema() 

200 

201 region = self.make_region() 

202 visit_time = self.visit_time 

203 

204 # make catalog with Objects 

205 catalog = makeObjectCatalog(region, 100, visit_time) 

206 

207 # store catalog 

208 apdb.store(visit_time, catalog) 

209 

210 # read it back and check sizes 

211 res = apdb.getDiaObjects(region) 

212 self.assert_catalog(res, len(catalog), self.getDiaObjects_table()) 

213 

214 def test_objectHistory(self) -> None: 

215 """Store and retrieve DiaObject history.""" 

216 

217 # don't care about sources. 

218 config = self.make_config() 

219 apdb = make_apdb(config) 

220 apdb.makeSchema() 

221 

222 region1 = self.make_region((1., 1., -1.)) 

223 region2 = self.make_region((-1., -1., -1.)) 

224 visit_time = [ 

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), 

233 ] 

234 end_time = DateTime("2021-03-02T00:00:00", DateTime.TAI) 

235 

236 nobj = 100 

237 catalog1 = makeObjectCatalog(region1, nobj, visit_time[0]) 

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) 

242 catalog2 = makeObjectCatalog(region2, nobj, visit_time[1], start_id=nobj*2) 

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) 

247 

248 # read it back and check sizes 

249 res = apdb.getDiaObjectsHistory(DateTime("2021-01-01T00:00:00", DateTime.TAI), end_time) 

250 self.assert_catalog(res, nobj * 8, ApdbTables.DiaObject) 

251 

252 res = apdb.getDiaObjectsHistory(DateTime("2021-01-01T00:01:00", DateTime.TAI), end_time) 

253 self.assert_catalog(res, nobj * 8, ApdbTables.DiaObject) 

254 

255 res = apdb.getDiaObjectsHistory(DateTime("2021-01-01T00:01:01", DateTime.TAI), end_time) 

256 self.assert_catalog(res, nobj * 7, ApdbTables.DiaObject) 

257 

258 res = apdb.getDiaObjectsHistory(DateTime("2021-01-01T00:02:30", DateTime.TAI), end_time) 

259 self.assert_catalog(res, nobj * 6, ApdbTables.DiaObject) 

260 

261 res = apdb.getDiaObjectsHistory(DateTime("2021-01-01T00:05:00", DateTime.TAI), end_time) 

262 self.assert_catalog(res, nobj * 4, ApdbTables.DiaObject) 

263 

264 res = apdb.getDiaObjectsHistory(DateTime("2021-01-01T00:06:30", DateTime.TAI), end_time) 

265 self.assert_catalog(res, nobj * 2, ApdbTables.DiaObject) 

266 

267 res = apdb.getDiaObjectsHistory(DateTime("2021-03-01T00:02:00.001", DateTime.TAI), end_time) 

268 self.assert_catalog(res, 0, ApdbTables.DiaObject) 

269 

270 res = apdb.getDiaObjectsHistory( 

271 DateTime("2021-01-01T00:00:00", DateTime.TAI), 

272 DateTime("2021-01-01T00:06:01", DateTime.TAI), 

273 ) 

274 self.assert_catalog(res, nobj * 6, ApdbTables.DiaObject) 

275 

276 res = apdb.getDiaObjectsHistory( 

277 DateTime("2021-01-01T00:00:00", DateTime.TAI), 

278 DateTime("2021-01-01T00:06:00", DateTime.TAI), 

279 ) 

280 self.assert_catalog(res, nobj * 5, ApdbTables.DiaObject) 

281 

282 res = apdb.getDiaObjectsHistory( 

283 DateTime("2021-01-01T00:00:00", DateTime.TAI), 

284 DateTime("2021-01-01T00:01:00", DateTime.TAI), 

285 ) 

286 self.assert_catalog(res, 0, ApdbTables.DiaObject) 

287 

288 res = apdb.getDiaObjectsHistory( 

289 DateTime("2021-01-01T00:00:00", DateTime.TAI), end_time, region=region1 

290 ) 

291 self.assert_catalog(res, nobj * 4, ApdbTables.DiaObject) 

292 

293 res = apdb.getDiaObjectsHistory( 

294 DateTime("2021-01-01T00:03:00", DateTime.TAI), end_time, region=region2 

295 ) 

296 self.assert_catalog(res, nobj * 3, ApdbTables.DiaObject) 

297 

298 res = apdb.getDiaObjectsHistory( 

299 DateTime("2021-01-01T00:00:00", DateTime.TAI), 

300 DateTime("2021-01-01T00:03:30", DateTime.TAI), 

301 region1, 

302 ) 

303 self.assert_catalog(res, nobj * 2, ApdbTables.DiaObject) 

304 

305 def test_storeSources(self) -> None: 

306 """Store and retrieve DiaSources.""" 

307 config = self.make_config() 

308 apdb = make_apdb(config) 

309 apdb.makeSchema() 

310 

311 region = self.make_region() 

312 visit_time = self.visit_time 

313 

314 # have to store Objects first 

315 objects = makeObjectCatalog(region, 100, visit_time) 

316 oids = list(objects["diaObjectId"]) 

317 sources = makeSourceCatalog(objects, visit_time) 

318 

319 # save the objects and sources 

320 apdb.store(visit_time, objects, sources) 

321 

322 # read it back, no ID filtering 

323 res = apdb.getDiaSources(region, None, visit_time) 

324 self.assert_catalog(res, len(sources), ApdbTables.DiaSource) 

325 

326 # read it back and filter by ID 

327 res = apdb.getDiaSources(region, oids, visit_time) 

328 self.assert_catalog(res, len(sources), ApdbTables.DiaSource) 

329 

330 # read it back to get schema 

331 res = apdb.getDiaSources(region, [], visit_time) 

332 self.assert_catalog(res, 0, ApdbTables.DiaSource) 

333 

334 def test_sourceHistory(self) -> None: 

335 """Store and retrieve DiaSource history.""" 

336 

337 # don't care about sources. 

338 config = self.make_config() 

339 apdb = make_apdb(config) 

340 apdb.makeSchema() 

341 visit_time = self.visit_time 

342 

343 region1 = self.make_region((1., 1., -1.)) 

344 region2 = self.make_region((-1., -1., -1.)) 

345 nobj = 100 

346 objects1 = makeObjectCatalog(region1, nobj, visit_time) 

347 objects2 = makeObjectCatalog(region2, nobj, visit_time, start_id=nobj*2) 

348 

349 visits = [ 

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), 

358 ] 

359 end_time = DateTime("2021-03-02T00:00:00", DateTime.TAI) 

360 

361 start_id = 0 

362 for visit_time, objects in visits: 

363 sources = makeSourceCatalog(objects, visit_time, start_id=start_id) 

364 apdb.store(visit_time, objects, sources) 

365 start_id += nobj 

366 

367 # read it back and check sizes 

368 res = apdb.getDiaSourcesHistory(DateTime("2021-01-01T00:00:00", DateTime.TAI), end_time) 

369 self.assert_catalog(res, nobj * 8, ApdbTables.DiaSource) 

370 

371 res = apdb.getDiaSourcesHistory(DateTime("2021-01-01T00:01:00", DateTime.TAI), end_time) 

372 self.assert_catalog(res, nobj * 8, ApdbTables.DiaSource) 

373 

374 res = apdb.getDiaSourcesHistory(DateTime("2021-01-01T00:01:01", DateTime.TAI), end_time) 

375 self.assert_catalog(res, nobj * 7, ApdbTables.DiaSource) 

376 

377 res = apdb.getDiaSourcesHistory(DateTime("2021-01-01T00:02:30", DateTime.TAI), end_time) 

378 self.assert_catalog(res, nobj * 6, ApdbTables.DiaSource) 

379 

380 res = apdb.getDiaSourcesHistory(DateTime("2021-01-01T00:05:00", DateTime.TAI), end_time) 

381 self.assert_catalog(res, nobj * 4, ApdbTables.DiaSource) 

382 

383 res = apdb.getDiaSourcesHistory(DateTime("2021-01-01T00:06:30", DateTime.TAI), end_time) 

384 self.assert_catalog(res, nobj * 2, ApdbTables.DiaSource) 

385 

386 res = apdb.getDiaSourcesHistory(DateTime("2021-03-01T00:02:00.001", DateTime.TAI), end_time) 

387 self.assert_catalog(res, 0, ApdbTables.DiaSource) 

388 

389 res = apdb.getDiaSourcesHistory( 

390 DateTime("2021-01-01T00:00:00", DateTime.TAI), 

391 DateTime("2021-01-01T00:06:01", DateTime.TAI), 

392 ) 

393 self.assert_catalog(res, nobj * 6, ApdbTables.DiaSource) 

394 

395 res = apdb.getDiaSourcesHistory( 

396 DateTime("2021-01-01T00:00:00", DateTime.TAI), 

397 DateTime("2021-01-01T00:06:00", DateTime.TAI), 

398 ) 

399 self.assert_catalog(res, nobj * 5, ApdbTables.DiaSource) 

400 

401 res = apdb.getDiaSourcesHistory( 

402 DateTime("2021-01-01T00:00:00", DateTime.TAI), 

403 DateTime("2021-01-01T00:01:00", DateTime.TAI), 

404 ) 

405 self.assert_catalog(res, 0, ApdbTables.DiaSource) 

406 

407 res = apdb.getDiaSourcesHistory( 

408 DateTime("2021-01-01T00:00:00", DateTime.TAI), end_time, region=region1 

409 ) 

410 self.assert_catalog(res, nobj * 4, ApdbTables.DiaSource) 

411 

412 res = apdb.getDiaSourcesHistory( 

413 DateTime("2021-01-01T00:03:00", DateTime.TAI), end_time, region=region2 

414 ) 

415 self.assert_catalog(res, nobj * 3, ApdbTables.DiaSource) 

416 

417 res = apdb.getDiaSourcesHistory( 

418 DateTime("2021-01-01T00:00:00", DateTime.TAI), 

419 DateTime("2021-01-01T00:03:30", DateTime.TAI), 

420 region1, 

421 ) 

422 self.assert_catalog(res, nobj * 2, ApdbTables.DiaSource) 

423 

424 def test_storeForcedSources(self) -> None: 

425 """Store and retrieve DiaForcedSources.""" 

426 

427 config = self.make_config() 

428 apdb = make_apdb(config) 

429 apdb.makeSchema() 

430 

431 region = self.make_region() 

432 visit_time = self.visit_time 

433 

434 # have to store Objects first 

435 objects = makeObjectCatalog(region, 100, visit_time) 

436 oids = list(objects["diaObjectId"]) 

437 catalog = makeForcedSourceCatalog(objects, visit_time) 

438 

439 apdb.store(visit_time, objects, forced_sources=catalog) 

440 

441 # read it back and check sizes 

442 res = apdb.getDiaForcedSources(region, oids, visit_time) 

443 self.assert_catalog(res, len(catalog), ApdbTables.DiaForcedSource) 

444 

445 # read it back to get schema 

446 res = apdb.getDiaForcedSources(region, [], visit_time) 

447 self.assert_catalog(res, 0, ApdbTables.DiaForcedSource) 

448 

449 def test_forcedSourceHistory(self) -> None: 

450 """Store and retrieve DiaForcedSource history.""" 

451 

452 # don't care about sources. 

453 config = self.make_config() 

454 apdb = make_apdb(config) 

455 apdb.makeSchema() 

456 visit_time = self.visit_time 

457 

458 region1 = self.make_region((1., 1., -1.)) 

459 region2 = self.make_region((-1., -1., -1.)) 

460 nobj = 100 

461 objects1 = makeObjectCatalog(region1, nobj, visit_time) 

462 objects2 = makeObjectCatalog(region2, nobj, visit_time, start_id=nobj*2) 

463 

464 visits = [ 

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), 

473 ] 

474 end_time = DateTime("2021-03-02T00:00:00", DateTime.TAI) 

475 

476 start_id = 0 

477 for visit_time, objects in visits: 

478 sources = makeForcedSourceCatalog(objects, visit_time, ccdVisitId=start_id) 

479 apdb.store(visit_time, objects, forced_sources=sources) 

480 start_id += 1 

481 

482 # read it back and check sizes 

483 res = apdb.getDiaForcedSourcesHistory(DateTime("2021-01-01T00:00:00", DateTime.TAI), end_time) 

484 self.assert_catalog(res, nobj * 8, ApdbTables.DiaForcedSource) 

485 

486 res = apdb.getDiaForcedSourcesHistory(DateTime("2021-01-01T00:01:00", DateTime.TAI), end_time) 

487 self.assert_catalog(res, nobj * 8, ApdbTables.DiaForcedSource) 

488 

489 res = apdb.getDiaForcedSourcesHistory(DateTime("2021-01-01T00:01:01", DateTime.TAI), end_time) 

490 self.assert_catalog(res, nobj * 7, ApdbTables.DiaForcedSource) 

491 

492 res = apdb.getDiaForcedSourcesHistory(DateTime("2021-01-01T00:02:30", DateTime.TAI), end_time) 

493 self.assert_catalog(res, nobj * 6, ApdbTables.DiaForcedSource) 

494 

495 res = apdb.getDiaForcedSourcesHistory(DateTime("2021-01-01T00:05:00", DateTime.TAI), end_time) 

496 self.assert_catalog(res, nobj * 4, ApdbTables.DiaForcedSource) 

497 

498 res = apdb.getDiaForcedSourcesHistory(DateTime("2021-01-01T00:06:30", DateTime.TAI), end_time) 

499 self.assert_catalog(res, nobj * 2, ApdbTables.DiaForcedSource) 

500 

501 res = apdb.getDiaForcedSourcesHistory(DateTime("2021-03-01T00:02:00.001", DateTime.TAI), end_time) 

502 self.assert_catalog(res, 0, ApdbTables.DiaForcedSource) 

503 

504 res = apdb.getDiaForcedSourcesHistory( 

505 DateTime("2021-01-01T00:00:00", DateTime.TAI), 

506 DateTime("2021-01-01T00:06:01", DateTime.TAI), 

507 ) 

508 self.assert_catalog(res, nobj * 6, ApdbTables.DiaForcedSource) 

509 

510 res = apdb.getDiaForcedSourcesHistory( 

511 DateTime("2021-01-01T00:00:00", DateTime.TAI), 

512 DateTime("2021-01-01T00:06:00", DateTime.TAI), 

513 ) 

514 self.assert_catalog(res, nobj * 5, ApdbTables.DiaForcedSource) 

515 

516 res = apdb.getDiaForcedSourcesHistory( 

517 DateTime("2021-01-01T00:00:00", DateTime.TAI), 

518 DateTime("2021-01-01T00:01:00", DateTime.TAI), 

519 ) 

520 self.assert_catalog(res, 0, ApdbTables.DiaForcedSource) 

521 

522 res = apdb.getDiaForcedSourcesHistory( 

523 DateTime("2021-01-01T00:00:00", DateTime.TAI), end_time, region=region1 

524 ) 

525 rows = nobj * 4 if self.fsrc_history_region_filtering else nobj * 8 

526 self.assert_catalog(res, rows, ApdbTables.DiaForcedSource) 

527 

528 res = apdb.getDiaForcedSourcesHistory( 

529 DateTime("2021-01-01T00:03:00", DateTime.TAI), end_time, region=region2 

530 ) 

531 rows = nobj * 3 if self.fsrc_history_region_filtering else nobj * 6 

532 self.assert_catalog(res, rows, ApdbTables.DiaForcedSource) 

533 

534 res = apdb.getDiaForcedSourcesHistory( 

535 DateTime("2021-01-01T00:00:00", DateTime.TAI), 

536 DateTime("2021-01-01T00:03:30", DateTime.TAI), 

537 region1, 

538 ) 

539 rows = nobj * 2 if self.fsrc_history_region_filtering else nobj * 3 

540 self.assert_catalog(res, rows, ApdbTables.DiaForcedSource) 

541 

542 def test_storeSSObjects(self) -> None: 

543 """Store and retrieve SSObjects.""" 

544 

545 # don't care about sources. 

546 config = self.make_config() 

547 apdb = make_apdb(config) 

548 apdb.makeSchema() 

549 

550 # make catalog with SSObjects 

551 catalog = makeSSObjectCatalog(100, flags=1) 

552 

553 # store catalog 

554 apdb.storeSSObjects(catalog) 

555 

556 # read it back and check sizes 

557 res = apdb.getSSObjects() 

558 self.assert_catalog(res, len(catalog), ApdbTables.SSObject) 

559 

560 # check that override works, make catalog with SSObjects, ID = 51-150 

561 catalog = makeSSObjectCatalog(100, 51, flags=2) 

562 apdb.storeSSObjects(catalog) 

563 res = apdb.getSSObjects() 

564 self.assert_catalog(res, 150, ApdbTables.SSObject) 

565 self.assertEqual(len(res[res["flags"] == 1]), 50) # type: ignore[attr-defined] 

566 self.assertEqual(len(res[res["flags"] == 2]), 100) # type: ignore[attr-defined] 

567 

568 def test_reassignObjects(self) -> None: 

569 """Reassign DiaObjects.""" 

570 

571 # don't care about sources. 

572 config = self.make_config() 

573 apdb = make_apdb(config) 

574 apdb.makeSchema() 

575 

576 region = self.make_region() 

577 visit_time = self.visit_time 

578 objects = makeObjectCatalog(region, 100, visit_time) 

579 oids = list(objects["diaObjectId"]) 

580 sources = makeSourceCatalog(objects, visit_time) 

581 apdb.store(visit_time, objects, sources) 

582 

583 catalog = makeSSObjectCatalog(100) 

584 apdb.storeSSObjects(catalog) 

585 

586 # read it back and filter by ID 

587 res = apdb.getDiaSources(region, oids, visit_time) 

588 self.assert_catalog(res, len(sources), ApdbTables.DiaSource) 

589 

590 apdb.reassignDiaSources({1: 1, 2: 2, 5: 5}) 

591 res = apdb.getDiaSources(region, oids, visit_time) 

592 self.assert_catalog(res, len(sources) - 3, ApdbTables.DiaSource) 

593 

594 with self.assertRaisesRegex(ValueError, r"do not exist.*\D1000"): # type: ignore[attr-defined] 

595 apdb.reassignDiaSources({1000: 1, 7: 3, }) 

596 self.assert_catalog(res, len(sources) - 3, ApdbTables.DiaSource) 

597 

598 def test_midPointTai_src(self) -> None: 

599 """Test for time filtering of DiaSources. 

600 """ 

601 config = self.make_config() 

602 apdb = make_apdb(config) 

603 apdb.makeSchema() 

604 

605 region = self.make_region() 

606 # 2021-01-01 plus 360 days is 2021-12-27 

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) 

612 

613 objects = makeObjectCatalog(region, 100, visit_time0) 

614 oids = list(objects["diaObjectId"]) 

615 sources = makeSourceCatalog(objects, src_time1, 0) 

616 apdb.store(src_time1, objects, sources) 

617 

618 sources = makeSourceCatalog(objects, src_time2, 100) 

619 apdb.store(src_time2, objects, sources) 

620 

621 # reading at time of last save should read all 

622 res = apdb.getDiaSources(region, oids, src_time2) 

623 self.assert_catalog(res, 200, ApdbTables.DiaSource) 

624 

625 # one second before 12 months 

626 res = apdb.getDiaSources(region, oids, visit_time0) 

627 self.assert_catalog(res, 200, ApdbTables.DiaSource) 

628 

629 # reading at later time of last save should only read a subset 

630 res = apdb.getDiaSources(region, oids, visit_time1) 

631 self.assert_catalog(res, 100, ApdbTables.DiaSource) 

632 

633 # reading at later time of last save should only read a subset 

634 res = apdb.getDiaSources(region, oids, visit_time2) 

635 self.assert_catalog(res, 0, ApdbTables.DiaSource) 

636 

637 def test_midPointTai_fsrc(self) -> None: 

638 """Test for time filtering of DiaForcedSources. 

639 """ 

640 config = self.make_config() 

641 apdb = make_apdb(config) 

642 apdb.makeSchema() 

643 

644 region = self.make_region() 

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) 

650 

651 objects = makeObjectCatalog(region, 100, visit_time0) 

652 oids = list(objects["diaObjectId"]) 

653 sources = makeForcedSourceCatalog(objects, src_time1, 1) 

654 apdb.store(src_time1, objects, forced_sources=sources) 

655 

656 sources = makeForcedSourceCatalog(objects, src_time2, 2) 

657 apdb.store(src_time2, objects, forced_sources=sources) 

658 

659 # reading at time of last save should read all 

660 res = apdb.getDiaForcedSources(region, oids, src_time2) 

661 self.assert_catalog(res, 200, ApdbTables.DiaForcedSource) 

662 

663 # one second before 12 months 

664 res = apdb.getDiaForcedSources(region, oids, visit_time0) 

665 self.assert_catalog(res, 200, ApdbTables.DiaForcedSource) 

666 

667 # reading at later time of last save should only read a subset 

668 res = apdb.getDiaForcedSources(region, oids, visit_time1) 

669 self.assert_catalog(res, 100, ApdbTables.DiaForcedSource) 

670 

671 # reading at later time of last save should only read a subset 

672 res = apdb.getDiaForcedSources(region, oids, visit_time2) 

673 self.assert_catalog(res, 0, ApdbTables.DiaForcedSource)