UPDATE
This commit is contained in:
@@ -1,77 +0,0 @@
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase, override_settings
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF="config.test_urls")
|
||||
class RagRecommendationApiTests(TestCase):
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
|
||||
@patch("rag.services.irrigation.get_irrigation_recommendation")
|
||||
def test_irrigation_recommendation_returns_direct_result(self, mock_get_irrigation_recommendation):
|
||||
mock_get_irrigation_recommendation.return_value = {
|
||||
"plan": {
|
||||
"frequencyPerWeek": 3,
|
||||
"durationMinutes": 25,
|
||||
},
|
||||
"raw_response": "{\"plan\": {\"frequencyPerWeek\": 3, \"durationMinutes\": 25}}",
|
||||
}
|
||||
|
||||
response = self.client.post(
|
||||
"/api/rag/recommend/irrigation/",
|
||||
data={
|
||||
"farm_uuid": "sensor-123",
|
||||
"plant_name": "گوجهفرنگی",
|
||||
"growth_stage": "میوهدهی",
|
||||
"irrigation_method_name": "قطرهای",
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()["data"]["plan"]["frequencyPerWeek"], 3)
|
||||
mock_get_irrigation_recommendation.assert_called_once_with(
|
||||
farm_uuid="sensor-123",
|
||||
plant_name="گوجهفرنگی",
|
||||
growth_stage="میوهدهی",
|
||||
irrigation_method_name="قطرهای",
|
||||
query=None,
|
||||
)
|
||||
|
||||
@patch("rag.services.fertilization.get_fertilization_recommendation")
|
||||
def test_fertilization_recommendation_returns_direct_result(self, mock_get_fertilization_recommendation):
|
||||
mock_get_fertilization_recommendation.return_value = {
|
||||
"plan": {
|
||||
"npkRatio": "20-20-20",
|
||||
"amountPerHectare": "80 kg",
|
||||
},
|
||||
"raw_response": "{\"plan\": {\"npkRatio\": \"20-20-20\", \"amountPerHectare\": \"80 kg\"}}",
|
||||
}
|
||||
|
||||
response = self.client.post(
|
||||
"/api/rag/recommend/fertilization/",
|
||||
data={
|
||||
"farm_uuid": "sensor-456",
|
||||
"plant_name": "گندم",
|
||||
"growth_stage": "رویشی",
|
||||
},
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.json()["data"]["plan"]["npkRatio"], "20-20-20")
|
||||
mock_get_fertilization_recommendation.assert_called_once_with(
|
||||
farm_uuid="sensor-456",
|
||||
plant_name="گندم",
|
||||
growth_stage="رویشی",
|
||||
query=None,
|
||||
)
|
||||
|
||||
def test_removed_status_endpoints_return_404(self):
|
||||
irrigation_response = self.client.get("/api/rag/recommend/irrigation/sample-task/status/")
|
||||
fertilization_response = self.client.get("/api/rag/recommend/fertilization/sample-task/status/")
|
||||
|
||||
self.assertEqual(irrigation_response.status_code, 404)
|
||||
self.assertEqual(fertilization_response.status_code, 404)
|
||||
@@ -2,12 +2,8 @@ from django.urls import path
|
||||
|
||||
from .views import (
|
||||
ChatView,
|
||||
IrrigationRecommendationView,
|
||||
FertilizationRecommendationView,
|
||||
)
|
||||
|
||||
urlpatterns = [
|
||||
path("chat/", ChatView.as_view()),
|
||||
path("recommend/irrigation/", IrrigationRecommendationView.as_view(), name="recommend-irrigation"),
|
||||
path("recommend/fertilization/", FertilizationRecommendationView.as_view(), name="recommend-fertilization"),
|
||||
]
|
||||
|
||||
-171
@@ -38,14 +38,6 @@ RagValidationErrorResponseSerializer = build_envelope_serializer(
|
||||
data_required=False,
|
||||
allow_null=True,
|
||||
)
|
||||
RagIrrigationResponseSerializer = build_envelope_serializer(
|
||||
"RagIrrigationResponseSerializer",
|
||||
drf_serializers.JSONField(),
|
||||
)
|
||||
RagFertilizationResponseSerializer = build_envelope_serializer(
|
||||
"RagFertilizationResponseSerializer",
|
||||
drf_serializers.JSONField(),
|
||||
)
|
||||
|
||||
|
||||
class ChatView(APIView):
|
||||
@@ -211,166 +203,3 @@ class ChatView(APIView):
|
||||
generate(),
|
||||
content_type="text/plain; charset=utf-8",
|
||||
)
|
||||
|
||||
|
||||
class IrrigationRecommendationView(APIView):
|
||||
"""
|
||||
توصیه آبیاری به صورت مستقیم.
|
||||
POST با farm_uuid، plant_name، growth_stage، irrigation_method_name.
|
||||
نتیجه همان لحظه برگشت داده میشود.
|
||||
"""
|
||||
|
||||
@extend_schema(
|
||||
tags=["RAG Recommendations"],
|
||||
summary="درخواست توصیه آبیاری",
|
||||
description=(
|
||||
"دادههای سنسور، گیاه و روش آبیاری را دریافت کرده و "
|
||||
"توصیه آبیاری را به صورت مستقیم برمیگرداند."
|
||||
),
|
||||
request=inline_serializer(
|
||||
name="IrrigationRecommendationRequest",
|
||||
fields={
|
||||
"farm_uuid": drf_serializers.CharField(help_text="شناسه یکتای مزرعه (اجباری)"),
|
||||
"sensor_uuid": drf_serializers.CharField(required=False, help_text="نام قدیمی برای farm_uuid"),
|
||||
"plant_name": drf_serializers.CharField(required=False, help_text="نام گیاه"),
|
||||
"growth_stage": drf_serializers.CharField(required=False, help_text="مرحله رشد گیاه"),
|
||||
"irrigation_method_name": drf_serializers.CharField(required=False, help_text="نام روش آبیاری"),
|
||||
"query": drf_serializers.CharField(required=False, help_text="سوال اختیاری"),
|
||||
},
|
||||
),
|
||||
responses={
|
||||
200: build_response(
|
||||
RagIrrigationResponseSerializer,
|
||||
"توصیه آبیاری با موفقیت تولید شد.",
|
||||
),
|
||||
400: build_response(
|
||||
RagValidationErrorResponseSerializer,
|
||||
"پارامتر ورودی نامعتبر است.",
|
||||
),
|
||||
500: build_response(
|
||||
RagValidationErrorResponseSerializer,
|
||||
"خطا در تولید توصیه آبیاری.",
|
||||
),
|
||||
},
|
||||
examples=[
|
||||
OpenApiExample(
|
||||
"نمونه درخواست",
|
||||
value={
|
||||
"farm_uuid": "11111111-1111-1111-1111-111111111111",
|
||||
"plant_name": "گوجهفرنگی",
|
||||
"growth_stage": "میوهدهی",
|
||||
"irrigation_method_name": "آبیاری قطرهای",
|
||||
},
|
||||
request_only=True,
|
||||
),
|
||||
],
|
||||
)
|
||||
def post(self, request: Request):
|
||||
from rag.services.irrigation import get_irrigation_recommendation
|
||||
|
||||
farm_uuid = request.data.get("farm_uuid") or request.data.get("sensor_uuid")
|
||||
if not farm_uuid:
|
||||
return Response(
|
||||
{"code": 400, "msg": "پارامتر farm_uuid الزامی است.", "data": None},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
try:
|
||||
result = get_irrigation_recommendation(
|
||||
farm_uuid=str(farm_uuid),
|
||||
plant_name=request.data.get("plant_name"),
|
||||
growth_stage=request.data.get("growth_stage"),
|
||||
irrigation_method_name=request.data.get("irrigation_method_name"),
|
||||
query=request.data.get("query"),
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Direct irrigation recommendation failed for farm %s", farm_uuid)
|
||||
return Response(
|
||||
{"code": 500, "msg": "خطا در تولید توصیه آبیاری.", "data": None},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": result},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
|
||||
class FertilizationRecommendationView(APIView):
|
||||
"""
|
||||
توصیه کودهی به صورت مستقیم.
|
||||
POST با farm_uuid، plant_name، growth_stage.
|
||||
نتیجه همان لحظه برگشت داده میشود.
|
||||
"""
|
||||
|
||||
@extend_schema(
|
||||
tags=["RAG Recommendations"],
|
||||
summary="درخواست توصیه کودهی",
|
||||
description=(
|
||||
"دادههای سنسور و گیاه را دریافت کرده و "
|
||||
"توصیه کودهی را به صورت مستقیم برمیگرداند."
|
||||
),
|
||||
request=inline_serializer(
|
||||
name="FertilizationRecommendationRequest",
|
||||
fields={
|
||||
"farm_uuid": drf_serializers.CharField(help_text="شناسه یکتای مزرعه (اجباری)"),
|
||||
"sensor_uuid": drf_serializers.CharField(required=False, help_text="نام قدیمی برای farm_uuid"),
|
||||
"plant_name": drf_serializers.CharField(required=False, help_text="نام گیاه"),
|
||||
"growth_stage": drf_serializers.CharField(required=False, help_text="مرحله رشد گیاه"),
|
||||
"query": drf_serializers.CharField(required=False, help_text="سوال اختیاری"),
|
||||
},
|
||||
),
|
||||
responses={
|
||||
200: build_response(
|
||||
RagFertilizationResponseSerializer,
|
||||
"توصیه کودهی با موفقیت تولید شد.",
|
||||
),
|
||||
400: build_response(
|
||||
RagValidationErrorResponseSerializer,
|
||||
"پارامتر ورودی نامعتبر است.",
|
||||
),
|
||||
500: build_response(
|
||||
RagValidationErrorResponseSerializer,
|
||||
"خطا در تولید توصیه کودهی.",
|
||||
),
|
||||
},
|
||||
examples=[
|
||||
OpenApiExample(
|
||||
"نمونه درخواست",
|
||||
value={
|
||||
"farm_uuid": "11111111-1111-1111-1111-111111111111",
|
||||
"plant_name": "گوجهفرنگی",
|
||||
"growth_stage": "رویشی",
|
||||
},
|
||||
request_only=True,
|
||||
),
|
||||
],
|
||||
)
|
||||
def post(self, request: Request):
|
||||
from rag.services.fertilization import get_fertilization_recommendation
|
||||
|
||||
farm_uuid = request.data.get("farm_uuid") or request.data.get("sensor_uuid")
|
||||
if not farm_uuid:
|
||||
return Response(
|
||||
{"code": 400, "msg": "پارامتر farm_uuid الزامی است.", "data": None},
|
||||
status=status.HTTP_400_BAD_REQUEST,
|
||||
)
|
||||
|
||||
try:
|
||||
result = get_fertilization_recommendation(
|
||||
farm_uuid=str(farm_uuid),
|
||||
plant_name=request.data.get("plant_name"),
|
||||
growth_stage=request.data.get("growth_stage"),
|
||||
query=request.data.get("query"),
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Direct fertilization recommendation failed for farm %s", farm_uuid)
|
||||
return Response(
|
||||
{"code": 500, "msg": "خطا در تولید توصیه کودهی.", "data": None},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
)
|
||||
|
||||
return Response(
|
||||
{"code": 200, "msg": "success", "data": result},
|
||||
status=status.HTTP_200_OK,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user