SIENTIAPDE-1325
Refactor initialization of Activities, Gates, and MongoDB classes for improved readability by using multi-line argument formatting. Update related tests to match the new initialization style.
This commit is contained in:
@@ -83,7 +83,12 @@ class Activities(Postgres, Redis, Gates, MongoDB):
|
||||
)
|
||||
|
||||
# Initialize Gates
|
||||
Gates.__init__(self, logger=logger, notification_handler=notification_handler, metrics_controller=metrics_controller)
|
||||
Gates.__init__(
|
||||
self,
|
||||
logger=logger,
|
||||
notification_handler=notification_handler,
|
||||
metrics_controller=metrics_controller,
|
||||
)
|
||||
|
||||
# Initialize MongoDB
|
||||
MongoDB.__init__(
|
||||
|
||||
@@ -37,7 +37,12 @@ class Gates(SientiaMonitoring):
|
||||
ensure data integrity and enable flexible data processing workflows.
|
||||
"""
|
||||
|
||||
def __init__(self, logger: Logger, notification_handler: NotificationHandler, metrics_controller: MetricsController):
|
||||
def __init__(
|
||||
self,
|
||||
logger: Logger,
|
||||
notification_handler: NotificationHandler,
|
||||
metrics_controller: MetricsController,
|
||||
):
|
||||
"""
|
||||
Initialize the Gates class with logging and notification services.
|
||||
|
||||
|
||||
@@ -60,7 +60,12 @@ class MongoDB(SientiaMonitoring):
|
||||
metrics_controller=metrics_controller,
|
||||
)
|
||||
|
||||
SientiaMonitoring.__init__(self, logger=logger, notification_handler=notification_handler, metrics_controller=metrics_controller)
|
||||
SientiaMonitoring.__init__(
|
||||
self,
|
||||
logger=logger,
|
||||
notification_handler=notification_handler,
|
||||
metrics_controller=metrics_controller,
|
||||
)
|
||||
|
||||
def close(self):
|
||||
"""
|
||||
|
||||
@@ -134,10 +134,8 @@ async def main():
|
||||
try:
|
||||
await asyncio.gather(*handlers)
|
||||
|
||||
except BaseException as e: # NOSONAR
|
||||
logger.custom_error(
|
||||
'An unhandled exception occurred: %s', metadata=metadata
|
||||
)
|
||||
except BaseException: # NOSONAR
|
||||
logger.custom_error('An unhandled exception occurred: %s', metadata=metadata)
|
||||
finally:
|
||||
if notification_handler:
|
||||
notification_handler.shutdown()
|
||||
|
||||
@@ -13,7 +13,9 @@ from scouter.activities.redis import Redis
|
||||
@patch('scouter.activities.activities.Redis.__init__')
|
||||
@patch('scouter.activities.activities.Gates.__init__')
|
||||
@patch('scouter.activities.activities.MetricsController')
|
||||
def test___init__(mock_metrics_controller, mock_gates_init, mock_redis_init, mock_postgres_init, mock_mongodb_init):
|
||||
def test___init__(
|
||||
mock_metrics_controller, mock_gates_init, mock_redis_init, mock_postgres_init, mock_mongodb_init
|
||||
):
|
||||
postgres_config = {
|
||||
'host': 'localhost',
|
||||
'port': 5432,
|
||||
@@ -39,7 +41,7 @@ def test___init__(mock_metrics_controller, mock_gates_init, mock_redis_init, moc
|
||||
redis_config=redis_config,
|
||||
mongodb_config=mongodb_config,
|
||||
logger=logger,
|
||||
notification_handler=notification_handler
|
||||
notification_handler=notification_handler,
|
||||
)
|
||||
|
||||
assert isinstance(activities, Activities)
|
||||
@@ -83,7 +85,10 @@ def test___init__(mock_metrics_controller, mock_gates_init, mock_redis_init, moc
|
||||
)
|
||||
|
||||
mock_gates_init.assert_called_once_with(
|
||||
ANY, logger=logger, notification_handler=notification_handler, metrics_controller=mock_metrics_controller.return_value
|
||||
ANY,
|
||||
logger=logger,
|
||||
notification_handler=notification_handler,
|
||||
metrics_controller=mock_metrics_controller.return_value,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -15,7 +15,11 @@ def gates_fixture():
|
||||
logger = Mock()
|
||||
notification_handler = MagicMock()
|
||||
metrics_controller = MagicMock()
|
||||
gates = Gates(logger=logger, notification_handler=notification_handler, metrics_controller=metrics_controller)
|
||||
gates = Gates(
|
||||
logger=logger,
|
||||
notification_handler=notification_handler,
|
||||
metrics_controller=metrics_controller,
|
||||
)
|
||||
gates.send_notification = MagicMock()
|
||||
gates.logger = logger
|
||||
gates.notification_handler = notification_handler
|
||||
|
||||
@@ -68,7 +68,8 @@ def test_del(mongodb_activity):
|
||||
async def test_load_latest_data_none_last_data_timestamp(mongodb_activity):
|
||||
"""Test load_latest_data"""
|
||||
|
||||
mongodb_activity.mongodb_repository.find = AsyncMock(return_value = [
|
||||
mongodb_activity.mongodb_repository.find = AsyncMock(
|
||||
return_value=[
|
||||
{
|
||||
'name': 'test1',
|
||||
'value': 1,
|
||||
@@ -76,7 +77,8 @@ async def test_load_latest_data_none_last_data_timestamp(mongodb_activity):
|
||||
'2023-01-01 12:00:00.000000+0000', DATETIME_FORMAT_MS_WITH_TZ
|
||||
),
|
||||
}
|
||||
])
|
||||
]
|
||||
)
|
||||
|
||||
result = await mongodb_activity.load_latest_data(
|
||||
{
|
||||
@@ -105,7 +107,8 @@ async def test_load_latest_data_none_last_data_timestamp(mongodb_activity):
|
||||
async def test_load_latest_data_not_none_last_data_timestamp(mongodb_activity):
|
||||
"""Test load_latest_data"""
|
||||
|
||||
mongodb_activity.mongodb_repository.find = AsyncMock(return_value = [
|
||||
mongodb_activity.mongodb_repository.find = AsyncMock(
|
||||
return_value=[
|
||||
{
|
||||
'name': 'test1',
|
||||
'value': 1,
|
||||
@@ -113,7 +116,8 @@ async def test_load_latest_data_not_none_last_data_timestamp(mongodb_activity):
|
||||
'2023-01-01 12:00:00.000000+0000', DATETIME_FORMAT_MS_WITH_TZ
|
||||
),
|
||||
}
|
||||
])
|
||||
]
|
||||
)
|
||||
|
||||
result = await mongodb_activity.load_latest_data(
|
||||
{
|
||||
|
||||
@@ -80,7 +80,7 @@ async def test_get_last_data_timestamp_none(redis_activity):
|
||||
'schedule_name': 'test_schedule',
|
||||
}
|
||||
|
||||
redis_activity.redis_repository.get = AsyncMock(return_value = None)
|
||||
redis_activity.redis_repository.get = AsyncMock(return_value=None)
|
||||
|
||||
result = await redis_activity.get_last_data_timestamp(test_data)
|
||||
|
||||
@@ -96,7 +96,7 @@ async def test_get_last_data_timestamp_not_none(redis_activity):
|
||||
"""Test get_last_data_timestamp"""
|
||||
test_data = {**metadata, 'workflow_name': 'test_pipeline', 'schedule_name': 'test_schedule'}
|
||||
|
||||
redis_activity.redis_repository.get = AsyncMock(return_value = '2023-01-01 12:00:00')
|
||||
redis_activity.redis_repository.get = AsyncMock(return_value='2023-01-01 12:00:00')
|
||||
|
||||
result = await redis_activity.get_last_data_timestamp(test_data)
|
||||
|
||||
@@ -259,9 +259,12 @@ async def test_group_and_hold_data_new_key(redis_activity):
|
||||
|
||||
# Verify set was called with correct arguments
|
||||
redis_activity.redis_repository.set.assert_called_once_with(
|
||||
'held_data_test_pipeline_test_schedule', {'sensor1': 25.5, 'sensor2': 30.0, 'timestamp': '2023-01-01 12:00:00'}, ttl=3600
|
||||
'held_data_test_pipeline_test_schedule',
|
||||
{'sensor1': 25.5, 'sensor2': 30.0, 'timestamp': '2023-01-01 12:00:00'},
|
||||
ttl=3600,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_and_hold_data_update_existing_fill_missing(redis_activity):
|
||||
"""Test updating existing data with group_and_hold_data"""
|
||||
@@ -314,11 +317,18 @@ async def test_group_and_hold_data_update_existing_fill_missing(redis_activity):
|
||||
|
||||
# Verify set was called with correct arguments
|
||||
redis_activity.redis_repository.set.assert_called_once_with(
|
||||
'held_data_test_workflow_test_schedule', {'sensor1': 25.5, 'sensor2': 28.0, 'sensor3': 42.0, 'sensor4': None, 'timestamp': '2023-01-01 12:00:00'}, ttl=3600
|
||||
'held_data_test_workflow_test_schedule',
|
||||
{
|
||||
'sensor1': 25.5,
|
||||
'sensor2': 28.0,
|
||||
'sensor3': 42.0,
|
||||
'sensor4': None,
|
||||
'timestamp': '2023-01-01 12:00:00',
|
||||
},
|
||||
ttl=3600,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_group_and_hold_data_with_none_values(redis_activity):
|
||||
"""Test handling of None values in group_and_hold_data"""
|
||||
|
||||
Reference in New Issue
Block a user