SIENTIAPDE-1148
Update sonar-project.properties to exclude worker.py from coverage, enhance error handling in worker.py, and add new tests for error scenarios in test_redis.py and test_mongo.py.
This commit is contained in:
@@ -88,6 +88,38 @@ async def test_get_last_data_timestamp_not_none(redis_activity):
|
||||
assert result == '2023-01-01 12:00:00'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_last_data_timestamp_error(redis_activity):
|
||||
"""Test get_last_data_timestamp error"""
|
||||
test_data = {
|
||||
**metadata,
|
||||
'workflow_name': 'test_pipeline',
|
||||
'schedule_name': 'test_schedule'
|
||||
}
|
||||
|
||||
redis_activity.send_notification = MagicMock()
|
||||
redis_activity.get = MagicMock(side_effect=Exception('test'))
|
||||
|
||||
try:
|
||||
|
||||
await redis_activity.get_last_data_timestamp(test_data)
|
||||
|
||||
except Exception as e:
|
||||
assert str(e) == 'test'
|
||||
|
||||
redis_activity.send_notification.assert_called_once_with(
|
||||
metadata=metadata['metadata'],
|
||||
notification_id="REDIS_GET_ERROR",
|
||||
message="Error getting last data timestamp: test",
|
||||
block="get_last_data_timestamp",
|
||||
level=NotificationLevel.ERROR,
|
||||
attachment_content=ANY
|
||||
)
|
||||
|
||||
else:
|
||||
assert False, "Expected exception"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_put_last_data_timestamp_empty_dataframe(redis_activity):
|
||||
"""Test put_last_data_timestamp with empty dataframe"""
|
||||
@@ -136,6 +168,42 @@ async def test_put_last_data_timestamp_not_empty_dataframe(redis_activity):
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_put_last_data_timestamp_error(redis_activity):
|
||||
"""Test put_last_data_timestamp error"""
|
||||
test_data = {
|
||||
**metadata,
|
||||
'workflow_name': 'test_pipeline',
|
||||
'schedule_name': 'test_schedule',
|
||||
'data': DataFrame({
|
||||
'name': ['sensor1', 'sensor2'],
|
||||
'value': [25.5, 30.0],
|
||||
'inserted_at': ['2023-01-01 12:00:00'] * 2
|
||||
}).to_dict('records')
|
||||
}
|
||||
|
||||
redis_activity.send_notification = MagicMock()
|
||||
redis_activity.set = MagicMock(side_effect=Exception('test'))
|
||||
|
||||
try:
|
||||
await redis_activity.put_last_data_timestamp(test_data)
|
||||
|
||||
except Exception as e:
|
||||
assert str(e) == 'test'
|
||||
|
||||
redis_activity.send_notification.assert_called_once_with(
|
||||
metadata=metadata['metadata'],
|
||||
notification_id="REDIS_SET_ERROR",
|
||||
message="Error setting last data timestamp: test",
|
||||
block="put_last_data_timestamp",
|
||||
level=NotificationLevel.ERROR,
|
||||
attachment_content=ANY
|
||||
)
|
||||
|
||||
else:
|
||||
assert False, "Expected exception"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_and_hold_data_new_key(redis_activity):
|
||||
"""Test group_and_hold_data with a new key"""
|
||||
@@ -283,6 +351,63 @@ async def test_group_and_hold_data_empty_dataframe(redis_activity):
|
||||
assert result == {}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_and_hold_data_error_get(redis_activity):
|
||||
"""Test group_and_hold_data error"""
|
||||
test_data = {
|
||||
**metadata,
|
||||
'workflow_name': 'test_workflow',
|
||||
'schedule_name': 'test_schedule',
|
||||
'retention_time': 3600,
|
||||
'model_id': 1,
|
||||
'data': DataFrame(columns=['name', 'value', 'timestamp']).to_dict('records')
|
||||
}
|
||||
|
||||
redis_activity.get = MagicMock(side_effect=Exception('test'))
|
||||
redis_activity.send_notification = MagicMock()
|
||||
|
||||
try:
|
||||
await redis_activity.group_and_hold_data(test_data)
|
||||
|
||||
except Exception as e:
|
||||
assert str(e) == 'test'
|
||||
|
||||
redis_activity.send_notification.assert_called_once_with(
|
||||
metadata=metadata['metadata'],
|
||||
notification_id="REDIS_GET_ERROR",
|
||||
message="Error getting held data: test",
|
||||
block="group_and_hold_data",
|
||||
level=NotificationLevel.ERROR,
|
||||
attachment_content=ANY
|
||||
)
|
||||
|
||||
else:
|
||||
assert False, "Expected exception"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_and_hold_data_error_set(redis_activity):
|
||||
"""Test group_and_hold_data error"""
|
||||
test_data = {
|
||||
**metadata,
|
||||
'workflow_name': 'test_workflow',
|
||||
'schedule_name': 'test_schedule',
|
||||
'retention_time': 3600,
|
||||
'model_id': 1,
|
||||
'data': DataFrame(columns=['name', 'value', 'timestamp']).to_dict('records')
|
||||
}
|
||||
|
||||
redis_activity.get = MagicMock(return_value=None)
|
||||
redis_activity.set = MagicMock(side_effect=Exception('test'))
|
||||
redis_activity.send_notification = MagicMock()
|
||||
|
||||
try:
|
||||
await redis_activity.group_and_hold_data(test_data)
|
||||
|
||||
except Exception as e:
|
||||
assert str(e) == 'test'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_store_data_package(redis_activity):
|
||||
"""Test store_data_package"""
|
||||
|
||||
Reference in New Issue
Block a user