Skip to content

Commit

Permalink
Unsaved work done on the pagination piece
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiky30 committed Mar 22, 2024
1 parent faed355 commit c16c789
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
26 changes: 26 additions & 0 deletions shedpi_hub_dashboard/tests/test_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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):
""" """
Expand Down
10 changes: 5 additions & 5 deletions shedpi_hub_dashboard/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -40,7 +41,7 @@ def get_queryset(self):
return self.queryset


class ExperimentalViewSet(
class DeviceModuleReadingPaginatedViewSet(
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
Expand All @@ -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)
4 changes: 2 additions & 2 deletions shedpi_hub_example_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
from rest_framework import routers

from shedpi_hub_dashboard.views import (
DeviceModuleReadingPaginatedViewSet,
DeviceModuleReadingViewSet,
DeviceModuleViewSet,
ExperimentalViewSet,
)

router = routers.DefaultRouter()
router.register(r"device-module", DeviceModuleViewSet)
router.register(r"device-module-readings", DeviceModuleReadingViewSet)
router.register(
r"device-module-readings-paginated",
ExperimentalViewSet,
DeviceModuleReadingPaginatedViewSet,
basename="devicemodulereading-paginated",
)

Expand Down

0 comments on commit c16c789

Please sign in to comment.