This commit is contained in:
2026-04-30 01:01:04 +03:30
parent 8139a49756
commit 5d8ad57b2d
21 changed files with 1841 additions and 76 deletions
+7 -5
View File
@@ -37,11 +37,13 @@ class PestDetectionAnalyzeResponseSerializer(serializers.Serializer):
class PestDetectionRiskRequestSerializer(serializers.Serializer):
farm_uuid = serializers.UUIDField(required=True, help_text="UUID مزرعه برای تحلیل ریسک آفت/بیماری.")
sensor_uuid = serializers.UUIDField(required=False, help_text="UUID سنسور مرتبط در صورت وجود.")
plant_name = serializers.CharField(required=False, allow_blank=True, default="", help_text="نام محصول یا گیاه.")
growth_stage = serializers.CharField(required=False, allow_blank=True, default="", help_text="مرحله رشد گیاه.")
query = serializers.CharField(required=False, allow_blank=True, default="", help_text="پرسش تکمیلی کاربر.")
farm_uuid = serializers.UUIDField(default="11111111-1111-1111-1111-111111111111", help_text="UUID مزرعه برای تحلیل ریسک آفت/بیماری.")
plant_name = serializers.CharField(required=False, allow_blank=True, default="پیاز", help_text="نام محصول یا گیاه.")
growth_stage = serializers.CharField(required=False, allow_blank=True, default="گلدهی", help_text="مرحله رشد گیاه.")
class PestDetectionRiskSummaryRequestSerializer(serializers.Serializer):
farm_uuid = serializers.UUIDField(required=True, help_text="UUID مزرعه برای خلاصه ریسک آفت/بیماری.")
class RiskBreakdownSerializer(serializers.Serializer):
+183 -7
View File
@@ -1,18 +1,34 @@
from unittest.mock import patch
from django.core.cache import cache
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.test import TestCase, override_settings
from django.urls import resolve
from rest_framework.test import APIRequestFactory, force_authenticate
from external_api_adapter.adapter import AdapterResponse
from farm_hub.models import FarmHub, FarmType
from farm_hub.models import FarmHub, FarmType, Product
from .views import AnalyzeView, RiskSummaryView, RiskView
TEST_CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
"LOCATION": "pest-detection-tests",
}
}
TEST_RISK_SUMMARY_CACHE_TTL = 14400
@override_settings(
CACHES=TEST_CACHES,
PEST_DISEASE_RISK_SUMMARY_CACHE_TTL=TEST_RISK_SUMMARY_CACHE_TTL,
)
class PestDetectionViewTests(TestCase):
def setUp(self):
cache.clear()
self.factory = APIRequestFactory()
self.user = get_user_model().objects.create_user(
username="farmer",
@@ -28,6 +44,8 @@ class PestDetectionViewTests(TestCase):
)
self.farm_type = FarmType.objects.create(name="زراعی")
self.farm = FarmHub.objects.create(owner=self.user, farm_type=self.farm_type, name="Farm 1")
self.product = Product.objects.create(farm_type=self.farm_type, name="پیاز")
self.farm.products.add(self.product)
self.other_farm = FarmHub.objects.create(owner=self.other_user, farm_type=self.farm_type, name="Farm 2")
@patch("pest_detection.views.external_api_request")
@@ -127,7 +145,6 @@ class PestDetectionViewTests(TestCase):
"farm_uuid": str(self.farm.farm_uuid),
"plant_name": "wheat",
"growth_stage": "",
"query": "",
},
)
@@ -163,9 +180,13 @@ class PestDetectionViewTests(TestCase):
self.assertEqual(response.data["data"]["drivers"], {"humidity": "high"})
mock_external_api_request.assert_called_once_with(
"ai",
"/api/pest-disease/risk-summary/",
"/api/pest-disease/risk/",
method="POST",
payload={"farm_uuid": str(self.farm.farm_uuid)},
payload={
"farm_uuid": str(self.farm.farm_uuid),
"plant_name": "پیاز",
"growth_stage": "گلدهی",
},
)
@patch("pest_detection.views.external_api_request")
@@ -196,11 +217,166 @@ class PestDetectionViewTests(TestCase):
self.assertEqual(response.data["data"]["farm_uuid"], str(self.farm.farm_uuid))
mock_external_api_request.assert_called_once_with(
"ai",
"/api/pest-disease/risk-summary/",
"/api/pest-disease/risk/",
method="POST",
payload={"farm_uuid": str(self.farm.farm_uuid)},
payload={
"farm_uuid": str(self.farm.farm_uuid),
"plant_name": "پیاز",
"growth_stage": "گلدهی",
},
)
@patch("pest_detection.views.external_api_request")
def test_risk_summary_uses_blank_plant_name_when_farm_has_no_products(self, mock_external_api_request):
mock_external_api_request.return_value = AdapterResponse(
status_code=200,
data={
"data": {
"result": {
"disease_risk": {"title": "Disease"},
"pest_risk": {"title": "Pest"},
"drivers": {},
}
}
},
)
farm_without_products = FarmHub.objects.create(owner=self.user, farm_type=self.farm_type, name="Farm 3")
request = self.factory.post(
"/api/pest-disease/risk-summary/",
{"farm_uuid": str(farm_without_products.farm_uuid)},
format="json",
)
force_authenticate(request, user=self.user)
response = RiskSummaryView.as_view()(request)
self.assertEqual(response.status_code, 200)
mock_external_api_request.assert_called_once_with(
"ai",
"/api/pest-disease/risk/",
method="POST",
payload={
"farm_uuid": str(farm_without_products.farm_uuid),
"plant_name": "",
"growth_stage": "گلدهی",
},
)
@patch("pest_detection.views.external_api_request")
def test_risk_summary_caches_last_four_responses(self, mock_external_api_request):
for index in range(5):
product = Product.objects.create(farm_type=self.farm_type, name=f"Product {index}")
farm = FarmHub.objects.create(owner=self.user, farm_type=self.farm_type, name=f"Farm {index + 10}")
farm.products.add(product)
mock_external_api_request.return_value = AdapterResponse(
status_code=200,
data={
"data": {
"result": {
"disease_risk": {"title": f"Disease {index}"},
"pest_risk": {"title": f"Pest {index}"},
"drivers": {"index": index},
}
}
},
)
request = self.factory.post(
"/api/pest-disease/risk-summary/",
{"farm_uuid": str(farm.farm_uuid)},
format="json",
)
force_authenticate(request, user=self.user)
response = RiskSummaryView.as_view()(request)
self.assertEqual(response.status_code, 200)
cached_items = cache.get(RiskSummaryView.RISK_SUMMARY_CACHE_KEY)
self.assertEqual(len(cached_items), 4)
self.assertEqual(cached_items[0]["drivers"], {"index": 4})
self.assertEqual(cached_items[-1]["drivers"], {"index": 1})
@patch("pest_detection.views.external_api_request")
def test_risk_summary_returns_cached_response_for_same_farm(self, mock_external_api_request):
mock_external_api_request.return_value = AdapterResponse(
status_code=200,
data={
"data": {
"result": {
"disease_risk": {"title": "Disease"},
"pest_risk": {"title": "Pest"},
"drivers": {"humidity": "high"},
}
}
},
)
for _ in range(2):
request = self.factory.post(
"/api/pest-disease/risk-summary/",
{"farm_uuid": str(self.farm.farm_uuid)},
format="json",
)
force_authenticate(request, user=self.user)
response = RiskSummaryView.as_view()(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["data"]["drivers"], {"humidity": "high"})
cache_key = RiskSummaryView._build_risk_summary_cache_key(self.user.id, self.farm.farm_uuid)
self.assertEqual(cache.get(cache_key)["farm_uuid"], str(self.farm.farm_uuid))
mock_external_api_request.assert_called_once()
@patch("pest_detection.views.cache.set")
@patch("pest_detection.views.external_api_request")
def test_risk_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": {
"result": {
"disease_risk": {"title": "Disease"},
"pest_risk": {"title": "Pest"},
"drivers": {},
}
}
},
)
request = self.factory.post(
"/api/pest-disease/risk-summary/",
{"farm_uuid": str(self.farm.farm_uuid)},
format="json",
)
force_authenticate(request, user=self.user)
response = RiskSummaryView.as_view()(request)
self.assertEqual(response.status_code, 200)
self.assertTrue(
any(call.kwargs.get("timeout") == TEST_RISK_SUMMARY_CACHE_TTL for call in mock_cache_set.call_args_list)
)
def test_risk_summary_rejects_extra_fields(self):
request = self.factory.post(
"/api/pest-disease/risk-summary/",
{
"farm_uuid": str(self.farm.farm_uuid),
"plant_name": "گندم",
"growth_stage": "رشد رویشی",
},
format="json",
)
force_authenticate(request, user=self.user)
response = RiskSummaryView.as_view()(request)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.data["code"], 400)
self.assertIn("non_field_errors", response.data["data"])
def test_risk_summary_rejects_foreign_farm_uuid(self):
request = self.factory.post(
"/api/pest-disease/risk-summary/",
+53 -14
View File
@@ -4,6 +4,8 @@ Pest detection API views.
import json
from django.conf import settings
from django.core.cache import cache
from drf_spectacular.utils import extend_schema
from rest_framework import status
from rest_framework.response import Response
@@ -18,10 +20,27 @@ from .serializers import (
PestDetectionRiskRequestSerializer,
PestDetectionRiskResponseSerializer,
PestDetectionRiskSummaryResponseSerializer,
PestDetectionRiskSummaryRequestSerializer,
)
class PestDetectionFarmMixin:
RISK_SUMMARY_CACHE_KEY = "pest-disease:risk-summary:recent"
RISK_SUMMARY_CACHE_LIMIT = 4
@classmethod
def _store_recent_risk_summary(cls, payload):
cached_items = cache.get(cls.RISK_SUMMARY_CACHE_KEY, [])
if not isinstance(cached_items, list):
cached_items = []
cached_items.insert(0, payload)
cache.set(cls.RISK_SUMMARY_CACHE_KEY, cached_items[:cls.RISK_SUMMARY_CACHE_LIMIT], timeout=None)
@staticmethod
def _build_risk_summary_cache_key(user_id, farm_uuid):
return f"pest-disease:risk-summary:{user_id}:{farm_uuid}"
@staticmethod
def _get_farm(request, farm_uuid):
if not farm_uuid:
@@ -62,6 +81,13 @@ class PestDetectionFarmMixin:
image_urls = parsed if parsed is not None else [image_urls]
return [str(item) for item in image_urls if str(item).strip()]
@staticmethod
def _get_first_farm_product_name(farm):
first_product = farm.products.order_by("id").first()
if first_product is None:
return ""
return (first_product.name or "").strip()
@staticmethod
def _attach_uploaded_files(payload, uploaded_images):
if not uploaded_images:
@@ -187,15 +213,12 @@ class RiskView(PestDetectionFarmMixin, APIView):
if error_response is not None:
return error_response
plant_name = self._get_first_farm_product_name(farm)
ai_payload = {
"farm_uuid": str(farm.farm_uuid),
"plant_name": payload.get("plant_name", ""),
"growth_stage": payload.get("growth_stage", ""),
"query": payload.get("query", ""),
"plant_name": plant_name,
"growth_stage": "گلدهی",
}
sensor_uuid = payload.get("sensor_uuid")
if sensor_uuid:
ai_payload["sensor_uuid"] = str(sensor_uuid)
adapter_response = external_api_request(
"ai",
@@ -216,26 +239,40 @@ class RiskView(PestDetectionFarmMixin, APIView):
class RiskSummaryView(PestDetectionFarmMixin, APIView):
@extend_schema(
tags=["Pest Detection"],
request=PestDetectionRiskRequestSerializer,
request=PestDetectionRiskSummaryRequestSerializer,
responses={200: status_response("PestDetectionRiskSummaryResponse", data=PestDetectionRiskSummaryResponseSerializer())},
)
def post(self, request):
farm_uuid = request.data.get("farm_uuid")
sensor_uuid = request.data.get("sensor_uuid")
serializer = PestDetectionRiskSummaryRequestSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
payload = serializer.validated_data
farm_uuid = payload.get("farm_uuid")
farm, error_response = self._get_farm(request, farm_uuid)
if error_response is not None:
return error_response
payload = {"farm_uuid": str(farm.farm_uuid)}
if sensor_uuid:
payload["sensor_uuid"] = str(sensor_uuid)
cache_key = self._build_risk_summary_cache_key(request.user.id, farm.farm_uuid)
cached_response = cache.get(cache_key)
if isinstance(cached_response, dict):
return Response(
{"code": 200, "msg": "success", "data": cached_response},
status=status.HTTP_200_OK,
)
plant_name = self._get_first_farm_product_name(farm)
ai_payload = {
"farm_uuid": str(farm.farm_uuid),
"plant_name": plant_name,
"growth_stage": "گلدهی",
}
adapter_response = external_api_request(
"ai",
"/api/pest-disease/risk-summary/",
"/api/pest-disease/risk/",
method="POST",
payload=payload,
payload=ai_payload,
)
if adapter_response.status_code >= 400:
@@ -248,6 +285,8 @@ class RiskSummaryView(PestDetectionFarmMixin, APIView):
"pestRisk": result.get("pestRisk") or result.get("pest_risk") or {},
"drivers": result.get("drivers") if isinstance(result.get("drivers"), dict) else {},
}
cache.set(cache_key, response_payload, timeout=settings.PEST_DISEASE_RISK_SUMMARY_CACHE_TTL)
self._store_recent_risk_summary(response_payload)
return Response(
{"code": 200, "msg": "success", "data": response_payload},
status=status.HTTP_200_OK,