Skip to content

Commit

Permalink
tests for semantic search
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienReuiller committed Feb 7, 2024
1 parent ca1647d commit f2af8eb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
73 changes: 73 additions & 0 deletions lemarche/www/siaes/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest import mock

from django.contrib.gis.geos import Point
from django.test import TestCase
from django.urls import reverse
Expand Down Expand Up @@ -1120,3 +1122,74 @@ def test_admin_has_extra_info(self):
self.client.force_login(self.user_admin)
response = self.client.get(url)
self.assertContains(response, "Informations Admin")


class SiaeSemanticSearchTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.siae_one = SiaeFactory()
cls.siae_two = SiaeFactory()
cls.siae_three = SiaeFactory()
cls.siae_four = SiaeFactory()
cls.city = PerimeterFactory(kind=Perimeter.KIND_CITY)
cls.url = reverse("siae:semantic_search_results")

def test_search_query_too_short(self):
with mock.patch(
"lemarche.www.siaes.views.api_elasticsearch.siaes_similarity_search"
) as mock_siaes_similarity_search:
response = self.client.get(f"{self.url}?search_query=ent&id_city_name=&city=")
self.assertNotIn("siaes", response.context)

mock_siaes_similarity_search.assert_not_called()

def test_search_query_no_result(self):
with mock.patch(
"lemarche.www.siaes.views.api_elasticsearch.siaes_similarity_search"
) as mock_siaes_similarity_search:
mock_siaes_similarity_search.return_value = []

response = self.client.get(f"{self.url}?search_query=entretien espace vert&id_city_name=&city=")
self.assertIn("siaes", response.context)
siaes = list(response.context["siaes"])
self.assertEqual(len(siaes), 0)
self.assertContains(response, "Oups, aucun prestataire trouvé !")

mock_siaes_similarity_search.assert_called_once()

def test_search_query_with_results(self):
with mock.patch(
"lemarche.tenders.models.api_elasticsearch.siaes_similarity_search"
) as mock_siaes_similarity_search, mock.patch(
"lemarche.tenders.models.api_elasticsearch.siaes_similarity_search_with_city"
) as mock_siaes_similarity_search_with_city:
mock_siaes_similarity_search.return_value = [self.siae_two.pk, self.siae_three.pk, self.siae_four.pk]

response = self.client.get(f"{self.url}?search_query=entretien espace vert&id_city_name=&city=")

self.assertIn("siaes", response.context)
siaes = list(response.context["siaes"])
self.assertEqual(len(siaes), 3)
self.assertIn(self.siae_two, siaes)
self.assertIn(self.siae_three, siaes)
self.assertIn(self.siae_four, siaes)

mock_siaes_similarity_search.assert_called_once()
mock_siaes_similarity_search_with_city.assert_not_called()

def test_search_query_with_city(self):
with mock.patch(
"lemarche.tenders.models.api_elasticsearch.siaes_similarity_search_with_city"
) as mock_siaes_similarity_search_with_city:
mock_siaes_similarity_search_with_city.return_value = [self.siae_two.pk, self.siae_four.pk]

response = self.client.get(
f"{self.url}?search_query=entretien foret&id_city_name={self.city.name}&city={self.city.slug}"
)

siaes = list(response.context["siaes"])
self.assertEqual(len(siaes), 2)
self.assertIn(self.siae_two, siaes)
self.assertIn(self.siae_four, siaes)

mock_siaes_similarity_search_with_city.assert_called_once()
6 changes: 3 additions & 3 deletions lemarche/www/siaes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from lemarche.conversations.models import Conversation
from lemarche.favorites.models import FavoriteList
from lemarche.siaes.models import Siae
from lemarche.utils.apis.api_elasticsearch import siaes_similarity_search, siaes_similarity_search_with_city
from lemarche.utils.apis import api_elasticsearch
from lemarche.utils.export import export_siae_to_csv, export_siae_to_excel
from lemarche.utils.s3 import API_CONNECTION_DICT
from lemarche.utils.urls import get_domain_url, get_encoded_url_from_params
Expand Down Expand Up @@ -349,9 +349,9 @@ def get(self, request):
search_query = form_semantic.cleaned_data.get("search_query", None)
city = form_semantic.cleaned_data.get("city", None)
if city:
siaes_id = siaes_similarity_search_with_city(search_query, city)
siaes_id = api_elasticsearch.siaes_similarity_search_with_city(search_query, city)
else:
siaes_id = siaes_similarity_search(search_query)
siaes_id = api_elasticsearch.siaes_similarity_search(search_query)
siaes = Siae.objects.filter(pk__in=siaes_id)
context = {
"siaes": siaes,
Expand Down

0 comments on commit f2af8eb

Please sign in to comment.