-
-
Notifications
You must be signed in to change notification settings - Fork 407
/
test_database_redis.py
110 lines (78 loc) · 3.39 KB
/
test_database_redis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import asyncio
import aioredis
import unittest
import asynctest
import asynctest.mock as amock
from contextlib import suppress
from opsdroid.database.redis import RedisDatabase
from opsdroid.cli.start import configure_lang
class MockRedisClient:
execute = None
class TestRedisDatabase(unittest.TestCase):
"""Test the opsdroid Redis database class."""
def setUp(self):
self.loop = asyncio.new_event_loop()
configure_lang({})
def test_init(self):
"""Test initialisation of database class.
This method will test the initialisation of the database
class. It will assert if the database class properties are
declared and equated to None.
"""
database = RedisDatabase({})
self.assertEqual(None, database.client)
self.assertEqual(0, database.database)
self.assertEqual("localhost", database.host)
self.assertEqual(6379, database.port)
self.assertEqual(None, database.password)
self.assertLogs("_LOGGER", "debug")
class TestRedisDatabaseAsync(asynctest.TestCase):
"""Test the opsdroid Redis Database class."""
async def test_connect(self):
opsdroid = amock.CoroutineMock()
database = RedisDatabase({}, opsdroid=opsdroid)
with amock.patch.object(aioredis, "create_pool") as mocked_connection:
mocked_connection.side_effect = NotImplementedError
with suppress(NotImplementedError):
await database.connect()
self.assertTrue(mocked_connection.called)
self.assertLogs("_LOGGER", "info")
async def test_connect_failure(self):
opsdroid = amock.CoroutineMock()
database = RedisDatabase({}, opsdroid=opsdroid)
with amock.patch.object(aioredis, "create_pool") as mocked_connection:
mocked_connection.side_effect = OSError()
with suppress(OSError):
await database.connect()
self.assertLogs("_LOGGER", "warning")
async def test_connect_logging(self):
opsdroid = amock.CoroutineMock()
database = RedisDatabase({}, opsdroid=opsdroid)
with amock.patch.object(aioredis, "create_pool") as mocked_connection:
mocked_connection.set_result = amock.CoroutineMock()
await database.connect()
self.assertTrue(mocked_connection.called)
self.assertLogs("_LOGGER", "info")
async def test_get(self):
db = RedisDatabase({})
db.client = MockRedisClient()
db.client.execute = amock.CoroutineMock(return_value='{"key":"value"}')
result = await db.get("string")
self.assertDictEqual(result, dict(key="value"))
async def test_get_return_None(self):
db = RedisDatabase({})
db.client = MockRedisClient()
db.client.execute = amock.CoroutineMock(return_value=None)
result = await db.get("string")
self.assertEqual(result, None)
async def test_put(self):
db = RedisDatabase({})
db.client = MockRedisClient()
db.client.execute = amock.CoroutineMock(return_value='{"key":"value"}')
result = await db.put("string", dict(key="value"))
async def test_disconnect(self):
db = RedisDatabase({})
db.client = MockRedisClient()
db.client.close = amock.CoroutineMock()
result = await db.disconnect()
self.assertTrue(db.client.close.called)