diff --git a/lemarche/www/siaes/tests.py b/lemarche/www/siaes/tests.py index 747e4f219..a00f5350a 100644 --- a/lemarche/www/siaes/tests.py +++ b/lemarche/www/siaes/tests.py @@ -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 @@ -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() diff --git a/lemarche/www/siaes/views.py b/lemarche/www/siaes/views.py index 04bfa98e2..75a9e2f39 100644 --- a/lemarche/www/siaes/views.py +++ b/lemarche/www/siaes/views.py @@ -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 @@ -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,