Skip to content

Commit

Permalink
Adding some schema validation and implementing tests for them (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-rodriguez authored Apr 30, 2024
1 parent 3f6e39a commit 0d1f28f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
35 changes: 27 additions & 8 deletions simple/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
"""

import enum

import sqlalchemy as sa
from sqlalchemy.orm import validates
from astrodbkit2.astrodb import Base
from astrodbkit2.views import view
from astropy.io.votable.ucd import check_ucd
from sqlalchemy import (
Boolean,
Column,
DateTime,
Enum,
Float,
ForeignKey,
String,
Enum,
DateTime,
ForeignKeyConstraint,
String,
)
from astrodbkit2.astrodb import Base
from astrodbkit2.views import view
from astropy.io.votable.ucd import check_ucd
from sqlalchemy.orm import validates

# -------------------------------------------------------------------------------------------------------------------
# Reference tables
Expand All @@ -44,6 +45,12 @@ class Publications(Base):
doi = Column(String(100))
description = Column(String(1000))

@validates("reference")
def validate_reference(self, key, value):
if value is None or len(value) > 30:
raise ValueError(f"Provided reference is invalid; too long or None: {value}")
return value


class Telescopes(Base):
__tablename__ = 'Telescopes'
Expand Down Expand Up @@ -102,7 +109,7 @@ def validate_ucd(self, key, value):
raise ValueError(f"UCD {value} not in controlled vocabulary")
return value

@validates("effective_wavelength_angstroms")
@validates("effective_wavelength")
def validate_wavelength(self, key, value):
if value is None or value < 0:
raise ValueError(f"Invalid effective wavelength received: {value}")
Expand Down Expand Up @@ -169,6 +176,18 @@ class Sources(Base):
other_references = Column(String(100))
comments = Column(String(1000))

@validates("ra")
def validate_ra(self, key, value):
if value > 360 or value < 0:
raise ValueError("RA not in allowed range (0..360)")
return value

@validates("dec")
def validate_dec(self, key, value):
if value > 90 or value < -90:
raise ValueError("Dec not in allowed range (-90..90)")
return value


class Names(Base):
__tablename__ = 'Names'
Expand Down
49 changes: 49 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Tests for the schema itself and any validating functions"""

import pytest

from simple.schema import PhotometryFilters, Publications, Sources


def schema_tester(table, values, error_state):
"""Helper function to handle the basic testing of the schema classes"""
if error_state is None:
_ = table(**values)
else:
with pytest.raises(error_state):
_ = table(**values)

@pytest.mark.parametrize("values, error_state",
[
({"band": "2MASS.J", "effective_wavelength": 1.2, "ucd": "phot;em.IR.J"}, None),
({"band": "2MASS.J", "effective_wavelength": 1.2, "ucd": "bad"}, ValueError),
({"band": "bad", "effective_wavelength": 1.2, "ucd": "phot;em.IR.J"}, ValueError),
({"band": "2MASS.J", "effective_wavelength": -99, "ucd": "phot;em.IR.J"}, ValueError),
])
def test_photometryfilters(values, error_state):
"""Validating PhotometryFilters"""
schema_tester(PhotometryFilters, values, error_state)


@pytest.mark.parametrize("values, error_state",
[
({"source": "FAKE", "ra": 1.2, "dec": 3.4, "reference": "Ref1"}, None),
({"source": "FAKE", "ra": 999, "dec": 3.4, "reference": "Ref1"}, ValueError),
({"source": "FAKE", "ra": -999, "dec": 3.4, "reference": "Ref1"}, ValueError),
({"source": "FAKE", "ra": 1.2, "dec": 999, "reference": "Ref1"}, ValueError),
({"source": "FAKE", "ra": 1.2, "dec": -999, "reference": "Ref1"}, ValueError),
])
def test_sources(values, error_state):
"""Validating Sources"""
schema_tester(Sources, values, error_state)


@pytest.mark.parametrize("values, error_state",
[
({"reference": "Ref1"}, None),
({"reference": None}, ValueError),
({"reference": "THIS-REFERENCE-IS-REALLY-REALLY-LONG"}, ValueError),
])
def test_publications(values, error_state):
"""Validating Publications"""
schema_tester(Publications, values, error_state)

0 comments on commit 0d1f28f

Please sign in to comment.