Skip to content

Commit

Permalink
Turn on alpha tests (#1937)
Browse files Browse the repository at this point in the history
  • Loading branch information
haakonvt authored Sep 18, 2024
1 parent 6bd3ca7 commit 6f3cad4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 44 deletions.
1 change: 1 addition & 0 deletions tests/tests_integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def cognite_client() -> CogniteClient:
def cognite_client_alpha() -> CogniteClient:
load_dotenv(REPO_ROOT / "alpha.env")
if "COGNITE_ALPHA_PROJECT" not in os.environ:
# TODO: If we are in CI, we should fail the test instead of skipping
pytest.skip("ALPHA environment variables not set. Skipping ALPHA tests.")
return CogniteClient.default_oauth_client_credentials(
project=os.environ["COGNITE_ALPHA_PROJECT"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from collections.abc import Iterable
from datetime import datetime
from datetime import datetime, timedelta

import pytest
from _pytest.mark import ParameterSet
Expand All @@ -22,7 +22,7 @@

def core_model_v1_node_test_cases() -> Iterable[ParameterSet]:
today = datetime.now()
yesterday = today.replace(day=today.day - 1)
yesterday = today - timedelta(days=1)
yield pytest.param(
cdm.CogniteAssetApply(
space=DATA_SPACE,
Expand Down Expand Up @@ -108,6 +108,7 @@ def core_model_v1_node_test_cases() -> Iterable[ParameterSet]:
name="Test model 3D",
description="Test model 3D for core model v1 tests with Python SDK",
aliases=["test_model_3d_alias"],
model_type="PointCloud", # TODO: Should be nullable, returns as 400 if not set
),
cdm.Cognite3DModel,
id="CogniteModel3D",
Expand Down Expand Up @@ -201,8 +202,8 @@ def core_model_v1_edge_test_cases() -> Iterable[ParameterSet]:


@pytest.fixture(scope="session")
def data_space(cognite_client_alpha: CogniteClient) -> Space:
space = cognite_client_alpha.data_modeling.spaces.apply(
def data_space(cognite_client: CogniteClient) -> Space:
space = cognite_client.data_modeling.spaces.apply(
SpaceApply(space=DATA_SPACE, description="Test space for core model v1 tests with Python SDK", name=DATA_SPACE)
)
return space
Expand All @@ -212,14 +213,14 @@ class TestCoreModelv1:
@pytest.mark.usefixtures("data_space")
@pytest.mark.parametrize("write_instance, read_type", list(core_model_v1_node_test_cases()))
def test_write_read_node(
self, write_instance: TypedNodeApply, read_type: type[TypedNode], cognite_client_alpha: CogniteClient
self, write_instance: TypedNodeApply, read_type: type[TypedNode], cognite_client: CogniteClient
) -> None:
created = cognite_client_alpha.data_modeling.instances.apply(write_instance)
created = cognite_client.data_modeling.instances.apply(write_instance)

assert len(created.nodes) == 1
assert created.nodes[0].as_id() == write_instance.as_id()

read = cognite_client_alpha.data_modeling.instances.retrieve_nodes(write_instance.as_id(), node_cls=read_type)
read = cognite_client.data_modeling.instances.retrieve_nodes(write_instance.as_id(), node_cls=read_type)

assert isinstance(read, read_type)
assert read.as_id() == write_instance.as_id()
Expand All @@ -239,16 +240,16 @@ def test_write_read_node(
@pytest.mark.usefixtures("data_space")
@pytest.mark.parametrize("write_instance, read_type", list(core_model_v1_edge_test_cases()))
def test_write_read_edge(
self, write_instance: TypedEdgeApply, read_type: type[TypedEdge], cognite_client_alpha: CogniteClient
self, write_instance: TypedEdgeApply, read_type: type[TypedEdge], cognite_client: CogniteClient
) -> None:
created = cognite_client_alpha.data_modeling.instances.apply(
created = cognite_client.data_modeling.instances.apply(
edges=write_instance, auto_create_start_nodes=True, auto_create_end_nodes=True
)

assert len(created.edges) == 1
assert created.edges[0].as_id() == write_instance.as_id()

read = cognite_client_alpha.data_modeling.instances.retrieve_edges(write_instance.as_id(), edge_cls=read_type)
read = cognite_client.data_modeling.instances.retrieve_edges(write_instance.as_id(), edge_cls=read_type)

assert isinstance(read, read_type)
assert read.as_id() == write_instance.as_id()
Expand Down
22 changes: 10 additions & 12 deletions tests/tests_integration/test_api/test_datapoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def all_retrieve_endpoints(cognite_client, retrieve_endpoints):


@pytest.fixture(scope="session")
def instance_ts_id(cognite_client_alpha: CogniteClient, alpha_test_space: Space) -> InstanceId:
def instance_ts_id(cognite_client: CogniteClient, alpha_test_space: Space) -> InstanceId:
my_ts = NodeApply(
space=alpha_test_space.space,
external_id="ts_python_sdk_instance_id_tests",
Expand All @@ -199,7 +199,7 @@ def instance_ts_id(cognite_client_alpha: CogniteClient, alpha_test_space: Space)
)
],
)
created_ts = cognite_client_alpha.data_modeling.instances.apply(my_ts).nodes
created_ts = cognite_client.data_modeling.instances.apply(my_ts).nodes

return created_ts[0].as_id()

Expand Down Expand Up @@ -2764,29 +2764,27 @@ def test_dict_format_with_status_codes_using_insert_multiple(self, cognite_clien
assert set(dps_numeric.status_symbol) == {"Good", "Uncertain", "Bad"}

def test_insert_retrieve_delete_datapoints_with_instance_id(
self, cognite_client_alpha: CogniteClient, instance_ts_id: InstanceId
self, cognite_client: CogniteClient, instance_ts_id: InstanceId
) -> None:
cognite_client_alpha.time_series.data.insert([(0, 0.0), (1.0, 1.0)], instance_id=instance_ts_id)
cognite_client.time_series.data.insert([(0, 0.0), (1.0, 1.0)], instance_id=instance_ts_id)

retrieved = cognite_client_alpha.time_series.data.retrieve(instance_id=instance_ts_id, start=0, end=2)
retrieved = cognite_client.time_series.data.retrieve(instance_id=instance_ts_id, start=0, end=2)

assert retrieved.timestamp == [0, 1]
assert retrieved.value == [0.0, 1.0]

cognite_client_alpha.time_series.data.delete_range(0, 2, instance_id=instance_ts_id)
cognite_client.time_series.data.delete_range(0, 2, instance_id=instance_ts_id)

retrieved = cognite_client_alpha.time_series.data.retrieve(instance_id=instance_ts_id, start=0, end=2)
retrieved = cognite_client.time_series.data.retrieve(instance_id=instance_ts_id, start=0, end=2)

assert retrieved.timestamp == []

def test_insert_multiple_with_instance_id(
self, cognite_client_alpha: CogniteClient, instance_ts_id: InstanceId
) -> None:
cognite_client_alpha.time_series.data.insert_multiple(
def test_insert_multiple_with_instance_id(self, cognite_client: CogniteClient, instance_ts_id: InstanceId) -> None:
cognite_client.time_series.data.insert_multiple(
[{"instance_id": instance_ts_id, "datapoints": [{"timestamp": 4, "value": 42}]}]
)

retrieved = cognite_client_alpha.time_series.data.retrieve(instance_id=instance_ts_id, start=3, end=5)
retrieved = cognite_client.time_series.data.retrieve(instance_id=instance_ts_id, start=3, end=5)

assert retrieved.timestamp == [4]
assert retrieved.value == [42]
26 changes: 11 additions & 15 deletions tests/tests_integration/test_api/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def test_upload_multipart(self, cognite_client: CogniteClient) -> None:
cognite_client.files.delete(session.file_metadata.id)

def test_create_retrieve_update_delete_with_instance_id(
self, cognite_client_alpha: CogniteClient, alpha_test_space: Space
self, cognite_client: CogniteClient, alpha_test_space: Space
) -> None:
file = CogniteFileApply(
space=alpha_test_space.space,
Expand All @@ -264,47 +264,43 @@ def test_create_retrieve_update_delete_with_instance_id(
)
instance_id = file.as_id()
try:
created = cognite_client_alpha.data_modeling.instances.apply(file)
created = cognite_client.data_modeling.instances.apply(file)
assert len(created.nodes) == 1
assert created.nodes[0].as_id() == instance_id

f1 = cognite_client_alpha.files.upload_content_bytes(b"f1", instance_id=instance_id)
f1 = cognite_client.files.upload_content_bytes(b"f1", instance_id=instance_id)
time.sleep(0.5)
download_links = cognite_client_alpha.files.retrieve_download_urls(instance_id=instance_id)
download_links = cognite_client.files.retrieve_download_urls(instance_id=instance_id)
assert len(download_links.values()) == 1
assert download_links[f1.instance_id].startswith("http")

content_1 = "abcde" * 1_200_000
content_2 = "fghij"
with cognite_client_alpha.files.multipart_upload_content_session(
parts=2, instance_id=instance_id
) as session:
with cognite_client.files.multipart_upload_content_session(parts=2, instance_id=instance_id) as session:
session.upload_part(0, content_1)
session.upload_part(1, content_2)

retrieved_content = cognite_client_alpha.files.download_bytes(instance_id=instance_id)
retrieved_content = cognite_client.files.download_bytes(instance_id=instance_id)
assert len(retrieved_content) == 6000005

retrieved = cognite_client_alpha.files.retrieve(instance_id=instance_id)
retrieved = cognite_client.files.retrieve(instance_id=instance_id)
assert retrieved is not None
assert retrieved.instance_id == instance_id

update_writable = retrieved.as_write()
update_writable.metadata = {"a": "b"}
update_writable.external_id = "file_python_sdk_instance_id_tests_updated"
updated_writable = cognite_client_alpha.files.update(update_writable)
updated_writable = cognite_client.files.update(update_writable)
assert updated_writable.metadata == {"a": "b"}
assert updated_writable.external_id == "file_python_sdk_instance_id_tests_updated"

updated = cognite_client_alpha.files.update(
FileMetadataUpdate(instance_id=instance_id).metadata.add({"c": "d"})
)
updated = cognite_client.files.update(FileMetadataUpdate(instance_id=instance_id).metadata.add({"c": "d"}))
assert updated.metadata == {"a": "b", "c": "d"}

retrieved = cognite_client_alpha.files.retrieve_multiple(instance_ids=[instance_id])
retrieved = cognite_client.files.retrieve_multiple(instance_ids=[instance_id])
assert retrieved.dump() == [updated.dump()]
finally:
cognite_client_alpha.data_modeling.instances.delete(nodes=instance_id)
cognite_client.data_modeling.instances.delete(nodes=instance_id)

def test_create_delete_ignore_unknown_ids(self, cognite_client: CogniteClient) -> None:
file_metadata = FileMetadata(name="mytestfile")
Expand Down
14 changes: 7 additions & 7 deletions tests/tests_integration/test_api/test_time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def test_aggregate_unique_metadata_keys(
}

def test_create_retrieve_update_delete_with_instance_id(
self, cognite_client_alpha: CogniteClient, alpha_test_space: Space, alpha_test_dataset: DataSet
self, cognite_client: CogniteClient, alpha_test_space: Space, alpha_test_dataset: DataSet
) -> None:
my_ts = CogniteTimeSeriesApply(
space=alpha_test_space.space,
Expand All @@ -291,30 +291,30 @@ def test_create_retrieve_update_delete_with_instance_id(
update = TimeSeriesUpdate(instance_id=my_ts.as_id()).metadata.add({"a": "b"})

try:
created = cognite_client_alpha.data_modeling.instances.apply(my_ts)
created = cognite_client.data_modeling.instances.apply(my_ts)
assert len(created.nodes) == 1
assert created.nodes[0].as_id() == my_ts.as_id()

retrieved = cognite_client_alpha.time_series.retrieve(instance_id=my_ts.as_id())
retrieved = cognite_client.time_series.retrieve(instance_id=my_ts.as_id())
assert retrieved is not None
assert retrieved.instance_id == my_ts.as_id()

update_writable = retrieved.as_write()
update_writable.metadata = {"c": "d"}
update_writable.external_id = "ts_python_sdk_instance_id_tests"
update_writable.data_set_id = alpha_test_dataset.id
updated_writable = cognite_client_alpha.time_series.update(update_writable)
updated_writable = cognite_client.time_series.update(update_writable)
assert updated_writable.metadata == {"c": "d"}
assert updated_writable.data_set_id == alpha_test_dataset.id
assert updated_writable.external_id == "ts_python_sdk_instance_id_tests"

updated = cognite_client_alpha.time_series.update(update)
updated = cognite_client.time_series.update(update)
assert updated.metadata == {"a": "b", "c": "d"}

retrieved = cognite_client_alpha.time_series.retrieve_multiple(instance_ids=[my_ts.as_id()])
retrieved = cognite_client.time_series.retrieve_multiple(instance_ids=[my_ts.as_id()])
assert retrieved.dump() == [updated.dump()]
finally:
cognite_client_alpha.data_modeling.instances.delete(nodes=my_ts.as_id())
cognite_client.data_modeling.instances.delete(nodes=my_ts.as_id())


class TestTimeSeriesHelperMethods:
Expand Down

0 comments on commit 6f3cad4

Please sign in to comment.