From fa97e2a1682ba0926c4db0e1a8e24eabbd767a84 Mon Sep 17 00:00:00 2001 From: Tinashe <70011086+tinashechiraya@users.noreply.github.com> Date: Tue, 3 Sep 2024 18:32:39 +0200 Subject: [PATCH] patch: modify test (#1076) * patch: modify test * patch: update view to return 404 as json --- django_project/monitor/observation_views.py | 10 ++++------ django_project/monitor/tests/test_observations.py | 11 ++++------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/django_project/monitor/observation_views.py b/django_project/monitor/observation_views.py index 6faf219e7..1b36af8f0 100644 --- a/django_project/monitor/observation_views.py +++ b/django_project/monitor/observation_views.py @@ -32,7 +32,6 @@ from rest_framework.viewsets import GenericViewSet from tensorflow import keras from django.contrib.auth.models import User -from rest_framework.response import Response from rest_framework import status @@ -60,11 +59,10 @@ def get_observations_by_site(request, site_id, format=None): observations = Observations.objects.filter(site=site) serializer = ObservationsAllFieldsSerializer(observations, many=True) - return JsonResponse( - {'status': 'success', 'observations': serializer.data} - ) - except Sites.DoesNotExist: - raise Http404("Site does not exist") + return JsonResponse({'status': 'success', 'observations': serializer.data}) + + except Sites.DoesNotExist as e: + return JsonResponse({'status': 'error', 'message': 'Site does not exist'}, status=404) # Use environment variables for Minio configuration diff --git a/django_project/monitor/tests/test_observations.py b/django_project/monitor/tests/test_observations.py index 0f2681a8e..17852f5e3 100644 --- a/django_project/monitor/tests/test_observations.py +++ b/django_project/monitor/tests/test_observations.py @@ -440,13 +440,10 @@ def test_observations_by_site_id(self): self.assertEqual(response['Content-Type'], 'application/json') def test_observations_by_nonexistent_site_id(self): - self.client.login(email='test@gmail.com', password='testuserpassword') - - url = reverse('observations-by-site', kwargs={'site_id': 999}) - - # Check if the Sites.DoesNotExist exception is raised - with self.assertRaises(Sites.DoesNotExist): - self.client.get(url) + self.client.login(email='test@gmail.com', password='testuserpassword') + url = reverse('observations-by-site', kwargs={'site_id': 999}) + response = self.client.get(url) + self.assertEqual(response.status_code, 404) def test_expert_auto_validated_observation(self): """