diff --git a/RedisLibrary/RedisLibraryKeywords.py b/RedisLibrary/RedisLibraryKeywords.py index e06a0f9..a137b07 100644 --- a/RedisLibrary/RedisLibraryKeywords.py +++ b/RedisLibrary/RedisLibraryKeywords.py @@ -582,3 +582,20 @@ def delete_item_from_list_redis(self, redis_conn, list_name, index, item=None): raise AssertionError redis_conn.lset(list_name, index, 'DELETE_ITEM') redis_conn.lrem(list_name, 1, 'DELETE_ITEM') + + @keyword('Disconnect from Redis') + def disconnect_from_redis(self, redis_conn): + """Securely disconnect from Redis database. + + Arguments: + - redis_conn: Redis connection object + + Examples: + | Disconnect from Redis | ${redis_conn} | + """ + try: + redis_conn.close() + logger.info("Disconnection successful") + except Exception as ex: + logger.error(f"Failed to disconnect: {ex}") + raise Exception(str(ex)) diff --git a/tests/test_RedisLibrary.py b/tests/test_RedisLibrary.py index 5da1feb..eeef15c 100644 --- a/tests/test_RedisLibrary.py +++ b/tests/test_RedisLibrary.py @@ -2,7 +2,7 @@ __author__ = 'Traitanit Huangsri' __email__ = 'traitanit.hua@gmail.com' - +from unittest.mock import MagicMock, patch from RedisLibrary import RedisLibrary import unittest, fakeredis, ast @@ -315,5 +315,10 @@ def test_delete_item_from_list_redis_item_not_matched(self): with self.assertRaises(AssertionError): self.redis.delete_item_from_list_redis(self.fake_redis, 'Country', 2, 'Spain') + def test_disconnect_from_redis(self): + self.fake_redis.close = MagicMock() + self.assertIsNone(self.redis.disconnect_from_redis(self.fake_redis)) + self.fake_redis.close.assert_called_once() + def tearDown(self): self.fake_redis.flushall()