From 62994a0811dcd77b5c58c140abdeadec1750e8e8 Mon Sep 17 00:00:00 2001 From: VoitecP Date: Mon, 8 Apr 2024 21:00:27 +0200 Subject: [PATCH] 'tests-fixed' --- Dshop/Dshop/settings.py | 4 +- Dshop/Dshop/urls.py | 1 + Dshop/apps/products_catalogue/api_urls.py | 2 +- .../tests/test_api_products.py | 96 +++++++++---------- 4 files changed, 52 insertions(+), 51 deletions(-) diff --git a/Dshop/Dshop/settings.py b/Dshop/Dshop/settings.py index 39ea34b..ebd1756 100644 --- a/Dshop/Dshop/settings.py +++ b/Dshop/Dshop/settings.py @@ -215,8 +215,8 @@ "DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema", "DEFAULT_PAGINATION_CLASS": 'rest_framework.pagination.PageNumberPagination', "PAGE_SIZE": 25, - "DEFAULT_FILTER_BACKENDS": ["django_filters.rest_framework.DjangoFilterBackend"] - + "DEFAULT_FILTER_BACKENDS": ["django_filters.rest_framework.DjangoFilterBackend"], + } # drf_spectacular diff --git a/Dshop/Dshop/urls.py b/Dshop/Dshop/urls.py index f47889c..db94999 100644 --- a/Dshop/Dshop/urls.py +++ b/Dshop/Dshop/urls.py @@ -42,6 +42,7 @@ path('api/users/', include('apps.users.api_urls')), path('api/', include('apps.products_catalogue.api_urls')), + path("api/schema/", SpectacularAPIView.as_view(), name="api-schema"), path( diff --git a/Dshop/apps/products_catalogue/api_urls.py b/Dshop/apps/products_catalogue/api_urls.py index 0a9ef13..46def2a 100644 --- a/Dshop/apps/products_catalogue/api_urls.py +++ b/Dshop/apps/products_catalogue/api_urls.py @@ -3,7 +3,7 @@ from rest_framework.routers import DefaultRouter router = DefaultRouter() -router.register('products/', ProductViewSet, basename='products-api') +router.register('products', ProductViewSet, basename='products-api') urlpatterns = [ path('', include(router.urls)), diff --git a/Dshop/apps/products_catalogue/tests/test_api_products.py b/Dshop/apps/products_catalogue/tests/test_api_products.py index d8ea8a2..dcbf775 100644 --- a/Dshop/apps/products_catalogue/tests/test_api_products.py +++ b/Dshop/apps/products_catalogue/tests/test_api_products.py @@ -112,56 +112,56 @@ def test_product_list_pagination_ten_products_page_too_far(tv_product): assert response.status_code == 404 -# @pytest.mark.parametrize("page_suffix", ["", "?page=1"]) -# @pytest.mark.django_db -# def test_product_list_pagination_ten_products_page_1(page_suffix, ten_tv_products): -# response = APIClient().get(reverse("products-api-list") + page_suffix) -# results = response.data['results'] -# assert len(results) == 5 -# assert results[0]['id'] == ten_tv_products[0].id -# assert results[4]['id'] == ten_tv_products[4].id -# assert response.data['count'] == 10 -# assert response.data['next'] == "http://testserver/api/products/?page=2" -# assert response.data['previous'] is None +@pytest.mark.parametrize("page_suffix", ["", "?page=1"]) +@pytest.mark.django_db +def test_product_list_pagination_ten_products_page_1(page_suffix, ten_tv_products): + response = APIClient().get(reverse("products-api-list") + page_suffix) + results = response.data['results'] + assert len(results) == 5 + assert results[0]['id'] == ten_tv_products[0].id + assert results[4]['id'] == ten_tv_products[4].id + assert response.data['count'] == 10 + assert response.data['next'] == "http://testserver/api/products/?page=2" + assert response.data['previous'] is None -# @pytest.mark.django_db -# def test_product_list_pagination_ten_products_page_2(ten_tv_products): -# response = APIClient().get(f"{reverse('products-api-list')}?page=2") -# results = response.data['results'] -# assert response.status_code == 200 -# assert len(results) == 5 -# assert results[0]['id'] == ten_tv_products[5].id -# assert results[4]['id'] == ten_tv_products[9].id -# assert response.data['count'] == 10 -# assert response.data['next'] is None -# assert response.data['previous'] == "http://testserver/api/products/" - - -# @pytest.mark.django_db -# def test_product_list_pagination_forty_three_products_page_4(forty_three_tv_products): -# response = APIClient().get(f"{reverse('products-api-list')}?page=4") -# results = response.data['results'] -# assert response.status_code == 200 -# assert len(results) == 5 -# assert results[0]['id'] == forty_three_tv_products[15].id -# assert results[4]['id'] == forty_three_tv_products[19].id -# assert response.data['count'] == 43 -# assert response.data['next'] == "http://testserver/api/products/?page=5" -# assert response.data['previous'] == "http://testserver/api/products/?page=3" - - -# @pytest.mark.django_db -# def test_product_list_pagination_forty_three_products_page_9(forty_three_tv_products): -# response = APIClient().get(f"{reverse('products-api-list')}?page=9") -# results = response.data['results'] -# assert response.status_code == 200 -# assert len(results) == 3 -# assert results[0]['id'] == forty_three_tv_products[40].id -# assert results[2]['id'] == forty_three_tv_products[42].id -# assert response.data['count'] == 43 -# assert response.data['next'] is None -# assert response.data['previous'] == "http://testserver/api/products/?page=8" +@pytest.mark.django_db +def test_product_list_pagination_ten_products_page_2(ten_tv_products): + response = APIClient().get(f"{reverse('products-api-list')}?page=2") + results = response.data['results'] + assert response.status_code == 200 + assert len(results) == 5 + assert results[0]['id'] == ten_tv_products[5].id + assert results[4]['id'] == ten_tv_products[9].id + assert response.data['count'] == 10 + assert response.data['next'] is None + assert response.data['previous'] == "http://testserver/api/products/" + + +@pytest.mark.django_db +def test_product_list_pagination_forty_three_products_page_4(forty_three_tv_products): + response = APIClient().get(f"{reverse('products-api-list')}?page=4") + results = response.data['results'] + assert response.status_code == 200 + assert len(results) == 5 + assert results[0]['id'] == forty_three_tv_products[15].id + assert results[4]['id'] == forty_three_tv_products[19].id + assert response.data['count'] == 43 + assert response.data['next'] == "http://testserver/api/products/?page=5" + assert response.data['previous'] == "http://testserver/api/products/?page=3" + + +@pytest.mark.django_db +def test_product_list_pagination_forty_three_products_page_9(forty_three_tv_products): + response = APIClient().get(f"{reverse('products-api-list')}?page=9") + results = response.data['results'] + assert response.status_code == 200 + assert len(results) == 3 + assert results[0]['id'] == forty_three_tv_products[40].id + assert results[2]['id'] == forty_three_tv_products[42].id + assert response.data['count'] == 43 + assert response.data['next'] is None + assert response.data['previous'] == "http://testserver/api/products/?page=8" @pytest.mark.django_db