UPDATE
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase, override_settings
|
||||
from rest_framework.test import APIClient
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF="location_data.urls")
|
||||
class NdviHealthApiTests(TestCase):
|
||||
def setUp(self):
|
||||
self.client = APIClient()
|
||||
|
||||
@patch("location_data.views.apps.get_app_config")
|
||||
def test_ndvi_health_api_returns_payload(self, mock_get_app_config):
|
||||
mock_service = SimpleNamespace(
|
||||
get_ndvi_health=lambda **_kwargs: {
|
||||
"ndviIndex": 0.68,
|
||||
"mean_ndvi": 0.68,
|
||||
"ndvi_map": {"grid": [[0.61, 0.7]]},
|
||||
"vegetation_health_class": "Healthy vegetation",
|
||||
"observation_date": "2026-04-02",
|
||||
"satellite_source": "sentinel-2",
|
||||
"healthData": [
|
||||
{
|
||||
"title": "سلامت پوشش گیاهی",
|
||||
"value": "Healthy vegetation",
|
||||
"color": "success",
|
||||
"icon": "tabler-plant",
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
mock_get_app_config.return_value = SimpleNamespace(
|
||||
get_ndvi_health_service=lambda: mock_service
|
||||
)
|
||||
|
||||
response = self.client.post(
|
||||
"/ndvi-health/",
|
||||
data={"farm_uuid": "550e8400-e29b-41d4-a716-446655440000"},
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
payload = response.json()["data"]
|
||||
self.assertEqual(payload["mean_ndvi"], 0.68)
|
||||
self.assertEqual(payload["vegetation_health_class"], "Healthy vegetation")
|
||||
|
||||
@patch("location_data.views.apps.get_app_config")
|
||||
def test_ndvi_health_api_returns_404_for_missing_farm(self, mock_get_app_config):
|
||||
mock_service = SimpleNamespace(
|
||||
get_ndvi_health=lambda **_kwargs: (_ for _ in ()).throw(ValueError("Farm not found."))
|
||||
)
|
||||
mock_get_app_config.return_value = SimpleNamespace(
|
||||
get_ndvi_health_service=lambda: mock_service
|
||||
)
|
||||
|
||||
response = self.client.post(
|
||||
"/ndvi-health/",
|
||||
data={"farm_uuid": "550e8400-e29b-41d4-a716-446655440000"},
|
||||
format="json",
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 404)
|
||||
self.assertEqual(response.json()["msg"], "Farm not found.")
|
||||
Reference in New Issue
Block a user