Hi Harshita,
Sure, please adjusted the code as shown below;
import pytest
from unittest.mock import MagicMock, patch
import pandas as pd
from cognite.client import CogniteClient
c = CogniteClient()
# Assuming you have a function process_data in your code
def process_data(database, table, key):
row_list = c.raw.rows.list(database, table, limit=-1, columns=["key", "LastUpdatedDatetime"]).to_pandas()
if not row_list.empty:
val = list(row_list[row_list.index.isin([key])]['LastUpdatedDatetime'])
return val
else:
return None
# Assuming you have a CogniteClient class defined in your code
class CogniteClient:
def __init__(self):
self.raw = MagicMock()
# Your test function
@pytest.mark.asyncio
async def test_your_function():
# Set up your test data
database = "your_database"
table = "your_table"
key = "your_key"
# Create a CogniteClient instance
c = CogniteClient()
# Set up the mock response for c.raw.rows.list
last_updated_datetime = "2023-03-16T17:51:54Z"
# Create a MagicMock for the list
mock_list = MagicMock()
# Set up the mock response for the list
mock_list.return_value = [{"key": "your_key", "LastUpdatedDatetime": last_updated_datetime}]
# Set the return value of to_pandas on the list
mock_list.to_pandas.return_value = pd.DataFrame(mock_list.return_value)
# Use patch to temporarily replace the method with the mock_list
with patch.object(c.raw.rows, 'list', return_value=mock_list):
# Call the function
result = process_data(database, table, key)
# Assert the expected behavior
assert result == ["2023-03-16T17:51:54Z"]
I have tested the above code with our test environment and able to passed the unit mock test. Please change the LastUpdatedDatetime, database, table, key with your environment values.
plugins: asyncio-0.21.0, cov-3.0.0, rerunfailures-11.1.2, xdist-3.2.1
asyncio: mode=strict
collected 1 item
test_data_processor.py . [100%]
============================================================================================================ 1 passed in 12.33s ============================================================================================================