Spaces:
Build error
Build error
| # test_tva.py — COMPREHENSIVE VAT 2025 TEST SUITE | |
| # Run with: python test_tva.py | |
| # Requires: calc_TVA.py and VAT_2025.json in same folder | |
| import json | |
| import sys | |
| from pathlib import Path | |
| # -------------------------------------------------------------- | |
| # 1. ADD CURRENT DIR TO PATH | |
| # -------------------------------------------------------------- | |
| TEST_DIR = Path(__file__).parent | |
| sys.path.insert(0, str(TEST_DIR)) | |
| # -------------------------------------------------------------- | |
| # 2. IMPORT calc_TVA AND INJECT JSON PATH | |
| # -------------------------------------------------------------- | |
| from calc_TVA import calculate_tva | |
| import calc_TVA | |
| JSON_PATH = TEST_DIR / "VAT_2025.json" | |
| if not JSON_PATH.exists(): | |
| print(f"ERROR: {JSON_PATH} not found!") | |
| sys.exit(1) | |
| calc_TVA.__dict__["_JSON_PATH"] = str(JSON_PATH) | |
| # -------------------------------------------------------------- | |
| # 3. TEST CASES | |
| # -------------------------------------------------------------- | |
| TESTS = [ | |
| # (amount, category, expected_tva, expected_rate_or_msg) | |
| (15000, "restauration", 1500.0, "10%"), | |
| (15000, "restaurant", 1500.0, "10%"), | |
| (15000, "repas", 1500.0, "10%"), | |
| (1200, "livre", 66.0, "5.5%"), | |
| (1200, "livres", 66.0, "5.5%"), | |
| (1200, "produits alimentaires", 66.0, "5.5%"), | |
| (1200, "cantine scolaire", 66.0, "5.5%"), | |
| (1200, "cinéma", 66.0, "5.5%"), | |
| (1200, "travaux énergétiques", 66.0, "5.5%"), | |
| (1200, "logements sociaux", 66.0, "5.5%"), | |
| (500, "médicaments remboursables", 10.5, "2.1%"), | |
| (500, "presse", 10.5, "2.1%"), | |
| (500, "animaux de boucherie", 10.5, "2.1%"), | |
| (10000, "voiture", 2000.0, "20%"), # default | |
| (10000, "téléphone", 2000.0, "20%"), | |
| (10000, "meuble", 2000.0, "20%"), | |
| (10000, "default", 2000.0, "20%"), | |
| (10000, "corse", None, "variable"), | |
| (10000, "dom", None, "variable"), | |
| (10000, "guadeloupe", None, "variable"), | |
| (10000, "outre-mer", None, "variable"), | |
| (0, "restauration", 0.0, "10%"), | |
| (99.99, "livre", 5.5, "5.5%"), | |
| ] | |
| # -------------------------------------------------------------- | |
| # 4. RUN TESTS | |
| # -------------------------------------------------------------- | |
| def run_tests(): | |
| passed = 0 | |
| failed = 0 | |
| print(f"{'='*60}") | |
| print(f" TVA 2025 TEST SUITE – {len(TESTS)} CASES") | |
| print(f"{'='*60}") | |
| for i, (amount, category, exp_tva, exp_hint) in enumerate(TESTS, 1): | |
| try: | |
| result = calculate_tva(amount, category) | |
| tva = result.get("result") | |
| expl = result.get("explanation", "") | |
| src = result.get("source", "") | |
| # ——— CASE 1: Variable rate ——— | |
| if exp_hint == "variable": | |
| if tva is None and "variable" in expl.lower(): | |
| status = "PASSED" | |
| passed += 1 | |
| else: | |
| status = "FAILED" | |
| failed += 1 | |
| # ——— CASE 2: Fixed rate ——— | |
| else: | |
| expected_rate = float(exp_hint.strip("%")) / 100 | |
| expected_tva_calc = round(amount * expected_rate, 2) | |
| if abs((tva or 0) - expected_tva_calc) <= 0.01: | |
| status = "PASSED" | |
| passed += 1 | |
| else: | |
| status = "FAILED" | |
| failed += 1 | |
| # ——— PRINT RESULT ——— | |
| print(f"[{status}] Test {i:02d}") | |
| print(f" → {amount:,.2f} € | '{category}'") | |
| if tva is not None: | |
| print(f" → TVA = {tva:,.2f} € | {expl}") | |
| else: | |
| print(f" → {expl}") | |
| print(f" → Source: {src}") | |
| print() | |
| except Exception as e: | |
| print(f"[ERROR] Test {i:02d} crashed: {e}\n") | |
| failed += 1 | |
| # ——— SUMMARY ——— | |
| print(f"{'='*60}") | |
| print(f" SUMMARY: {passed} PASSED, {failed} FAILED") | |
| if failed == 0: | |
| print(f" ALL TESTS PASSED! calc_TVA.py is 100% correct.") | |
| else: | |
| print(f" FIX THE FAILED CASES ABOVE.") | |
| print(f"{'='*60}") | |
| # -------------------------------------------------------------- | |
| # 5. EXECUTE | |
| # -------------------------------------------------------------- | |
| if __name__ == "__main__": | |
| run_tests() |