|
| 1 | +# AdoptAI API Tests |
| 2 | + |
| 3 | +Suite de tests complète pour valider l'API AdoptAI déployée et son handler Lambda. |
| 4 | + |
| 5 | +## Structure |
| 6 | + |
| 7 | +``` |
| 8 | +test/ |
| 9 | +├── python/ # Tests unitaires Python |
| 10 | +│ ├── conftest.py # Fixtures pytest (mocks S3) |
| 11 | +│ ├── test_endpoints.py # Tests des endpoints |
| 12 | +│ ├── test_filters.py # Tests des filtres |
| 13 | +│ ├── test_helpers.py # Tests des fonctions utilitaires |
| 14 | +│ └── test_error_handling.py # Tests de gestion d'erreurs |
| 15 | +├── api.integration.test.ts # Tests en production (API déployée) |
| 16 | +└── README.md # Cette documentation |
| 17 | +``` |
| 18 | + |
| 19 | +## Prérequis |
| 20 | + |
| 21 | +### TypeScript (Vitest) |
| 22 | +```bash |
| 23 | +cd cdk |
| 24 | +yarn install |
| 25 | +``` |
| 26 | + |
| 27 | +### Python (pytest avec uv) |
| 28 | +```bash |
| 29 | +cd cdk |
| 30 | + |
| 31 | +# Créer l'environnement virtuel |
| 32 | +uv venv |
| 33 | + |
| 34 | +# Installer les dépendances |
| 35 | +uv pip install -r requirements-dev.txt |
| 36 | +``` |
| 37 | + |
| 38 | +## Exécution des tests |
| 39 | + |
| 40 | +### Tests unitaires |
| 41 | + |
| 42 | +```bash |
| 43 | +cd cdk |
| 44 | +yarn test |
| 45 | +``` |
| 46 | + |
| 47 | +**Ce qui est testé** : |
| 48 | +- ✅ Tests Lambda Python (pytest) - 70 tests |
| 49 | + - Endpoints et structure des réponses |
| 50 | + - Logique de filtrage (date, stage, time, search, now) |
| 51 | + - Fonctions utilitaires (parse_time, datetime) |
| 52 | + - Gestion d'erreurs et caching |
| 53 | + |
| 54 | +**Total** : 70 tests unitaires |
| 55 | + |
| 56 | +### Tests en production |
| 57 | + |
| 58 | +```bash |
| 59 | +cd cdk |
| 60 | +yarn test:prod |
| 61 | +``` |
| 62 | + |
| 63 | +**Ce qui est testé** : |
| 64 | +- ✅ Appels HTTP réels à https://adoptai.codecrafter.fr/ |
| 65 | +- ✅ Tous les endpoints (/, /llms.txt, /robots.txt, /health, /sessions, /speakers, 404) |
| 66 | +- ✅ Paramètres de filtrage (date, stage, time, search, now) |
| 67 | +- ✅ Validation structure des réponses |
| 68 | +- ✅ Headers CORS et Content-Type UTF-8 |
| 69 | +- ✅ Encodage des caractères spéciaux (•) |
| 70 | + |
| 71 | +**Total** : 20 tests d'intégration |
| 72 | + |
| 73 | +### Rapport de couverture HTML (optionnel) |
| 74 | + |
| 75 | +```bash |
| 76 | +cd cdk |
| 77 | +yarn test:html |
| 78 | +``` |
| 79 | + |
| 80 | +Génère un rapport HTML de couverture pour le code Python. |
| 81 | +**Ouvrir** : `cdk/lib/lambda/htmlcov/index.html` |
| 82 | + |
| 83 | +## Description des tests |
| 84 | + |
| 85 | +### Tests unitaires Python (70 tests) |
| 86 | + |
| 87 | +#### test_endpoints.py (~10 tests) |
| 88 | +- GET / et /llms.txt retournent la documentation |
| 89 | +- GET /robots.txt retourne robots.txt |
| 90 | +- GET /health retourne status healthy |
| 91 | +- GET /sessions retourne toutes les sessions |
| 92 | +- GET /speakers retourne tous les speakers |
| 93 | +- 404 pour chemins inconnus |
| 94 | +- OPTIONS retourne headers CORS |
| 95 | +- Les champs internes (_start_dt, _end_dt) ne sont pas exposés |
| 96 | + |
| 97 | +#### test_filters.py (~35 tests) |
| 98 | +- Filtrage par date (2025-11-25, 2025-11-26) |
| 99 | +- Filtrage par stage (case-insensitive) |
| 100 | +- Filtrage par time (morning/afternoon) |
| 101 | +- Recherche par texte (titre, speaker, company) |
| 102 | +- Filtres combinés |
| 103 | +- Paramètre `now` (sessions en cours et à venir dans 30 min) |
| 104 | +- Recherche speakers |
| 105 | + |
| 106 | +#### test_helpers.py (~27 tests) |
| 107 | +- parse_time: conversion heure → minutes (9:00 AM → 540) |
| 108 | +- parse_session_datetime: parsing date + heure → datetime Paris |
| 109 | +- get_paris_now: retourne datetime timezone Paris |
| 110 | +- filter_sessions_by_now: détection sessions en cours/à venir |
| 111 | +- Edge cases: midnight, noon, sessions sans end time |
| 112 | + |
| 113 | +#### test_error_handling.py (~13 tests) |
| 114 | +- Erreurs S3 (ClientError, JSON invalide) |
| 115 | +- Fallback llms.txt quand fichier absent |
| 116 | +- Caching des données (sessions, speakers, llms.txt) |
| 117 | +- Sessions/speakers avec champs manquants |
| 118 | +- Query strings malformées |
| 119 | + |
| 120 | +### Tests d'intégration API (20 tests) |
| 121 | + |
| 122 | +#### GET / et /llms.txt (~2 tests) |
| 123 | +- Documentation retournée avec UTF-8 correct |
| 124 | +- Caractères spéciaux (•) affichés correctement |
| 125 | + |
| 126 | +#### GET /sessions (~13 tests) |
| 127 | +- Toutes les sessions (243) |
| 128 | +- Filtrage date, stage, time, search |
| 129 | +- Paramètre now (sessions en cours/à venir) |
| 130 | +- Filtres combinés |
| 131 | +- Validation structure JSON |
| 132 | +- Résultats vides |
| 133 | + |
| 134 | +#### GET /speakers (~2 tests) |
| 135 | +- Tous les speakers (499) |
| 136 | +- Recherche speakers |
| 137 | + |
| 138 | +#### Autres endpoints (~3 tests) |
| 139 | +- /robots.txt, /health, 404 |
| 140 | +- Headers CORS (OPTIONS) |
| 141 | + |
| 142 | +## Statistiques |
| 143 | + |
| 144 | +- **Total tests** : 90 tests |
| 145 | + - 70 tests unitaires (Python Lambda) |
| 146 | + - 20 tests d'intégration (API production) |
| 147 | +- **Couverture** : Tous les endpoints et cas d'usage documentés dans llms.txt |
| 148 | + |
| 149 | +## Commandes rapides |
| 150 | + |
| 151 | +```bash |
| 152 | +# Tests unitaires (Python Lambda) |
| 153 | +yarn test |
| 154 | + |
| 155 | +# Tests API en production |
| 156 | +yarn test:prod |
| 157 | + |
| 158 | +# Rapport de couverture HTML |
| 159 | +yarn test:html |
| 160 | +``` |
| 161 | + |
| 162 | +## CI/CD |
| 163 | + |
| 164 | +Pour intégrer dans un pipeline CI/CD : |
| 165 | + |
| 166 | +```yaml |
| 167 | +# .github/workflows/test.yml |
| 168 | +- name: Install dependencies |
| 169 | + run: | |
| 170 | + cd cdk |
| 171 | + yarn install |
| 172 | + uv venv |
| 173 | + uv pip install -r requirements-dev.txt |
| 174 | +
|
| 175 | +- name: Run unit tests |
| 176 | + run: | |
| 177 | + cd cdk |
| 178 | + yarn test |
| 179 | +
|
| 180 | +- name: Run production tests |
| 181 | + run: | |
| 182 | + cd cdk |
| 183 | + yarn test:prod |
| 184 | +``` |
0 commit comments