from unittest.mock import patch, MagicMock, ANY from pytest import mark from sientia_do.temporal.activities.postgres import Postgres from scouter.activities.activities import Activities from scouter.activities.redis import Redis from scouter.activities.kafka import Kafka from scouter.activities.gates import Gates @patch('scouter.activities.activities.Postgres.__init__') @patch('scouter.activities.activities.Redis.__init__') @patch('scouter.activities.activities.Kafka.__init__') @patch('scouter.activities.activities.Gates.__init__') def test___init__(mock_gates_init, mock_kafka_init, mock_redis_init, mock_postgres_init): postgres_config = { 'host': 'localhost', 'port': 5432, 'user': 'postgres', 'password': 'postgres', 'dbname': 'postgres', 'min_connections': 1, 'max_connections': 10 } redis_config = { 'host': 'localhost', 'port': 6379, 'username': 'redis', 'password': 'redis' } kafka_config = { 'bootstrap_servers': 'localhost:9092', 'polling_time': 1000, 'group_id': 'test-group' } logger = MagicMock() notification_handler = MagicMock() activities = Activities( postgres_config=postgres_config, redis_config=redis_config, kafka_config=kafka_config, logger=logger, notification_handler=notification_handler ) assert isinstance(activities, Activities) assert isinstance(activities, Postgres) assert isinstance(activities, Redis) assert isinstance(activities, Kafka) assert isinstance(activities, Gates) mock_postgres_init.assert_called_once_with( ANY, host=postgres_config['host'], port=postgres_config['port'], user=postgres_config['user'], password=postgres_config['password'], dbname=postgres_config['dbname'], min_connections=postgres_config['min_connections'], max_connections=postgres_config['max_connections'], logger=logger, notification_handler=notification_handler ) mock_redis_init.assert_called_once_with( ANY, host=redis_config['host'], port=redis_config['port'], username=redis_config['username'], password=redis_config['password'], logger=logger, notification_handler=notification_handler ) mock_kafka_init.assert_called_once_with( ANY, bootstrap_servers=kafka_config['bootstrap_servers'], polling_time=kafka_config['polling_time'], group_id=kafka_config['group_id'], logger=logger, notification_handler=notification_handler ) mock_gates_init.assert_called_once_with( ANY, logger=logger, notification_handler=notification_handler ) @patch('scouter.activities.activities.Postgres.__init__') @patch('scouter.activities.activities.Redis.__init__') @patch('scouter.activities.activities.Kafka.__init__') @patch('scouter.activities.activities.Gates.__init__') @patch('scouter.activities.activities.Postgres.close') @patch('scouter.activities.activities.Kafka.close') def test_shutdown(mock_kafka_close, mock_postgres_close, _mock_gates_init, _mock_redis_init, _mock_kafka_init, _mock_postgres_init): postgres_config = { 'host': 'localhost', 'port': 5432, 'user': 'postgres', 'password': 'postgres', 'dbname': 'postgres', 'min_connections': 1, 'max_connections': 10 } redis_config = { 'host': 'localhost', 'port': 6379, 'username': 'redis', 'password': 'redis' } kafka_config = { 'bootstrap_servers': 'localhost:9092', 'polling_time': 1000, 'group_id': 'test-group' } logger = MagicMock() notification_handler = MagicMock() activities = Activities( postgres_config=postgres_config, redis_config=redis_config, kafka_config=kafka_config, logger=logger, notification_handler=notification_handler ) activities.shutdown() mock_postgres_close.assert_called_once() mock_kafka_close.assert_called_once()