|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch |
| 3 | +import clients as clients |
| 4 | +import importlib |
| 5 | + |
| 6 | + |
| 7 | +class TestClients(unittest.TestCase): |
| 8 | + |
| 9 | + BUCKET_NAME = "default-bucket" |
| 10 | + AWS_REGION = "eu-west-2" |
| 11 | + REDIS_HOST = "mock-redis-host" |
| 12 | + REDIS_PORT = 6379 |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + # patch boto3_client |
| 16 | + self.boto3_client_patch = patch("boto3.client") |
| 17 | + self.mock_boto3_client = self.boto3_client_patch.start() |
| 18 | + # patch logging |
| 19 | + self.logging_patch = patch("logging.getLogger") |
| 20 | + self.mock_logging = self.logging_patch.start() |
| 21 | + # patch |
| 22 | + |
| 23 | + self.getenv_patch = patch("os.getenv") |
| 24 | + self.mock_getenv = self.getenv_patch.start() |
| 25 | + self.mock_getenv.side_effect = lambda key, default=None: { |
| 26 | + "CONFIG_BUCKET_NAME": self.BUCKET_NAME, |
| 27 | + "AWS_REGION": self.AWS_REGION, |
| 28 | + "REDIS_HOST": self.REDIS_HOST, |
| 29 | + "REDIS_PORT": self.REDIS_PORT |
| 30 | + }.get(key, default) |
| 31 | + |
| 32 | + # patch redis |
| 33 | + self.redis_patch = patch("redis.StrictRedis") |
| 34 | + self.mock_redis = self.redis_patch.start() |
| 35 | + # default mock returns for redis and s3 client |
| 36 | + self.mock_redis.return_value = self.mock_redis |
| 37 | + self.mock_boto3_client.return_value = self.mock_boto3_client |
| 38 | + self.mock_boto3_client.return_value.send_message = {} |
| 39 | + |
| 40 | + def tearDown(self): |
| 41 | + self.getenv_patch.stop() |
| 42 | + self.boto3_client_patch.stop() |
| 43 | + self.logging_patch.stop() |
| 44 | + self.redis_patch.stop() |
| 45 | + |
| 46 | + def test_os_environ(self): |
| 47 | + # Test if environment variables are set correctly |
| 48 | + importlib.reload(clients) |
| 49 | + self.assertEqual(clients.CONFIG_BUCKET_NAME, self.BUCKET_NAME) |
| 50 | + self.assertEqual(clients.REGION_NAME, self.AWS_REGION) |
| 51 | + self.assertEqual(clients.REDIS_HOST, self.REDIS_HOST) |
| 52 | + self.assertEqual(clients.REDIS_PORT, self.REDIS_PORT) |
| 53 | + |
| 54 | + def test_boto3_client(self): |
| 55 | + ''' Test boto3 client is created with correct parameters ''' |
| 56 | + importlib.reload(clients) |
| 57 | + self.mock_boto3_client.assert_called_once_with("s3", region_name=self.AWS_REGION) |
| 58 | + |
| 59 | + def test_redis_client(self): |
| 60 | + ''' Test redis client is created with correct parameters ''' |
| 61 | + importlib.reload(clients) |
| 62 | + self.mock_redis.assert_called_once_with( |
| 63 | + host=self.REDIS_HOST, |
| 64 | + port=self.REDIS_PORT, |
| 65 | + decode_responses=True |
| 66 | + ) |
| 67 | + |
| 68 | + def test_logging_setup(self): |
| 69 | + ''' Test logging is set up correctly ''' |
| 70 | + importlib.reload(clients) |
| 71 | + self.assertTrue(hasattr(clients, 'logger')) |
| 72 | + |
| 73 | + def test_logging_configuration(self): |
| 74 | + ''' Test logging configuration ''' |
| 75 | + importlib.reload(clients) |
| 76 | + clients.logger.setLevel.assert_called_once_with("INFO") |
| 77 | + |
| 78 | + def test_redis_client_initialization(self): |
| 79 | + ''' Test redis client initialization ''' |
| 80 | + importlib.reload(clients) |
| 81 | + self.mock_redis.assert_called_once_with(host=self.REDIS_HOST, port=self.REDIS_PORT, decode_responses=True) |
| 82 | + self.assertTrue(hasattr(clients, 'redis_client')) |
| 83 | + self.assertIsInstance(clients.redis_client, self.mock_redis.return_value.__class__) |
| 84 | + |
| 85 | + def test_logging_initialization(self): |
| 86 | + ''' Test logging initialization ''' |
| 87 | + importlib.reload(clients) |
| 88 | + self.mock_logging.assert_called_once_with() |
| 89 | + self.assertTrue(hasattr(clients, 'logger')) |
| 90 | + clients.logger.setLevel.assert_any_call("INFO") |
0 commit comments