UPDATE
This commit is contained in:
+158
-1
@@ -1,6 +1,7 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
from django.core.cache import cache
|
||||
from django.test import TestCase, override_settings
|
||||
from rest_framework.test import APIRequestFactory
|
||||
|
||||
from external_api_adapter.adapter import AdapterResponse
|
||||
@@ -10,8 +11,24 @@ from account.models import User
|
||||
from .views import SoilAnomalyDetectionView, SoilMoistureHeatmapView, SoilSummaryView
|
||||
|
||||
|
||||
TEST_CACHES = {
|
||||
"default": {
|
||||
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
|
||||
"LOCATION": "soil-tests",
|
||||
}
|
||||
}
|
||||
|
||||
TEST_SOIL_SUMMARY_CACHE_TTL = 14400
|
||||
TEST_SOIL_ANOMALIES_CACHE_TTL = 14400
|
||||
|
||||
|
||||
@override_settings(
|
||||
CACHES=TEST_CACHES,
|
||||
SOIL_ANOMALIES_CACHE_TTL=TEST_SOIL_ANOMALIES_CACHE_TTL,
|
||||
)
|
||||
class SoilAnomalyDetectionViewTests(TestCase):
|
||||
def setUp(self):
|
||||
cache.clear()
|
||||
self.factory = APIRequestFactory()
|
||||
self.user = User.objects.create_user(
|
||||
username="soil-user",
|
||||
@@ -61,6 +78,75 @@ class SoilAnomalyDetectionViewTests(TestCase):
|
||||
payload={"farm_uuid": str(self.farm.farm_uuid)},
|
||||
)
|
||||
|
||||
@patch("soil.views.external_api_request")
|
||||
def test_anomalies_cache_last_four_responses(self, mock_external_api_request):
|
||||
for index in range(5):
|
||||
farm = FarmHub.objects.create(owner=self.user, farm_type=self.farm_type, name=f"Soil Farm Cache {index}")
|
||||
mock_external_api_request.return_value = AdapterResponse(
|
||||
status_code=200,
|
||||
data={
|
||||
"data": {
|
||||
"farm_uuid": str(farm.farm_uuid),
|
||||
"summary": f"summary {index}",
|
||||
"anomalies": [],
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
request = self.factory.get(f"/api/soil/anomalies/?farm_uuid={farm.farm_uuid}")
|
||||
response = SoilAnomalyDetectionView.as_view()(request)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
cached_items = cache.get("soil:anomalies:recent")
|
||||
|
||||
self.assertEqual(len(cached_items), 4)
|
||||
self.assertEqual(cached_items[0]["summary"], "summary 4")
|
||||
self.assertEqual(cached_items[-1]["summary"], "summary 1")
|
||||
|
||||
@patch("soil.views.external_api_request")
|
||||
def test_anomalies_return_cached_response_for_same_farm(self, mock_external_api_request):
|
||||
mock_external_api_request.return_value = AdapterResponse(
|
||||
status_code=200,
|
||||
data={
|
||||
"data": {
|
||||
"farm_uuid": str(self.farm.farm_uuid),
|
||||
"summary": "cached summary",
|
||||
"anomalies": [],
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
for _ in range(2):
|
||||
request = self.factory.get(f"/api/soil/anomalies/?farm_uuid={self.farm.farm_uuid}")
|
||||
response = SoilAnomalyDetectionView.as_view()(request)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.data["data"]["summary"], "cached summary")
|
||||
|
||||
self.assertEqual(cache.get(f"soil:anomalies:{self.farm.farm_uuid}")["summary"], "cached summary")
|
||||
mock_external_api_request.assert_called_once()
|
||||
|
||||
@patch("soil.views.cache.set")
|
||||
@patch("soil.views.external_api_request")
|
||||
def test_anomalies_use_env_ttl_for_cache(self, mock_external_api_request, mock_cache_set):
|
||||
mock_external_api_request.return_value = AdapterResponse(
|
||||
status_code=200,
|
||||
data={
|
||||
"data": {
|
||||
"farm_uuid": str(self.farm.farm_uuid),
|
||||
"summary": "summary",
|
||||
"anomalies": [],
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
request = self.factory.get(f"/api/soil/anomalies/?farm_uuid={self.farm.farm_uuid}")
|
||||
response = SoilAnomalyDetectionView.as_view()(request)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTrue(any(call.kwargs.get("timeout") == TEST_SOIL_ANOMALIES_CACHE_TTL for call in mock_cache_set.call_args_list))
|
||||
|
||||
def test_anomalies_require_farm_uuid(self):
|
||||
request = self.factory.get("/api/soil/anomalies/")
|
||||
response = SoilAnomalyDetectionView.as_view()(request)
|
||||
@@ -146,8 +232,13 @@ class SoilMoistureHeatmapViewTests(TestCase):
|
||||
self.assertEqual(response.data["data"]["farm_uuid"][0], "Farm not found.")
|
||||
|
||||
|
||||
@override_settings(
|
||||
CACHES=TEST_CACHES,
|
||||
SOIL_SUMMARY_CACHE_TTL=TEST_SOIL_SUMMARY_CACHE_TTL,
|
||||
)
|
||||
class SoilSummaryViewTests(TestCase):
|
||||
def setUp(self):
|
||||
cache.clear()
|
||||
self.factory = APIRequestFactory()
|
||||
self.user = User.objects.create_user(
|
||||
username="soil-summary-user",
|
||||
@@ -193,6 +284,72 @@ class SoilSummaryViewTests(TestCase):
|
||||
payload={"farm_uuid": str(self.farm.farm_uuid)},
|
||||
)
|
||||
|
||||
@patch("soil.views.external_api_request")
|
||||
def test_summary_returns_cached_response_for_same_farm(self, mock_external_api_request):
|
||||
mock_external_api_request.return_value = AdapterResponse(
|
||||
status_code=200,
|
||||
data={
|
||||
"data": {
|
||||
"farm_uuid": str(self.farm.farm_uuid),
|
||||
"healthScore": 82,
|
||||
"profileSource": "Tomato",
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
for _ in range(2):
|
||||
request = self.factory.get(f"/api/soil/summary/?farm_uuid={self.farm.farm_uuid}")
|
||||
response = SoilSummaryView.as_view()(request)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.data["data"]["healthScore"], 82)
|
||||
|
||||
self.assertEqual(cache.get(f"soil:summary:{self.farm.farm_uuid}")["healthScore"], 82)
|
||||
mock_external_api_request.assert_called_once()
|
||||
|
||||
@patch("soil.views.cache.set")
|
||||
@patch("soil.views.external_api_request")
|
||||
def test_summary_uses_env_ttl_for_cache(self, mock_external_api_request, mock_cache_set):
|
||||
mock_external_api_request.return_value = AdapterResponse(
|
||||
status_code=200,
|
||||
data={
|
||||
"data": {
|
||||
"farm_uuid": str(self.farm.farm_uuid),
|
||||
"healthScore": 82,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
request = self.factory.get(f"/api/soil/summary/?farm_uuid={self.farm.farm_uuid}")
|
||||
response = SoilSummaryView.as_view()(request)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTrue(any(call.kwargs.get("timeout") == TEST_SOIL_SUMMARY_CACHE_TTL for call in mock_cache_set.call_args_list))
|
||||
|
||||
@patch("soil.views.external_api_request")
|
||||
def test_summary_caches_last_four_responses(self, mock_external_api_request):
|
||||
for index in range(5):
|
||||
farm = FarmHub.objects.create(owner=self.user, farm_type=self.farm_type, name=f"Soil Summary Cache {index}")
|
||||
mock_external_api_request.return_value = AdapterResponse(
|
||||
status_code=200,
|
||||
data={
|
||||
"data": {
|
||||
"farm_uuid": str(farm.farm_uuid),
|
||||
"healthScore": 80 + index,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
request = self.factory.get(f"/api/soil/summary/?farm_uuid={farm.farm_uuid}")
|
||||
response = SoilSummaryView.as_view()(request)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
cached_items = cache.get("soil:summary:recent")
|
||||
self.assertEqual(len(cached_items), 4)
|
||||
self.assertEqual(cached_items[0]["healthScore"], 84)
|
||||
self.assertEqual(cached_items[-1]["healthScore"], 81)
|
||||
|
||||
def test_summary_requires_farm_uuid(self):
|
||||
request = self.factory.get("/api/soil/summary/")
|
||||
response = SoilSummaryView.as_view()(request)
|
||||
|
||||
+59
-2
@@ -1,3 +1,5 @@
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
@@ -20,6 +22,39 @@ from .services import (
|
||||
)
|
||||
|
||||
|
||||
SOIL_ANOMALIES_CACHE_KEY = "soil:anomalies:recent"
|
||||
SOIL_ANOMALIES_CACHE_LIMIT = 4
|
||||
SOIL_SUMMARY_CACHE_KEY = "soil:summary:recent"
|
||||
SOIL_SUMMARY_CACHE_LIMIT = 4
|
||||
|
||||
|
||||
def _store_recent_soil_anomalies(payload):
|
||||
cached_items = cache.get(SOIL_ANOMALIES_CACHE_KEY, [])
|
||||
if not isinstance(cached_items, list):
|
||||
cached_items = []
|
||||
|
||||
cached_items.insert(0, payload)
|
||||
cache.set(SOIL_ANOMALIES_CACHE_KEY, cached_items[:SOIL_ANOMALIES_CACHE_LIMIT], timeout=None)
|
||||
|
||||
|
||||
def _store_recent_soil_summary(payload):
|
||||
cached_items = cache.get(SOIL_SUMMARY_CACHE_KEY, [])
|
||||
if not isinstance(cached_items, list):
|
||||
cached_items = []
|
||||
|
||||
cached_items.insert(0, payload)
|
||||
cache.set(SOIL_SUMMARY_CACHE_KEY, cached_items[:SOIL_SUMMARY_CACHE_LIMIT], timeout=None)
|
||||
|
||||
|
||||
def _build_soil_summary_cache_key(farm_uuid):
|
||||
return f"soil:summary:{farm_uuid}"
|
||||
|
||||
|
||||
def _build_soil_anomalies_cache_key(farm_uuid):
|
||||
return f"soil:anomalies:{farm_uuid}"
|
||||
|
||||
|
||||
|
||||
def _get_farm_from_request(request):
|
||||
farm_uuid = request.query_params.get("farm_uuid")
|
||||
if not farm_uuid:
|
||||
@@ -85,6 +120,14 @@ class SoilAnomalyDetectionView(APIView):
|
||||
status=status.HTTP_404_NOT_FOUND,
|
||||
)
|
||||
|
||||
cache_key = _build_soil_anomalies_cache_key(farm.farm_uuid)
|
||||
cached_anomalies = cache.get(cache_key)
|
||||
if isinstance(cached_anomalies, dict):
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": cached_anomalies},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
adapter_response = external_api_request(
|
||||
"ai",
|
||||
"/api/soile/anomaly-detection/",
|
||||
@@ -102,8 +145,11 @@ class SoilAnomalyDetectionView(APIView):
|
||||
status=adapter_response.status_code,
|
||||
)
|
||||
|
||||
response_payload = _extract_adapter_result(adapter_response.data)
|
||||
cache.set(cache_key, response_payload, timeout=settings.SOIL_ANOMALIES_CACHE_TTL)
|
||||
_store_recent_soil_anomalies(response_payload)
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": _extract_adapter_result(adapter_response.data)},
|
||||
{"code": 200, "msg": "success", "data": response_payload},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
@@ -177,6 +223,14 @@ class SoilSummaryView(APIView):
|
||||
status=status.HTTP_404_NOT_FOUND,
|
||||
)
|
||||
|
||||
cache_key = _build_soil_summary_cache_key(farm.farm_uuid)
|
||||
cached_summary = cache.get(cache_key)
|
||||
if isinstance(cached_summary, dict):
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": cached_summary},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
adapter_response = external_api_request(
|
||||
"ai",
|
||||
"/api/soile/health-summary/",
|
||||
@@ -194,7 +248,10 @@ class SoilSummaryView(APIView):
|
||||
status=adapter_response.status_code,
|
||||
)
|
||||
|
||||
response_payload = _extract_adapter_result(adapter_response.data)
|
||||
cache.set(cache_key, response_payload, timeout=settings.SOIL_SUMMARY_CACHE_TTL)
|
||||
_store_recent_soil_summary(response_payload)
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": _extract_adapter_result(adapter_response.data)},
|
||||
{"code": 200, "msg": "success", "data": response_payload},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user