SPB Git

Repository Public

test_mortgage_calc.py 9.1 KB 223 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project          : Immo·Ka9# File             : tests/test_mortgage_calc.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_calc.py : mathématiques hypothécaires canadiennes —91#   valeurs de référence vérifiables à la main (composition semestrielle).92# -----------------------------------------------------------------------------93import unittest9495from immoka.mortgage import calc969798class TestPayment(unittest.TestCase):99    def test_golden_semi_annual(self):100        # Référence canonique : 100 000 $ @ 6 % / 25 ans, composition101        # semestrielle (Loi sur l'intérêt) → 639,81 $/mois.102        self.assertEqual(calc.payment(100_000, 6.0, 25, "monthly"), 639.81)103104    def test_monthly_compounding_differs(self):105        # La formule américaine (composition mensuelle) donne ~644,30 $ :106        # si les deux coïncidaient, la convention serait mal appliquée.107        us = calc.payment(100_000, 6.0, 25, "monthly", compounding="monthly")108        self.assertAlmostEqual(us, 644.30, delta=0.02)109        self.assertGreater(us, calc.payment(100_000, 6.0, 25, "monthly"))110111    def test_zero_rate(self):112        self.assertEqual(calc.payment(120_000, 0.0, 10, "monthly"), 1000.0)113114    def test_accelerated_biweekly_is_half_monthly(self):115        m = calc.payment(400_000, 5.0, 25, "monthly")116        self.assertEqual(calc.payment(400_000, 5.0, 25, "accelerated-biweekly"),117                         round(m / 2, 2))118119    def test_accelerated_weekly_is_quarter_monthly(self):120        m = calc.payment(400_000, 5.0, 25, "monthly")121        self.assertEqual(calc.payment(400_000, 5.0, 25, "accelerated-weekly"),122                         round(m / 4, 2))123124    def test_negative_rate_raises(self):125        with self.assertRaises(ValueError):126            calc.periodic_rate(-1.0)127128129class TestSchedule(unittest.TestCase):130    def test_amortizes_to_zero(self):131        rows = calc.schedule(300_000, 4.5, 25, "monthly")132        self.assertEqual(rows[-1]["balance"], 0.0)133        # 25 ans × 12 : dernier versement ajusté, ±1 période d'arrondi134        self.assertAlmostEqual(len(rows), 300, delta=1)135136    def test_principal_sums_to_loan(self):137        rows = calc.schedule(250_000, 5.25, 20, "monthly")138        self.assertAlmostEqual(sum(r["principal"] for r in rows), 250_000,139                               delta=0.05)140141    def test_accelerated_pays_off_early(self):142        self.assertLess(calc.payoff_years(300_000, 5.0, 25,143                                          "accelerated-biweekly"), 23.0)144        # ±1 période d'arrondi sur le dernier versement ajusté145        self.assertAlmostEqual(calc.payoff_years(300_000, 5.0, 25, "monthly"),146                               25.0, delta=0.1)147148149class TestTermSummary(unittest.TestCase):150    def test_five_year_term(self):151        s = calc.term_summary(300_000, 4.5, 25, 60, "monthly")152        self.assertEqual(s["payments_in_term"], 60)153        self.assertFalse(s["paid_off"])154        self.assertGreater(s["balance_end_of_term"], 0)155        self.assertLess(s["balance_end_of_term"], 300_000)156        self.assertAlmostEqual(s["principal_paid"] + s["balance_end_of_term"],157                               300_000, delta=0.05)158        self.assertEqual(s["annual_cost"], round(s["payment"] * 12, 2))159160161class TestInverse(unittest.TestCase):162    def test_max_loan_inverse_of_payment(self):163        pmt = calc.payment(350_000, 4.79, 25, "monthly")164        self.assertAlmostEqual(calc.max_loan(pmt, 4.79, 25, "monthly"),165                               350_000, delta=5)166167    def test_required_rate_inverse_of_payment(self):168        pmt = calc.payment(350_000, 4.79, 25, "monthly")169        self.assertAlmostEqual(calc.required_rate(350_000, pmt, 25, "monthly"),170                               4.79, delta=0.01)171172    def test_required_rate_none_if_impossible(self):173        # même 0 % exige > 1000 $/mois pour 350 k$ sur 25 ans174        self.assertIsNone(calc.required_rate(350_000, 1000, 25, "monthly"))175176    def test_max_loan_accelerated(self):177        self.assertEqual(calc.max_loan(1000, 5.0, 25, "accelerated-biweekly"),178                         calc.max_loan(2000, 5.0, 25, "monthly"))179180181class TestStress(unittest.TestCase):182    def test_qualifying_rate(self):183        self.assertEqual(calc.qualifying_rate(4.19), 6.19)184        self.assertEqual(calc.qualifying_rate(2.5), 5.25)   # plancher185        self.assertEqual(calc.qualifying_rate(3.25), 5.25)  # 3.25+2 = plancher186187    def test_stress_scenarios(self):188        rows = calc.stress_scenarios(300_000, 4.0, 25)189        self.assertEqual([r["bump"] for r in rows], [0.0, 1.0, 2.0, 3.0])190        self.assertEqual(rows[0]["payment"],191                         calc.payment(300_000, 4.0, 25, "monthly"))192        pays = [r["payment"] for r in rows]193        self.assertEqual(pays, sorted(pays))  # monte avec le taux194195    def test_renewal(self):196        r = calc.renewal_scenarios(300_000, 4.5, 25, 60)197        self.assertEqual(r["remaining_amortization_years"], 20.0)198        self.assertGreater(r["balance_at_renewal"], 0)199        self.assertTrue(all(s["payment"] > 0 for s in r["scenarios"]))200201202class TestGdsTds(unittest.TestCase):203    def test_ratios(self):204        # 120 k$/an → 10 000 $/mois ; logement = 2000+300+100+0,5×400 = 2600205        r = calc.gds_tds(120_000, 2000, 300, 100, 400, 500)206        self.assertEqual(r["gds"], 26.0)207        self.assertEqual(r["tds"], 31.0)208        self.assertTrue(r["gds_ok"] and r["tds_ok"])209210    def test_over_limits(self):211        r = calc.gds_tds(60_000, 2000, 300, 100, 0, 500)212        self.assertFalse(r["gds_ok"])213        self.assertFalse(r["tds_ok"])214215    def test_no_income(self):216        r = calc.gds_tds(0, 2000)217        self.assertIsNone(r["gds"])218        self.assertIsNone(r["tds_ok"])219220221if __name__ == "__main__":222    unittest.main()223
Files main
Menu

Theme