From 0214671f82920cc1f66d1e1fcaa69da8958d9f9c Mon Sep 17 00:00:00 2001 From: Mathieu Agopian Date: Wed, 11 Sep 2024 11:10:13 +0200 Subject: [PATCH] Pair with @thibauddauce: fix failing tests --- udata/core/dataset/models.py | 2 ++ udata/core/elasticsearch/__init__.py | 9 +++++++-- udata/tests/api/test_dataservices_api.py | 12 ++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/udata/core/dataset/models.py b/udata/core/dataset/models.py index 058c448cd5..9386cbaaa3 100644 --- a/udata/core/dataset/models.py +++ b/udata/core/dataset/models.py @@ -16,6 +16,7 @@ from udata.app import cache from udata.core import storages +from udata.core.elasticsearch import elasticsearch from udata.core.owned import Owned, OwnedQuerySet from udata.frontend.markdown import mdstrip from udata.i18n import lazy_gettext as _ @@ -186,6 +187,7 @@ def clean(self, **kwargs): return +@elasticsearch() class License(db.Document): # We need to declare id explicitly since we do not use the default # value set by Mongo. diff --git a/udata/core/elasticsearch/__init__.py b/udata/core/elasticsearch/__init__.py index 9cf8d1bd43..cda014f202 100644 --- a/udata/core/elasticsearch/__init__.py +++ b/udata/core/elasticsearch/__init__.py @@ -5,6 +5,7 @@ from typing import Callable, Optional, Type, TypeVar import mongoengine.fields as mongo_fields +from elasticsearch import NotFoundError from elasticsearch_dsl import ( SF, Boolean, @@ -124,7 +125,7 @@ def generate_elasticsearch_model( index_name = cls._get_collection_name() # Testing name to have a new index in each test. - index_name = "".join(random.choices(string.ascii_lowercase, k=10)) + index_name = index_name + "".join(random.choices(string.ascii_lowercase, k=10)) class Index: name = index_name @@ -153,7 +154,10 @@ def elasticsearch_index(cls, document, **kwargs): if not indexable or indexable(document): elasticsearch_document.save() else: - elasticsearch_document.delete() + try: + elasticsearch_document.delete() + except NotFoundError: + pass signals.post_save.connect(elasticsearch_index, sender=cls) @@ -192,6 +196,7 @@ def elasticsearch_search(query_text: str): cls.__elasticsearch_model__ = ElasticSearchModel cls.__elasticsearch_index__ = elasticsearch_index cls.__elasticsearch_search__ = elasticsearch_search + return cls def get_searchable_fields(cls): diff --git a/udata/tests/api/test_dataservices_api.py b/udata/tests/api/test_dataservices_api.py index 3be3e3cc1f..0158b3828b 100644 --- a/udata/tests/api/test_dataservices_api.py +++ b/udata/tests/api/test_dataservices_api.py @@ -397,3 +397,15 @@ def test_elasticsearch(self): assert dataservices[0]["title"] == dataservice_a.title assert dataservices[1]["title"] == dataservice_c.title + + dataservice_b.deleted_at = datetime.utcnow() + dataservice_b.save() + + time.sleep(3) + + dataservices = self.get(url_for("api.dataservices", q="AMDAC")).json["data"] + + assert len(dataservices) == 2 + + assert dataservices[0]["title"] == dataservice_a.title + assert dataservices[1]["title"] == dataservice_c.title