Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project : Immo·Ka9# File : tests/test_mortgage_providers.py10# Module : tests11# Repository : https://git.spboucher.ai/immo-ka12# Created : 2026-08-2413# Last Modified : 2026-08-2414#15# =============================================================================16# AUTHOR & PROFESSIONAL AFFILIATION17# =============================================================================18#19# Author : Simon-Pierre Boucher20# Position : Professor of Finance21# Institution : Université du Québec en Outaouais (UQO)22# Department : Département des sciences administratives23# Organization : SP Boucher24#25# Website : https://spboucher.ai26# Git : https://git.spboucher.ai27#28# =============================================================================29# OFFICIAL CONTACT DIRECTORY30# =============================================================================31#32# General : info@spboucher.ai33# Professional : contact@spboucher.ai34# Direct : simon@spboucher.ai35# Research : research@spboucher.ai36# Development : dev@spboucher.ai37# Support : support@spboucher.ai38# Security/Admin : admin@spboucher.ai39# Notifications : notifications@spboucher.ai40#41# Preferred Contact: contact@spboucher.ai42#43# =============================================================================44# COPYRIGHT & INTELLECTUAL PROPERTY45# =============================================================================46#47# Copyright (c) 202648# Simon-Pierre Boucher49#50# All rights reserved where applicable.51#52# This file is governed by the applicable project license,53# copyright notices, institutional agreements and relevant law.54#55# Unless expressly authorized by the applicable license or56# rights holder, copying, redistribution, sublicensing,57# publication and commercial exploitation are not permitted.58#59# Existing third-party rights and notices remain unaffected.60#61# =============================================================================62# CONFIDENTIALITY & SECURITY63# =============================================================================64#65# Classification : Public66# Distribution : Public repository; use is governed by the project license67# Security Contact : admin@spboucher.ai68#69# Access to this source does not independently grant permission70# to redistribute or disclose confidential materials.71#72# Do not embed credentials, private keys, access tokens,73# certificates or confidential personal information.74#75# =============================================================================76# LICENSE & MAINTENANCE77# =============================================================================78#79# License : None specified (no license file in this repository)80# Maintainer : Simon-Pierre Boucher81# Technical Contact: dev@spboucher.ai82#83# =============================================================================84# END OF SP BOUCHER SOURCE HEADER85# =============================================================================8687# -----------------------------------------------------------------------------88# Immo-Ka — Agrégateur de maisons à vendre (province de Québec)89# Auteur : Simon-Pierre Boucher — contact@spboucher.ai90# tests/test_mortgage_providers.py : les 12 connecteurs sur fixtures réelles91# (payloads capturés le 2026-08-24). Chaque provider doit produire un lot92# propre (validate_batch sans problème) et retrouver des valeurs connues.93# -----------------------------------------------------------------------------94import unittest95from pathlib import Path9697from immoka.mortgage.providers import PROVIDERS98from immoka.mortgage.validate import validate_batch99100FIX = Path(__file__).parent / "fixtures" / "mortgage"101102# slug -> (fixture, nombre de produits attendu)103EXPECTED = {104 "bank_of_canada": ("bank-of-canada.json", 5),105 "bmo": ("bmo.json", 20),106 "cibc": ("cibc.json", 24),107 "desjardins": ("desjardins.html", 19),108 "eq_bank": ("eq-bank.html", 13),109 "first_national": ("first-national.html", 41),110 "mcap": ("mcap.html", 1),111 "national_bank": ("national-bank.html", 19),112 "rbc": ("rbc.json", 19),113 "scotiabank": ("scotiabank.json", 21),114 "tangerine": ("tangerine.json", 8),115 "td": ("td.json", 30),116}117118119def parse_fixture(slug: str) -> list[dict]:120 prov = PROVIDERS[slug]()121 fixture, _ = EXPECTED[slug]122 products = prov.parse((FIX / fixture).read_text())123 if slug == "scotiabank": # deuxième flux : promos variables124 products += prov.parse_promos(125 (FIX / "scotiabank-varmortgage.json").read_text())126 return products127128129def find(products, **crit):130 return [p for p in products131 if all(p.get(k) == v for k, v in crit.items())]132133134class TestRegistry(unittest.TestCase):135 def test_twelve_providers_registered(self):136 self.assertEqual(set(PROVIDERS), set(EXPECTED))137138139class TestAllFixturesClean(unittest.TestCase):140 def test_counts_and_validation(self):141 for slug, (_, expected_n) in EXPECTED.items():142 with self.subTest(provider=slug):143 products = parse_fixture(slug)144 ok, problems = validate_batch(products)145 self.assertEqual(problems, [])146 self.assertEqual(len(ok), expected_n)147 self.assertEqual(len(products), expected_n)148149 def test_every_product_has_provenance(self):150 for slug in EXPECTED:151 for p in parse_fixture(slug):152 with self.subTest(provider=slug, product=p.get("product_name")):153 self.assertTrue(p.get("source_url"))154 self.assertTrue(p.get("institution"))155156157class TestKnownValues(unittest.TestCase):158 """Valeurs lues à la main dans les fixtures commitées."""159160 def test_td_5y_special(self):161 products = parse_fixture("td")162 specials = find(products, rate_type="fixed", term_months=60,163 kind="special", insured_status="uninsured")164 self.assertEqual(len(specials), 1)165 self.assertEqual(specials[0]["rate"], 4.84)166 # l'« affiché » des codes variables TD est le prime : jamais émis167 # comme taux variable, une seule entrée rate_type=other168 primes = find(products, rate_type="other")169 self.assertEqual(len(primes), 1)170 self.assertEqual(primes[0]["product_name"], "TD Mortgage Prime")171172 def test_cibc_prime_and_special(self):173 products = parse_fixture("cibc")174 prime = find(products, product_name="Taux préférentiel CIBC")175 self.assertEqual(len(prime), 1)176 self.assertEqual(prime[0]["rate"], 4.45)177 self.assertEqual(prime[0]["purpose"], "unknown") # exclu du best_rate178 var5 = find(products, rate_type="variable", term_months=60,179 kind="special")180 self.assertEqual({p["rate"] for p in var5} & {4.10}, {4.10})181182 def test_bmo_specials(self):183 products = parse_fixture("bmo")184 fixed3 = find(products, rate_type="fixed", term_months=36,185 kind="special",186 product_name="Fixe fermé 3 ans (offre spéciale)")187 self.assertEqual(len(fixed3), 1)188 self.assertEqual(fixed3[0]["rate"], 4.64)189190 def test_national_bank_posted_grid(self):191 products = parse_fixture("national_bank")192 closed_1y = find(products, rate_type="fixed", term_months=12,193 kind="posted", product_name="Fixe fermé 1 an")194 self.assertEqual([p["rate"] for p in closed_1y], [5.49])195196 def test_eq_bank_showcase(self):197 products = parse_fixture("eq_bank")198 fixe5 = find(products, rate_type="fixed", term_months=60,199 kind="special", product_name="Fixe 5 ans (vitrine EQ)")200 self.assertEqual(len(fixe5), 1)201 self.assertEqual(fixe5[0]["rate"], 4.04)202203 def test_boc_reference_series_excluded_from_market(self):204 # Les séries de référence BdC ne doivent jamais concourir au meilleur205 # taux d'achat : purpose=unknown pour les taux « cible » et prime.206 products = parse_fixture("bank_of_canada")207 for p in find(products, rate_type="other"):208 self.assertEqual(p["purpose"], "unknown")209210 def test_no_prime_rate_masquerades_as_product(self):211 # Aucun provider ne doit publier un « préférentiel » comme taux212 # hypothécaire fixe/variable comparable (purpose=purchase).213 for slug in EXPECTED:214 for p in parse_fixture(slug):215 name = (p.get("product_name") or "").lower()216 if "préférentiel" in name or "prime" in name:217 with self.subTest(provider=slug, product=name):218 self.assertIn(p["rate_type"], ("other",))219 self.assertEqual(p.get("purpose"), "unknown")220221222if __name__ == "__main__":223 unittest.main()224