Testing
Object to use as a mock for CogniteClient
- class cognite.client.testing.CogniteClientMock(*args, **kw)
Mock for CogniteClient object
All APIs are replaced with specced MagicMock objects.
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 TimeSeries >>> from cognite.client.testing import monkeypatch_cognite_client >>> >>> with monkeypatch_cognite_client(): >>> client = CogniteClient() >>> client.time_series.create(TimeSeries(external_id="blabla"))
This example shows how to set the return value of a given method:
>>> from cognite.client import CogniteClient >>> 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=[] >>> ) >>> 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 import CogniteClient >>> 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) >>> client = CogniteClient() >>> try: >>> res = client.iam.token.inspect() >>> except CogniteAPIError as e: >>> assert 400 == e.code >>> assert "Something went wrong" == e.message