diff --git a/src/utils/table_sql.py b/src/utils/table_sql.py index 3aa826f..2cdf112 100644 --- a/src/utils/table_sql.py +++ b/src/utils/table_sql.py @@ -134,10 +134,26 @@ def dashboard_filters_to_sql( clauses.append('"dateNotification" > ?') params.append(datetime.now() - timedelta(days=365)) + if dashboard_acheteur_id: + clauses.append('"acheteur_id" LIKE ?') + params.append(f"%{dashboard_acheteur_id}%") + + if dashboard_titulaire_id: + clauses.append('"titulaire_id" LIKE ?') + params.append(f"%{dashboard_titulaire_id}%") + if dashboard_marche_type: clauses.append('"type" = ?') params.append(dashboard_marche_type) + if dashboard_marche_objet: + clauses.append('"objet" ILIKE ?') + params.append(f"%{dashboard_marche_objet}%") + + if dashboard_marche_code_cpv: + clauses.append('"codeCPV" LIKE ?') + params.append(f"{dashboard_marche_code_cpv}%") + if dashboard_marche_innovant and dashboard_marche_innovant != "all": clauses.append('"marcheInnovant" = ?') params.append(dashboard_marche_innovant) diff --git a/tests/test_dashboard_filters_to_sql.py b/tests/test_dashboard_filters_to_sql.py index 5d542eb..6e9d398 100644 --- a/tests/test_dashboard_filters_to_sql.py +++ b/tests/test_dashboard_filters_to_sql.py @@ -52,3 +52,39 @@ def test_sous_traitance_value_non_adds_clause(): ) assert where_sql == 'YEAR("dateNotification") = ? AND "sousTraitanceDeclaree" = ?' assert params == [2025, "non"] + + +def test_acheteur_id_uses_like_wildcards(): + where_sql, params = dashboard_filters_to_sql( + dashboard_year="2025", + dashboard_acheteur_id="12345678900010", + ) + assert where_sql == 'YEAR("dateNotification") = ? AND "acheteur_id" LIKE ?' + assert params == [2025, "%12345678900010%"] + + +def test_titulaire_id_uses_like_wildcards(): + where_sql, params = dashboard_filters_to_sql( + dashboard_year="2025", + dashboard_titulaire_id="999", + ) + assert where_sql == 'YEAR("dateNotification") = ? AND "titulaire_id" LIKE ?' + assert params == [2025, "%999%"] + + +def test_marche_objet_uses_case_insensitive_ilike(): + where_sql, params = dashboard_filters_to_sql( + dashboard_year="2025", + dashboard_marche_objet="travaux", + ) + assert where_sql == 'YEAR("dateNotification") = ? AND "objet" ILIKE ?' + assert params == [2025, "%travaux%"] + + +def test_code_cpv_uses_prefix_like(): + where_sql, params = dashboard_filters_to_sql( + dashboard_year="2025", + dashboard_marche_code_cpv="4521", + ) + assert where_sql == 'YEAR("dateNotification") = ? AND "codeCPV" LIKE ?' + assert params == [2025, "4521%"]