From a0e1e2388df6b77e6c84666b5cbfb73de1a76953 Mon Sep 17 00:00:00 2001 From: kls Date: Wed, 10 Jul 2024 08:15:41 -0500 Subject: [PATCH] Update mdm tests --- .../master_data_management_service.py | 23 ++-- tests/test_int_geosys.py | 1 + ...est_unit_master_data_management_service.py | 113 ++++++++++++------ 3 files changed, 88 insertions(+), 49 deletions(-) diff --git a/geosyspy/services/master_data_management_service.py b/geosyspy/services/master_data_management_service.py index 2395d42..4042982 100644 --- a/geosyspy/services/master_data_management_service.py +++ b/geosyspy/services/master_data_management_service.py @@ -1,4 +1,5 @@ """ Mastaer data managenement service class""" + import logging from datetime import datetime from typing import List, Optional @@ -76,8 +77,7 @@ def retrieve_season_fields_in_polygon(self, polygon: str) -> List[object]: api_call: str = urljoin( self.base_url, GeosysApiEndpoints.MASTER_DATA_MANAGEMENT_ENDPOINT.value + "/seasonfields", - '?Geometry=$within:' + polygon - + '&$limit=none' + "?Geometry=$within:" + polygon + "&$limit=none", ) return self.http_client.get(api_call) @@ -139,7 +139,6 @@ def check_season_field_exists(self, season_field_id: str) -> str: return True return False - def get_available_crops_code(self) -> List[str]: """Extracts the list of available crops for the connected user @@ -178,11 +177,10 @@ def get_profile(self, fields: Optional[str] = None) -> List[str]: Raises: ValueError: The response status code is not as expected. """ - if fields == None: + if fields is None: mdm_url: str = urljoin( self.base_url, - GeosysApiEndpoints.MASTER_DATA_MANAGEMENT_ENDPOINT.value - + f"/profile", + GeosysApiEndpoints.MASTER_DATA_MANAGEMENT_ENDPOINT.value + "/profile", ) else: mdm_url: str = urljoin( @@ -203,7 +201,7 @@ def get_profile(self, fields: Optional[str] = None) -> List[str]: def get_season_fields(self, season_field_ids: str) -> List[object]: """Extracts the list of seasonfields for the input ids - For now retrieves only geometry, to complete with future needs + For now retrieves only geometry, to complete with future needs Args: @@ -213,13 +211,14 @@ def get_season_fields(self, season_field_ids: str) -> List[object]: Raises: ValueError: The response status code is not as expected. """ - + mdm_url: str = urljoin( self.base_url, GeosysApiEndpoints.MASTER_DATA_MANAGEMENT_ENDPOINT.value - +'/seasonfields?$fields=id,geometry,sowingDate,estimatedHarvestDate,Crop.Id,acreage' #add other fields if needed - +'&Id=$in:' + '|'.join(season_field_ids) - +'&$limit=none' + + "/seasonfields?$fields=id,geometry,sowingDate,estimatedHarvestDate,Crop.Id,acreage" # add other fields if needed + + "&Id=$in:" + + "|".join(season_field_ids) + + "&$limit=none", ) response = self.http_client.get(mdm_url) @@ -230,4 +229,4 @@ def get_season_fields(self, season_field_ids: str) -> List[object]: return dict_response raise ValueError( f"Cannot handle HTTP response : {str(response.status_code)} : {str(dict_response)}" - ) \ No newline at end of file + ) diff --git a/tests/test_int_geosys.py b/tests/test_int_geosys.py index 5b75546..92579c7 100644 --- a/tests/test_int_geosys.py +++ b/tests/test_int_geosys.py @@ -159,6 +159,7 @@ def test_get_satellite_image_time_series(self): indicators=["Reflectance"], polygon=POLYGON ) + print(dict(dataset.dims)) assert dict(dataset.dims) == {'band': 7, 'y': 27, 'x': 26, 'time': 10} def test_get_agriquest_weather_time_series(self): diff --git a/tests/test_unit_master_data_management_service.py b/tests/test_unit_master_data_management_service.py index f0da6a0..0b5c935 100644 --- a/tests/test_unit_master_data_management_service.py +++ b/tests/test_unit_master_data_management_service.py @@ -5,70 +5,109 @@ from tests.test_helper import * geometry = "POLYGON((-91.17523978603823 40.29787117039518,-91.17577285022956 40.29199489606421,-91.167613719932 40.29199489606421,-91.1673028670095 40.29867040193312,-91.17523978603823 40.29787117039518))" -sfids = ['g6ap335','5nlm9e1'] +sfids = ["g6ap335", "5nlm9e1"] + class TestMasterDataManagementService: 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", + ) service = MasterDataManagementService(base_url=url, http_client=http_client) - @patch('geosyspy.utils.http_client.HttpClient.post') + @patch("geosyspy.utils.http_client.HttpClient.post") def test_create_season_field_id(self, post_response): - post_response.return_value = mock_http_response_text_content("POST", load_data_from_textfile( - "master_data_management_post_extract_id_mock_http_response")) + post_response.return_value = mock_http_response_text_content( + "POST", + load_data_from_textfile( + "master_data_management_post_extract_id_mock_http_response" + ), + ) response = self.service.create_season_field_id(polygon=geometry) assert response.status_code == 200 - @patch('geosyspy.utils.http_client.HttpClient.post') + @patch("geosyspy.utils.http_client.HttpClient.post") def test_extract_season_field_id(self, post_response): - post_response.return_value = mock_http_response_text_content("POST", load_data_from_textfile( - "master_data_management_post_extract_id_mock_http_response"),status_code=201) + post_response.return_value = mock_http_response_text_content( + "POST", + load_data_from_textfile( + "master_data_management_post_extract_id_mock_http_response" + ), + status_code=201, + ) response = self.service.extract_season_field_id(polygon=geometry) assert response == "ajqxm3v" - @patch('geosyspy.utils.http_client.HttpClient.get') + @patch("geosyspy.utils.http_client.HttpClient.get") def test_extract_season_field_id(self, get_response): - get_response.return_value = mock_http_response_text_content("GET", load_data_from_textfile( - "master_data_management_get_unique_id_mock_http_response"), status_code=201) + get_response.return_value = mock_http_response_text_content( + "GET", + load_data_from_textfile( + "master_data_management_get_unique_id_mock_http_response" + ), + status_code=201, + ) - response = self.service.get_season_field_unique_id(season_field_id="fakeSeasonFieldId") + response = self.service.get_season_field_unique_id( + season_field_id="fakeSeasonFieldId" + ) assert response == "4XcGhZvA1OjpO3gUwYM61e" - @patch('geosyspy.utils.http_client.HttpClient.get') + @patch("geosyspy.utils.http_client.HttpClient.get") def test_retrieve_season_fields_in_polygon(self, get_response): - get_response.return_value = mock_http_response_text_content('GET', load_data_from_textfile( - "master_data_management_retrieve_sfids_mock_http_response"), status_code=201) - + get_response.return_value = mock_http_response_text_content( + "GET", + load_data_from_textfile( + "master_data_management_retrieve_sfids_mock_http_response" + ), + status_code=201, + ) + response = self.service.retrieve_season_fields_in_polygon(polygon=geometry) assert response.status_code == 200 - @patch('geosyspy.utils.http_client.HttpClient.get') + @patch("geosyspy.utils.http_client.HttpClient.get") def test_get_season_fields(self, get_response): - get_response.return_value = mock_http_response_text_content('GET', load_data_from_textfile( - "master_data_management_retrieve_sfids_mock_http_response"), status_code=201) - + get_response.return_value = mock_http_response_text_content( + "GET", + load_data_from_textfile( + "master_data_management_retrieve_sfids_mock_http_response" + ), + status_code=201, + ) + response = self.service.get_season_fields(sfids) assert len(response) == 20 - @patch('geosyspy.utils.http_client.HttpClient.get') + @patch("geosyspy.utils.http_client.HttpClient.get") def test_get_profile(self, get_response): - get_response.return_value = mock_http_response_text_content('GET', load_data_from_textfile( - "master_data_management_post_profile_mock_http_response"), status_code=201) - + get_response.return_value = mock_http_response_text_content( + "GET", + load_data_from_textfile( + "master_data_management_post_profile_mock_http_response" + ), + status_code=201, + ) + response = self.service.get_profile() - assert response.status_code == 200 + assert "id" in response - @patch('geosyspy.utils.http_client.HttpClient.get') - def test_get_profile(self, get_response): - get_response.return_value = mock_http_response_text_content('GET', load_data_from_textfile( - "master_data_management_post_profile_mock_http_response"), status_code=201) - - response = self.service.get_profile("") - assert response.status_code == 200 + @patch("geosyspy.utils.http_client.HttpClient.get") + def test_get_profile_fields(self, get_response): + get_response.return_value = mock_http_response_text_content( + "GET", + load_data_from_textfile( + "master_data_management_post_profile_mock_http_response" + ), + status_code=201, + ) + + response = self.service.get_profile(fields="unitProfileUnitCategories") + assert "unitProfileUnitCategories" in response