|
1 | 1 | import unittest |
2 | | -from unittest.mock import patch |
3 | | -import common.clients as clients |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +import logging |
4 | 4 | import importlib |
| 5 | +import common.clients as clients |
5 | 6 |
|
6 | 7 |
|
7 | 8 | class TestClients(unittest.TestCase): |
8 | | - |
9 | 9 | BUCKET_NAME = "default-bucket" |
10 | 10 | AWS_REGION = "eu-west-2" |
11 | 11 |
|
12 | 12 | def setUp(self): |
13 | | - self.boto3_client_patch = patch("boto3.client") |
| 13 | + # Patch boto3.client |
| 14 | + self.boto3_client_patch = patch("boto3.client", autospec=True) |
14 | 15 | self.mock_boto3_client = self.boto3_client_patch.start() |
15 | | - self.logging_patch = patch("logging.getLogger") |
16 | | - self.mock_logging = self.logging_patch.start() |
17 | | - self.logger_info_patcher = patch("logging.Logger.info") |
18 | | - self.mock_logger_info = self.logger_info_patcher.start() |
19 | | - self.getenv_patch = patch("os.getenv") |
| 16 | + self.addCleanup(self.boto3_client_patch.stop) |
| 17 | + |
| 18 | + # Patch logging.getLogger |
| 19 | + self.logging_patch = patch("logging.getLogger", autospec=True) |
| 20 | + self.mock_getLogger = self.logging_patch.start() |
| 21 | + self.addCleanup(self.logging_patch.stop) |
| 22 | + |
| 23 | + # Patch os.getenv |
| 24 | + self.getenv_patch = patch("os.getenv", autospec=True) |
20 | 25 | self.mock_getenv = self.getenv_patch.start() |
| 26 | + self.addCleanup(self.getenv_patch.stop) |
| 27 | + |
| 28 | + # Set environment variable mock return values |
21 | 29 | self.mock_getenv.side_effect = lambda key, default=None: { |
22 | 30 | "CONFIG_BUCKET_NAME": self.BUCKET_NAME, |
23 | 31 | "AWS_REGION": self.AWS_REGION, |
24 | 32 | }.get(key, default) |
25 | 33 |
|
26 | | - self.mock_boto3_client.return_value = self.mock_boto3_client |
27 | | - self.mock_boto3_client.return_value.send_message = {} |
28 | | - |
29 | | - def tearDown(self): |
30 | | - patch.stopall() |
| 34 | + # Simulate logger instance and patch setLevel |
| 35 | + self.mock_logger_instance = MagicMock() |
| 36 | + self.mock_getLogger.return_value = self.mock_logger_instance |
31 | 37 |
|
32 | | - def test_os_environ(self): |
33 | | - # Test if environment variables are set correctly |
| 38 | + # Reload the module under test to apply patches |
34 | 39 | importlib.reload(clients) |
| 40 | + |
| 41 | + def test_env_variables_loaded(self): |
| 42 | + """Test that environment variables are loaded correctly""" |
35 | 43 | self.assertEqual(clients.CONFIG_BUCKET_NAME, self.BUCKET_NAME) |
36 | 44 | self.assertEqual(clients.REGION_NAME, self.AWS_REGION) |
37 | 45 |
|
38 | | - def test_boto3_client(self): |
39 | | - ''' Test boto3 client is created with correct parameters ''' |
40 | | - importlib.reload(clients) |
| 46 | + def test_boto3_client_created_for_s3(self): |
| 47 | + """Test that S3 boto3 client is created with correct region""" |
41 | 48 | self.mock_boto3_client.assert_any_call("s3", region_name=self.AWS_REGION) |
42 | 49 |
|
43 | | - def test_firehose_client(self): |
44 | | - ''' Test firehose client is created with correct parameters ''' |
45 | | - importlib.reload(clients) |
| 50 | + def test_boto3_client_created_for_firehose(self): |
| 51 | + """Test that Firehose boto3 client is created with correct region""" |
46 | 52 | self.mock_boto3_client.assert_any_call("firehose", region_name=self.AWS_REGION) |
47 | 53 |
|
48 | | - def test_logging_setup(self): |
49 | | - ''' Test logging is set up correctly ''' |
50 | | - importlib.reload(clients) |
51 | | - self.assertTrue(hasattr(clients, 'logger')) |
| 54 | + def test_logger_is_initialized(self): |
| 55 | + """Test that a logger instance is initialized""" |
| 56 | + self.mock_getLogger.assert_called_once_with() |
| 57 | + self.assertTrue(hasattr(clients, "logger")) |
52 | 58 |
|
53 | | - def test_logging_configuration(self): |
54 | | - ''' Test logging configuration ''' |
55 | | - importlib.reload(clients) |
56 | | - clients.logger.setLevel.assert_called_once_with("INFO") |
57 | | - |
58 | | - def test_logging_initialization(self): |
59 | | - ''' Test logging initialization ''' |
60 | | - importlib.reload(clients) |
61 | | - self.mock_logging.assert_called_once_with() |
62 | | - self.assertTrue(hasattr(clients, 'logger')) |
63 | | - clients.logger.setLevel.assert_any_call("INFO") |
| 59 | + def test_logger_set_level(self): |
| 60 | + """Test that logger level is set to INFO""" |
| 61 | + self.mock_logger_instance.setLevel.assert_called_once_with(logging.INFO) |
0 commit comments