Facing issue while writing test cases using monkey_cognite_patch.
Hi Team,
We are working on a POC , where we need to create unit test cases where we are facing different issues .For reference we are using code from Cognite Hub
Below attaching our code and output expectations . Please help with the approach to resolve this issue
Expectation from print is ts_internal_id = 3973245479016710 Actual Output -
Page 1 / 1
Hi @vidhi.c.jain , did the above help?
In @erlend.vollset code above, ts_data is an actual pandas DataFrame, which allows later pandas “data wrangling” code to succeed (in the test). I suspect one of your variables named “df_*” are still a mock.
(Note that Erlend sets the return value to a TimeSeriesList. The later .get call then uses the actual get-method on the object (i.e. not some mocked method). Same goes with the .to_pandas call after that.)
I can chain these intermediate calls as you suggested but my question is will this result a proper result which is expected with above like of code ? Will it be able to pass that step ? df_filtered_timefilter = df_filtered[(event_start_date <= df_filtered["start_time"]) & (df_filtered["end_time"] <= event_end_date)] Or will these patches will be considered as real df so that the errored line could pass through to next step.
Hi @erlend.vollset I have tried using monkey patch in your suggested pattern for example
"value":3973245479016710,"Brazil:3W:Well-Bore-19B_target_throughput","Brazil:3W:Well-Bore-19B_target_throughput",False,{}, "Barrel",3332326574821917,False,"target volume for a oil well bore",l],8739280581150161,"2023-05-02 09:37:04.944000", "2023-05-02 09:37:04.944000"]}
Use case is in between these calls I have used is df_filtered_timefilter = df_filteredm(event_start_date <= df_filteredd"start_time"]) & (df_filtered""end_time"] <= event_end_date)]
But i am getting this error <MagicMock name='mock.assets.retrieve().subtree().events().to_pandas().__getitem__()' id='2821478081008'>
and code is breaking is between these patches
Second what @Håkon V. Treider writes here. You need to consider intermediate chained calls. This works:
from unittest.mock import MagicMock
from cognite.client.data_classes import TimeSeries, TimeSeriesList, Asset from cognite.client.testing import monkeypatch_cognite_client
ts_retrieved = TimeSeries( id=3973245479016710, external_id="Brazil:3W:Well-Bore-19B_target_throughput", name="Brazil:3W:Well-Bore-19B_target_throughput", is_string=False, metadata={}, unit="Barrel", asset_id=3332326574821917, is_step=False, description="target volume for a oil well bore", security_categories=e], data_set_id=8739280581150161, created_time=123, last_updated_time=345 )
]
asset_name = "some_asset"
with monkeypatch_cognite_client() as client: asset_retrieve_response_mock = MagicMock() asset_retrieve_response_mock.time_series.return_value = TimeSeriesList(ts_retrieved) client.assets.retrieve.return_value = asset_retrieve_response_mock
You chain a lot of methods here and so mocking will be a bit entangled as well. First off, you should not call the methods on the mock when setting a return value:
Secondly, you need to think about what is returned by these intermediate chained calls, i.e. you first get an asset object, then you ask for the time series connected to it. This gives you a TimeSeriesList object. On this object, you get the specific time series by using the .get method supplying an identifier etc, etc, etc.
You should probably mock these objects separately, i.e.