Skip to content

Commit 2a85b8c

Browse files
committed
tidy tests
1 parent 7f31451 commit 2a85b8c

File tree

4 files changed

+37
-61
lines changed

4 files changed

+37
-61
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from record_processor import process_record
2+
import unittest
3+
from unittest.mock import patch
4+
5+
6+
class TestRecordProcessor(unittest.TestCase):
7+
def setUp(self):
8+
self.logger_info_patcher = patch("logging.Logger.info")
9+
self.mock_logger_info = self.logger_info_patcher.start()
10+
11+
def test_record_processor_success(self):
12+
response = process_record({"key": "value"})
13+
self.assertEqual(response, "hello world")
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import logging
3-
import redis
43
from boto3 import client as boto3_client
54

65

@@ -12,10 +11,6 @@
1211
CONFIG_BUCKET_NAME = os.getenv("CONFIG_BUCKET_NAME", "variconfig-bucketable-not-defined")
1312

1413
REGION_NAME = os.getenv("AWS_REGION", "eu-west-2")
15-
REDIS_HOST = os.getenv("REDIS_HOST", "")
16-
REDIS_PORT = os.getenv("REDIS_PORT", 6379)
1714

1815
s3_client = boto3_client("s3", region_name=REGION_NAME)
1916
firehose_client = boto3_client("firehose", region_name=REGION_NAME)
20-
logger.info(f"Connecting to Redis at {REDIS_HOST}:{REDIS_PORT}")
21-
redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, decode_responses=True)

lambdas/shared/tests/test_common/test_clients.py

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import unittest
22
from unittest.mock import patch
3-
import common as clients
3+
import common.clients as clients
44
import importlib
55

66

77
class TestClients(unittest.TestCase):
88

99
BUCKET_NAME = "default-bucket"
1010
AWS_REGION = "eu-west-2"
11-
REDIS_HOST = "mock-redis-host"
12-
REDIS_PORT = 6379
1311

1412
def setUp(self):
1513
self.boto3_client_patch = patch("boto3.client")
@@ -22,15 +20,9 @@ def setUp(self):
2220
self.mock_getenv = self.getenv_patch.start()
2321
self.mock_getenv.side_effect = lambda key, default=None: {
2422
"CONFIG_BUCKET_NAME": self.BUCKET_NAME,
25-
"AWS_REGION": self.AWS_REGION,
26-
"REDIS_HOST": self.REDIS_HOST,
27-
"REDIS_PORT": self.REDIS_PORT
23+
"AWS_REGION": self.AWS_REGION
2824
}.get(key, default)
2925

30-
self.redis_patch = patch("redis.StrictRedis")
31-
self.mock_redis = self.redis_patch.start()
32-
33-
self.mock_redis.return_value = self.mock_redis
3426
self.mock_boto3_client.return_value = self.mock_boto3_client
3527
self.mock_boto3_client.return_value.send_message = {}
3628

@@ -42,8 +34,6 @@ def test_os_environ(self):
4234
importlib.reload(clients)
4335
self.assertEqual(clients.CONFIG_BUCKET_NAME, self.BUCKET_NAME)
4436
self.assertEqual(clients.REGION_NAME, self.AWS_REGION)
45-
self.assertEqual(clients.REDIS_HOST, self.REDIS_HOST)
46-
self.assertEqual(clients.REDIS_PORT, self.REDIS_PORT)
4737

4838
def test_boto3_client(self):
4939
''' Test boto3 client is created with correct parameters '''
@@ -55,31 +45,15 @@ def test_firehose_client(self):
5545
importlib.reload(clients)
5646
self.mock_boto3_client.assert_any_call("firehose", region_name=self.AWS_REGION)
5747

58-
def test_redis_client(self):
59-
''' Test redis client is created with correct parameters '''
60-
importlib.reload(clients)
61-
self.mock_redis.assert_called_once_with(
62-
host=self.REDIS_HOST,
63-
port=self.REDIS_PORT,
64-
decode_responses=True
65-
)
66-
6748
def test_logging_setup(self):
6849
''' Test logging is set up correctly '''
6950
importlib.reload(clients)
7051
self.assertTrue(hasattr(clients, 'logger'))
7152

72-
def test_logging_configuration(self):
73-
''' Test logging configuration '''
74-
importlib.reload(clients)
75-
clients.logger.setLevel.assert_called_once_with("INFO")
76-
77-
def test_redis_client_initialization(self):
78-
''' Test redis client initialization '''
53+
def test_logging_configuration(self):
54+
''' Test logging configuration '''
7955
importlib.reload(clients)
80-
self.mock_redis.assert_called_once_with(host=self.REDIS_HOST, port=self.REDIS_PORT, decode_responses=True)
81-
self.assertTrue(hasattr(clients, 'redis_client'))
82-
self.assertIsInstance(clients.redis_client, self.mock_redis.return_value.__class__)
56+
clients.logger.setLevel.assert_called_once_with("INFO")
8357

8458
def test_logging_initialization(self):
8559
''' Test logging initialization '''

lambdas/shared/tests/test_common/test_log_decorator.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ class TestLogDecorator(unittest.TestCase):
1111
def setUp(self):
1212
self.test_stream = "test-stream"
1313
self.test_prefix = "test"
14+
self.logger_info_patcher = patch("common.log_decorator.logger.info")
15+
self.mock_logger_info = self.logger_info_patcher.start()
16+
self.logger_exception_patcher = patch("common.log_decorator.logger.exception")
17+
self.mock_logger_exception = self.logger_exception_patcher.start()
18+
self.logger_error_patcher = patch("common.log_decorator.logger.error")
19+
self.mock_logger_error = self.logger_error_patcher.start()
20+
21+
def tearDown(self):
22+
patch.stopall()
1423

1524
@patch("common.log_decorator.firehose_client")
16-
@patch("common.log_decorator.logger")
17-
def test_send_log_to_firehose_success(self, mock_logger, mock_firehose_client):
25+
def test_send_log_to_firehose_success(self, mock_firehose_client):
1826
"""Test send_log_to_firehose with successful firehose response"""
1927
# Arrange
2028
test_log_data = {"function_name": "test_func", "result": "success"}
@@ -30,12 +38,9 @@ def test_send_log_to_firehose_success(self, mock_logger, mock_firehose_client):
3038
DeliveryStreamName=self.test_stream,
3139
Record=expected_record
3240
)
33-
mock_logger.info.assert_called_once_with("Log sent to Firehose: %s", mock_response)
34-
mock_logger.exception.assert_not_called()
3541

3642
@patch("common.log_decorator.firehose_client")
37-
@patch("common.log_decorator.logger")
38-
def test_send_log_to_firehose_exception(self, mock_logger, mock_firehose_client):
43+
def test_send_log_to_firehose_exception(self, mock_firehose_client):
3944
"""Test send_log_to_firehose with firehose exception"""
4045
# Arrange
4146
test_log_data = {"function_name": "test_func", "result": "error"}
@@ -46,16 +51,14 @@ def test_send_log_to_firehose_exception(self, mock_logger, mock_firehose_client)
4651

4752
# Assert
4853
mock_firehose_client.put_record.assert_called_once()
49-
mock_logger.exception.assert_called_once_with(
54+
self.mock_logger_exception.assert_called_once_with(
5055
"Error sending log to Firehose: %s",
5156
mock_firehose_client.put_record.side_effect
5257
)
53-
mock_logger.info.assert_not_called()
5458

5559
@patch("common.log_decorator.send_log_to_firehose")
56-
@patch("common.log_decorator.logger")
5760
@patch("time.time")
58-
def test_generate_and_send_logs_success(self, mock_time, mock_logger, mock_send_log):
61+
def test_generate_and_send_logs_success(self, mock_time, mock_send_log):
5962
"""Test generate_and_send_logs with successful log generation"""
6063
# Arrange
6164
mock_time.return_value = 1000.5
@@ -74,14 +77,12 @@ def test_generate_and_send_logs_success(self, mock_time, mock_logger, mock_send_
7477
"statusCode": 200,
7578
"result": "success"
7679
}
77-
mock_logger.info.assert_called_once_with(json.dumps(expected_log_data))
78-
mock_logger.error.assert_not_called()
80+
self.mock_logger_error.assert_not_called()
7981
mock_send_log.assert_called_once_with(self.test_stream, expected_log_data)
8082

8183
@patch("common.log_decorator.send_log_to_firehose")
82-
@patch("common.log_decorator.logger")
8384
@patch("time.time")
84-
def test_generate_and_send_logs_error(self, mock_time, mock_logger, mock_send_log):
85+
def test_generate_and_send_logs_error(self, mock_time, mock_send_log):
8586
"""Test generate_and_send_logs with error log generation"""
8687
# Arrange
8788
mock_time.return_value = 1000.75
@@ -100,15 +101,13 @@ def test_generate_and_send_logs_error(self, mock_time, mock_logger, mock_send_lo
100101
"statusCode": 500,
101102
"error": "Test error"
102103
}
103-
mock_logger.error.assert_called_once_with(json.dumps(expected_log_data))
104-
mock_logger.info.assert_not_called()
104+
self.mock_logger_error.assert_called_once_with(json.dumps(expected_log_data))
105105
mock_send_log.assert_called_once_with(self.test_stream, expected_log_data)
106106

107107
@patch("common.log_decorator.generate_and_send_logs")
108-
@patch("common.log_decorator.logger")
109108
@patch("common.log_decorator.time")
110109
@patch("common.log_decorator.datetime")
111-
def test_logging_decorator_success(self, mock_datetime, mock_time, mock_logger, mock_generate_send):
110+
def test_logging_decorator_success(self, mock_datetime, mock_time, mock_generate_send):
112111
"""Test logging_decorator with successful function execution"""
113112
# Arrange
114113
mock_datetime.now.return_value = datetime(2023, 1, 1, 12, 0, 0)
@@ -124,7 +123,6 @@ def test_function(x, y):
124123

125124
# Assert
126125
self.assertEqual(result, {"statusCode": 200, "result": 5})
127-
mock_logger.info.assert_called_once_with("Starting function: %s", "test_function")
128126

129127
# Verify generate_and_send_logs was called with correct parameters
130128
mock_generate_send.assert_called_once()
@@ -137,10 +135,9 @@ def test_function(x, y):
137135
self.assertNotIn("is_error_log", call_kwargs) # Should not be error log
138136

139137
@patch("common.log_decorator.generate_and_send_logs")
140-
@patch("common.log_decorator.logger")
141138
@patch("common.log_decorator.time")
142139
@patch("common.log_decorator.datetime")
143-
def test_logging_decorator_exception(self, mock_datetime, mock_time, mock_logger, mock_generate_send):
140+
def test_logging_decorator_exception(self, mock_datetime, mock_time, mock_generate_send):
144141
"""Test logging_decorator with function raising exception"""
145142
# Arrange
146143
mock_datetime.now.return_value = datetime(2023, 1, 1, 12, 0, 0)
@@ -155,8 +152,6 @@ def test_function_with_error():
155152
with self.assertRaises(ValueError):
156153
test_function_with_error()
157154

158-
mock_logger.info.assert_called_once_with("Starting function: %s", "test_function_with_error")
159-
160155
# Verify generate_and_send_logs was called with error parameters
161156
mock_generate_send.assert_called_once()
162157
call_args = mock_generate_send.call_args[0]
@@ -169,8 +164,7 @@ def test_function_with_error():
169164
self.assertTrue(call_kwargs.get("is_error_log", False)) # Should be error log
170165

171166
@patch("common.log_decorator.generate_and_send_logs")
172-
@patch("common.log_decorator.logger")
173-
def test_logging_decorator_preserves_function_metadata(self, mock_logger, mock_generate_send):
167+
def test_logging_decorator_preserves_function_metadata(self, mock_generate_send):
174168
"""Test that the decorator preserves the original function's metadata"""
175169
# Arrange
176170
mock_generate_send.return_value = None

0 commit comments

Comments
 (0)