This commit is contained in:
2026-04-24 03:02:22 +03:30
parent a76af4e766
commit f04a9fe71f
6 changed files with 246 additions and 79 deletions
+27 -24
View File
@@ -2,51 +2,54 @@ from unittest.mock import patch
from django.test import SimpleTestCase
from rag.chat import build_chat_context
from rag.chat import build_rag_context
class ChatContextTests(SimpleTestCase):
@patch("rag.chat.search_with_query")
@patch("rag.chat._rank_text_chunks_by_query")
@patch("rag.chat.search_with_texts")
@patch("rag.chat.chunk_text")
def test_build_chat_context_combines_farm_and_kb_context(
def test_build_rag_context_includes_full_farm_and_kb_results(
self,
mock_chunk_text,
mock_rank_text_chunks_by_query,
mock_search_with_query,
mock_search_with_texts,
):
mock_chunk_text.return_value = ["chunk-a", "chunk-b"]
mock_rank_text_chunks_by_query.return_value = ["chunk-b"]
mock_search_with_query.return_value = [
{"text": "kb text 1"},
{"text": "kb text 2"},
mock_chunk_text.return_value = ["farm chunk 1", "farm chunk 2"]
mock_search_with_texts.return_value = [
{"id": "kb-1", "score": 0.8, "text": "kb text 1", "metadata": {}},
{"id": "kb-2", "score": 0.7, "text": "kb text 2", "metadata": {}},
]
context = build_chat_context(
context = build_rag_context(
query="وضعیت مزرعه چطور است؟",
farm_uuid="farm-123",
sensor_uuid="farm-123",
service_id="chat",
farm_details={"sensor_payload": {"sensor-7-1": {"soil_moisture": 30}}},
)
self.assertIn("[بخش‌های مرتبط بازیابی‌شده از اطلاعات مزرعه]", context)
self.assertIn("chunk-b", context)
self.assertIn("[اطلاعات بازیابی‌شده از پایگاه دانش]", context)
self.assertIn("[اطلاعات کامل مزرعه]", context)
self.assertIn("soil_moisture", context)
self.assertIn("[متن‌های مرجع]", context)
self.assertIn("kb text 1", context)
self.assertIn("kb text 2", context)
mock_search_with_texts.assert_called_once()
sent_texts = mock_search_with_texts.call_args.kwargs["texts"]
self.assertEqual(sent_texts[0], "وضعیت مزرعه چطور است؟")
self.assertIn("farm chunk 1", sent_texts)
self.assertIn("farm chunk 2", sent_texts)
@patch("rag.chat.search_with_query", return_value=[])
@patch("rag.chat._rank_text_chunks_by_query", return_value=[])
@patch("rag.chat.search_with_texts", return_value=[])
@patch("rag.chat.chunk_text", return_value=["farm chunk"])
def test_build_chat_context_falls_back_to_full_farm_context(
def test_build_rag_context_returns_full_farm_when_kb_empty(
self,
_mock_chunk_text,
_mock_rank_text_chunks_by_query,
_mock_search_with_query,
_mock_search_with_texts,
):
context = build_chat_context(
context = build_rag_context(
query="رطوبت چقدر است؟",
farm_uuid="farm-123",
sensor_uuid="farm-123",
service_id="chat",
farm_details={"sensor_payload": {"sensor-7-1": {"soil_moisture": 30}}},
)
self.assertEqual(context, "")
self.assertIn("[اطلاعات کامل مزرعه]", context)
self.assertIn("soil_moisture", context)
+40
View File
@@ -68,6 +68,46 @@ class RecommendationServiceDefaultsTests(TestCase):
mock_build_rag_context.assert_called_once()
mock_build_plant_text.assert_called_once_with("گوجه‌فرنگی", "میوه‌دهی")
mock_build_irrigation_method_text.assert_called_once_with("آبیاری قطره‌ای")
self.assertEqual(
result["selected_irrigation_method"]["name"],
"آبیاری قطره‌ای",
)
@patch("rag.services.irrigation.calculate_forecast_water_needs", return_value=[])
@patch("rag.services.irrigation.resolve_kc", return_value=0.9)
@patch("rag.services.irrigation.resolve_crop_profile", return_value={})
@patch("rag.services.irrigation.build_irrigation_method_text", return_value="method text")
@patch("rag.services.irrigation.build_plant_text", return_value="plant text")
@patch("rag.services.irrigation.build_rag_context", return_value="")
@patch("rag.services.irrigation.get_chat_client")
def test_irrigation_recommendation_persists_selected_method_on_farm(
self,
mock_get_chat_client,
_mock_build_rag_context,
_mock_build_plant_text,
mock_build_irrigation_method_text,
_mock_resolve_crop_profile,
_mock_resolve_kc,
_mock_calculate_forecast_water_needs,
):
sprinkler = IrrigationMethod.objects.create(name="بارانی")
self.farm.irrigation_method = None
self.farm.save(update_fields=["irrigation_method", "updated_at"])
mock_response = Mock()
mock_response.choices = [Mock(message=Mock(content='{"plan": {"frequencyPerWeek": 4}}'))]
mock_get_chat_client.return_value.chat.completions.create.return_value = mock_response
result = get_irrigation_recommendation(
sensor_uuid=str(self.farm_uuid),
growth_stage="میوه‌دهی",
irrigation_method_name="بارانی",
)
self.farm.refresh_from_db()
self.assertEqual(self.farm.irrigation_method_id, sprinkler.id)
self.assertEqual(result["selected_irrigation_method"]["id"], sprinkler.id)
mock_build_irrigation_method_text.assert_called_once_with("بارانی")
@patch("rag.services.fertilization.build_plant_text", return_value="plant text")
@patch("rag.services.fertilization.build_rag_context", return_value="")