-
Notifications
You must be signed in to change notification settings - Fork 38
feat(time series): Add time_series_type helper param to data_modeling.time_series.list (DM-4186)
#2870
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
Open
haakonvt
wants to merge
9
commits into
master
Choose a base branch
from
improve-state-ts-list
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat(time series): Add time_series_type helper param to data_modeling.time_series.list (DM-4186)
#2870
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
56cefc0
add build filter helper function in DM TS API
haakonvt 25321cc
add 'is_state' param to DM TS API /list
haakonvt ad71d92
add note to legacy TS API about "no state ts here"
haakonvt 476c17c
add test for data_modeling.time_series.list with is_state T/F
haakonvt 8940df9
add unit tests for _build_filter and the is_state filter in data_mode…
haakonvt cf036c9
add TimeSeriesType literal alias
haakonvt 1cd5e83
replace is_state with time_series_type
haakonvt 8062bdc
update tests for time_series_type in data_modeling.time_series.list
haakonvt 8ef17bc
rerun sync codegen after rebase
haakonvt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
95 changes: 95 additions & 0 deletions
95
tests/tests_unit/test_api/test_data_modeling/test_time_series.py
This file contains hidden or 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,95 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from collections.abc import Sequence | ||
| from typing import Any | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| import pytest | ||
|
|
||
| from cognite.client import CogniteClient | ||
| from cognite.client._api.data_modeling.time_series import _build_filter | ||
| from cognite.client._cognite_client import AsyncCogniteClient | ||
| from cognite.client.data_classes.data_modeling import NodeList | ||
| from cognite.client.data_classes.filters import Filter | ||
| from cognite.client.data_classes.time_series import TimeSeriesType | ||
|
|
||
| TYPE_PROPERTY = ["cdf_cdm", "CogniteTimeSeries/v1", "type"] | ||
| TYPE_IS_STATE = {"equals": {"property": TYPE_PROPERTY, "value": "state"}} | ||
| TYPE_IN_NUMERIC_STRING = {"in": {"property": TYPE_PROPERTY, "values": ["numeric", "string"]}} | ||
| SPACE_FILTER = {"equals": {"property": ["node", "space"], "value": "sp"}} | ||
|
|
||
|
|
||
| def as_sent(flt: Filter | None) -> dict[str, Any] | None: | ||
| # Properties are loaded as tuples of strings, but end up as lists after json has serialized. | ||
| # Thus we have this small helper to convert it to the expected format. | ||
| if flt is None: | ||
| return None | ||
| else: | ||
| return json.loads(json.dumps(flt.dump(camel_case_property=False))) | ||
|
|
||
|
|
||
| class TestBuildFilter: | ||
| @pytest.mark.parametrize("filter_as_dict", [True, False]) | ||
| @pytest.mark.parametrize( | ||
| "filter, time_series_type, expected", | ||
| [ | ||
| (None, None, None), | ||
| (None, "state", TYPE_IS_STATE), # single -> equals | ||
| (None, ["state"], TYPE_IS_STATE), # ...also in a sequence | ||
| (None, ("state",), TYPE_IS_STATE), | ||
| (None, ["numeric", "string"], TYPE_IN_NUMERIC_STRING), # multiple -> in | ||
| (None, ("numeric", "string"), TYPE_IN_NUMERIC_STRING), # any sequence works | ||
| (SPACE_FILTER, None, SPACE_FILTER), | ||
| (SPACE_FILTER, "state", {"and": [TYPE_IS_STATE, SPACE_FILTER]}), | ||
| (SPACE_FILTER, ["numeric", "string"], {"and": [TYPE_IN_NUMERIC_STRING, SPACE_FILTER]}), | ||
| ], | ||
| ) | ||
| def test_build_filter( | ||
| self, | ||
| filter: dict[str, Any] | None, | ||
| time_series_type: TimeSeriesType | Sequence[TimeSeriesType] | None, | ||
| expected: dict[str, Any] | None, | ||
| filter_as_dict: bool, | ||
| ) -> None: | ||
| given: Filter | dict | None = filter | ||
| if not (filter is None or filter_as_dict): | ||
| given = Filter.load(filter) | ||
|
|
||
| assert expected == as_sent(_build_filter(given, time_series_type=time_series_type)) | ||
|
|
||
| @pytest.mark.parametrize("time_series_type", [[], ()]) | ||
| def test_build_filter_raises_on_empty_time_series_types(self, time_series_type: Sequence[TimeSeriesType]) -> None: | ||
| with pytest.raises(ValueError, match="'time_series_type' must not be empty, pass None"): | ||
| _build_filter(None, time_series_type=time_series_type) | ||
|
|
||
|
|
||
| class TestDMTimeSeriesListTimeSeriesTypes: | ||
| @pytest.fixture | ||
| def list_mock(self, async_client: AsyncCogniteClient, monkeypatch: pytest.MonkeyPatch) -> AsyncMock: | ||
| mock = AsyncMock(return_value=NodeList([])) | ||
| monkeypatch.setattr(async_client.data_modeling.instances, "list", mock) | ||
| return mock | ||
|
|
||
| @staticmethod | ||
| def sent_filter(list_mock: AsyncMock) -> dict[str, Any] | None: | ||
| flt = list_mock.call_args.kwargs["filter"] | ||
| if flt is None or isinstance(flt, dict): | ||
| return flt | ||
| return json.loads(json.dumps(flt.dump(camel_case_property=False))) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "kwargs, expected", | ||
| [ | ||
| ({}, None), | ||
| ({"time_series_type": "state"}, TYPE_IS_STATE), | ||
| ({"time_series_type": ["numeric", "string"]}, TYPE_IN_NUMERIC_STRING), | ||
| ({"filter": SPACE_FILTER}, SPACE_FILTER), | ||
| ({"time_series_type": "state", "filter": SPACE_FILTER}, {"and": [TYPE_IS_STATE, SPACE_FILTER]}), | ||
| ], | ||
| ) | ||
| def test_time_series_types_is_combined_with_filter( | ||
| self, cognite_client: CogniteClient, list_mock: AsyncMock, kwargs: dict[str, Any], expected: dict | None | ||
| ) -> None: | ||
| cognite_client.data_modeling.time_series.list(**kwargs) | ||
| assert self.sent_filter(list_mock) == expected | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.