Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kelle committed Jul 23, 2024
1 parent 2f1d7ed commit 570f0e3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
3 changes: 1 addition & 2 deletions simple/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ class SpectralTypes(Base):
regime = Column(
String(30),
ForeignKey("Regimes.regime", ondelete="cascade", onupdate="cascade"),
nullable=True,
primary_key=True,
)
adopted = Column(Boolean)
Expand All @@ -318,7 +317,7 @@ class SpectralTypes(Base):
primary_key=True,
)

@validates("source", "spectral_type_code", "reference")
@validates("source", "spectral_type_code", "regime", "reference")
def validate_required(self, key, value):
if value is None:
raise ValueError(f"Value required for {key}")
Expand Down
49 changes: 27 additions & 22 deletions simple/utils/spectral_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
]

logger = logging.getLogger("SIMPLE")
logger.setLevel(logging.INFO) # set default log level to INFO


def ingest_spectral_type(
Expand Down Expand Up @@ -72,6 +73,26 @@ def ingest_spectral_type(
db.query(db.SpectralTypes).filter(db.SpectralTypes.c.source == db_name).table()
)

# Check for duplicates
duplicate_check = (
db.query(db.SpectralTypes.c.source)
.filter(
and_(
db.SpectralTypes.c.source == db_name,
db.SpectralTypes.c.regime == regime,
db.SpectralTypes.c.reference == reference,
)
)
.count()
)
if duplicate_check > 0:
msg = f"Spectral type already in the database: {db_name}, {regime}, {reference}"
if raise_error:
logger.error(msg)
raise AstroDBError(msg)
else:
logger.warning(msg)

# set adopted flag
if source_spt_data is None or len(source_spt_data) == 0:
adopted = True
Expand Down Expand Up @@ -99,9 +120,12 @@ def ingest_spectral_type(
adopted = True
logger.debug(f"The new spectral type's adopted flag is:, {adopted}")
else:
msg = "Unexpected state"
logger.error(msg)
raise RuntimeError
msg = f"Unexpected state while ingesting {source}"
if raise_error:
logger.error(msg)
raise RuntimeError
else:
logger.warning(msg)

if spectral_type_code is None:
spectral_type_code = convert_spt_string_to_code(spectral_type_string)
Expand All @@ -121,25 +145,6 @@ def ingest_spectral_type(
"reference": reference,
}

check = (
db.query(db.SpectralTypes.c.source)
.filter(
and_(
db.SpectralTypes.c.source == db_name,
db.SpectralTypes.c.regime == regime,
db.SpectralTypes.c.reference == reference,
)
)
.count()
)
if check == 1:
msg = f"Spectral type for {db_name} already in the database"
if raise_error:
logger.error(msg)
raise AstroDBError(msg)
else:
logger.warning(msg)

logger.debug(f"Trying to insert {spt_data} into Spectral Types table ")

try:
Expand Down
4 changes: 1 addition & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ def test_ingest_spectral_type_errors(temp_db):
reference=spt_data4["reference"],
regime=spt_data4["regime"],
)
assert "Spectral type for Fake 1 already in the database" in str(
error_message.value
)
assert "Spectral type already in the database" in str(error_message.value)
# assert "The publication does not exist in the database" in str(error_message.value)


Expand Down

0 comments on commit 570f0e3

Please sign in to comment.