-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create itemupdate model; create unittest for new methods in stac item
- Loading branch information
1 parent
38a5509
commit 783c2c5
Showing
7 changed files
with
232 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import Optional, Any | ||
from pydantic import BaseModel, Field | ||
from pystac import Asset, Link | ||
from shapely.geometry import mapping | ||
|
||
|
||
class ItemUpdate(BaseModel): | ||
"""Model representing a partial update for a STAC item.""" | ||
model_config = { | ||
"arbitrary_types_allowed": True | ||
} | ||
|
||
stac_extensions: Optional[list[str]] = None | ||
geometry: Optional[dict[str, Any]] = None | ||
bbox: Optional[list[float]] = Field(None, min_items=4, max_items=4) # Must be [minX, minY, maxX, maxY] | ||
properties: Optional[dict[str, Any]] = None | ||
assets: Optional[dict[str, Asset]] = None | ||
links: Optional[list[Link]] = None | ||
|
||
def set_geometry(self, geom) -> None: | ||
"""Convert a shapely geometry to GeoJSON format.""" | ||
self.geometry = mapping(geom) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from unittest.mock import MagicMock, patch | ||
|
||
from config.config import Config | ||
from config.models.m2m_authentication_config import M2MAuthenticationConfig | ||
from datacosmos.client import DatacosmosClient | ||
|
||
|
||
@patch("datacosmos.client.DatacosmosClient._authenticate_and_initialize_client") | ||
def test_patch_request(mock_auth_client): | ||
"""Test that the client performs a PATCH request correctly.""" | ||
# Mock the HTTP client | ||
mock_http_client = MagicMock() | ||
mock_response = MagicMock() | ||
mock_response.status_code = 200 | ||
mock_response.json.return_value = {"message": "updated"} | ||
mock_http_client.request.return_value = mock_response | ||
mock_auth_client.return_value = mock_http_client | ||
|
||
config = Config( | ||
authentication=M2MAuthenticationConfig( | ||
type="m2m", | ||
client_id="test-client-id", | ||
client_secret="test-client-secret", | ||
token_url="https://mock.token.url/oauth/token", | ||
audience="https://mock.audience", | ||
) | ||
) | ||
|
||
client = DatacosmosClient(config=config) | ||
response = client.patch( | ||
"https://mock.api/some-endpoint", json={"key": "updated-value"} | ||
) | ||
|
||
# Assertions | ||
assert response.status_code == 200 | ||
assert response.json() == {"message": "updated"} | ||
mock_http_client.request.assert_called_once_with( | ||
"PATCH", "https://mock.api/some-endpoint", json={"key": "updated-value"} | ||
) | ||
mock_auth_client.call_count == 2 |
49 changes: 49 additions & 0 deletions
49
tests/unit/datacosmos/stac/stac_client/test_create_item.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from unittest.mock import MagicMock, patch | ||
from pystac import Item | ||
|
||
from config.config import Config | ||
from config.models.m2m_authentication_config import M2MAuthenticationConfig | ||
from datacosmos.client import DatacosmosClient | ||
from datacosmos.stac.stac_client import STACClient | ||
|
||
|
||
@patch("requests_oauthlib.OAuth2Session.fetch_token") | ||
@patch.object(DatacosmosClient, "post") | ||
@patch("datacosmos.stac.stac_client.check_api_response") | ||
def test_create_item(mock_check_api_response, mock_post, mock_fetch_token): | ||
"""Test creating a new STAC item.""" | ||
mock_fetch_token.return_value = {"access_token": "mock-token", "expires_in": 3600} | ||
|
||
mock_response = MagicMock() | ||
mock_response.json.return_value = { | ||
"id": "item-1", | ||
"collection": "test-collection", | ||
"type": "Feature", | ||
"stac_version": "1.0.0", | ||
"geometry": {"type": "Point", "coordinates": [0, 0]}, | ||
"properties": {"datetime": "2023-12-01T12:00:00Z"}, | ||
"assets": {}, | ||
"links": [], | ||
} | ||
mock_post.return_value = mock_response | ||
|
||
config = Config( | ||
authentication=M2MAuthenticationConfig( | ||
type="m2m", | ||
client_id="test-client-id", | ||
client_secret="test-client-secret", | ||
token_url="https://mock.token.url/oauth/token", | ||
audience="https://mock.audience", | ||
) | ||
) | ||
|
||
client = DatacosmosClient(config=config) | ||
stac_client = STACClient(client) | ||
|
||
item = Item.from_dict(mock_response.json()) | ||
created_item = stac_client.create_item("test-collection", item) | ||
|
||
assert created_item.id == "item-1" | ||
assert created_item.properties["datetime"] == "2023-12-01T12:00:00Z" | ||
mock_post.assert_called_once() | ||
mock_check_api_response.assert_called_once() |
36 changes: 36 additions & 0 deletions
36
tests/unit/datacosmos/stac/stac_client/test_delete_item.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from unittest.mock import MagicMock, patch | ||
|
||
from config.config import Config | ||
from config.models.m2m_authentication_config import M2MAuthenticationConfig | ||
from datacosmos.client import DatacosmosClient | ||
from datacosmos.stac.stac_client import STACClient | ||
|
||
|
||
@patch("requests_oauthlib.OAuth2Session.fetch_token") | ||
@patch.object(DatacosmosClient, "delete") | ||
@patch("datacosmos.stac.stac_client.check_api_response") | ||
def test_delete_item(mock_check_api_response, mock_delete, mock_fetch_token): | ||
"""Test deleting a STAC item.""" | ||
mock_fetch_token.return_value = {"access_token": "mock-token", "expires_in": 3600} | ||
|
||
mock_response = MagicMock() | ||
mock_response.status_code = 204 # Successful deletion | ||
mock_delete.return_value = mock_response | ||
|
||
config = Config( | ||
authentication=M2MAuthenticationConfig( | ||
type="m2m", | ||
client_id="test-client-id", | ||
client_secret="test-client-secret", | ||
token_url="https://mock.token.url/oauth/token", | ||
audience="https://mock.audience", | ||
) | ||
) | ||
|
||
client = DatacosmosClient(config=config) | ||
stac_client = STACClient(client) | ||
|
||
stac_client.delete_item("item-1", "test-collection") | ||
|
||
mock_delete.assert_called_once() | ||
mock_check_api_response.assert_called_once() |
50 changes: 50 additions & 0 deletions
50
tests/unit/datacosmos/stac/stac_client/test_update_item.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from unittest.mock import MagicMock, patch | ||
from pystac import Item | ||
from datacosmos.stac.models.item_update import ItemUpdate | ||
|
||
from config.config import Config | ||
from config.models.m2m_authentication_config import M2MAuthenticationConfig | ||
from datacosmos.client import DatacosmosClient | ||
from datacosmos.stac.stac_client import STACClient | ||
|
||
|
||
@patch("requests_oauthlib.OAuth2Session.fetch_token") | ||
@patch.object(DatacosmosClient, "patch") | ||
@patch("datacosmos.stac.stac_client.check_api_response") | ||
def test_update_item(mock_check_api_response, mock_patch, mock_fetch_token): | ||
"""Test updating an existing STAC item.""" | ||
mock_fetch_token.return_value = {"access_token": "mock-token", "expires_in": 3600} | ||
|
||
mock_response = MagicMock() | ||
mock_response.json.return_value = { | ||
"id": "item-1", | ||
"collection": "test-collection", | ||
"type": "Feature", | ||
"stac_version": "1.0.0", | ||
"geometry": {"type": "Point", "coordinates": [0, 0]}, | ||
"properties": {"datetime": "2023-12-01T12:00:00Z", "new_property": "value"}, | ||
"assets": {}, | ||
"links": [], | ||
} | ||
mock_patch.return_value = mock_response | ||
|
||
config = Config( | ||
authentication=M2MAuthenticationConfig( | ||
type="m2m", | ||
client_id="test-client-id", | ||
client_secret="test-client-secret", | ||
token_url="https://mock.token.url/oauth/token", | ||
audience="https://mock.audience", | ||
) | ||
) | ||
|
||
client = DatacosmosClient(config=config) | ||
stac_client = STACClient(client) | ||
|
||
update_data = ItemUpdate(properties={"new_property": "value"}) | ||
updated_item = stac_client.update_item("item-1", "test-collection", update_data) | ||
|
||
assert updated_item.id == "item-1" | ||
assert updated_item.properties["new_property"] == "value" | ||
mock_patch.assert_called_once() | ||
mock_check_api_response.assert_called_once() |