Skip to content

Commit

Permalink
Pair with @ThibaudDauce: fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
magopian committed Sep 11, 2024
1 parent 675a13e commit 0214671
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions udata/core/dataset/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 7 additions & 2 deletions udata/core/elasticsearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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):
Expand Down
12 changes: 12 additions & 0 deletions udata/tests/api/test_dataservices_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0214671

Please sign in to comment.