Skip to content

Commit

Permalink
Reworking tests to be more compact
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-rodriguez committed Jul 9, 2024
1 parent 578d231 commit eecb5e2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 165 deletions.
11 changes: 2 additions & 9 deletions simple/utils/spectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def ingest_spectrum(

if len(db_name) != 1:
msg = f"No unique source match for {source} in the database"
flags["message"] = msg
if raise_error:
raise AstroDBError(msg)
else:
Expand Down Expand Up @@ -193,15 +194,7 @@ def ingest_spectrum(

flags["added"] = True
logger.info(f"Added {source} : \n" f"{row_data}")
except sqlalchemy.exc.IntegrityError as e:
msg = f"Integrity Error: {source} \n {e}"
logger.error(msg + f" \n {row_data}")
flags["message"] = msg
if raise_error:
raise AstroDBError(msg)
else:
return flags
except sqlite3.IntegrityError as e:
except (sqlite3.IntegrityError, sqlalchemy.exc.IntegrityError) as e:
msg = f"Integrity Error: {source} \n {e}"
logger.error(msg)
flags["message"] = msg
Expand Down
209 changes: 53 additions & 156 deletions tests/test_spectra_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,169 +24,66 @@
"which is discouraged by the FITS standard.*",
),
)
def test_ingest_spectrum_errors(temp_db):

# A lot of the tests fail because they were checking very specific parts of ingest_spectrum

# Ingesting a spectrum with missing regime
@pytest.mark.parametrize("test_input, message", [
({"source": "apple"}, "Value required for regime"), # missing regime
({"source": "apple",
"telescope": "IRTF",
"instrument": "SpeX",
"mode": "Prism",
"regime": "nir",
"obs_date": "2020-01-01",
}, "NOT NULL constraint failed: Spectra.reference"), # missing reference
({"source": "apple",
"telescope": "IRTF",
"instrument": "SpeX",
"mode": "Prism",
"regime": "nir",
"obs_date": "2020-01-01",
"reference": "Ref 5",
}, "FOREIGN KEY constraint failed"), # invalid reference
({"source": "kiwi",
"telescope": "IRTF",
"instrument": "SpeX",
"mode": "Prism",
"regime": "nir",
"obs_date": "2020-01-01",
"reference": "Ref 1",
}, "No unique source match for kiwi in the database"), # invalid source
({"source": "apple",
"telescope": "IRTF",
"instrument": "SpeX",
"mode": "Prism",
"regime": "nir",
"reference": "Ref 1",
}, "Invalid date received: None"), # missing date
({"source": "apple",
"telescope": "IRTF",
"instrument": "SpeX",
"mode": "Prism",
"regime": "fake regime",
"obs_date": "2020-01-01",
"reference": "Ref 1",
}, "FOREIGN KEY constraint failed"), # invalid regime
])
def test_ingest_spectrum_errors(temp_db, test_input, message):
# Test for ingest_spectrum that is expected to return errors

# Prepare parameters to send to ingest_spectrum
spectrum = "https://bdnyc.s3.amazonaws.com/tests/U10176.fits"
with pytest.raises(AstroDBError) as error_message:
_ = ingest_spectrum(temp_db, source="apple", spectrum=spectrum)
assert "Value required for regime" in str(error_message.value)

result = ingest_spectrum(
temp_db, source="apple", spectrum=spectrum, raise_error=False
)
assert result["added"] is False
assert "Value required for regime" in result["message"]


# Ingesting with missing reference
with pytest.raises(AstroDBError) as error_message:
_ = ingest_spectrum(
temp_db,
source="apple",
telescope="IRTF",
instrument="SpeX",
mode="Prism",
regime="nir",
spectrum=spectrum,
obs_date="2020-01-01",
)
assert "NOT NULL constraint failed: Spectra.reference" in str(error_message.value)
# assert "Reference is required" in str(error_message.value)

result = ingest_spectrum(
temp_db, source="apple", regime="nir", spectrum=spectrum, raise_error=False, obs_date="2020-01-01"
)
assert result["added"] is False
assert "NOT NULL constraint failed: Spectra.reference" in result["message"]


# Ingesting with invalid reference (does not already exist)
with pytest.raises(AstroDBError) as error_message:
_ = ingest_spectrum(
temp_db,
source="apple",
regime="nir",
spectrum=spectrum,
telescope="IRTF",
instrument="SpeX",
mode="Prism",
reference="Ref 5",
obs_date="2020-01-01",
)
print(error_message)
# assert "not in Publications table" in str(error_message.value)

result = ingest_spectrum(
temp_db,
source="apple",
regime="nir",
spectrum=spectrum,
telescope="IRTF",
instrument="SpeX",
mode="Prism",
reference="Ref 5",
raise_error=False,
obs_date="2020-01-01",
)
for k, v in result.items():
print(k, v)
assert result["added"] is False


# Ingesting for invalid source (not already in database)
with pytest.raises(AstroDBError) as error_message:
_ = ingest_spectrum(
temp_db,
source="kiwi",
regime="nir",
spectrum=spectrum,
reference="Ref 1",
telescope="IRTF",
instrument="SpeX",
mode="Prism",
obs_date="2020-01-01",
)
# assert "No unique source match for kiwi in the database" in str(error_message.value)
parameters = {"db": temp_db, "spectrum": spectrum}
parameters.update(test_input)

result = ingest_spectrum(
temp_db,
source="kiwi",
regime="nir",
spectrum=spectrum,
reference="Ref 1",
raise_error=False,
telescope="IRTF",
instrument="SpeX",
mode="Prism",
obs_date="2020-01-01",
)
print(result)
assert result["added"] is False


# Ingesting with missing date
# Check that error was raised
with pytest.raises(AstroDBError) as error_message:
_ = ingest_spectrum(
temp_db,
source="apple",
regime="nir",
spectrum=spectrum,
reference="Ref 1",
telescope="IRTF",
instrument="SpeX",
mode="Prism",
)
assert "Invalid date received" in str(error_message.value)
# assert "missing observation date" in str(error_message.value)
_ = ingest_spectrum(**parameters)
assert message in str(error_message.value)

result = ingest_spectrum(
temp_db,
source="apple",
regime="nir",
spectrum=spectrum,
reference="Ref 1",
telescope="IRTF",
instrument="SpeX",
mode="Prism",
raise_error=False,
)
# Suppress error but check that it was still captured
result = ingest_spectrum(**parameters, raise_error=False)
assert result["added"] is False
assert "Invalid date received" in result["message"]
assert message in result["message"]


# Ingesting with invalid regime
with pytest.raises(AstroDBError) as error_message:
_ = ingest_spectrum(
temp_db,
source="orange",
regime="far-uv",
spectrum=spectrum,
reference="Ref 1",
obs_date="1/1/2024",
telescope="Keck I",
instrument="LRIS",
mode="OG570",
)
# assert "not in Regimes table" in str(error_message.value)

result = ingest_spectrum(
temp_db,
source="orange",
regime="far-uv",
spectrum=spectrum,
reference="Ref 1",
obs_date="1/1/2024",
telescope="Keck I",
instrument="LRIS",
mode="OG570",
raise_error=False,
)
print(result)
assert result["added"] is False

@pytest.mark.filterwarnings("ignore:Verification")
@pytest.mark.filterwarnings("ignore", message=".*Card 'AIRMASS' is not FITS standard.*")
@pytest.mark.filterwarnings(
Expand Down

0 comments on commit eecb5e2

Please sign in to comment.