feat(observatoire): IN départements et skip conditionnel par ID (#72)
This commit is contained in:
@@ -137,10 +137,26 @@ def dashboard_filters_to_sql(
|
|||||||
if dashboard_acheteur_id:
|
if dashboard_acheteur_id:
|
||||||
clauses.append('"acheteur_id" LIKE ?')
|
clauses.append('"acheteur_id" LIKE ?')
|
||||||
params.append(f"%{dashboard_acheteur_id}%")
|
params.append(f"%{dashboard_acheteur_id}%")
|
||||||
|
else:
|
||||||
|
if dashboard_acheteur_categorie:
|
||||||
|
clauses.append('"acheteur_categorie" = ?')
|
||||||
|
params.append(dashboard_acheteur_categorie)
|
||||||
|
if dashboard_acheteur_departement_code:
|
||||||
|
placeholders = ", ".join(["?"] * len(dashboard_acheteur_departement_code))
|
||||||
|
clauses.append(f'"acheteur_departement_code" IN ({placeholders})')
|
||||||
|
params.extend(dashboard_acheteur_departement_code)
|
||||||
|
|
||||||
if dashboard_titulaire_id:
|
if dashboard_titulaire_id:
|
||||||
clauses.append('"titulaire_id" LIKE ?')
|
clauses.append('"titulaire_id" LIKE ?')
|
||||||
params.append(f"%{dashboard_titulaire_id}%")
|
params.append(f"%{dashboard_titulaire_id}%")
|
||||||
|
else:
|
||||||
|
if dashboard_titulaire_categorie:
|
||||||
|
clauses.append('"titulaire_categorie" = ?')
|
||||||
|
params.append(dashboard_titulaire_categorie)
|
||||||
|
if dashboard_titulaire_departement_code:
|
||||||
|
placeholders = ", ".join(["?"] * len(dashboard_titulaire_departement_code))
|
||||||
|
clauses.append(f'"titulaire_departement_code" IN ({placeholders})')
|
||||||
|
params.extend(dashboard_titulaire_departement_code)
|
||||||
|
|
||||||
if dashboard_marche_type:
|
if dashboard_marche_type:
|
||||||
clauses.append('"type" = ?')
|
clauses.append('"type" = ?')
|
||||||
|
|||||||
@@ -88,3 +88,59 @@ def test_code_cpv_uses_prefix_like():
|
|||||||
)
|
)
|
||||||
assert where_sql == 'YEAR("dateNotification") = ? AND "codeCPV" LIKE ?'
|
assert where_sql == 'YEAR("dateNotification") = ? AND "codeCPV" LIKE ?'
|
||||||
assert params == [2025, "4521%"]
|
assert params == [2025, "4521%"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_acheteur_departement_multiple_uses_in_clause():
|
||||||
|
where_sql, params = dashboard_filters_to_sql(
|
||||||
|
dashboard_year="2025",
|
||||||
|
dashboard_acheteur_departement_code=["75", "92", "93"],
|
||||||
|
)
|
||||||
|
assert where_sql == (
|
||||||
|
'YEAR("dateNotification") = ? AND "acheteur_departement_code" IN (?, ?, ?)'
|
||||||
|
)
|
||||||
|
assert params == [2025, "75", "92", "93"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_acheteur_categorie_adds_clause():
|
||||||
|
where_sql, params = dashboard_filters_to_sql(
|
||||||
|
dashboard_year="2025",
|
||||||
|
dashboard_acheteur_categorie="Commune",
|
||||||
|
)
|
||||||
|
assert where_sql == 'YEAR("dateNotification") = ? AND "acheteur_categorie" = ?'
|
||||||
|
assert params == [2025, "Commune"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_titulaire_categorie_and_departement():
|
||||||
|
where_sql, params = dashboard_filters_to_sql(
|
||||||
|
dashboard_year="2025",
|
||||||
|
dashboard_titulaire_categorie="PME",
|
||||||
|
dashboard_titulaire_departement_code=["35"],
|
||||||
|
)
|
||||||
|
assert where_sql == (
|
||||||
|
'YEAR("dateNotification") = ? '
|
||||||
|
'AND "titulaire_categorie" = ? '
|
||||||
|
'AND "titulaire_departement_code" IN (?)'
|
||||||
|
)
|
||||||
|
assert params == [2025, "PME", "35"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_acheteur_id_present_skips_categorie_and_departement():
|
||||||
|
where_sql, params = dashboard_filters_to_sql(
|
||||||
|
dashboard_year="2025",
|
||||||
|
dashboard_acheteur_id="123",
|
||||||
|
dashboard_acheteur_categorie="Commune",
|
||||||
|
dashboard_acheteur_departement_code=["75"],
|
||||||
|
)
|
||||||
|
assert where_sql == 'YEAR("dateNotification") = ? AND "acheteur_id" LIKE ?'
|
||||||
|
assert params == [2025, "%123%"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_titulaire_id_present_skips_categorie_and_departement():
|
||||||
|
where_sql, params = dashboard_filters_to_sql(
|
||||||
|
dashboard_year="2025",
|
||||||
|
dashboard_titulaire_id="999",
|
||||||
|
dashboard_titulaire_categorie="PME",
|
||||||
|
dashboard_titulaire_departement_code=["35"],
|
||||||
|
)
|
||||||
|
assert where_sql == 'YEAR("dateNotification") = ? AND "titulaire_id" LIKE ?'
|
||||||
|
assert params == [2025, "%999%"]
|
||||||
|
|||||||
Reference in New Issue
Block a user