Testing

The SDK provides mock classes for both sync and async clients to facilitate unit testing. These mocks use create_autospec with spec_set=True to provide better type safety and call signature checking.

Object to use as a mock for CogniteClient

class cognite.client.testing.CogniteClientMock(*args: Any, **kwargs: Any)

Mock for CogniteClient object

All APIs are replaced with specced MagicMock objects.

Object to use as a mock for AsyncCogniteClient

class cognite.client.testing.AsyncCogniteClientMock(*args: Any, **kwargs: Any)

Mock for AsyncCogniteClient object

All APIs are replaced with specced MagicMock objects and all async methods with AsyncMocks.

Use a context manager to monkeypatch CogniteClient

cognite.client.testing.monkeypatch_cognite_client() Iterator[CogniteClientMock]

Context manager for monkeypatching the CogniteClient.

Will patch all clients and replace them with specced MagicMock objects.

Yields:

CogniteClientMock – The mock with which the CogniteClient has been replaced

Examples

In this example we can run the following code without actually executing the underlying API calls:

>>> from cognite.client import CogniteClient
>>> from cognite.client.data_classes import TimeSeriesWrite
>>> from cognite.client.testing import monkeypatch_cognite_client
>>>
>>> with monkeypatch_cognite_client():
>>>     client = CogniteClient()
>>>     client.time_series.create(TimeSeriesWrite(external_id="blabla"))

This example shows how to set the return value of a given method:

>>> from cognite.client.data_classes.iam import TokenInspection
>>> from cognite.client.testing import monkeypatch_cognite_client
>>>
>>> with monkeypatch_cognite_client() as c_mock:
>>>     c_mock.iam.token.inspect.return_value = TokenInspection(
>>>         subject="subject", projects=[], capabilities=[]
>>>     )
>>>     # Init. a new client yields the same mocked client:
>>>     client = CogniteClient()
>>>     res = client.iam.token.inspect()
>>>     assert "subject" == res.subject

Here you can see how to have a given method raise an exception:

>>> from cognite.client.exceptions import CogniteAPIError
>>> from cognite.client.testing import monkeypatch_cognite_client
>>>
>>> with monkeypatch_cognite_client() as c_mock:
>>>     c_mock.iam.token.inspect.side_effect = CogniteAPIError(message="Something went wrong", code=400)
>>>     try:
>>>         res = c_mock.iam.token.inspect()
>>>     except CogniteAPIError as e:
>>>         assert 400 == e.code
>>>         assert "Something went wrong" == e.message

Use a context manager to monkeypatch AsyncCogniteClient

cognite.client.testing.monkeypatch_async_cognite_client() Iterator[AsyncCogniteClientMock]

Context manager for monkeypatching the AsyncCogniteClient.

Will patch all clients and replace them with specced MagicMock objects.

Yields:

AsyncCogniteClientMock – The mock with which the AsyncCogniteClient has been replaced

Examples

In this example we can run the following code without actually executing the underlying API calls:

>>> from cognite.client import AsyncCogniteClient
>>> from cognite.client.data_classes import TimeSeriesWrite
>>> from cognite.client.testing import monkeypatch_async_cognite_client
>>>
>>> with monkeypatch_async_cognite_client():
>>>     client = AsyncCogniteClient()
>>>     await client.time_series.create(TimeSeriesWrite(external_id="blabla"))

This example shows how to set the return value of a given method:

>>> from cognite.client.data_classes.iam import TokenInspection
>>> from cognite.client.testing import monkeypatch_async_cognite_client
>>>
>>> with monkeypatch_async_cognite_client() as c_mock:
>>>     c_mock.iam.token.inspect.return_value = TokenInspection(
>>>         subject="subject", projects=[], capabilities=[]
>>>     )
>>>     # Init. a new client yields the same mocked client:
>>>     client = AsyncCogniteClient()
>>>     res = await client.iam.token.inspect()
>>>     assert "subject" == res.subject

Here you can see how to have a given method raise an exception:

>>> from cognite.client.exceptions import CogniteAPIError
>>> from cognite.client.testing import monkeypatch_async_cognite_client
>>>
>>> with monkeypatch_async_cognite_client() as c_mock:
>>>     c_mock.iam.token.inspect.side_effect = CogniteAPIError(message="Something went wrong", code=400)
>>>     try:
>>>         res = await c_mock.iam.token.inspect()
>>>     except CogniteAPIError as e:
>>>         assert 400 == e.code
>>>         assert "Something went wrong" == e.message