Ejemplo n.º 1
0
 def test_embedded(self) -> None:
     """Test embedded method to check JSON-serialization and full join."""
     assert Recommendation.from_id(1).to_json(join=True) == {
         'id':
         1,
         'group_id':
         1,
         'tag_id':
         1,
         'time':
         '2020-10-24 20:02:00' +
         ('' if config.backend == 'sqlite' else '-04:00'),
         'priority':
         1,
         'object_id':
         1,
         'facility_id':
         1,
         'user_id':
         2,
         'forecast_id':
         1,
         'predicted_observation_id':
         11,
         'observation_id':
         19,
         'accepted':
         True,
         'rejected':
         False,
         'data': {},
         'group':
         RecommendationGroup.from_id(1).to_json(join=True),
         'tag':
         RecommendationTag.from_id(1).to_json(join=True),
         'user':
         User.from_id(2).to_json(join=True),
         'facility':
         Facility.from_id(1).to_json(join=True),
         'object':
         Object.from_id(1).to_json(join=True),
         'forecast':
         Forecast.from_id(1).to_json(join=True),
         'predicted':
         Observation.from_id(11).to_json(join=True),
         'observed':
         Observation.from_id(19).to_json(join=True),
     }
Ejemplo n.º 2
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)
     }
Ejemplo n.º 3
0
 def test_embedded(self) -> None:
     """Test embedded method to check JSON-serialization and auto-join."""
     assert Observation.from_id(1).to_json(join=True) == {
         'id': 1,
         'time': '2020-10-24 18:00:00' + ('' if config.backend == 'sqlite' else '-04:00'),
         'object_id': 1,
         'type_id': 3,
         'source_id': 2,
         'value': 18.1,
         'error': 0.08,
         'recorded': '2020-10-24 18:01:00' + ('' if config.backend == 'sqlite' else '-04:00'),
         'object': {
             'id': 1,
             'type_id': 1,
             'aliases': {
                 'antares': 'ANT2020ae7t5xa',
                 'ztf': 'ZTF20actrfli',
                 'tag': 'determined_thirsty_cray',
             },
             'ra': 133.0164572,
             'dec': 44.80034109999999,
             'redshift': None,
             'data': {},
             'type': {
                 'id': 1,
                 'name': 'Unknown',
                 'description': 'Objects with unknown or unspecified type',
             },
         },
         'type': {
             'id': 3,
             'name': 'r-ztf',
             'units': 'mag',
             'description': 'R-band apparent magnitude (ZTF).'
         },
         'source': {
             'id': 2,
             'type_id': 3,
             'facility_id': None,
             'user_id': None,
             'name': 'antares',
             'description': 'Antares is an alert broker developed by NOAO for ZTF and LSST.',
             'data': {},
             'type': {
                 'id': 3,
                 'name': 'broker',
                 'description': 'Alerts from data brokers.'
             },
         },
     }
Ejemplo n.º 4
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
Ejemplo n.º 5
0
 def test_embedded(self) -> None:
     """Test embedded method to check JSON-serialization and auto-join."""
     assert File.from_id(1).to_json(join=True) == {
         'id':
         1,
         'observation_id':
         19,
         'type_id':
         1,
         'data': [
             'H4sICIVdxV8C/2xvY2FsXzMuZml0cwAAAAD//+zRMQrCMBjF8au8G2iLuDkoRghoKSRD1mhS6JBE',
             'kjj09lbBLUEKHb/fAf48eILf+isDDiiQ2OAR/BCiS8gBFy4FUtbe6GhQdOKy56rc2+/mno5RTzA6',
             'a+TpafFHd1RcoLKvnXv+5e42Igy/8uisT2Pwqd5ryr1mi8W+vXa9HlOSdefqH8t7nxghhBBCCFnN',
             'GwAA///sxbEJAAAIAzCh///sILj1g2TJnNiuAwC8BQAA//8DABVnAU2AFgAA'
         ],
         'type':
         FileType.from_id(1).to_json(join=True),
         'observation':
         Observation.from_id(19).to_json(join=True)
     }
Ejemplo n.º 6
0
 def test_tuple(self, testdata: TestData) -> None:
     """Test tuple-conversion."""
     for data in testdata['observation']:
         observation = Observation.from_dict(data)
         assert tuple(data.values()) == observation.to_tuple()
Ejemplo n.º 7
0
 def test_embedded_no_join(self, testdata: TestData) -> None:
     """Tests embedded method to check JSON-serialization."""
     for data in testdata['observation']:
         embedded_data = {**data, 'time': str(data['time']), 'recorded': str(data['recorded'])}
         assert embedded_data == json_roundtrip(Observation(**data).to_json(join=False))
Ejemplo n.º 8
0
 def test_init(self, testdata: TestData) -> None:
     """Create observation instance and validate accessors."""
     for data in testdata['observation']:
         observation = Observation(**data)
         for key, value in data.items():
             assert getattr(observation, key) == value
Ejemplo n.º 9
0
 def test_dict(self, testdata: TestData) -> None:
     """Test round-trip of dict translations."""
     for data in testdata['observation']:
         observation = Observation.from_dict(data)
         assert data == observation.to_dict()
Ejemplo n.º 10
0
 def test_with_object(self) -> None:
     """Test query for observations for a given object."""
     for object_id, count in [(1, 9), (10, 3)]:
         results = Observation.with_object(object_id)
         assert all(isinstance(obs, Observation) for obs in results)
         assert len(results) == count
Ejemplo n.º 11
0
 def test_with_source(self) -> None:
     """Test query for observations for a given source."""
     for source_id in [3, 4, 5, 6]:
         results = Observation.with_source(source_id)
         assert all(isinstance(obs, Observation) for obs in results)
         assert len(results) == 6
Ejemplo n.º 12
0
 def test_relationship_source_type(self, testdata: TestData) -> None:
     """Test observation foreign key relationship on source -> source_type."""
     for i, record in enumerate(testdata['observation']):
         assert Observation.from_id(i + 1).source.type.id == Source.from_id(record['source_id']).type.id
Ejemplo n.º 13
0
 def test_relationship_object_type(self, testdata: TestData) -> None:
     """Test observation foreign key relationship on object -> object_type."""
     for i, record in enumerate(testdata['observation']):
         assert Observation.from_id(i + 1).object.type.id == Object.from_id(record['object_id']).type.id
Ejemplo n.º 14
0
 def test_id_already_exists(self) -> None:
     """Test exception on observation `id` already exists."""
     with pytest.raises(IntegrityError):
         Observation.add({'id': 1, 'time': datetime.now(), 'object_id': 1, 'type_id': 1,
                          'source_id': 1, 'value': 3.14, 'error': None})
Ejemplo n.º 15
0
 def test_id_missing(self) -> None:
     """Test exception on missing observation `id`."""
     with pytest.raises(NotFound):
         Observation.from_id(-1)
Ejemplo n.º 16
0
 def test_from_id(self, testdata: TestData) -> None:
     """Test loading observation from `id`."""
     # NOTE: `id` not set until after insert
     for i, record in enumerate(testdata['observation']):
         assert Observation.from_id(i + 1).id == i + 1