def test_embedded(self) -> None: """Test embedded method to check JSON-serialization and auto-join.""" assert Alert.from_id(1).to_json(join=True) == { 'id': 1, 'observation_id': 1, 'data': { 'alert': { 'alert_id': 'ztf:...', 'dec': 44.80034109999999, 'mjd': 59146.916666666664, 'properties': { 'ztf_fid': 2, 'ztf_id': 'ZTF20actrfli', 'ztf_magpsf': 18.1, 'ztf_sigmapsf': 0.08 }, 'ra': 133.0164572 }, 'dec': 44.80034109999999, 'locus_id': 'ANT2020ae7t5xa', 'properties': {}, 'ra': 133.0164572 }, 'observation': Observation.from_id(1).to_json(join=True) }
def test_stream_to_database(self) -> None: """Stream alerts from client to database.""" with MockClient(topic='topic', credentials=('key', 'secret')) as stream: for count, alert in enumerate(stream): received = alert.to_database() assert Alert.from_id(received.id) == received assert received.observation.id == received.observation_id if count > 100: break
def test_backfill(self) -> None: """Create alert with prior history and test backfill.""" alert = MockAlert.from_random() alert.previous = [MockAlert.from_random() for _ in range(10)] for a in alert.previous: a.data = { **a.data, 'source_name': alert.source_name, 'object_aliases': alert.object_aliases, 'object_type_name': alert.object_type_name, } alert.previous = sorted(alert.previous, key=(lambda _a: _a.observation_time)) obs_count = Observation.count() alert_count = Alert.count() records = alert.backfill_database() assert Observation.count() == obs_count + len(alert.previous) + 1 assert Alert.count() == alert_count + len(alert.previous) + 1 obs_count = Observation.count() alert_count = Alert.count() for a in records[-2:]: Alert.delete(a.id) Observation.delete(a.observation_id) assert Observation.count() == obs_count - 2 assert Alert.count() == alert_count - 2 obs_count = Observation.count() alert_count = Alert.count() alert.backfill_database() assert Observation.count() == obs_count + 2 assert Alert.count() == alert_count + 2
def test_from_observation(self, testdata: TestData) -> None: """Test loading alert from `observation_id`.""" for i, record in enumerate(testdata['alert']): assert Alert.from_observation(record['observation_id']).observation_id == record['observation_id']
def test_id_already_exists(self) -> None: """Test exception on alert `id` already exists.""" with pytest.raises(IntegrityError): Alert.add({'id': 1, 'observation_id': 11, # NOTE: observation_id=11 is a forecast 'data': {}})
def test_id_missing(self) -> None: """Test exception on missing alert `id`.""" with pytest.raises(NotFound): Alert.from_id(-1)
def test_from_id(self, testdata: TestData) -> None: """Test loading alert from `id`.""" # NOTE: `id` not set until after insert for i, record in enumerate(testdata['alert']): assert Alert.from_id(i + 1).id == i + 1
def test_tuple(self, testdata: TestData) -> None: """Test tuple-conversion.""" for data in testdata['alert']: alert = Alert.from_dict(data) assert tuple(data.values()) == alert.to_tuple()
def test_embedded_no_join(self, testdata: TestData) -> None: """Tests embedded method to check JSON-serialization.""" for data in testdata['alert']: assert data == json_roundtrip(Alert(**data).to_json(join=False))
def test_dict(self, testdata: TestData) -> None: """Test round-trip of dict translations.""" for data in testdata['alert']: alert = Alert.from_dict(data) assert data == alert.to_dict()
def test_init(self, testdata: TestData) -> None: """Create alert instance and validate accessors.""" for data in testdata['alert']: alert = Alert(**data) for key, value in data.items(): assert getattr(alert, key) == value
def test_relationship_observation_type(self, testdata: TestData) -> None: """Test alert foreign key relationship on observation.""" for i, record in enumerate(testdata['alert']): assert Alert.from_id(i + 1).observation.id == record['observation_id']
def test_observation_already_exists(self) -> None: """Test exception on alert `observation_id` already exists.""" with pytest.raises(IntegrityError): Alert.add({'id': -1, 'observation_id': 1, 'data': {}})
def test_observation_missing(self) -> None: """Test exception on missing alert `observation`.""" with pytest.raises(NotFound): Alert.from_observation(-1)