Spaces:
Sleeping
Sleeping
Merge pull request #9 from RaphaelRIVIERE/feature/coverage
Browse files- README.md +19 -1
- tests/test_db.py +25 -2
README.md
CHANGED
|
@@ -6,6 +6,7 @@ colorTo: green
|
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
---
|
|
|
|
| 9 |
|
| 10 |
# Déployez un modèle de Machine Learning
|
| 11 |
|
|
@@ -37,7 +38,8 @@ mon-projet/
|
|
| 37 |
│
|
| 38 |
├── tests/
|
| 39 |
│ ├── test_api.py
|
| 40 |
-
│
|
|
|
|
| 41 |
│
|
| 42 |
├── .github/workflows/
|
| 43 |
├── requirements.txt
|
|
@@ -327,6 +329,20 @@ Rapport HTML navigable (généré dans `htmlcov/`) :
|
|
| 327 |
pytest tests/ --cov=app --cov-report=term-missing --cov-report=html
|
| 328 |
```
|
| 329 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
### Structure des tests
|
| 331 |
|
| 332 |
| Fichier | Contenu |
|
|
@@ -334,6 +350,8 @@ pytest tests/ --cov=app --cov-report=term-missing --cov-report=html
|
|
| 334 |
| `tests/conftest.py` | Fixtures partagées (client de test, payload valide) |
|
| 335 |
| `tests/test_api.py` | Tests des endpoints HTTP (health, predict, auth) |
|
| 336 |
| `tests/test_model.py` | Tests du pipeline ML (chargement, prédiction, seuil) |
|
|
|
|
|
|
|
| 337 |
|
| 338 |
## Déploiement
|
| 339 |
|
|
|
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
+

|
| 10 |
|
| 11 |
# Déployez un modèle de Machine Learning
|
| 12 |
|
|
|
|
| 38 |
│
|
| 39 |
├── tests/
|
| 40 |
│ ├── test_api.py
|
| 41 |
+
│ ├── test_model.py
|
| 42 |
+
│ └── test_db.py
|
| 43 |
│
|
| 44 |
├── .github/workflows/
|
| 45 |
├── requirements.txt
|
|
|
|
| 329 |
pytest tests/ --cov=app --cov-report=term-missing --cov-report=html
|
| 330 |
```
|
| 331 |
|
| 332 |
+
### Résultat de couverture
|
| 333 |
+
|
| 334 |
+
Dernière mesure : **95%** (13 tests, 193 instructions)
|
| 335 |
+
|
| 336 |
+
| Fichier | Couverture |
|
| 337 |
+
|---|---|
|
| 338 |
+
| `app/db/crud.py` | 100% |
|
| 339 |
+
| `app/db/models.py` | 100% |
|
| 340 |
+
| `app/schemas/prediction.py` | 100% |
|
| 341 |
+
| `app/routes/predict.py` | 98% |
|
| 342 |
+
| `app/main.py` | 96% |
|
| 343 |
+
| `app/db/session.py` | 56% *(infrastructure PostgreSQL, non testée en isolation)* |
|
| 344 |
+
|
| 345 |
+
|
| 346 |
### Structure des tests
|
| 347 |
|
| 348 |
| Fichier | Contenu |
|
|
|
|
| 350 |
| `tests/conftest.py` | Fixtures partagées (client de test, payload valide) |
|
| 351 |
| `tests/test_api.py` | Tests des endpoints HTTP (health, predict, auth) |
|
| 352 |
| `tests/test_model.py` | Tests du pipeline ML (chargement, prédiction, seuil) |
|
| 353 |
+
| `tests/test_db.py` | Tests de la couche base de données (CRUD, contraintes d'intégrité) |
|
| 354 |
+
|
| 355 |
|
| 356 |
## Déploiement
|
| 357 |
|
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()
|