Skip to content

Commit

Permalink
Add more extensive tests of FITSKeyword and fix a couple of bugs unco…
Browse files Browse the repository at this point in the history
…vered by tests

Closes #138
  • Loading branch information
mwcraig committed Feb 6, 2014
1 parent 7271ec6 commit ffa9407
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
9 changes: 5 additions & 4 deletions msumastro/header_processing/fitskeyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def __init__(self, name=None, value=None, comment=None, synonyms=None):
self.synonyms = []
else:
self.synonyms = synonyms
return

def __str__(self):
if self.value is None:
Expand All @@ -38,7 +37,7 @@ def __str__(self):
value_string = str(self.value)
return ("%s = %s / %s \n with synonyms: %s" %
(self.name.upper(), value_string, self.comment,
",".join(str(syn).upper() for syn in self.synonyms)))
",".join([str(syn).upper() for syn in self.synonyms])))

def _set_keyword_case(self, keyword):
return keyword.upper()
Expand Down Expand Up @@ -69,7 +68,8 @@ def synonyms(self):
@synonyms.setter
def synonyms(self, inp_synonyms):
if not inp_synonyms:
return []
self._synonyms = []
return
if isinstance(inp_synonyms, basestring):
synonym_list = [inp_synonyms]
elif isinstance(inp_synonyms, list):
Expand Down Expand Up @@ -156,7 +156,8 @@ def set_value_from_header(self, hdu_or_header):
if len(set(values)) > 1:
error_msg = 'Found multiple values for keyword %s:\nValues: %s'
raise ValueError(error_msg %
(','.join(self.names), ','.join(values)))
(','.join(self.names),
','.join([str(v) for v in values])))
self.value = values[0]
else:
raise ValueError('Keyword not found in header: %s' % self)
47 changes: 47 additions & 0 deletions msumastro/header_processing/tests/test_fitskeyword.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
from astropy.io.fits.hdu import PrimaryHDU
from astropy.io.fits import Header

from ..fitskeyword import FITSKeyword

Expand Down Expand Up @@ -83,3 +85,48 @@ def test_setting_name_to_one_of_synonyms(self):
synonyms=self.synonyms)
k.name = self.synonyms[0]
assert(len(k.synonyms) == len(self.synonyms) - 1)

def test_string_repesentation(self):
# If I have no value does my string contain only my name and comment?
k = FITSKeyword(name=self.name, comment=self.comment)
string_k = str(k)
assert self.name.upper() in string_k
assert self.comment in string_k
# Does value and synonyms appear in string?
k = FITSKeyword(name=self.name, comment=self.comment, value=self.value,
synonyms=self.synonyms)
string_k = str(k)
assert str(self.value) in string_k
for syn in self.synonyms:
assert syn.upper() in string_k

def test_error_raised_if_invalid_synonym(self):
# Does synonym which is neither string nor list raise error?
with pytest.raises(ValueError):
FITSKeyword(name='adfa', synonyms=12)

def test_bad_hdu_or_header_arg_raises_error(self):
hdu_or_header = 'Not a header or hdu'
with pytest.raises(ValueError):
self.keyword.add_to_header(hdu_or_header)
with pytest.raises(ValueError):
self.keyword.set_value_from_header(hdu_or_header)

def test_set_value_from_header(self):
# Do I raise a value error if the keyword isn't found?
with pytest.raises(ValueError):
self.keyword.set_value_from_header(self.hdu.header)
new_value = 3 * self.value
self.hdu.header[self.name] = new_value
# Did I get the new value from the hdu?
self.keyword.set_value_from_header(self.hdu)
assert self.keyword.value == new_value
# reset to original value
self.keyword.value = self.value
# Can I get the value from the header?
self.keyword.set_value_from_header(self.hdu.header)
assert self.keyword.value == new_value
# Do multiple (non-identical) values raise a value error?
self.hdu.header[self.synonyms[0]] = 7 * new_value
with pytest.raises(ValueError):
self.keyword.set_value_from_header(self.hdu.header)

0 comments on commit ffa9407

Please sign in to comment.