Skip to content

Commit

Permalink
make IRS2Model inherit from ReferenceFileModel (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram authored Oct 23, 2024
1 parent 503320f commit 1236c76
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions changes/348.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change IRS2Model parent class to ReferenceFileModel
4 changes: 2 additions & 2 deletions src/stdatamodels/jwst/datamodels/irs2.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from .model_base import JwstDataModel
from .reference import ReferenceFileModel


__all__ = ['IRS2Model']


class IRS2Model(JwstDataModel):
class IRS2Model(ReferenceFileModel):
"""
A data model for the IRS2 refpix reference file.
Expand Down
58 changes: 58 additions & 0 deletions src/stdatamodels/jwst/datamodels/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import warnings

from asdf.exceptions import ValidationError
from asdf.schema import load_schema
from astropy.io import fits
from astropy.table import Table
from astropy.time import Time
Expand All @@ -19,6 +20,7 @@
Level1bModel)
from stdatamodels.jwst import datamodels
from stdatamodels.jwst.datamodels import _defined_models as defined_models
from stdatamodels.schema import walk_schema

ROOT_DIR = os.path.join(os.path.dirname(__file__), 'data')
FITS_FILE = os.path.join(ROOT_DIR, 'test.fits')
Expand Down Expand Up @@ -317,6 +319,28 @@ def test_abvega_offset_model():
model.validate()


def test_defined_models_up_to_date():
"""
Test that defined_models contains all JwstDataModel classes
"""
def get_classes(klass, classes=None):
if classes is None:
classes = set()
classes.add(klass)
for subclass in klass.__subclasses__():
get_classes(subclass, classes)
return classes

# get all datamodel classes including JwstDataModel
# and all subclasses (ignoring any that start with "_")
all_classes = {
klass for
klass in get_classes(JwstDataModel)
if klass.__name__[0] != "_"
}
assert set(defined_models.values()) == all_classes


@pytest.mark.parametrize("model", [v for v in defined_models.values()])
def test_all_datamodels_init(model):
"""
Expand All @@ -325,6 +349,40 @@ def test_all_datamodels_init(model):
model()


@pytest.mark.parametrize("model", [v for v in defined_models.values()])
def test_reference_model_schema_inheritance(model):
"""
Test that models that inherit from ReferenceFileModel use
referencefile.schema and that all models that use
referencefile.schema inherit from ReferenceFileModel.
"""
is_ref_model = issubclass(model, datamodels.ReferenceFileModel)
if not model.schema_url: # this model has no schema
# so it shouldn't be a subclass
assert not is_ref_model
# and we're dont testing this model
return

# crawl the schema looking for all ids
schema = load_schema(model.schema_url, resolve_references=True)

def cb(subschema, path, combiner, ctx, recurse):
if not isinstance(subschema, dict):
return
if 'id' not in subschema:
return
if subschema["id"] == "http://stsci.edu/schemas/jwst_datamodel/referencefile.schema":
ctx['has_ref'] = True

ctx = {'has_ref': False}
walk_schema(schema, cb, ctx=ctx)
refs_referencefile_schema = ctx['has_ref']
if is_ref_model:
assert refs_referencefile_schema, f"Reference model {model} does not ref referencefile.schema"
else:
assert not refs_referencefile_schema, f"Model {model} does not inherit from ReferenceFileModel"


def test_meta_date_management(tmp_path):
model = JwstDataModel({"meta": {"date": "2000-01-01T00:00:00.000"}})
assert model.meta.date == "2000-01-01T00:00:00.000"
Expand Down

0 comments on commit 1236c76

Please sign in to comment.