Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOGE-55: Fix dict snake case dump #1409

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cognite/client/data_classes/extractionpipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
PropertySpec,
)
from cognite.client.data_classes.shared import TimestampRange
from cognite.client.utils._text import convert_all_keys_to_camel_case
from cognite.client.utils._text import convert_all_keys_to_camel_snake_otherwise

if TYPE_CHECKING:
from cognite.client import CogniteClient
Expand Down Expand Up @@ -43,7 +43,7 @@ def __init__(self, name: str, email: str, role: str, send_notification: bool) ->
send_notification = CognitePropertyClassUtil.declare_property("sendNotification")

def dump(self, camel_case: bool = False) -> dict[str, Any]:
return convert_all_keys_to_camel_case(self) if camel_case else dict(self)
return convert_all_keys_to_camel_snake_otherwise(self, camel_case)


class ExtractionPipeline(CogniteResource):
Expand Down
5 changes: 3 additions & 2 deletions cognite/client/data_classes/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
CogniteResource,
CogniteResourceList,
)
from cognite.client.utils._text import convert_all_keys_to_camel_case, to_camel_case
from cognite.client.utils._text import convert_all_keys_to_camel_snake_otherwise, to_camel_case

if TYPE_CHECKING:
from cognite.client import CogniteClient
Expand Down Expand Up @@ -107,7 +107,7 @@ def _load(cls, raw_label: dict[str, Any]) -> Label:
return cls(external_id=raw_label["externalId"])

def dump(self, camel_case: bool = False) -> dict[str, Any]:
return convert_all_keys_to_camel_case(self) if camel_case else dict(self)
return convert_all_keys_to_camel_snake_otherwise(self, camel_case)


class LabelFilter(dict, CogniteFilter):
Expand Down Expand Up @@ -148,6 +148,7 @@ def _wrap_labels(values: list[str] | None) -> list[dict[str, str]] | None:
return [{"externalId": v} for v in values]

def dump(self, camel_case: bool = False) -> dict[str, Any]:
# TODO: convert_all_keys_to_camel_snake_otherwise
keys = map(to_camel_case, self.keys()) if camel_case else self.keys()
return dict(zip(keys, map(self._wrap_labels, self.values())))

Expand Down
2 changes: 2 additions & 0 deletions cognite/client/data_classes/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def _load(cls, raw_geo_location: dict[str, Any]) -> GeoLocation:
)

def dump(self, camel_case: bool = False) -> dict[str, Any]:
# TODO: convert_all_keys_to_camel_snake_otherwise(self, camel_case)
return convert_all_keys_to_camel_case(self) if camel_case else dict(self)


Expand All @@ -143,4 +144,5 @@ def _load(cls, raw_geo_location_filter: dict[str, Any]) -> GeoLocationFilter:
return cls(relation=raw_geo_location_filter["relation"], shape=raw_geo_location_filter["shape"])

def dump(self, camel_case: bool = False) -> dict[str, Any]:
# TODO: convert_all_keys_to_camel_snake_otherwise(self, camel_case)
return convert_all_keys_to_camel_case(self) if camel_case else dict(self)
7 changes: 7 additions & 0 deletions cognite/client/utils/_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ def shorten(obj: Any, width: int = 20, placeholder: str = "...") -> str:
if len(s) <= width:
return s
return f"{s[:width-n]}{placeholder}"


def convert_all_keys_to_camel_snake_otherwise(dct: dict[str, Any], camel_case: bool = False) -> dict[str, Any]:
if camel_case:
return {to_camel_case(key): value for key, value in dct.items()}
else:
return {to_snake_case(key): value for key, value in dct.items()}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can’t you use convert_dict_to_case?

5 changes: 0 additions & 5 deletions tests/tests_unit/test_api/test_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ def test_create_labels_using_wrong_type(self, cognite_client):
with pytest.raises(TypeError):
cognite_client.labels.create([Label(external_id="1", name="my_label")])

def test_load_list(self):
assert Label._load_list(None) is None
labels = [{"externalId": "a"}, "b", Label("c"), LabelDefinition("d")]
assert Label._load_list(labels) == [Label("a"), Label("b"), Label("c"), Label("d")]

def test_list_with_dataset_ids(self, cognite_client, mock_labels_response):
res = cognite_client.labels.list(data_set_ids=[123], data_set_external_ids=["x"])
assert res[0].data_set_id == 1
Expand Down
2 changes: 1 addition & 1 deletion tests/tests_unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def test_to_pandas_method(self):
"parent-test-1",
"A test asset",
123,
[{"externalId": "ROTATING_EQUIPMENT", "name": "Rotating equipment"}],
[{"external_id": "ROTATING_EQUIPMENT", "name": "Rotating equipment"}],
]
}

Expand Down
48 changes: 48 additions & 0 deletions tests/tests_unit/test_data_classes/test_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pytest

from cognite.client.data_classes.labels import Label, LabelDefinition


class TestLabel:
@pytest.mark.parametrize(
["fields", "is_camel_case", "expected"],
[
(
{"external_id": "what-the-fox", "snake_rules": True},
True,
{"externalId": "what-the-fox", "snakeRules": True},
),
(
{"external_id": "what-the-fox", "snake_rules": True},
False,
{"external_id": "what-the-fox", "snake_rules": True},
),
(
{"externalId": "what-the-fox", "snakeRules": True},
False,
{"external_id": "what-the-fox", "snake_rules": True},
),
(
{"externalId": "what-the-fox", "snakeRules": True},
True,
{"externalId": "what-the-fox", "snakeRules": True},
),
(
{"externalId": "what-the-fox", "snake_rules": True},
True,
{"externalId": "what-the-fox", "snakeRules": True},
),
(
{"externalId": "what-the-fox", "snake_rules": True},
False,
{"external_id": "what-the-fox", "snake_rules": True},
),
],
)
def test_dump(self, fields: dict, is_camel_case: bool, expected: dict):
assert Label(**fields).dump(is_camel_case) == expected

def test_load_list(self):
assert Label._load_list(None) is None
labels = [{"externalId": "a"}, "b", Label("c"), LabelDefinition("d")]
assert Label._load_list(labels) == [Label("a"), Label("b"), Label("c"), Label("d")]