SIENTIAPDE-1094
Refactor Redis activity tests to improve initialization and assertion logic
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import json
|
||||
from unittest.mock import MagicMock, patch
|
||||
from unittest.mock import MagicMock, patch, ANY
|
||||
from datetime import datetime
|
||||
import pytest
|
||||
import numpy as np
|
||||
@@ -9,65 +8,36 @@ from scouter.activities.redis import Redis
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@patch('scouter.activities.redis.redis.Redis')
|
||||
def redis_activity(_mock_redis_client):
|
||||
@patch('scouter.activities.redis.RedisBase.__init__')
|
||||
def redis_activity(_mock_redis_init):
|
||||
logger = MagicMock()
|
||||
notification_handler = MagicMock(spec=NotificationHandler)
|
||||
return Redis(host='localhost', port=6379,
|
||||
logger=logger, notification_handler=notification_handler,
|
||||
username='test', password='test')
|
||||
activity = Redis(host='localhost', port=6379,
|
||||
logger=logger, notification_handler=notification_handler,
|
||||
username='test', password='test')
|
||||
|
||||
activity.redis_client = MagicMock()
|
||||
activity.logger = logger
|
||||
activity.notification_handler = notification_handler
|
||||
return activity
|
||||
|
||||
|
||||
@patch('scouter.activities.redis.redis.Redis')
|
||||
def test_redis_initialization(mock_redis_client):
|
||||
@patch('scouter.activities.redis.RedisBase.__init__')
|
||||
def test_redis_initialization(mock_redis_init):
|
||||
"""Test Redis activity initialization"""
|
||||
redis_activity = Redis(host='localhost', port=6379,
|
||||
logger=MagicMock(), notification_handler=MagicMock(),
|
||||
username='test', password='test')
|
||||
assert redis_activity.host == 'localhost'
|
||||
assert redis_activity.port == 6379
|
||||
assert redis_activity.username == 'test'
|
||||
assert redis_activity.password == 'test'
|
||||
mock_redis_client.assert_called_once_with(
|
||||
host='localhost',
|
||||
port=6379,
|
||||
decode_responses=True,
|
||||
username='test',
|
||||
password='test'
|
||||
)
|
||||
|
||||
|
||||
def test_get_existing_key(redis_activity):
|
||||
"""Test getting an existing key from Redis"""
|
||||
test_data = {'key': 'value'}
|
||||
redis_activity.redis_client.get.return_value = json.dumps(test_data)
|
||||
|
||||
result = redis_activity.get('test_key')
|
||||
|
||||
assert result == test_data
|
||||
redis_activity.redis_client.get.assert_called_once_with('test_key')
|
||||
|
||||
|
||||
def test_get_nonexistent_key(redis_activity):
|
||||
"""Test getting a non-existent key from Redis"""
|
||||
redis_activity.redis_client.get.return_value = None
|
||||
|
||||
result = redis_activity.get('nonexistent_key')
|
||||
|
||||
assert result is None
|
||||
redis_activity.redis_client.get.assert_called_once_with('nonexistent_key')
|
||||
|
||||
|
||||
def test_set_key(redis_activity):
|
||||
"""Test setting a key in Redis"""
|
||||
test_data = {'key': 'value'}
|
||||
|
||||
redis_activity.set('test_key', test_data, ttl=300)
|
||||
|
||||
redis_activity.redis_client.set.assert_called_once_with(
|
||||
'test_key',
|
||||
json.dumps(test_data),
|
||||
ex=300
|
||||
logger = MagicMock()
|
||||
notification_handler = MagicMock(spec=NotificationHandler)
|
||||
Redis(host='localhost', port=6379,
|
||||
logger=logger, notification_handler=notification_handler,
|
||||
username='test', password='test')
|
||||
mock_redis_init.assert_called_once_with(
|
||||
ANY,
|
||||
'localhost',
|
||||
6379,
|
||||
'test',
|
||||
'test',
|
||||
logger,
|
||||
notification_handler
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user