This commit is contained in:
2026-04-30 02:10:27 +03:30
parent 5d8ad57b2d
commit 46a50545bb
9 changed files with 1158 additions and 24 deletions
+19 -4
View File
@@ -59,19 +59,32 @@ class YieldHarvestSummarySerializer(serializers.Serializer):
class CropSimulationRequestSerializer(serializers.Serializer):
farm_uuid = serializers.UUIDField(required=True, help_text="UUID مزرعه برای اجرای شبیه‌سازی.")
plant_name = serializers.CharField(required=False, allow_blank=True, default="", help_text="نام گیاه یا محصول.")
farm_uuid = serializers.UUIDField(
required=True,
initial="11111111-1111-1111-1111-111111111111",
help_text="UUID مزرعه برای اجرای شبیه‌سازی.",
)
class GrowthSimulationRequestSerializer(serializers.Serializer):
plant_name = serializers.CharField(required=True, help_text="نام گیاه برای شروع شبیه‌سازی رشد.")
plant_name = serializers.CharField(
required=False,
allow_blank=True,
default="",
help_text="نام گیاه؛ اگر farm_uuid ارسال شود از محصول مزرعه استفاده می‌شود.",
)
dynamic_parameters = serializers.ListField(
child=serializers.CharField(),
required=True,
allow_empty=False,
help_text="لیست پارامترهای دینامیک موردنیاز مانند DVS یا LAI.",
)
farm_uuid = serializers.UUIDField(required=False, allow_null=True, help_text="UUID مزرعه؛ در صورت نبود باید weather ارسال شود.")
farm_uuid = serializers.UUIDField(
required=False,
allow_null=True,
initial="11111111-1111-1111-1111-111111111111",
help_text="UUID مزرعه؛ در صورت نبود باید weather ارسال شود.",
)
weather = serializers.JSONField(required=False, help_text="آب‌وهوا به‌صورت object یا array.")
soil_parameters = serializers.DictField(required=False, help_text="پارامترهای خاک.")
site_parameters = serializers.DictField(required=False, help_text="پارامترهای سایت.")
@@ -82,6 +95,8 @@ class GrowthSimulationRequestSerializer(serializers.Serializer):
def validate(self, attrs):
if not attrs.get("farm_uuid") and attrs.get("weather") in (None, "", [], {}):
raise serializers.ValidationError("At least one of 'farm_uuid' or 'weather' must be provided.")
if not attrs.get("farm_uuid") and not (attrs.get("plant_name") or "").strip():
raise serializers.ValidationError({"plant_name": ["This field is required when farm_uuid is not provided."]})
return attrs
+156 -4
View File
@@ -5,7 +5,7 @@ from django.test import TestCase
from rest_framework.test import APIClient, 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 (
CurrentFarmChartView,
@@ -36,6 +36,8 @@ class CropSimulationViewTests(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.other_farm = FarmHub.objects.create(owner=self.other_user, farm_type=self.farm_type, name="Farm 2")
self.product = Product.objects.create(farm_type=self.farm_type, name="گوجه‌فرنگی")
self.farm.products.add(self.product)
self.api_client.force_authenticate(user=self.user)
@patch("yield_harvest.views.external_api_request")
@@ -100,6 +102,41 @@ class CropSimulationViewTests(TestCase):
self.assertEqual(response.status_code, 202)
self.assertEqual(response.json()["data"]["task_id"], "growth-task-123")
@patch("yield_harvest.views.external_api_request")
def test_growth_yield_harvest_route_queues_simulation_task(self, mock_external_api_request):
mock_external_api_request.return_value = AdapterResponse(
status_code=202,
data={
"data": {
"task_id": "growth-task-123",
"status_url": "/api/crop-simulation/growth/growth-task-123/status/",
"plant_name": "گوجه‌فرنگی",
}
},
)
response = self.api_client.post(
"/api/yield-harvest/growth/",
{
"plant_name": "wheat",
"dynamic_parameters": ["DVS", "LAI"],
"farm_uuid": str(self.farm.farm_uuid),
},
format="json",
)
self.assertEqual(response.status_code, 202)
mock_external_api_request.assert_called_once_with(
"ai",
"/api/crop-simulation/growth/",
method="POST",
payload={
"plant_name": "گوجه‌فرنگی",
"dynamic_parameters": ["DVS", "LAI"],
"farm_uuid": str(self.farm.farm_uuid),
},
)
def test_growth_requires_farm_uuid_or_weather(self):
request = self.factory.post(
"/api/yield-harvest/crop-simulation/growth/",
@@ -171,6 +208,18 @@ class CropSimulationViewTests(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["data"]["status"], "SUCCESS")
@patch("yield_harvest.views.external_api_request")
def test_growth_status_yield_harvest_route_proxies_to_ai_service(self, mock_external_api_request):
mock_external_api_request.return_value = AdapterResponse(
status_code=200,
data={"data": {"task_id": "growth-task-123", "status": "SUCCESS"}},
)
response = self.api_client.get("/api/yield-harvest/growth/growth-task-123/status/?page=1&page_size=10")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["data"]["status"], "SUCCESS")
def test_legacy_plant_simulator_routes_are_unavailable(self):
legacy_paths = [
"/api/yield-harvest/plant-simulator/config/",
@@ -193,7 +242,7 @@ class CropSimulationViewTests(TestCase):
"data": {
"result": {
"farm_uuid": str(self.farm.farm_uuid),
"plant_name": "wheat",
"plant_name": "گوجه‌فرنگی",
"scenario_id": 1,
"categories": ["day1"],
"series": {"biomass": [1.2]},
@@ -218,7 +267,7 @@ class CropSimulationViewTests(TestCase):
"ai",
"/api/crop-simulation/current-farm-chart/",
method="POST",
payload={"farm_uuid": str(self.farm.farm_uuid), "plant_name": "wheat"},
payload={"farm_uuid": str(self.farm.farm_uuid), "plant_name": "گوجه‌فرنگی"},
)
@patch("yield_harvest.views.external_api_request")
@@ -237,6 +286,27 @@ class CropSimulationViewTests(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["data"]["farm_uuid"], str(self.farm.farm_uuid))
@patch("yield_harvest.views.external_api_request")
def test_current_farm_chart_yield_harvest_route_proxies_to_ai_service(self, mock_external_api_request):
mock_external_api_request.return_value = AdapterResponse(
status_code=200,
data={"data": {"result": {"farm_uuid": str(self.farm.farm_uuid), "plant_name": "گوجه‌فرنگی"}}},
)
response = self.api_client.post(
"/api/yield-harvest/current-farm-chart/",
{"farm_uuid": str(self.farm.farm_uuid), "plant_name": "wheat"},
format="json",
)
self.assertEqual(response.status_code, 200)
mock_external_api_request.assert_called_once_with(
"ai",
"/api/crop-simulation/current-farm-chart/",
method="POST",
payload={"farm_uuid": str(self.farm.farm_uuid), "plant_name": "گوجه‌فرنگی"},
)
@patch("yield_harvest.views.external_api_request")
def test_harvest_prediction_proxies_to_ai_service(self, mock_external_api_request):
mock_external_api_request.return_value = AdapterResponse(
@@ -268,7 +338,7 @@ class CropSimulationViewTests(TestCase):
"ai",
"/api/crop-simulation/harvest-prediction/",
method="POST",
payload={"farm_uuid": str(self.farm.farm_uuid), "plant_name": ""},
payload={"farm_uuid": str(self.farm.farm_uuid), "plant_name": "گوجه‌فرنگی"},
)
@patch("yield_harvest.views.external_api_request")
@@ -287,6 +357,27 @@ class CropSimulationViewTests(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["data"]["daysUntil"], 96)
@patch("yield_harvest.views.external_api_request")
def test_harvest_prediction_yield_harvest_route_proxies_to_ai_service(self, mock_external_api_request):
mock_external_api_request.return_value = AdapterResponse(
status_code=200,
data={"data": {"result": {"date": "2026-07-15", "daysUntil": 96}}},
)
response = self.api_client.post(
"/api/yield-harvest/harvest-prediction/",
{"farm_uuid": str(self.farm.farm_uuid)},
format="json",
)
self.assertEqual(response.status_code, 200)
mock_external_api_request.assert_called_once_with(
"ai",
"/api/crop-simulation/harvest-prediction/",
method="POST",
payload={"farm_uuid": str(self.farm.farm_uuid), "plant_name": "گوجه‌فرنگی"},
)
@patch("yield_harvest.views.external_api_request")
def test_yield_prediction_proxies_to_ai_service(self, mock_external_api_request):
mock_external_api_request.return_value = AdapterResponse(
@@ -313,6 +404,12 @@ class CropSimulationViewTests(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["data"]["predictedYieldTons"], 8.4)
mock_external_api_request.assert_called_once_with(
"ai",
"/api/crop-simulation/yield-prediction/",
method="POST",
payload={"farm_uuid": str(self.farm.farm_uuid), "plant_name": "گوجه‌فرنگی"},
)
@patch("yield_harvest.views.external_api_request")
def test_yield_prediction_top_level_route_proxies_to_ai_service(self, mock_external_api_request):
@@ -330,6 +427,49 @@ class CropSimulationViewTests(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["data"]["predictedYieldTons"], 8.4)
@patch("yield_harvest.views.external_api_request")
def test_yield_prediction_yield_harvest_route_proxies_to_ai_service(self, mock_external_api_request):
mock_external_api_request.return_value = AdapterResponse(
status_code=200,
data={"data": {"result": {"farm_uuid": str(self.farm.farm_uuid), "predictedYieldTons": 8.4}}},
)
response = self.api_client.post(
"/api/yield-harvest/yield-prediction/",
{"farm_uuid": str(self.farm.farm_uuid)},
format="json",
)
self.assertEqual(response.status_code, 200)
mock_external_api_request.assert_called_once_with(
"ai",
"/api/crop-simulation/yield-prediction/",
method="POST",
payload={"farm_uuid": str(self.farm.farm_uuid), "plant_name": "گوجه‌فرنگی"},
)
@patch("yield_harvest.views.external_api_request")
def test_yield_prediction_falls_back_to_farm_type_product_when_farm_products_are_empty(self, mock_external_api_request):
farm_without_products = FarmHub.objects.create(owner=self.user, farm_type=self.farm_type, name="Farm fallback")
mock_external_api_request.return_value = AdapterResponse(
status_code=200,
data={"data": {"result": {"farm_uuid": str(farm_without_products.farm_uuid), "predictedYieldTons": 8.4}}},
)
response = self.api_client.post(
"/api/yield-harvest/yield-prediction/",
{"farm_uuid": str(farm_without_products.farm_uuid)},
format="json",
)
self.assertEqual(response.status_code, 200)
mock_external_api_request.assert_called_once_with(
"ai",
"/api/crop-simulation/yield-prediction/",
method="POST",
payload={"farm_uuid": str(farm_without_products.farm_uuid), "plant_name": "گوجه‌فرنگی"},
)
@patch("yield_harvest.views.external_api_request")
def test_yield_harvest_summary_top_level_route_proxies_to_ai_service(self, mock_external_api_request):
mock_external_api_request.return_value = AdapterResponse(
@@ -368,6 +508,18 @@ class CropSimulationViewTests(TestCase):
},
)
@patch("yield_harvest.views.external_api_request")
def test_yield_harvest_summary_yield_harvest_route_proxies_to_ai_service(self, mock_external_api_request):
mock_external_api_request.return_value = AdapterResponse(
status_code=200,
data={"data": {"result": {"farm_uuid": str(self.farm.farm_uuid), "yield_prediction_chart": {"series": []}}}},
)
response = self.api_client.get(f"/api/yield-harvest/yield-harvest-summary/?farm_uuid={self.farm.farm_uuid}")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["data"]["farm_uuid"], str(self.farm.farm_uuid))
def test_crop_simulation_rejects_foreign_farm_uuid(self):
request = self.factory.post(
"/api/yield-harvest/crop-simulation/yield-prediction/",
+6 -6
View File
@@ -11,10 +11,10 @@ from .views import (
urlpatterns = [
path("summary/", YieldHarvestSummaryView.as_view(), name="yield-harvest-summary"),
path("crop-simulation/current-farm-chart/", CurrentFarmChartView.as_view(), name="yield-harvest-current-farm-chart"),
path("crop-simulation/growth/", GrowthSimulationView.as_view(), name="yield-harvest-growth"),
path("crop-simulation/growth/<str:task_id>/status/", GrowthSimulationStatusView.as_view(), name="yield-harvest-growth-status"),
path("crop-simulation/harvest-prediction/", HarvestPredictionView.as_view(), name="yield-harvest-harvest-prediction"),
path("crop-simulation/yield-prediction/", YieldPredictionView.as_view(), name="yield-harvest-yield-prediction"),
path("crop-simulation/yield-harvest-summary/", YieldHarvestSummaryView.as_view(), name="yield-harvest-crop-simulation-summary"),
path("current-farm-chart/", CurrentFarmChartView.as_view(), name="yield-harvest-current-farm-chart"),
path("growth/", GrowthSimulationView.as_view(), name="yield-harvest-growth"),
path("growth/<str:task_id>/status/", GrowthSimulationStatusView.as_view(), name="yield-harvest-growth-status"),
path("harvest-prediction/", HarvestPredictionView.as_view(), name="yield-harvest-harvest-prediction"),
path("yield-prediction/", YieldPredictionView.as_view(), name="yield-harvest-yield-prediction"),
path("yield-harvest-summary/", YieldHarvestSummaryView.as_view(), name="yield-harvest-crop-simulation-summary"),
]
+31 -5
View File
@@ -179,6 +179,18 @@ class CropSimulationBaseView(APIView):
status=adapter_response.status_code,
)
@staticmethod
def _get_first_farm_product_name(farm):
first_product = farm.products.order_by("id").first()
if first_product is not None:
return (first_product.name or "").strip()
fallback_product = farm.farm_type.products.order_by("id").first()
if fallback_product is not None:
return (fallback_product.name or "").strip()
return ""
class CurrentFarmChartView(CropSimulationBaseView):
ai_path = "/api/crop-simulation/current-farm-chart/"
@@ -197,7 +209,10 @@ class CurrentFarmChartView(CropSimulationBaseView):
if error_response is not None:
return error_response
ai_payload = {"farm_uuid": str(farm.farm_uuid), "plant_name": payload.get("plant_name", "")}
ai_payload = {
"farm_uuid": str(farm.farm_uuid),
"plant_name": self._get_first_farm_product_name(farm),
}
adapter_response = external_api_request("ai", self.ai_path, method="POST", payload=ai_payload)
if adapter_response.status_code >= 400:
@@ -226,7 +241,10 @@ class HarvestPredictionView(CropSimulationBaseView):
if error_response is not None:
return error_response
ai_payload = {"farm_uuid": str(farm.farm_uuid), "plant_name": payload.get("plant_name", "")}
ai_payload = {
"farm_uuid": str(farm.farm_uuid),
"plant_name": self._get_first_farm_product_name(farm),
}
adapter_response = external_api_request("ai", self.ai_path, method="POST", payload=ai_payload)
if adapter_response.status_code >= 400:
@@ -255,7 +273,10 @@ class YieldPredictionView(CropSimulationBaseView):
if error_response is not None:
return error_response
ai_payload = {"farm_uuid": str(farm.farm_uuid), "plant_name": payload.get("plant_name", "")}
ai_payload = {
"farm_uuid": str(farm.farm_uuid),
"plant_name": self._get_first_farm_product_name(farm),
}
adapter_response = external_api_request("ai", self.ai_path, method="POST", payload=ai_payload)
if adapter_response.status_code >= 400:
@@ -278,8 +299,13 @@ class GrowthSimulationView(APIView):
serializer.is_valid(raise_exception=True)
payload = serializer.validated_data.copy()
if payload.get("farm_uuid") is not None:
payload["farm_uuid"] = str(payload["farm_uuid"])
farm_uuid = payload.get("farm_uuid")
if farm_uuid is not None:
farm, error_response = CropSimulationBaseView._get_farm(request, farm_uuid)
if error_response is not None:
return error_response
payload["farm_uuid"] = str(farm.farm_uuid)
payload["plant_name"] = CropSimulationBaseView._get_first_farm_product_name(farm)
adapter_response = external_api_request(
"ai",