Skip to content

Commit

Permalink
detect warnings instead of log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
weaverba137 committed Sep 20, 2023
1 parent 96b2b54 commit e841857
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
18 changes: 12 additions & 6 deletions py/desidatamodel/test/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
"""Test desidatamodel.check functions
"""
import os
import sys
from packaging import version
import unittest
from unittest.mock import patch

skip_unit_warning = False
try:
from desiutil.annotate import FITSUnitWarning
except ImportError:
skip_unit_warning = True

from .datamodeltestcase import DataModelTestCase, DM

from .. import DataModelError
Expand Down Expand Up @@ -310,9 +314,10 @@ def test_extract_metadata_bad_keyword_unit(self):
meta = model.extract_metadata(error=True)
self.assertEqual(str(e.exception), erg_msg)
model.hdumeta = None
meta = model.extract_metadata(error=False)
with self.assertWarns(FITSUnitWarning) as w:
meta = model.extract_metadata(error=False)
self.assertEqual(str(w.warning), erg_msg)
self.assertLog(log, -1, "HDU 0 in {0} should have a more meaningful EXTNAME than 'PRIMARY'.".format(modelfile))
self.assertLog(log, -2, erg_msg)

def test_extract_metadata_missing_keyword_unit(self):
"""Test reading metadata with missing units for header keywords.
Expand Down Expand Up @@ -347,8 +352,9 @@ def test_extract_metadata_bad_column_unit(self):
meta = model.extract_metadata(error=True)
self.assertEqual(str(e.exception), erg_msg)
model.hdumeta = None
meta = model.extract_metadata(error=False)
self.assertLog(log, -1, erg_msg)
with self.assertWarns(FITSUnitWarning) as w:
meta = model.extract_metadata(error=False)
self.assertEqual(str(w.warning), erg_msg)

def test_extract_metadata_missing_column_type(self):
"""Test reading metadata with missing FITS column types.
Expand Down
1 change: 0 additions & 1 deletion py/desidatamodel/test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""Test data model files for validity.
"""
import os
import unittest

from .datamodeltestcase import DataModelTestCase

Expand Down
16 changes: 10 additions & 6 deletions py/desidatamodel/test/test_stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
# -*- coding: utf-8 -*-
"""Test desidatamodel.stub functions
"""
import os
import importlib.resources as ir
import unittest
from unittest.mock import patch, call
from astropy.io import fits
from astropy.io.fits.card import Undefined
from collections import OrderedDict

skip_unit_warning = False
try:
from desiutil.annotate import FITSUnitWarning
except ImportError:
skip_unit_warning = True
from .datamodeltestcase import DataModelTestCase
from .. import DataModelError
from ..stub import (Stub, extrakey, file_size, fits_column_format,
Expand Down Expand Up @@ -381,9 +384,10 @@ def test_Stub_image_format(self):
with self.assertRaises(ValueError) as e:
i = stub.image_format(hdr)
self.assertEqual(str(e.exception), erg_msg)
stub = Stub([sim_hdu(hdr)], error=False)
i = stub.image_format(hdr)
self.assertLog(log, 1, erg_msg)
with self.assertWarns(FITSUnitWarning) as w:
stub = Stub([sim_hdu(hdr)], error=False)
i = stub.image_format(hdr)
self.assertEqual(str(w.warning), erg_msg)
self.assertEqual(i, 'Data: FITS image [BITPIX=128, 1000x1000]')
hdr = sim_header()
hdr['SIMPLE'] = True
Expand All @@ -394,7 +398,7 @@ def test_Stub_image_format(self):
hdr['BUNIT'] = '10**-17 erg/(cm**2*s*Angstrom)'
stub = Stub([sim_hdu(hdr)], error=False)
i = stub.image_format(hdr)
self.assertLog(log, 4, "BUNIT = '10**-17 erg/(cm**2*s*Angstrom)'")
self.assertLog(log, 2, "BUNIT = '10**-17 erg/(cm**2*s*Angstrom)'")
self.assertEqual(i, 'Data: FITS image [BITPIX=128, 1000x1000]')
#
# Check compressed HDU
Expand Down
13 changes: 8 additions & 5 deletions py/desidatamodel/test/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
# -*- coding: utf-8 -*-
"""Test desidatamodel.unit functions
"""
# import os
import unittest
skip_unit_warning = False
try:
from desiutil.annotate import FITSUnitWarning
except ImportError:
skip_unit_warning = True
from .datamodeltestcase import DataModelTestCase
from ..unit import validate_unit, log

Expand All @@ -18,12 +21,12 @@ def test_check_model(self):
erg_msg = self.badUnitMessage('ergs')
c = validate_unit('erg')
self.assertIsNone(c)
c = validate_unit('ergs', error=False)
with self.assertWarns(FITSUnitWarning) as w:
c = validate_unit('ergs', error=False)
self.assertEqual(str(w.warning), erg_msg)
self.assertIsNone(c)
c = validate_unit('nanomaggies', error=True)
self.assertEqual(c, "'nanomaggies'")
self.assertLog(log, -1, erg_msg)
with self.assertRaises(ValueError) as e:
c = validate_unit('ergs', error=True)
self.assertEqual(str(e.exception), erg_msg)
self.assertLog(log, -1, erg_msg)

0 comments on commit e841857

Please sign in to comment.