|
1 | 1 | import unittest
|
2 |
| -from unittest.mock import patch |
| 2 | +from unittest.mock import patch, MagicMock |
3 | 3 | from dice_py.command.command import Command
|
| 4 | +from dice_py.connection.client import DiceClient |
4 | 5 | import asyncio
|
5 | 6 |
|
6 | 7 |
|
7 | 8 | class TestCommand(unittest.TestCase):
|
8 | 9 |
|
| 10 | + def setUp(self): |
| 11 | + self.client = MagicMock(spec=DiceClient) |
| 12 | + self.command = Command(self.client) |
| 13 | + self.loop = asyncio.get_event_loop() |
| 14 | + |
9 | 15 | @patch("dice_py.command.executor.Executor._execute_command", return_value="OK")
|
10 |
| - @patch("dice_py.command.executor.DiceClient") |
11 |
| - def test_set(self, mock_client, mock_execute_command): |
12 |
| - command = Command(mock_client) |
13 |
| - loop = asyncio.get_event_loop() |
14 |
| - response = loop.run_until_complete(command.set("key", "value")) |
15 |
| - |
16 |
| - mock_execute_command.assert_called_once_with("SET key value") |
17 |
| - self.assertEqual(response, "OK") |
18 |
| - |
19 |
| - @patch("dice_py.command.executor.Executor._execute_command", return_value="value") |
20 |
| - @patch("dice_py.command.executor.DiceClient") |
21 |
| - def test_get(self, mock_client, mock_execute_command): |
22 |
| - command = Command(mock_client) |
23 |
| - loop = asyncio.get_event_loop() |
24 |
| - response = loop.run_until_complete(command.get("key")) |
25 |
| - |
26 |
| - mock_execute_command.assert_called_once_with("GET key") |
27 |
| - self.assertEqual(response, "value") |
| 16 | + def test_set(self, mock_execute_command): |
| 17 | + set_response = self.loop.run_until_complete( |
| 18 | + self.command.set("test_key", "test_value") |
| 19 | + ) |
| 20 | + mock_execute_command.assert_called_once_with("SET test_key test_value") |
| 21 | + self.assertEqual(set_response, "OK") |
| 22 | + |
| 23 | + @patch( |
| 24 | + "dice_py.command.executor.Executor._execute_command", return_value="test_value" |
| 25 | + ) |
| 26 | + def test_get(self, mock_execute_command): |
| 27 | + get_response = self.loop.run_until_complete(self.command.get("test_key")) |
| 28 | + mock_execute_command.assert_called_once_with("GET test_key") |
| 29 | + self.assertEqual(get_response, "test_value") |
| 30 | + |
| 31 | + @patch("dice_py.command.executor.Executor._execute_command", return_value="1") |
| 32 | + def test_delete(self, mock_execute_command): |
| 33 | + delete_response = self.loop.run_until_complete(self.command.delete("test_key")) |
| 34 | + mock_execute_command.assert_called_once_with("DEL test_key") |
| 35 | + self.assertTrue(delete_response) |
| 36 | + |
| 37 | + @patch("dice_py.command.executor.Executor._execute_command", return_value="1") |
| 38 | + def test_exists(self, mock_execute_command): |
| 39 | + exists_response = self.loop.run_until_complete(self.command.exists("test_key")) |
| 40 | + mock_execute_command.assert_called_once_with("EXISTS test_key") |
| 41 | + self.assertTrue(exists_response) |
| 42 | + |
| 43 | + @patch("dice_py.command.executor.Executor._execute_command", return_value="1") |
| 44 | + def test_expire(self, mock_execute_command): |
| 45 | + expire_response = self.loop.run_until_complete( |
| 46 | + self.command.expire("test_key", 60) |
| 47 | + ) |
| 48 | + mock_execute_command.assert_called_once_with("EXPIRE test_key 60") |
| 49 | + self.assertTrue(expire_response) |
| 50 | + |
| 51 | + @patch( |
| 52 | + "dice_py.command.executor.Executor._execute_command", |
| 53 | + return_value="test_key test_key_2", |
| 54 | + ) |
| 55 | + def test_keys(self, mock_execute_command): |
| 56 | + keys_response = self.loop.run_until_complete(self.command.keys("*")) |
| 57 | + mock_execute_command.assert_called_once_with("KEYS *") |
| 58 | + self.assertEqual(keys_response, ["test_key", "test_key_2"]) |
| 59 | + |
| 60 | + @patch( |
| 61 | + "dice_py.command.executor.Executor._execute_command", |
| 62 | + return_value="OK", |
| 63 | + ) |
| 64 | + def test_flush(self, mock_execute_command): |
| 65 | + flush_response = self.loop.run_until_complete(self.command.flush()) |
| 66 | + mock_execute_command.assert_called_once_with("FLUSHDB") |
| 67 | + self.assertTrue(flush_response) |
| 68 | + |
| 69 | + @patch("dice_py.command.executor.Executor._execute_command", return_value="11") |
| 70 | + def test_incr(self, mock_execute_command): |
| 71 | + incr_response = self.loop.run_until_complete(self.command.incr("test_key")) |
| 72 | + mock_execute_command.assert_called_once_with("INCR test_key") |
| 73 | + self.assertEqual(incr_response, 11) |
| 74 | + |
| 75 | + @patch("dice_py.command.executor.Executor._execute_command", return_value="9") |
| 76 | + def test_decr(self, mock_execute_command): |
| 77 | + decr_response = self.loop.run_until_complete(self.command.decr("test_key")) |
| 78 | + mock_execute_command.assert_called_once_with("DECR test_key") |
| 79 | + self.assertEqual(decr_response, 9) |
| 80 | + |
| 81 | + @patch("dice_py.command.executor.Executor._execute_command", return_value="11") |
| 82 | + def test_incrby(self, mock_execute_command): |
| 83 | + _ = self.loop.run_until_complete(self.command.set("test_key", 6)) |
| 84 | + incrby_response = self.loop.run_until_complete( |
| 85 | + self.command.incrby("test_key", 5) |
| 86 | + ) |
| 87 | + self.assertEqual(incrby_response, 11) |
| 88 | + |
| 89 | + @patch("dice_py.command.executor.Executor._execute_command", return_value="1") |
| 90 | + def test_decrby(self, mock_execute_command): |
| 91 | + _ = self.loop.run_until_complete(self.command.set("test_key", 6)) |
| 92 | + decrby_response = self.loop.run_until_complete( |
| 93 | + self.command.decrby("test_key", 5) |
| 94 | + ) |
| 95 | + self.assertEqual(decrby_response, 1) |
| 96 | + |
| 97 | + @patch("dice_py.command.executor.Executor._execute_command", return_value="-1") |
| 98 | + def test_ttl_not_set(self, mock_execute_command): |
| 99 | + _ = self.loop.run_until_complete(self.command.set("test_key", 10)) |
| 100 | + ttl_response = self.loop.run_until_complete(self.command.ttl("test_key")) |
| 101 | + self.assertEqual(ttl_response, -1) |
| 102 | + |
| 103 | + @patch("dice_py.command.executor.Executor._execute_command", return_value="10") |
| 104 | + def test_ttl(self, mock_execute_command): |
| 105 | + _ = self.loop.run_until_complete(self.command.set("test_key", 10)) |
| 106 | + _ = self.loop.run_until_complete(self.command.expire("test_key", 10)) |
| 107 | + ttl_response = self.loop.run_until_complete(self.command.ttl("test_key")) |
| 108 | + self.assertEqual(ttl_response, 10) |
0 commit comments