This commit is contained in:
2026-05-05 21:01:58 +03:30
parent 39efd537bf
commit 4e28bacad6
54 changed files with 2729 additions and 1115 deletions
+48 -26
View File
@@ -3,8 +3,8 @@ from django.test import TestCase
from rest_framework.test import APIRequestFactory, force_authenticate
from unittest.mock import patch
from external_api_adapter.adapter import AdapterResponse
from farm_hub.models import FarmHub, FarmType, Product
from .services import PlantSyncError
from .views import PlantListView, PlantNameListView, SelectedPlantListView
@@ -19,30 +19,15 @@ class PlantApiTests(TestCase):
)
self.farm_type = FarmType.objects.create(name="زراعی")
@patch("plants.services.external_api_request")
def test_list_syncs_plants_from_ai_and_returns_full_payload(self, mock_external_api_request):
mock_external_api_request.return_value = AdapterResponse(
status_code=200,
data={
"code": 200,
"msg": "success",
"data": [
{
"name": "Tomato",
"light": "full sun",
"watering": "regular",
"soil": "loam",
"temperature": "20-30",
"growth_stage": "vegetative",
"planting_season": "spring",
"harvest_time": "70-90 days",
"spacing": "45-60 cm",
"fertilizer": "NPK",
"icon": "tomato",
"growth_profile": {"stage_thresholds": {"flowering": 300, "fruiting": 500}},
}
],
},
@patch("plants.views.push_plants_to_ai")
def test_list_returns_backend_catalog_with_sync_metadata(self, mock_push_plants_to_ai):
mock_push_plants_to_ai.return_value = []
Product.objects.create(
farm_type=self.farm_type,
name="Tomato",
icon="tomato",
growth_stage="vegetative",
growth_profile={"stage_thresholds": {"flowering": 300, "fruiting": 500}},
)
request = self.factory.get("/api/plants/")
force_authenticate(request, user=self.user)
@@ -54,7 +39,10 @@ class PlantApiTests(TestCase):
self.assertEqual(response.data["data"][0]["name"], "Tomato")
self.assertEqual(response.data["data"][0]["icon"], "tomato")
self.assertIn("flowering", response.data["data"][0]["growth_stages"])
mock_external_api_request.assert_called_once_with("ai", "/api/plants/", method="GET")
self.assertEqual(response.data["meta"]["flow_type"], "backend_owned_data_with_ai_enrichment")
self.assertEqual(response.data["meta"]["source_type"], "db")
self.assertEqual(response.data["meta"]["sync_status"], "synced")
mock_push_plants_to_ai.assert_called_once()
@patch("plants.views.push_plants_to_ai")
def test_names_endpoint_fills_default_icon_and_growth_stages(self, mock_push_plants_to_ai):
@@ -78,6 +66,9 @@ class PlantApiTests(TestCase):
product.refresh_from_db()
self.assertEqual(product.icon, "leaf")
self.assertEqual(product.growth_stages, ["initial", "vegetative", "flowering", "fruiting", "maturity"])
self.assertEqual(response.data["meta"]["flow_type"], "backend_owned_data")
self.assertEqual(response.data["meta"]["source_type"], "db")
self.assertEqual(response.data["meta"]["sync_status"], "synced")
@patch("plants.views.push_plants_to_ai")
def test_selected_endpoint_returns_farmer_products(self, mock_push_plants_to_ai):
@@ -97,3 +88,34 @@ class PlantApiTests(TestCase):
self.assertEqual(response.data["data"][0]["name"], "Pepper")
self.assertEqual(set(response.data["data"][0].keys()), {"name", "icon", "growth_stages"})
self.assertNotEqual(response.data["data"][0]["name"], tomato.name)
self.assertEqual(response.data["meta"]["ownership"], "backend")
self.assertEqual(response.data["meta"]["sync_status"], "synced")
@patch("plants.views.push_plants_to_ai")
def test_list_exposes_backend_ownership_even_when_ai_sync_fails(self, mock_push_plants_to_ai):
mock_push_plants_to_ai.side_effect = PlantSyncError("sync failed")
Product.objects.create(farm_type=self.farm_type, name="Tomato", icon="leaf", growth_stages=["vegetative"])
request = self.factory.get("/api/plants/")
force_authenticate(request, user=self.user)
response = PlantListView.as_view()(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data["meta"]["flow_type"], "backend_owned_data_with_ai_enrichment")
self.assertEqual(response.data["meta"]["sync_status"], "failed")
def test_selected_endpoint_reads_seeded_backend_products_without_runtime_mock_data(self):
tomato = Product.objects.create(farm_type=self.farm_type, name="Tomato", icon="leaf", growth_stages=["vegetative"])
pepper = Product.objects.create(farm_type=self.farm_type, name="Pepper", icon="leaf", growth_stages=["flowering"])
farm = FarmHub.objects.create(owner=self.user, farm_type=self.farm_type, name="seeded-farm")
farm.products.add(tomato, pepper)
request = self.factory.get(f"/api/plants/selected/?farm_uuid={farm.farm_uuid}")
force_authenticate(request, user=self.user)
with patch("plants.views.push_plants_to_ai", return_value=[]):
response = SelectedPlantListView.as_view()(request)
self.assertEqual(response.status_code, 200)
self.assertCountEqual([item["name"] for item in response.data["data"]], ["Tomato", "Pepper"])
self.assertEqual(response.data["meta"]["source_type"], "db")
+64 -8
View File
@@ -5,6 +5,7 @@ from rest_framework.views import APIView
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiParameter, extend_schema
from config.integration_contract import build_integration_meta
from config.swagger import code_response, farm_uuid_query_param
from farm_hub.models import FarmHub, Product
from .serializers import PlantNameSerializer, PlantSerializer
@@ -15,12 +16,12 @@ class PlantBaseView(APIView):
permission_classes = [IsAuthenticated]
@staticmethod
def _sync_plants_if_possible():
def _attempt_ai_catalog_sync():
try:
push_plants_to_ai()
except PlantSyncError:
return False
return True
return False, "failed"
return True, "synced"
@staticmethod
def _get_farm(request, farm_uuid):
@@ -39,13 +40,34 @@ class PlantListView(PlantBaseView):
)
def get(self, request):
products = ensure_plant_defaults(Product.objects.order_by("name"))
sync_attempted = True
sync_status = "synced"
try:
push_plants_to_ai(products)
except PlantSyncError as exc:
sync_status = "failed"
if not products:
return Response({"code": 503, "msg": str(exc)}, status=status.HTTP_503_SERVICE_UNAVAILABLE)
data = PlantSerializer(products, many=True).data
return Response({"code": 200, "msg": "success", "data": data}, status=status.HTTP_200_OK)
return Response(
{
"code": 200,
"msg": "success",
"data": data,
"meta": build_integration_meta(
flow_type="backend_owned_data_with_ai_enrichment",
source_type="db",
source_service="backend_plants",
ownership="backend",
live=False,
cached=False,
sync_attempted=sync_attempted,
sync_status=sync_status,
notes=["Backend plant catalog is canonical; AI receives sync snapshots only."],
),
},
status=status.HTTP_200_OK,
)
class PlantDetailView(PlantBaseView):
@@ -74,10 +96,27 @@ class PlantNameListView(PlantBaseView):
responses={200: code_response("PlantNameListResponse", data=PlantNameSerializer(many=True))},
)
def get(self, request):
self._sync_plants_if_possible()
sync_attempted, sync_status = self._attempt_ai_catalog_sync()
products = ensure_plant_defaults(Product.objects.order_by("name"))
data = PlantNameSerializer(products, many=True).data
return Response({"code": 200, "msg": "success", "data": data}, status=status.HTTP_200_OK)
return Response(
{
"code": 200,
"msg": "success",
"data": data,
"meta": build_integration_meta(
flow_type="backend_owned_data",
source_type="db",
source_service="backend_plants",
ownership="backend",
live=False,
cached=False,
sync_attempted=sync_attempted,
sync_status=sync_status,
),
},
status=status.HTTP_200_OK,
)
class SelectedPlantListView(PlantBaseView):
@@ -87,9 +126,26 @@ class SelectedPlantListView(PlantBaseView):
responses={200: code_response("SelectedPlantListResponse", data=PlantNameSerializer(many=True))},
)
def get(self, request):
self._sync_plants_if_possible()
sync_attempted, sync_status = self._attempt_ai_catalog_sync()
farm = self._get_farm(request, request.query_params.get("farm_uuid"))
ensure_plant_defaults(farm.products.all())
products = farm.products.order_by("name")
data = PlantNameSerializer(products, many=True).data
return Response({"code": 200, "msg": "success", "data": data}, status=status.HTTP_200_OK)
return Response(
{
"code": 200,
"msg": "success",
"data": data,
"meta": build_integration_meta(
flow_type="backend_owned_data",
source_type="db",
source_service="backend_plants",
ownership="backend",
live=False,
cached=False,
sync_attempted=sync_attempted,
sync_status=sync_status,
),
},
status=status.HTTP_200_OK,
)