Skip to content

Commit 581086e

Browse files
authored
fix: handle invalid geo locations better (#2529)
1 parent c890725 commit 581086e

4 files changed

Lines changed: 20 additions & 3 deletions

File tree

cognite/client/data_classes/assets.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
ExternalIDTransformerMixin,
3939
IdTransformerMixin,
4040
PropertySpec,
41+
UnknownCogniteObject,
4142
WriteableCogniteResource,
4243
WriteableCogniteResourceList,
4344
)
@@ -111,8 +112,11 @@ def __init__(
111112
labels: list[Label] | None = None,
112113
geo_location: GeoLocation | None = None,
113114
) -> None:
114-
if geo_location is not None and not isinstance(geo_location, GeoLocation):
115-
raise TypeError("Asset.geo_location should be of type GeoLocation")
115+
if geo_location is not None:
116+
if isinstance(geo_location, dict):
117+
geo_location = GeoLocation.load(geo_location)
118+
if not isinstance(geo_location, GeoLocation | UnknownCogniteObject):
119+
raise TypeError("Asset.geo_location should be of type GeoLocation")
116120
self.external_id = external_id
117121
self.name = name
118122
self.parent_id = parent_id

cognite/client/data_classes/files.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ExternalIDTransformerMixin,
1818
IdTransformerMixin,
1919
PropertySpec,
20+
UnknownCogniteObject,
2021
WriteableCogniteResource,
2122
WriteableCogniteResourceList,
2223
)
@@ -70,7 +71,7 @@ def __init__(
7071
if geo_location is not None:
7172
if isinstance(geo_location, dict):
7273
geo_location = GeoLocation.load(geo_location)
73-
if not isinstance(geo_location, GeoLocation):
74+
if not isinstance(geo_location, GeoLocation | UnknownCogniteObject):
7475
raise TypeError("FileMetadata.geo_location should be of type GeoLocation")
7576
self.external_id = external_id
7677
self.instance_id = instance_id

tests/tests_unit/test_api/test_assets.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from cognite.client._api.assets import Asset, AssetList, AssetUpdate
66
from cognite.client.data_classes import AggregateResultItem, AssetFilter, Label, LabelFilter, TimestampRange
7+
from cognite.client.data_classes._base import UnknownCogniteObject
78
from cognite.client.exceptions import CogniteAPIError
89
from cognite.client.utils._text import convert_all_keys_to_snake_case
910
from tests.utils import jsgz_load
@@ -255,6 +256,11 @@ def test_create_asset_with_label(self, cognite_client, mock_assets_response):
255256
mock_assets_response.calls[0].request.body
256257
)["items"][0]
257258

259+
def test_create_with_invalid_geoLocation(self, cognite_client):
260+
invalid_geo_location = {"foo": "bar"}
261+
asset = Asset(name="bla", geo_location=invalid_geo_location)
262+
assert isinstance(asset.geo_location, UnknownCogniteObject)
263+
258264
def test_search(self, cognite_client, mock_assets_response):
259265
res = cognite_client.assets.search(filter=AssetFilter(name="1"))
260266
assert mock_assets_response.calls[0].response.json()["items"] == res.dump(camel_case=True)

tests/tests_unit/test_api/test_files.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
LabelFilter,
2121
TimestampRange,
2222
)
23+
from cognite.client.data_classes._base import UnknownCogniteObject
2324
from cognite.client.data_classes.data_modeling.ids import NodeId
2425
from cognite.client.exceptions import CogniteAPIError, CogniteAuthorizationError
2526
from tests.utils import jsgz_load, set_request_limit
@@ -273,6 +274,11 @@ def test_create_with_geoLocation_request(self, cognite_client, mock_file_create_
273274
body["geoLocation"] == mock_geo_location.dump(camel_case=True) for body in [request_body, response_body]
274275
)
275276

277+
def test_create_with_invalid_geoLocation(self, cognite_client):
278+
invalid_geo_location = {"foo": "bar"}
279+
file_metadata = FileMetadata(name="bla", geo_location=invalid_geo_location)
280+
assert isinstance(file_metadata.geo_location, UnknownCogniteObject)
281+
276282
def test_retrieve_single(self, cognite_client, mock_files_response):
277283
res = cognite_client.files.retrieve(id=1)
278284
assert isinstance(res, FileMetadata)

0 commit comments

Comments
 (0)