Skip to content

Commit cb2fa1c

Browse files
committed
clients test
1 parent f7e0489 commit cb2fa1c

2 files changed

Lines changed: 91 additions & 8 deletions

File tree

redis_sync/src/clients.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
import os
22
import logging
33
import redis
4-
# from redis_cacher import RedisCacher
54
from boto3 import client as boto3_client
65

7-
# Logger
86
logging.basicConfig(level="INFO")
97
logger = logging.getLogger()
108
logger.setLevel("INFO")
119

1210
CONFIG_BUCKET_NAME = os.getenv("CONFIG_BUCKET_NAME", "variable-not-defined")
1311
REGION_NAME = os.getenv("AWS_REGION", "eu-west-2")
14-
REDIS_HOST = os.getenv("REDIS_HOST", "immunisation-redis-cluster.0y9mwl.0001.euw2.cache.amazonaws.com")
12+
REDIS_HOST = os.getenv("REDIS_HOST", "")
1513
REDIS_PORT = os.getenv("REDIS_PORT", 6379)
1614

1715
s3_client = boto3_client("s3", region_name=REGION_NAME)
1816
logger.info(f"Connecting to Redis at {REDIS_HOST}:{REDIS_PORT}")
1917
redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, decode_responses=True)
20-
21-
# logger.info("Creating RedisCacher instance.")
22-
# redis_cacher = RedisCacher(REDIS_HOST, REDIS_PORT, logger)
23-
24-
# disease_vaccine_mapping = DiseaseMapping(redis_cacher)

redis_sync/tests/test_clients.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)