From c16c7892b8c3604cf5e1657a1460ad7ef2147657 Mon Sep 17 00:00:00 2001 From: Andrew Aikman Date: Fri, 22 Mar 2024 19:41:33 +0000 Subject: [PATCH] Unsaved work done on the pagination piece --- shedpi_hub_dashboard/tests/test_endpoints.py | 26 ++++++++++++++++++++ shedpi_hub_dashboard/views.py | 10 ++++---- shedpi_hub_example_project/urls.py | 4 +-- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/shedpi_hub_dashboard/tests/test_endpoints.py b/shedpi_hub_dashboard/tests/test_endpoints.py index 315eacf..c8734c9 100644 --- a/shedpi_hub_dashboard/tests/test_endpoints.py +++ b/shedpi_hub_dashboard/tests/test_endpoints.py @@ -2,6 +2,7 @@ import pytest from rest_framework import status +from rest_framework.exceptions import ValidationError from rest_framework.reverse import reverse from shedpi_hub_dashboard.tests.utils.factories import ( @@ -75,6 +76,31 @@ def test_device_module_readings_list_pagination(client): assert len(response.data["results"]) == 100 +@pytest.mark.django_db +def test_device_module_readings_list_pagination_no_module_supplied(client): + """ + When no module is supplied the user should be supplied a valida tion message telling them so + """ + schema = { + "$id": "https://example.com/person.schema.json", + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Reading", + "type": "object", + "properties": { + "temperature": {"type": "string", "description": "The Temperature"}, + }, + } + device_module = DeviceModuleFactory(schema=schema) + DeviceModuleReadingFactory.create_batch( + 110, device_module=device_module, data={"temperature": "20"} + ) + + url = reverse("devicemodulereading-paginated-list") + + with pytest.raises(ValidationError): + client.get(url) + + @pytest.mark.django_db def test_device_module_readings_list_no_device_module_supplied(client): """ """ diff --git a/shedpi_hub_dashboard/views.py b/shedpi_hub_dashboard/views.py index f7d2825..2926c01 100644 --- a/shedpi_hub_dashboard/views.py +++ b/shedpi_hub_dashboard/views.py @@ -1,5 +1,6 @@ from django.template.response import TemplateResponse from rest_framework import mixins, viewsets +from rest_framework.exceptions import ValidationError from rest_framework.viewsets import GenericViewSet from .models import DeviceModule, DeviceModuleReading @@ -40,7 +41,7 @@ def get_queryset(self): return self.queryset -class ExperimentalViewSet( +class DeviceModuleReadingPaginatedViewSet( mixins.ListModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, @@ -51,10 +52,9 @@ class ExperimentalViewSet( pagination_class = CreatedAtBasedCursorPagination def get_queryset(self): - # FIXME: Validate that the user supplied this get param! device_module_id = self.request.query_params.get("device_module") - if device_module_id: - return self.queryset.filter(device_module=device_module_id) + if not device_module_id: + raise ValidationError({"device_module": "Not supplied"}) - return self.queryset + return self.queryset.filter(device_module=device_module_id) diff --git a/shedpi_hub_example_project/urls.py b/shedpi_hub_example_project/urls.py index 4b87646..49e2c4d 100644 --- a/shedpi_hub_example_project/urls.py +++ b/shedpi_hub_example_project/urls.py @@ -5,9 +5,9 @@ from rest_framework import routers from shedpi_hub_dashboard.views import ( + DeviceModuleReadingPaginatedViewSet, DeviceModuleReadingViewSet, DeviceModuleViewSet, - ExperimentalViewSet, ) router = routers.DefaultRouter() @@ -15,7 +15,7 @@ router.register(r"device-module-readings", DeviceModuleReadingViewSet) router.register( r"device-module-readings-paginated", - ExperimentalViewSet, + DeviceModuleReadingPaginatedViewSet, basename="devicemodulereading-paginated", )