From 0b8cba4affc84402fe7778ace8544d86e387e4fd Mon Sep 17 00:00:00 2001 From: kls Date: Thu, 11 Jul 2024 22:24:04 -0500 Subject: [PATCH] Update: test for difference map --- geosyspy/services/map_product_service.py | 8 +-- tests/test_unit_map_product_service.py | 73 +++++++++++++++++++----- 2 files changed, 62 insertions(+), 19 deletions(-) diff --git a/geosyspy/services/map_product_service.py b/geosyspy/services/map_product_service.py index df74b8c..c03625c 100644 --- a/geosyspy/services/map_product_service.py +++ b/geosyspy/services/map_product_service.py @@ -192,21 +192,21 @@ def get_product( return df def get_zipped_tiff_difference_map( - self, field_id: str, image_id_before: str, image_id_after: str + self, field_id: str, image_id_earliest: str, image_id_latest: str ): """ Retrieves tiff resulting of a difference between 2 in-season images for a given season field from MP API. Args: season_field_id (str): The identifier for the season field. - image_id_before (str): The image reference from the satellite coverage before. - image_id_after (str): The image reference from the satellite coverage after. + image_id_earliest (str): The earliest image reference from the satellite coverage. + image_id_latest (str): The latest image reference from the satellite coverage. Returns: zipped tiff """ - parameters = f"/{image_id_before}/base-reference-map/DIFFERENCE_INSEASON_NDVI/difference-with/{image_id_after}/image.tiff.zip?$epsg-out=3857" + parameters = f"/{image_id_latest}/base-reference-map/DIFFERENCE_INSEASON_NDVI/difference-with/{image_id_earliest}/image.tiff.zip?$epsg-out=3857" download_tiff_url: str = urljoin( self.base_url, GeosysApiEndpoints.FLM_BASE_REFERENCE_MAP.value.format(field_id) diff --git a/tests/test_unit_map_product_service.py b/tests/test_unit_map_product_service.py index 957fa4a..3262be8 100644 --- a/tests/test_unit_map_product_service.py +++ b/tests/test_unit_map_product_service.py @@ -1,37 +1,80 @@ import datetime as dt -import numpy as np from unittest.mock import patch +import numpy as np + from geosyspy.services.map_product_service import MapProductService +from geosyspy.utils.constants import * from geosyspy.utils.http_client import * from tests.test_helper import * -from geosyspy.utils.constants import * class TestMapProductService: url = "https://testurl.com" - http_client = HttpClient("client_id_123", - "client_secret_123456", - "username_123", - "password_123", - "preprod", - "na") + http_client = HttpClient( + "client_id_123", + "client_secret_123456", + "username_123", + "password_123", + "preprod", + "na", + ) priority_queue = "realtime" - service = MapProductService(base_url=url, http_client=http_client,priority_queue=priority_queue) + service = MapProductService( + base_url=url, http_client=http_client, priority_queue=priority_queue + ) - @patch('geosyspy.utils.http_client.HttpClient.post') + @patch("geosyspy.utils.http_client.HttpClient.post") def test_get_satellite_coverage(self, get_response): - get_response.return_value = mock_http_response_text_content("GET", load_data_from_textfile( - "satellite_coverage_image_references_mock_http_response")) + get_response.return_value = mock_http_response_text_content( + "GET", + load_data_from_textfile( + "satellite_coverage_image_references_mock_http_response" + ), + ) start_date = dt.datetime.strptime("2022-01-01", "%Y-%m-%d") end_date = dt.datetime.strptime("2023-01-01", "%Y-%m-%d") info = self.service.get_satellite_coverage( - "fakeSeasonFieldId", None, start_date, end_date, "NDVI", [SatelliteImageryCollection.SENTINEL_2] + "fakeSeasonFieldId", + None, + start_date, + end_date, + "NDVI", + [SatelliteImageryCollection.SENTINEL_2], ) - assert {"coveragePercent", "image.id", "image.availableBands", "image.sensor", - "image.spatialResolution", "image.date", "seasonField.id"}.issubset(set(info.columns)) + assert { + "coveragePercent", + "image.id", + "image.availableBands", + "image.sensor", + "image.spatialResolution", + "image.date", + "seasonField.id", + }.issubset(set(info.columns)) + + @patch("geosyspy.utils.http_client.HttpClient.post") + def test_get_difference_map(self, get_response): + get_response.return_value = mock_http_response_text_content( + "GET", + load_data_from_textfile( + "satellite_coverage_image_references_mock_http_response" + ), + ) + image_id_earliest = "sentinel-2-l2a%7CS2B_13SGC_20230520_0_L2A" + image_id_latest = "sentinel-2-l2a%7CS2B_13SGC_20230530_0_L2A" + field_id = "bgbrzez" + response = self.service.get_zipped_tiff_difference_map( + field_id, image_id_earliest, image_id_latest + ) + assert response.status_code == 200, "Expected status code to be 200" + # Assert that the content type is 'image/tiff' + assert ( + response.headers["Content-Type"] == "image/tiff+zip" + ), "Expected content type to be 'image/tiff+zip'" + # Assert that the content is not empty + assert response.content, "Expected non-empty response content"