Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project : Immo·Ka9# File : tests/test_mortgage_cmhc.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_cmhc.py : assurance prêt hypothécaire (SCHL) — mise de91# fonds minimale, barème de primes, admissibilité, TVQ sur la prime.92# -----------------------------------------------------------------------------93import unittest9495from immoka.mortgage import cmhc969798class TestMinDownPayment(unittest.TestCase):99 def test_tiers(self):100 self.assertEqual(cmhc.min_down_payment(400_000), 20_000) # 5 %101 self.assertEqual(cmhc.min_down_payment(500_000), 25_000)102 self.assertEqual(cmhc.min_down_payment(600_000), 35_000) # 25k + 10k103 self.assertEqual(cmhc.min_down_payment(1_000_000), 75_000)104 self.assertEqual(cmhc.min_down_payment(1_500_000), 300_000) # 20 %105 self.assertEqual(cmhc.min_down_payment(2_000_000), 400_000)106107 def test_zero(self):108 self.assertEqual(cmhc.min_down_payment(0), 0.0)109110111class TestPremiumRate(unittest.TestCase):112 def test_bands(self):113 self.assertEqual(cmhc.premium_rate(0.65), 0.0060)114 self.assertEqual(cmhc.premium_rate(0.80), 0.0240)115 self.assertEqual(cmhc.premium_rate(0.90), 0.0310)116 self.assertEqual(cmhc.premium_rate(0.95), 0.0400)117 self.assertIsNone(cmhc.premium_rate(0.96)) # > 95 % : non assurable118119 def test_30y_surcharge(self):120 self.assertAlmostEqual(cmhc.premium_rate(0.95, amort_years=30), 0.0420)121122123class TestInsuranceQuote(unittest.TestCase):124 def test_insured_600k_40k_down(self):125 # prêt 560 k$, RPV 93,3 % → prime 4 % = 22 400 $, TVQ 9,975 %126 q = cmhc.insurance_quote(600_000, 40_000)127 self.assertTrue(q["required"] and q["eligible"])128 self.assertEqual(q["premium"], 22_400)129 self.assertEqual(q["total_mortgage"], 582_400)130 # taxe sur les primes d'assurance (Revenu Québec, 2026-04-09) :131 # 9 % jusqu'au 2026-12-31, 9,975 % ensuite132 self.assertEqual(q["qc_tax"], round(22_400 * cmhc.qc_tax_rate(), 2))133 self.assertEqual(cmhc.qc_tax_rate("2026-11-25"), 0.09)134 self.assertEqual(cmhc.qc_tax_rate("2026-12-31"), 0.09)135 self.assertEqual(cmhc.qc_tax_rate("2027-01-01"), 0.09975)136 self.assertEqual(q["premium_rate"], 4.0)137138 def test_uninsured_20_pct_down(self):139 q = cmhc.insurance_quote(500_000, 100_000)140 self.assertFalse(q["required"])141 self.assertEqual(q["premium"], 0.0)142 self.assertEqual(q["total_mortgage"], 400_000)143144 def test_over_max_price_not_eligible(self):145 q = cmhc.insurance_quote(1_600_000, 200_000)146 self.assertTrue(q["required"])147 self.assertFalse(q["eligible"])148 self.assertEqual(q["premium"], 0.0)149 self.assertTrue(any("1,5 M$" in i for i in q["issues"]))150151 def test_below_min_down_not_eligible(self):152 q = cmhc.insurance_quote(600_000, 20_000) # min légal = 35 000 $153 self.assertFalse(q["eligible"])154 self.assertTrue(any("minimum légal" in i for i in q["issues"]))155156 def test_insured_30y_flagged(self):157 q = cmhc.insurance_quote(400_000, 40_000, amort_years=30)158 self.assertTrue(q["eligible"])159 self.assertTrue(any("premier acheteur" in i for i in q["issues"]))160 # surprime 0,20 % appliquée (RPV 90 % : 3,10 + 0,20 = 3,30 %)161 self.assertEqual(q["premium_rate"], 3.30)162163 def test_insured_over_30y_refused(self):164 q = cmhc.insurance_quote(400_000, 40_000, amort_years=35)165 self.assertFalse(q["eligible"])166167 def test_invalid_params(self):168 q = cmhc.insurance_quote(0, 0)169 self.assertFalse(q["eligible"])170 self.assertIn("paramètres invalides", q["issues"])171172173if __name__ == "__main__":174 unittest.main()175