diff --git a/simple/utils/spectra.py b/simple/utils/spectra.py index 56fe34f8b..bff481fbe 100644 --- a/simple/utils/spectra.py +++ b/simple/utils/spectra.py @@ -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: @@ -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 diff --git a/tests/test_spectra_utils.py b/tests/test_spectra_utils.py index cae4895aa..ebb69e1c0 100644 --- a/tests/test_spectra_utils.py +++ b/tests/test_spectra_utils.py @@ -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(