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
|
# 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
|
# Initialize MongoDB
|
||||||
MongoDB.__init__(
|
MongoDB.__init__(
|
||||||
|
|||||||
@@ -37,7 +37,12 @@ class Gates(SientiaMonitoring):
|
|||||||
ensure data integrity and enable flexible data processing workflows.
|
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.
|
Initialize the Gates class with logging and notification services.
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,12 @@ class MongoDB(SientiaMonitoring):
|
|||||||
metrics_controller=metrics_controller,
|
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):
|
def close(self):
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -134,10 +134,8 @@ async def main():
|
|||||||
try:
|
try:
|
||||||
await asyncio.gather(*handlers)
|
await asyncio.gather(*handlers)
|
||||||
|
|
||||||
except BaseException as e: # NOSONAR
|
except BaseException: # NOSONAR
|
||||||
logger.custom_error(
|
logger.custom_error('An unhandled exception occurred: %s', metadata=metadata)
|
||||||
'An unhandled exception occurred: %s', metadata=metadata
|
|
||||||
)
|
|
||||||
finally:
|
finally:
|
||||||
if notification_handler:
|
if notification_handler:
|
||||||
notification_handler.shutdown()
|
notification_handler.shutdown()
|
||||||
|
|||||||
@@ -13,7 +13,9 @@ from scouter.activities.redis import Redis
|
|||||||
@patch('scouter.activities.activities.Redis.__init__')
|
@patch('scouter.activities.activities.Redis.__init__')
|
||||||
@patch('scouter.activities.activities.Gates.__init__')
|
@patch('scouter.activities.activities.Gates.__init__')
|
||||||
@patch('scouter.activities.activities.MetricsController')
|
@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 = {
|
postgres_config = {
|
||||||
'host': 'localhost',
|
'host': 'localhost',
|
||||||
'port': 5432,
|
'port': 5432,
|
||||||
@@ -39,7 +41,7 @@ def test___init__(mock_metrics_controller, mock_gates_init, mock_redis_init, moc
|
|||||||
redis_config=redis_config,
|
redis_config=redis_config,
|
||||||
mongodb_config=mongodb_config,
|
mongodb_config=mongodb_config,
|
||||||
logger=logger,
|
logger=logger,
|
||||||
notification_handler=notification_handler
|
notification_handler=notification_handler,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert isinstance(activities, Activities)
|
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(
|
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()
|
logger = Mock()
|
||||||
notification_handler = MagicMock()
|
notification_handler = MagicMock()
|
||||||
metrics_controller = 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.send_notification = MagicMock()
|
||||||
gates.logger = logger
|
gates.logger = logger
|
||||||
gates.notification_handler = notification_handler
|
gates.notification_handler = notification_handler
|
||||||
|
|||||||
@@ -68,15 +68,17 @@ def test_del(mongodb_activity):
|
|||||||
async def test_load_latest_data_none_last_data_timestamp(mongodb_activity):
|
async def test_load_latest_data_none_last_data_timestamp(mongodb_activity):
|
||||||
"""Test load_latest_data"""
|
"""Test load_latest_data"""
|
||||||
|
|
||||||
mongodb_activity.mongodb_repository.find = AsyncMock(return_value = [
|
mongodb_activity.mongodb_repository.find = AsyncMock(
|
||||||
{
|
return_value=[
|
||||||
'name': 'test1',
|
{
|
||||||
'value': 1,
|
'name': 'test1',
|
||||||
'inserted_at': datetime.strptime(
|
'value': 1,
|
||||||
'2023-01-01 12:00:00.000000+0000', DATETIME_FORMAT_MS_WITH_TZ
|
'inserted_at': datetime.strptime(
|
||||||
),
|
'2023-01-01 12:00:00.000000+0000', DATETIME_FORMAT_MS_WITH_TZ
|
||||||
}
|
),
|
||||||
])
|
}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
result = await mongodb_activity.load_latest_data(
|
result = await mongodb_activity.load_latest_data(
|
||||||
{
|
{
|
||||||
@@ -105,15 +107,17 @@ 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):
|
async def test_load_latest_data_not_none_last_data_timestamp(mongodb_activity):
|
||||||
"""Test load_latest_data"""
|
"""Test load_latest_data"""
|
||||||
|
|
||||||
mongodb_activity.mongodb_repository.find = AsyncMock(return_value = [
|
mongodb_activity.mongodb_repository.find = AsyncMock(
|
||||||
{
|
return_value=[
|
||||||
'name': 'test1',
|
{
|
||||||
'value': 1,
|
'name': 'test1',
|
||||||
'inserted_at': datetime.strptime(
|
'value': 1,
|
||||||
'2023-01-01 12:00:00.000000+0000', DATETIME_FORMAT_MS_WITH_TZ
|
'inserted_at': datetime.strptime(
|
||||||
),
|
'2023-01-01 12:00:00.000000+0000', DATETIME_FORMAT_MS_WITH_TZ
|
||||||
}
|
),
|
||||||
])
|
}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
result = await mongodb_activity.load_latest_data(
|
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',
|
'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)
|
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 get_last_data_timestamp"""
|
||||||
test_data = {**metadata, 'workflow_name': 'test_pipeline', 'schedule_name': 'test_schedule'}
|
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)
|
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
|
# Verify set was called with correct arguments
|
||||||
redis_activity.redis_repository.set.assert_called_once_with(
|
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
|
@pytest.mark.asyncio
|
||||||
async def test_group_and_hold_data_update_existing_fill_missing(redis_activity):
|
async def test_group_and_hold_data_update_existing_fill_missing(redis_activity):
|
||||||
"""Test updating existing data with group_and_hold_data"""
|
"""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
|
# Verify set was called with correct arguments
|
||||||
redis_activity.redis_repository.set.assert_called_once_with(
|
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
|
@pytest.mark.asyncio
|
||||||
async def test_group_and_hold_data_with_none_values(redis_activity):
|
async def test_group_and_hold_data_with_none_values(redis_activity):
|
||||||
"""Test handling of None values in group_and_hold_data"""
|
"""Test handling of None values in group_and_hold_data"""
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ def test_build_mongodb_config_defaults():
|
|||||||
os.environ['MONGODB_DATABASE_NAME'] = 'sientia'
|
os.environ['MONGODB_DATABASE_NAME'] = 'sientia'
|
||||||
os.environ['MONGODB_USERNAME'] = 'sientia'
|
os.environ['MONGODB_USERNAME'] = 'sientia'
|
||||||
os.environ['MONGODB_PASSWORD'] = 'sientia'
|
os.environ['MONGODB_PASSWORD'] = 'sientia'
|
||||||
|
|
||||||
config = build_mongodb_config()
|
config = build_mongodb_config()
|
||||||
|
|
||||||
assert config == {
|
assert config == {
|
||||||
|
|||||||
Reference in New Issue
Block a user