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
+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/",