Skip to content

Commit

Permalink
Update mdm tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KarineGEO6 committed Jul 10, 2024
1 parent 9353f31 commit a0e1e23
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 49 deletions.
23 changes: 11 additions & 12 deletions geosyspy/services/master_data_management_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Mastaer data managenement service class"""

import logging
from datetime import datetime
from typing import List, Optional
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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)}"
)
)
1 change: 1 addition & 0 deletions tests/test_int_geosys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
113 changes: 76 additions & 37 deletions tests/test_unit_master_data_management_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a0e1e23

Please sign in to comment.