Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project : Vrai-Prix Android10 * File : app/src/test/kotlin/com/spboucher/vraiprix/DecodingTest.kt11 * Description : décodage tolérant de réponses réelles de l'API v3 (fiches12 * aux quatre régimes, position, comparables, ventes,13 * inscriptions, marché, indice, territoires, modèle,14 * recherche, estimation libre, carte).15 * Module : test16 * Repository : https://git.spboucher.ai/vrai-prix-android17 * Project Website : https://www.vrai-prix.com18 * Created : 2026-11-2919 * Last Modified : 2026-11-2920 *21 * =============================================================================22 * AUTHOR & PROFESSIONAL AFFILIATION23 * =============================================================================24 *25 * Author : Simon-Pierre Boucher26 * Position : Professor of Finance27 * Institution : Université du Québec en Outaouais (UQO)28 * Department : Département des sciences administratives29 * Organization : SP Boucher30 *31 * Website : https://spboucher.ai32 * Git : https://git.spboucher.ai33 *34 * =============================================================================35 * OFFICIAL CONTACT DIRECTORY36 * =============================================================================37 *38 * General : info@spboucher.ai39 * Professional : contact@spboucher.ai40 * Direct : simon@spboucher.ai41 * Research : research@spboucher.ai42 * Development : dev@spboucher.ai43 * Support : support@spboucher.ai44 * Security/Admin : admin@spboucher.ai45 * Notifications : notifications@spboucher.ai46 *47 * Preferred Contact: dev@spboucher.ai48 *49 * =============================================================================50 * COPYRIGHT & INTELLECTUAL PROPERTY51 * =============================================================================52 *53 * Copyright (c) 202654 * Simon-Pierre Boucher55 *56 * All rights reserved where applicable.57 *58 * This file is governed by the applicable project license,59 * copyright notices, institutional agreements and relevant law.60 *61 * Unless expressly authorized by the applicable license or62 * rights holder, copying, redistribution, sublicensing,63 * publication and commercial exploitation are not permitted.64 *65 * Existing third-party rights and notices remain unaffected.66 *67 * =============================================================================68 * CONFIDENTIALITY & SECURITY69 * =============================================================================70 *71 * Classification : Public72 * Distribution : Public repository; use is governed by the project license73 * Security Contact : admin@spboucher.ai74 *75 * Access to this source does not independently grant permission76 * to redistribute or disclose confidential materials.77 *78 * Do not embed credentials, private keys, access tokens,79 * certificates or confidential personal information.80 *81 * =============================================================================82 * LICENSE & MAINTENANCE83 * =============================================================================84 *85 * License : None specified (no license file in this repository)86 * Maintainer : Simon-Pierre Boucher87 * Technical Contact: dev@spboucher.ai88 *89 * =============================================================================90 * END OF SP BOUCHER SOURCE HEADER91 * =============================================================================92 */9394package com.spboucher.vraiprix9596import com.spboucher.vraiprix.core.json.J97import com.spboucher.vraiprix.core.model.Comparables98import com.spboucher.vraiprix.core.model.FreeEstimate99import com.spboucher.vraiprix.core.model.IndexSeries100import com.spboucher.vraiprix.core.model.Listing101import com.spboucher.vraiprix.core.model.ListingsPage102import com.spboucher.vraiprix.core.model.Market103import com.spboucher.vraiprix.core.model.MarketSeries104import com.spboucher.vraiprix.core.model.ModelInfo105import com.spboucher.vraiprix.core.model.MotifFamily106import com.spboucher.vraiprix.core.model.PositionAnswer107import com.spboucher.vraiprix.core.model.PricePosition108import com.spboucher.vraiprix.core.model.Property109import com.spboucher.vraiprix.core.model.PropertyPoints110import com.spboucher.vraiprix.core.model.Ranking111import com.spboucher.vraiprix.core.model.SaleDetail112import com.spboucher.vraiprix.core.model.SalesPage113import com.spboucher.vraiprix.core.model.SearchResult114import com.spboucher.vraiprix.core.model.StreetUnits115import com.spboucher.vraiprix.core.model.Territory116import com.spboucher.vraiprix.core.model.TerritoryList117import com.spboucher.vraiprix.core.model.Validation118import com.spboucher.vraiprix.core.model.ValueStatus119import org.junit.Assert.assertEquals120import org.junit.Assert.assertFalse121import org.junit.Assert.assertNotNull122import org.junit.Assert.assertNull123import org.junit.Assert.assertTrue124import org.junit.Test125126class DecodingTest {127 private fun fiche(name: String) = Property.from(Fixtures.json(name))128129 @Test fun jsonValueIsTolerant() {130 val j = J.parse("""{"a":"12","b":3.0,"c":null,"d":[1,"x"],"e":{"f":true},"id":65005774217243190030000,"g":" "}""")!!131 assertEquals(12.0, j["a"].double!!, 0.0)132 assertEquals(3, j["b"].int)133 assertEquals("3", j["b"].string)134 assertTrue(j["c"].isNull)135 assertNull(j["c"].text)136 assertNull(j["absent"]["plus"]["loin"].text)137 assertEquals(2, j["d"].items.size)138 assertEquals(true, j["e"]["f"].bool)139 assertNull(j["e"].text)140 // un identifiant de 23 chiffres envoyé comme nombre reste exact141 assertEquals("65005774217243190030000", j["id"].text)142 assertNull(j["g"].text)143 assertNull(J.parse("<!DOCTYPE html><html></html>"))144 }145146 @Test fun firmValue() {147 for (name in listOf("v3-fiche-valeur", "prod/v3-fiche-valeur")) {148 val p = fiche(name)149 assertEquals("23027379018778110000000", p.id)150 assertEquals(ValueStatus.VALEUR, p.valeur.status)151 val a = p.valeur.amounts!!152 assertTrue(a.low <= a.value && a.value <= a.high)153 assertTrue(p.valeur.isFirm)154 assertTrue(p.title.isNotEmpty())155 assertFalse("l'adresse du rôle n'est plus en capitales", p.title.any { it.isLetter() } && p.title.filter { it.isLetter() }.drop(1).all { it.isUpperCase() })156 assertTrue(p.territoires.any { it.niveau == "municipalite" })157 assertNotNull(p.role.montant)158 assertNotNull(p.annee.value)159 }160 }161162 @Test fun orderOfMagnitude() {163 for (name in listOf("v3-fiche-ordre", "v3-fiche-chalet", "prod/v3-fiche-ordre")) {164 val v = fiche(name).valeur165 assertEquals(name, ValueStatus.ORDRE, v.status)166 assertNotNull(name, v.amounts)167 assertFalse(v.isFirm)168 assertTrue(v.isApproximate)169 assertNotNull(v.motif)170 }171 }172173 @Test fun abstentionHasNoAmount() {174 for (name in listOf("v3-fiche-abstention", "prod/v3-fiche-abstention")) {175 val v = fiche(name).valeur176 assertEquals(ValueStatus.ABSTENTION, v.status)177 assertNull(v.amounts)178 assertTrue(v.motif!!.libelle.isNotEmpty())179 }180 }181182 @Test fun outOfScopeHasNoAmount() {183 for (name in listOf("v3-fiche-hors-champ", "prod/v3-fiche-hors-champ")) {184 val v = fiche(name).valeur185 assertEquals(ValueStatus.HORS_CHAMP, v.status)186 assertNull(v.amounts)187 assertEquals(MotifFamily.HORS_CHAMP, v.motif!!.family)188 }189 }190191 @Test fun listedPropertyCarriesItsListingAndPosition() {192 for (name in listOf("v3-fiche-inscrite", "prod/v3-fiche-inscrite")) {193 val p = fiche(name)194 val l = p.listing!!195 assertEquals("duproprio:1000921", l.id)196 assertEquals("unite", l.niveau)197 assertEquals(p.id, l.proprieteId)198 assertNotNull(l.prixDemande)199 assertNotNull(l.position!!.position)200 // le prix comparé est le prix demandé ramené au prix de vente attendu201 assertTrue(l.position!!.prixCompare!! < l.prixDemande!!)202 assertTrue(l.miniature!!.startsWith("/api/img"))203 }204 }205206 @Test fun positionAnswer() {207 val a = PositionAnswer.from(Fixtures.json("v3-position"))208 assertNotNull(a.block.position)209 assertNotNull(a.block.ecartPct)210 val prod = PositionAnswer.from(Fixtures.json("prod/v3-position"))211 assertNotNull(prod.block.position)212 // hors champ : aucune position, un motif213 val hc = PositionAnswer.from(Fixtures.json("v3-position-hc"))214 assertNull(hc.block.position)215 assertNotNull(hc.block.motifLibelle ?: hc.block.motif)216 }217218 @Test fun comparables() {219 for (name in listOf("v3-comparables", "prod/v3-comparables")) {220 val c = Comparables.from(Fixtures.json(name))221 assertTrue(c.items.isNotEmpty())222 assertTrue(c.items.all { it.venteId.isNotEmpty() && it.prix != null })223 assertEquals(c.items.size, c.items.map { it.venteId }.distinct().size)224 }225 }226227 @Test fun salesPages() {228 for (name in listOf("v3-ventes", "v3-ventes-marche", "prod/v3-ventes")) {229 val s = SalesPage.from(Fixtures.json(name))230 assertTrue(name, s.items.isNotEmpty())231 assertTrue(s.items.all { it.id.isNotEmpty() && it.prix != null && it.date != null })232 assertNotNull(s.total)233 }234 assertTrue(SalesPage.from(Fixtures.json("v3-ventes-marche")).items.all { it.deMarche == true })235 assertNotNull(SalesPage.from(Fixtures.json("prod/v3-ventes")).nextCursor)236 val bbox = SalesPage.from(Fixtures.json("v3-ventes-bbox"))237 assertTrue(bbox.items.any { it.hasCoordinate })238 }239240 @Test fun saleDetail() {241 val d = SaleDetail.from(Fixtures.json("v3-vente"))242 assertEquals("A8W6TE8RZZexotmTJsnvAu", d.vente.id)243 assertNotNull(d.vente.prix)244 assertNotNull(d.niveau)245 }246247 @Test fun listings() {248 for (name in listOf("v3-inscriptions", "prod/v3-inscriptions")) {249 val p = ListingsPage.from(Fixtures.json(name))250 assertTrue(p.items.isNotEmpty())251 assertTrue(p.items.all { it.id.isNotEmpty() })252 assertTrue(p.positions.isNotEmpty())253 // aucun prix situé hors d'une valeur ferme254 for (l in p.items) if (l.position?.position != null) assertTrue(l.id, l.valeur?.isFirm ?: (l.position?.value != null))255 }256 val one = Listing.from(Fixtures.json("v3-inscription"))257 assertEquals("duproprio:1000921", one.id)258 assertTrue(one.images.isNotEmpty() || one.image != null)259 assertTrue(ListingsPage.from(Fixtures.json("v3-inscriptions-bbox")).items.any { it.hasCoordinate })260 }261262 @Test fun marketKeepsEachNatureApart() {263 for (name in listOf("v3-marche", "v3-marche-qc", "prod/v3-marche")) {264 val m = Market.from(Fixtures.json(name))265 assertNotNull(name, m.territoire)266 assertNotNull(m.ventes)267 assertNotNull(m.valeurs)268 }269 assertTrue(MarketSeries.from(Fixtures.json("v3-marche-series")).items.isNotEmpty())270 }271272 @Test fun indexSeries() {273 val own = IndexSeries.from(Fixtures.json("v3-indice-qc"))274 assertTrue(own.publie && own.propre)275 assertTrue(own.serie.size > 4)276 assertNotNull(own.lastClosed)277 assertTrue(own.lastClosed!!.closed)278 // série d'un parent : le territoire applicable est nommé279 val parent = IndexSeries.from(Fixtures.json("v3-indice-serie"))280 if (!parent.propre) assertNotNull(parent.applicable)281 val prod = IndexSeries.from(Fixtures.json("prod/v3-indice"))282 assertTrue(prod.serie.isNotEmpty())283 assertEquals("T3 2026".replace(" ", " "), com.spboucher.vraiprix.core.util.Fmt.period("2026-T3"))284 }285286 @Test fun rankingsAndTerritories() {287 for (name in listOf("v3-classements", "v3-classements-indice", "prod/v3-classements")) {288 val r = Ranking.from(Fixtures.json(name))289 assertTrue(name, r.items.isNotEmpty())290 assertEquals(1, r.items.first().rang)291 }292 assertTrue(TerritoryList.from(Fixtures.json("v3-territoires")).items.isNotEmpty())293 assertTrue(TerritoryList.from(Fixtures.json("prod/v3-territoires")).items.all { it.ref.niveau == "region" })294 val mun = Territory.from(Fixtures.json("prod/v3-territoire-mun"))295 assertEquals("municipalite", mun.ref.niveau)296 assertEquals("Lévis", mun.ref.nom)297 assertTrue(mun.fil.isNotEmpty())298 assertTrue(mun.enfants.any { it.niveau == "rue" })299 val rue = Territory.from(Fixtures.json("v3-territoire-rue"))300 assertEquals("rue", rue.ref.niveau)301 assertEquals("Rue Marianne", rue.ref.title)302 val units = StreetUnits.from(Fixtures.json("v3-rue-unites"))303 assertTrue(units.items.isNotEmpty() && units.items.all { it.id.length == 23 })304 }305306 @Test fun modelAndValidation() {307 val m = ModelInfo.from(Fixtures.json("prod/v3-modele"))308 assertEquals("v3-20261128-191255", m.version)309 assertTrue(m.statuts.getValue("valeur") > 0)310 assertTrue(m.horsChampTotal > 0)311 assertNotNull(m.erreurMedianeMarche)312 val v = Validation.from(Fixtures.json("prod/v3-validation"))313 assertTrue(v.items.any { it.methode == "v3" })314 }315316 @Test fun search() {317 val s = SearchResult.from(Fixtures.json("prod/v3-recherche"))318 assertEquals("65005774217243190030000", s.adresses.first().uniteId)319 assertEquals("1234 Rue Saint-Denis", s.adresses.first().title)320 assertTrue(s.lieux.all { it.isStreet })321 val place = SearchResult.from(Fixtures.json("prod/v3-recherche-lieu")).lieux.first()322 assertEquals("municipalite", place.type)323 assertEquals("levis", place.territory)324 assertTrue(SearchResult.from(J.EMPTY).isEmpty)325 }326327 @Test fun freeEstimateIsNeverFirm() {328 for (name in listOf("v3-estimation-libre", "prod/v3-estimation-libre")) {329 val e = FreeEstimate.from(Fixtures.json(name))330 assertEquals(ValueStatus.ORDRE, e.status)331 assertNotNull(e.block.amounts)332 assertFalse(e.block.isFirm)333 }334 }335336 @Test fun mapPoints() {337 for (name in listOf("v3-proprietes-bbox", "prod/v3-proprietes-bbox")) {338 val p = PropertyPoints.from(Fixtures.json(name))339 assertTrue(p.items.isNotEmpty())340 assertTrue(p.items.all { it.lat != 0.0 && it.lng != 0.0 })341 }342 }343344 @Test fun everyFixtureDecodesWithoutThrowing() {345 // un modèle lu sur la mauvaise réponse ne lève jamais : champs absents, listes vides346 val readers: List<(J) -> Any> = listOf(Property::from, SalesPage::from, ListingsPage::from, Market::from, IndexSeries::from,347 Territory::from, ModelInfo::from, SearchResult::from, FreeEstimate::from, SaleDetail::from, Comparables::from, Listing::from)348 for (name in listOf("v3-fiche-valeur", "v3-ventes", "v3-erreur", "v3-grille", "v3-municipalites", "prod/v3-erreur", "prod/v3-modele", "version"))349 for (r in readers) r(Fixtures.json(name))350 }351}352