Spaces:
Sleeping
Sleeping
test(db): add delete/integrity coverage
Browse files- tests/test_db.py +25 -2
tests/test_db.py
CHANGED
|
@@ -33,17 +33,40 @@ def db_session():
|
|
| 33 |
Base.metadata.drop_all(engine)
|
| 34 |
|
| 35 |
|
| 36 |
-
def
|
| 37 |
record = log_prediction(db_session, VALID_INPUT, VALID_OUTPUT)
|
| 38 |
assert record.id is not None
|
| 39 |
assert record.prediction == 0
|
| 40 |
assert record.probabilite == 0.30
|
| 41 |
|
| 42 |
|
| 43 |
-
def
|
| 44 |
log_prediction(db_session, VALID_INPUT, VALID_OUTPUT)
|
| 45 |
result = db_session.query(Prediction).first()
|
| 46 |
assert result is not None
|
| 47 |
assert result.age == 35
|
| 48 |
assert result.poste == "Consultant"
|
| 49 |
assert result.prediction == 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
Base.metadata.drop_all(engine)
|
| 34 |
|
| 35 |
|
| 36 |
+
def test_log_prediction_creates_record(db_session):
|
| 37 |
record = log_prediction(db_session, VALID_INPUT, VALID_OUTPUT)
|
| 38 |
assert record.id is not None
|
| 39 |
assert record.prediction == 0
|
| 40 |
assert record.probabilite == 0.30
|
| 41 |
|
| 42 |
|
| 43 |
+
def test_log_prediction_is_retrievable(db_session):
|
| 44 |
log_prediction(db_session, VALID_INPUT, VALID_OUTPUT)
|
| 45 |
result = db_session.query(Prediction).first()
|
| 46 |
assert result is not None
|
| 47 |
assert result.age == 35
|
| 48 |
assert result.poste == "Consultant"
|
| 49 |
assert result.prediction == 0
|
| 50 |
+
|
| 51 |
+
def test_delete_prediction(db_session):
|
| 52 |
+
record = log_prediction(db_session, VALID_INPUT, VALID_OUTPUT)
|
| 53 |
+
record_id = record.id
|
| 54 |
+
|
| 55 |
+
db_session.delete(record)
|
| 56 |
+
db_session.commit()
|
| 57 |
+
|
| 58 |
+
result = db_session.get(Prediction, record_id)
|
| 59 |
+
assert result is None
|
| 60 |
+
|
| 61 |
+
def test_missing_required_field_raises_integrity_error(db_session):
|
| 62 |
+
from sqlalchemy.exc import IntegrityError
|
| 63 |
+
|
| 64 |
+
# On crée un Prediction en omettant le champ 'prediction' → NULL interdit
|
| 65 |
+
invalid_record = Prediction(
|
| 66 |
+
**VALID_INPUT.model_dump(),
|
| 67 |
+
probabilite=0.30,
|
| 68 |
+
# prediction= intentionnellement absent → None → violation NOT NULL
|
| 69 |
+
)
|
| 70 |
+
db_session.add(invalid_record)
|
| 71 |
+
with pytest.raises(IntegrityError):
|
| 72 |
+
db_session.commit()
|