diff --git a/cognite/client/_api/datapoints.py b/cognite/client/_api/datapoints.py index 6cb79ef51..01d40fbaa 100644 --- a/cognite/client/_api/datapoints.py +++ b/cognite/client/_api/datapoints.py @@ -1525,12 +1525,13 @@ def insert( The timestamp can be given by datetime as above, or in milliseconds since epoch. Status codes can also be passed as normal integers; this is necessary if a subcategory or modifier flag is needed, e.g. 3145728: 'GoodClamped': + >>> from cognite.client.data_classes.data_modeling import NodeId >>> datapoints = [ ... (150000000000, 1000), ... (160000000000, 2000, 3145728), ... (170000000000, 2000, 2147483648), # Same as StatusCode.Bad ... ] - >>> client.time_series.data.insert(datapoints, id=2) + >>> client.time_series.data.insert(datapoints, instance_id=NodeId("my-space", "my-ts-xid")) Or they can be a list of dictionaries: @@ -1595,24 +1596,32 @@ def insert_multiple(self, datapoints: list[dict[str, str | int | list | Datapoin >>> from cognite.client import CogniteClient + >>> from cognite.client.data_classes.data_modeling import NodeId >>> from cognite.client.data_classes import StatusCode >>> from datetime import datetime, timezone >>> client = CogniteClient() >>> to_insert = [ ... {"id": 1, "datapoints": [ ... (datetime(2018,1,1, tzinfo=timezone.utc), 1000), - ... (datetime(2018,1,2, tzinfo=timezone.utc), 2000, StatusCode.Good), - ... (datetime(2018,1,3, tzinfo=timezone.utc), 3000, StatusCode.Uncertain), - ... (datetime(2018,1,4, tzinfo=timezone.utc), None, StatusCode.Bad), - ... ]}] + ... (datetime(2018,1,2, tzinfo=timezone.utc), 2000, StatusCode.Good)], + ... }, + ... {"external_id": "foo", "datapoints": [ + ... (datetime(2018,1,3, tzinfo=timezone.utc), 3000), + ... (datetime(2018,1,4, tzinfo=timezone.utc), 4000, StatusCode.Uncertain)], + ... }, + ... {"instance_id": NodeId("my-space", "my-ts-xid"), "datapoints": [ + ... (datetime(2018,1,5, tzinfo=timezone.utc), 5000), + ... (datetime(2018,1,6, tzinfo=timezone.utc), None, StatusCode.Bad)], + ... } + ... ] Passing datapoints using the dictionary format with timestamp given in milliseconds since epoch: >>> import math >>> to_insert.append( - ... {"external_id": "foo", "datapoints": [ - ... {"timestamp": 170000000, "value": 4000}, - ... {"timestamp": 180000000, "value": 5000, "status": {"symbol": "Uncertain"}}, + ... {"external_id": "bar", "datapoints": [ + ... {"timestamp": 170000000, "value": 7000}, + ... {"timestamp": 180000000, "value": 8000, "status": {"symbol": "Uncertain"}}, ... {"timestamp": 190000000, "value": None, "status": {"code": StatusCode.Bad}}, ... {"timestamp": 200000000, "value": math.inf, "status": {"code": StatusCode.Bad, "symbol": "Bad"}}, ... ]}) @@ -1625,7 +1634,7 @@ def insert_multiple(self, datapoints: list[dict[str, str | int | list | Datapoin >>> client.time_series.data.insert_multiple(to_insert) """ if not isinstance(datapoints, Sequence): - raise ValueError("Input must be a list of dictionaries") + raise TypeError("Input to 'insert_multiple' must be a list of dictionaries") DatapointsPoster(self).insert(datapoints) def delete_range( diff --git a/tests/tests_integration/test_api/test_three_d.py b/tests/tests_integration/test_api/test_three_d.py index d083a8495..2a5e35cf7 100644 --- a/tests/tests_integration/test_api/test_three_d.py +++ b/tests/tests_integration/test_api/test_three_d.py @@ -7,6 +7,12 @@ from cognite.client import CogniteClient from cognite.client.data_classes import ThreeDModel, ThreeDModelRevisionUpdate, ThreeDModelUpdate, ThreeDModelWrite from cognite.client.exceptions import CogniteAPIError +from cognite.client.utils._text import random_string + + +@pytest.fixture(scope="class") +def test_model_name(): + return f"NewTestModel-{random_string(6)}" @pytest.fixture(scope="class") @@ -17,8 +23,8 @@ def test_revision(cognite_client): @pytest.fixture(scope="class") -def new_model(cognite_client: CogniteClient) -> ThreeDModel: - res = cognite_client.three_d.models.create(name="NewTestModel") +def new_model(cognite_client: CogniteClient, test_model_name: str) -> ThreeDModel: + res = cognite_client.three_d.models.create(name=test_model_name) yield res cognite_client.three_d.models.delete(id=res.id) assert cognite_client.three_d.models.retrieve(id=res.id) is None @@ -30,11 +36,12 @@ def test_nodes_tree_index_order(cognite_client): return cognite_client.three_d.list_nodes(model_id=model_id, revision_id=revision.id) +@pytest.mark.usefixtures("new_model") class TestThreeDModelsAPI: - def test_list_and_retrieve(self, cognite_client): + def test_list_and_retrieve(self, cognite_client, test_model_name): res = cognite_client.three_d.models.list(limit=1) assert 1 == len(res) - res = next(r for r in cognite_client.three_d.models(limit=None) if r.name == "MyModel775") + res = next(r for r in cognite_client.three_d.models(limit=None) if r.name == test_model_name) assert res == cognite_client.three_d.models.retrieve(res.id) def test_create_update_delete(self, cognite_client) -> None: