diff --git a/BackEnd/search/tests/test_advanced_search.py b/BackEnd/search/tests/test_advanced_search.py index 0014ada1e..7e2912210 100644 --- a/BackEnd/search/tests/test_advanced_search.py +++ b/BackEnd/search/tests/test_advanced_search.py @@ -57,7 +57,9 @@ def setUp(self) -> None: def test_get_search_in_all_companies_official_order_by_name(self): response = self.client.get(path="/api/search/advanced?search=official") - names_from_response = [prof["name"] for prof in response.data] + names_from_response = [ + prof["name"] for prof in response.data["results"] + ] self.assertEqual(200, response.status_code) self.assertEqual( names_from_response, @@ -74,24 +76,26 @@ def test_get_search_in_all_companies_official_order_by_name(self): def test_get_search_name_service_info_lower_case(self): response = self.client.get(path="/api/search/advanced?search=kyiv") self.assertEqual(200, response.status_code) - self.assertEqual(2, len(response.data)) + self.assertEqual(2, response.data["total_items"]) def test_get_search_name_service_info_authorized(self): self.client.force_authenticate(self.user) response = self.client.get(path="/api/search/advanced?search=kyiv") self.assertEqual(200, response.status_code) - self.assertEqual(2, len(response.data)) + self.assertEqual(2, response.data["total_items"]) def test_get_search_name_service_info_upper_case(self): response = self.client.get(path="/api/search/advanced?search=KYIV") self.assertEqual(200, response.status_code) - self.assertEqual(2, len(response.data)) + self.assertEqual(2, response.data["total_items"]) def test_get_search_parcial_item(self): response = self.client.get(path="/api/search/advanced?search=ch") - names_from_response = [prof["name"] for prof in response.data] + names_from_response = [ + prof["name"] for prof in response.data["results"] + ] self.assertEqual(200, response.status_code) - self.assertEqual(3, len(response.data)) + self.assertEqual(3, response.data["total_items"]) self.assertEqual( names_from_response, ["Charkiv", "Chernigiv", "Dnipro"] ) @@ -99,27 +103,31 @@ def test_get_search_parcial_item(self): def test_get_search_not_exist(self): response = self.client.get(path="/api/search/advanced?search=QQQ") self.assertEqual(200, response.status_code) - self.assertEqual([], response.json()) + self.assertEqual([], response.data["results"]) def test_get_search_name_product_info(self): response = self.client.get(path="/api/search/advanced?search=dnipro") self.assertEqual(200, response.status_code) - self.assertEqual(2, len(response.data)) + self.assertEqual(2, response.data["total_items"]) def test_get_search_name_service_product_common_info(self): response = self.client.get(path="/api/search/advanced?search=KYIV") - names_from_response = [prof["name"] for prof in response.data] + names_from_response = [ + prof["name"] for prof in response.data["results"] + ] self.assertEqual(200, response.status_code) - self.assertEqual(2, len(response.data)) + self.assertEqual(2, response.data["total_items"]) self.assertEqual(names_from_response, ["Kryvyi Rig", "Kyiv"]) def test_get_search_devide_item(self): response = self.client.get( path="/api/search/advanced?search=product info" ) - names_from_response = [prof["name"] for prof in response.data] + names_from_response = [ + prof["name"] for prof in response.data["results"] + ] self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual(names_from_response, ["Kryvyi Rig"]) def test_advanced_search_serializer_fields(self): @@ -144,4 +152,4 @@ def test_advanced_search_serializer_fields(self): "person": self.company_kyiv.person_id, } ] - self.assertEqual(expected_result, response.json()) + self.assertEqual(expected_result, response.data["results"]) diff --git a/BackEnd/search/tests/test_search_by_name_region.py b/BackEnd/search/tests/test_search_by_name_region.py index b279afff2..42d7a4b89 100644 --- a/BackEnd/search/tests/test_search_by_name_region.py +++ b/BackEnd/search/tests/test_search_by_name_region.py @@ -40,12 +40,12 @@ def setUp(self) -> None: def test_get_profile_filtered_by_name_unauthorized(self): response = self.client.get(path="/api/search/?name=Kyivbud") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) def test_get_profile_filtered_by_region_eng_unauthorized(self): response = self.client.get(path="/api/search/?regions_eng=Dnipro") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -69,14 +69,14 @@ def test_get_profile_filtered_by_region_eng_unauthorized(self): "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profile_filtered_by_region_eng_authorized(self): self.client.force_authenticate(self.user) response = self.client.get(path="/api/search/?regions_eng=Dnipro") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -100,14 +100,14 @@ def test_get_profile_filtered_by_region_eng_authorized(self): "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profile_filtered_by_region_ukr_authorized(self): self.client.force_authenticate(self.user) response = self.client.get(path="/api/search/?regions_ukr=Дніпро") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -131,13 +131,13 @@ def test_get_profile_filtered_by_region_ukr_authorized(self): "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profile_filtered_by_region_ukr_unauthorized(self): response = self.client.get(path="/api/search/?regions_ukr=Дніпро") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -161,14 +161,14 @@ def test_get_profile_filtered_by_region_ukr_unauthorized(self): "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profile_filtered_by_region_ukr_partial_authorized(self): self.client.force_authenticate(self.user) response = self.client.get(path="/api/search/?regions_ukr=Дніп") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -192,13 +192,13 @@ def test_get_profile_filtered_by_region_ukr_partial_authorized(self): "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profile_filtered_by_region_ukr_partial_unauthorized(self): response = self.client.get(path="/api/search/?regions_ukr=Дніпро") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -222,14 +222,14 @@ def test_get_profile_filtered_by_region_ukr_partial_unauthorized(self): "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profile_filtered_by_region_ukr_lower_case_authorized(self): self.client.force_authenticate(self.user) response = self.client.get(path="/api/search/?regions_ukr=дніпро") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -253,40 +253,40 @@ def test_get_profile_filtered_by_region_ukr_lower_case_authorized(self): "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profile_filtered_by_region_ukr_not_exist_authorized(self): self.client.force_authenticate(self.user) response = self.client.get(path="/api/search/?regions_ukr=УУ") self.assertEqual(200, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profile_filtered_by_region_eng_not_exist_authorized(self): self.client.force_authenticate(self.user) response = self.client.get(path="/api/search/?regions_eng=WWWW") self.assertEqual(200, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profile_filtered_by_region_ukr_not_exist_unauthorized(self): response = self.client.get(path="/api/search/?regions_ukr=УУ") self.assertEqual(200, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profile_filtered_by_region_eng_not_exist_unauthorized(self): response = self.client.get(path="/api/search/?regions_eng=WWWW") self.assertEqual(200, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profile_filtered_by_name_authorized(self): self.client.force_authenticate(self.user) response = self.client.get(path="/api/search/?name=Kyivbud") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -310,39 +310,39 @@ def test_get_profile_filtered_by_name_authorized(self): "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profiles_without_filter_unauthorized(self): response = self.client.get(path="/api/search/") self.assertEqual(200, response.status_code) - self.assertEqual(4, len(response.data)) + self.assertEqual(4, response.data["total_items"]) def test_get_profiles_without_filter_authorized(self): self.client.force_authenticate(self.user) response = self.client.get(path="/api/search/") self.assertEqual(200, response.status_code) - self.assertEqual(4, len(response.data)) + self.assertEqual(4, response.data["total_items"]) def test_get_profiles_filter_wrong_name_authorized(self): self.client.force_authenticate(self.user) response = self.client.get(path="/api/search/?name=Chernovcimilk") self.assertEqual(200, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profiles_filter_wrong_name_unauthorized(self): response = self.client.get(path="/api/search/?name=Chernovcimilk") self.assertEqual(200, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profile_filtered_by_name_and_region_eng_unauthorized(self): response = self.client.get( path="/api/search/?name=Kyivbud®ions_eng=Kyiv" ) self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -366,7 +366,7 @@ def test_get_profile_filtered_by_name_and_region_eng_unauthorized(self): "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profile_filtered_by_name_and_region_eng_authorized(self): @@ -375,7 +375,7 @@ def test_get_profile_filtered_by_name_and_region_eng_authorized(self): path="/api/search/?name=Kyivbud®ion=Kyiv" ) self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -399,7 +399,7 @@ def test_get_profile_filtered_by_name_and_region_eng_authorized(self): "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profile_filtered_by_wrong_name_and_region_ukr_authorized( @@ -410,16 +410,16 @@ def test_get_profile_filtered_by_wrong_name_and_region_ukr_authorized( path="/api/search/?name=Pizza®ions_ukr=Київ" ) self.assertEqual(200, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profile_filtered_by_wrong_name_and_region_ukr_unauthorized( self, ): response = self.client.get(path="/api/search/?name=Pizza®ion=Київ") self.assertEqual(200, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profile_filtered_by_name_and_wrong_region_eng_authorized( self, @@ -429,16 +429,16 @@ def test_get_profile_filtered_by_name_and_wrong_region_eng_authorized( path="/api/search/?name=Kyivbud®ions_eng=Ghernigiv" ) self.assertEqual(status.HTTP_200_OK, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profile_filtered_by_name_and_wrong_region_unauthorized(self): response = self.client.get( path="/api/search/?name=Kyivbud®ions_eng=Ghernigiv" ) self.assertEqual(200, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profile_filtered_by_wrong_name_and_wrong_region_authorized( self, @@ -448,8 +448,8 @@ def test_get_profile_filtered_by_wrong_name_and_wrong_region_authorized( path="/api/search/?name=Pizza®ions_eng=London" ) self.assertEqual(200, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profile_filtered_by_wrong_name_and_wrong_region_unauthorized( self, @@ -458,25 +458,25 @@ def test_get_profile_filtered_by_wrong_name_and_wrong_region_unauthorized( path="/api/search/?name=Pizza®ions_ukr=Лондон" ) self.assertEqual(200, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profile_filtered_by_region_eng_partially_authorized(self): self.client.force_authenticate(self.user) response = self.client.get(path="/api/search/?regions_eng=Dnip") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) def test_get_profile_filtered_by_region_eng_partially_unauthorized(self): response = self.client.get(path="/api/search/?regions_eng=Dnip") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) def test_get_profile_filtered_by_name_partially_authorized(self): self.client.force_authenticate(self.user) response = self.client.get(path="/api/search/?name=Kyiv") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -500,13 +500,13 @@ def test_get_profile_filtered_by_name_partially_authorized(self): "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profile_filtered_by_name_partially_unauthorized(self): response = self.client.get(path="/api/search/?name=Kyiv") self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -530,7 +530,7 @@ def test_get_profile_filtered_by_name_partially_unauthorized(self): "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profile_filtered_by_name_and_region_eng_partialyy_authorized( @@ -541,7 +541,7 @@ def test_get_profile_filtered_by_name_and_region_eng_partialyy_authorized( path="/api/search/?name=Kyiv®ions_eng=Ky" ) self.assertEqual(200, response.status_code) - self.assertEqual(1, len(response.data)) + self.assertEqual(1, response.data["total_items"]) self.assertEqual( [ { @@ -565,7 +565,7 @@ def test_get_profile_filtered_by_name_and_region_eng_partialyy_authorized( "is_saved": False, } ], - response.json(), + response.data["results"], ) def test_get_profile_filtered_by_wrong_name_and_wrong_region_partially_authorized( @@ -576,8 +576,8 @@ def test_get_profile_filtered_by_wrong_name_and_wrong_region_partially_authorize path="/api/search/?name=Kyivf®ions_eng=Kyif" ) self.assertEqual(200, response.status_code) - self.assertEqual(0, len(response.data)) - self.assertEqual([], response.json()) + self.assertEqual(0, response.data["total_items"]) + self.assertEqual([], response.data["results"]) def test_get_profile_ordered_by_name_asc_unauthorized( self, @@ -667,8 +667,8 @@ def test_get_profile_ordered_by_name_asc_unauthorized( response = self.client.get(path="/api/search/?ordering=name") self.assertEqual(200, response.status_code) - self.assertEqual(4, len(response.data)) - self.assertEqual(ordered_response_data, response.json()) + self.assertEqual(4, response.data["total_items"]) + self.assertEqual(ordered_response_data, response.data["results"]) def test_get_profile_ordered_by_name_asc_authorized( self, @@ -759,8 +759,8 @@ def test_get_profile_ordered_by_name_asc_authorized( response = self.client.get(path="/api/search/?ordering=name") self.assertEqual(200, response.status_code) - self.assertEqual(4, len(response.data)) - self.assertEqual(ordered_response_data, response.json()) + self.assertEqual(4, response.data["total_items"]) + self.assertEqual(ordered_response_data, response.data["results"]) def test_get_profile_ordered_by_name_dsc_unauthorized( self, @@ -850,8 +850,8 @@ def test_get_profile_ordered_by_name_dsc_unauthorized( response = self.client.get(path="/api/search/?ordering=-name") self.assertEqual(200, response.status_code) - self.assertEqual(4, len(response.data)) - self.assertEqual(ordered_response_data, response.json()) + self.assertEqual(4, response.data["total_items"]) + self.assertEqual(ordered_response_data, response.data["results"]) def test_get_profile_ordered_by_name_dsc_authorized( self, @@ -942,5 +942,5 @@ def test_get_profile_ordered_by_name_dsc_authorized( response = self.client.get(path="/api/search/?ordering=-name") self.assertEqual(200, response.status_code) - self.assertEqual(4, len(response.data)) - self.assertEqual(ordered_response_data, response.json()) + self.assertEqual(4, response.data["total_items"]) + self.assertEqual(ordered_response_data, response.data["results"]) diff --git a/BackEnd/search/views.py b/BackEnd/search/views.py index 7667f807b..9d05e777a 100644 --- a/BackEnd/search/views.py +++ b/BackEnd/search/views.py @@ -2,14 +2,17 @@ import django_filters from rest_framework import filters +from forum.pagination import ForumPagination from profiles.models import Profile, SavedCompany from .serializers import CompanySerializers, CompanyAdvancedSerializers from search.filters import CompanyFilter class SearchCompanyView(ListAPIView): - queryset = Profile.objects.active_only().prefetch_related( - "regions", "categories", "activities" + queryset = ( + Profile.objects.active_only() + .prefetch_related("regions", "categories", "activities") + .order_by("id") ) serializer_class = CompanySerializers filter_backends = [ @@ -17,6 +20,7 @@ class SearchCompanyView(ListAPIView): filters.OrderingFilter, ] filterset_class = CompanyFilter + pagination_class = ForumPagination ordering_fields = ["name"] def get_serializer_context(self): @@ -32,10 +36,13 @@ def get_serializer_context(self): class AdvancedSearchView(ListAPIView): - queryset = Profile.objects.active_only().prefetch_related( - "regions", "categories", "activities" + queryset = ( + Profile.objects.active_only() + .prefetch_related("regions", "categories", "activities") + .order_by("id") ) serializer_class = CompanyAdvancedSerializers + pagination_class = ForumPagination filter_backends = [ filters.SearchFilter, filters.OrderingFilter,