Skip to content

Commit

Permalink
fix(test): fix backend test
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesfize committed Dec 31, 2024
1 parent 3b0805b commit 207d552
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
19 changes: 7 additions & 12 deletions backend/geonature/core/gn_meta/models/aframework.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,15 @@ def filter_by_params(cls, params={}, *, _ds_search=True, query=None):

search = params.get("search")
if search:

search = search.strip()
# Where clauses to include other matching possibilities (id, uuid, date)
where_clauses = []
if search.isdigit(): # ID AF match
where_clauses.append(TAcquisitionFramework.id_acquisition_framework == int(search))

if len(search) > 5: # UUID and date match
if len(search) >= MIN_LENGTH_UUID_OR_DATE_SEARCH_STRING: # UUID and date match
where_clauses.append(
sa.cast(TAcquisitionFramework.unique_acquisition_framework_id, sa.String).ilike(
sa.cast(TAcquisitionFramework.unique_acquisition_framework_id, sa.String).like(
f"%{search}%"
)
)
Expand Down Expand Up @@ -362,14 +362,9 @@ def filter_by_params(cls, params={}, *, _ds_search=True, query=None):
TAcquisitionFramework.id_acquisition_framework,
)
).cte("matched_words_af_cte")

query = (
select(TAcquisitionFramework)
.where(
matched_words_af_cte.c.id_acquisition_framework
== TAcquisitionFramework.id_acquisition_framework
)
.order_by(matched_words_af_cte.c.match_count.desc())
)
query = query.where(
matched_words_af_cte.c.id_acquisition_framework
== TAcquisitionFramework.id_acquisition_framework
).order_by(matched_words_af_cte.c.match_count.desc())

return query
2 changes: 2 additions & 0 deletions backend/geonature/core/gn_meta/models/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from geonature.utils.env import DB, db

MIN_LENGTH_UUID_OR_DATE_SEARCH_STRING = 5


class DateFilterSchema(ma.Schema):
year = ma.fields.Integer()
Expand Down
4 changes: 2 additions & 2 deletions backend/geonature/core/gn_meta/models/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,13 @@ class DatasetFilterSchema(MetadataFilterSchema):

search = params.get("search")
if search:

search = search.strip()
# Where clauses to include other matching possibilities (id, uuid)
where_clauses = []
if search.isdigit(): # ID AF match
where_clauses.append(cls.id_dataset == int(search))

if len(search) > 5: # UUID match
if len(search) >= MIN_LENGTH_UUID_OR_DATE_SEARCH_STRING: # UUID match
where_clauses.append(sa.cast(cls.unique_dataset_id, sa.String).ilike(f"%{search}%"))

# if name search include acquisition framework
Expand Down
2 changes: 1 addition & 1 deletion backend/geonature/tests/test_gn_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from io import StringIO
from unittest.mock import patch


import pytest
from flask import url_for
from geoalchemy2.shape import to_shape
Expand Down Expand Up @@ -824,7 +825,6 @@ def test_get_dataset_search(self, users, datasets, module):
def test_get_dataset_search_uuid(self, users, datasets):
ds = datasets["own_dataset"]
set_logged_user(self.client, users["admin_user"])

response = self.client.get(
url_for("gn_meta.get_datasets"),
json={"search": str(ds.unique_dataset_id)[:5]},
Expand Down

0 comments on commit 207d552

Please sign in to comment.