SPB Git forge

spb/rent-ka

Public
8commits 1branches 0releases
7.4 MBsize
maindefault branch
19 days agolast push
Python 68.8% TypeScript 18.6% CSS 8.7% JavaScript 3.3% HTML 0.6%
3.4 KB · 98 lines python
Raw Blame History
1# -----------------------------------------------------------------------------2# Rent-Ka — Rental listings aggregator (Canada, outside Québec)3# Author: Simon-Pierre Boucher — contact@spboucher.ai4# test_search.py : /api/search — la recherche unifiée liste + carte.5#   Règle d'or : total == len(points), quelles que soient les contraintes.6# -----------------------------------------------------------------------------7import pytest8from fastapi.testclient import TestClient91011@pytest.fixture(scope="module")12def client():13    from rentka import db, web14    con = db.connect()15    try:16        n = con.execute("SELECT COUNT(*) c FROM listings").fetchone()["c"]17        has_fv = con.execute(18            "SELECT name FROM sqlite_master WHERE name='fairvalue'").fetchone()19    finally:20        con.close()21    if not n or not has_fv:22        pytest.skip("empty/uninitialized DB — /api/search tests need the "23                    "seeded node database")24    return TestClient(web.app)252627def _get(client, **params):28    r = client.get("/api/search", params=params)29    assert r.status_code == 200, r.text30    return r.json()313233def test_regle_dor_total_egale_points(client):34    d = _get(client, page_size=5)35    assert d["total"] == len(d["points"])36    assert len(d["listings"]) <= 537    # points triés comme la liste : le 1er point est la 1re annonce38    if d["listings"]:39        assert d["points"][0][0] == d["listings"][0]["uid"]404142def test_bbox_restreint_et_conserve_egalite(client):43    tout = _get(client, page_size=1)44    zone = _get(client, bbox="-71.45,46.70,-71.10,46.92", page_size=1)45    assert zone["total"] <= tout["total"]46    assert zone["total"] == len(zone["points"])47    for _uid, lng, lat, _p, _v in zone["points"]:48        assert -71.45 <= lng <= -71.10 and 46.70 <= lat <= 46.92495051def test_polygone_sous_ensemble_de_son_emprise(client):52    poly = "-71.24,46.80;-71.20,46.82;-71.24,46.83"53    dedans = _get(client, poly=poly, page_size=1)54    emprise = _get(client, bbox="-71.24,46.80,-71.20,46.83", page_size=1)55    assert dedans["total"] == len(dedans["points"])56    assert dedans["total"] <= emprise["total"]575859def test_polygone_invalide(client):60    r = client.get("/api/search", params={"poly": "abc"})61    assert r.status_code == 40062    r = client.get("/api/search", params={"poly": "-71,46;-71.2,46.2"})63    assert r.status_code == 400  # 2 sommets646566def test_tris(client):67    asc = _get(client, sort="prix", page_size=10,68               bbox="-71.45,46.70,-71.10,46.92")69    prix = [p[3] for p in asc["points"] if p[3] is not None]70    assert prix == sorted(prix)71    desc = _get(client, sort="prix_desc", page_size=10,72                bbox="-71.45,46.70,-71.10,46.92")73    prix_d = [p[3] for p in desc["points"] if p[3] is not None]74    assert prix_d == sorted(prix_d, reverse=True)757677def test_tri_inconnu(client):78    r = client.get("/api/search", params={"sort": "alea"})79    assert r.status_code == 400808182def test_page_ramenee_dans_les_bornes(client):83    d = _get(client, page=99999, page_size=50,84             bbox="-71.45,46.70,-71.10,46.92")85    assert d["page"] == max(1, -(-d["total"] // 50))868788def test_include_liste_sans_points(client):89    d = _get(client, include="liste", page_size=5)90    assert d["points"] is None91    assert d["total"] > 0 or d["listings"] == []929394def test_filtre_deal_et_verdicts(client):95    d = _get(client, deal="sous", page_size=5)96    assert d["total"] == len(d["points"])97    assert all(p[4] == "s" for p in d["points"])98