Пример #1
0
 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)
     }
Пример #2
0
 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
Пример #3
0
    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
Пример #4
0
 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']
Пример #5
0
 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': {}})
Пример #6
0
 def test_id_missing(self) -> None:
     """Test exception on missing alert `id`."""
     with pytest.raises(NotFound):
         Alert.from_id(-1)
Пример #7
0
 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
Пример #8
0
 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()
Пример #9
0
 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))
Пример #10
0
 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()
Пример #11
0
 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
Пример #12
0
 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']
Пример #13
0
 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': {}})
Пример #14
0
 def test_observation_missing(self) -> None:
     """Test exception on missing alert `observation`."""
     with pytest.raises(NotFound):
         Alert.from_observation(-1)