forked from EvandroLG/serialized-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
79 lines (57 loc) · 2.87 KB
/
test.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
from unittest import TestCase, mock
from serialized_redis import SerializedRedis
import redis
import pickle
class TestSerializedRedis(TestCase):
def setUp(self):
self.redis = SerializedRedis()
def tearDown(self):
self.redis.delete('mimi')
def verify_result(self, input_value, expected_value):
self.redis.set('mimi', input_value)
self.assertEqual(self.redis.get('mimi'), expected_value)
@mock.patch('pickle.dumps')
def test_set_method_should_call_method_dumps_from_pickle_with_correct_parameter(self, *args):
self.redis.set('mimi', [1, 2, 3])
pickle.dumps.assert_any_call([1, 2, 3])
self.assertTrue(pickle.dumps.call_count == 1)
@mock.patch('pickle.dumps')
def test_rpushx_method_should_call_method_dumps_from_pickle_with_correct_parameter(self, *args):
self.redis.rpushx('mimi', [1, 2, 3])
pickle.dumps.assert_any_call([1, 2, 3])
self.assertTrue(pickle.dumps.call_count == 1)
@mock.patch('redis.Redis.get')
@mock.patch('pickle.loads')
def test_get_method_should_call_method_loads_from_pickle(self, *args):
self.redis.set('mimi', [1, 2, 3])
self.redis.get('mimi')
self.assertTrue(pickle.loads.call_count == 1)
@mock.patch('pickle.dumps')
def test_hset_method_should_call_method_loads_From_pickle(self, *args):
self.redis.hset('mimi', 'meme', [1, 2, 3])
self.assertTrue(pickle.dumps.call_count == 1)
def test_hget_method_should_return_a_list_when_hget_is_walled(self):
self.redis.hset('mimi', 'meme', [1, 2, 3])
result = self.redis.hget('mimi', 'meme')
self.assertEqual(result, [1, 2, 3])
def test_get_method_should_return_a_list_serialized(self):
self.verify_result([1, 2, 3], [1, 2, 3])
def test_get_method_should_return_a_dict_seralized(self):
self.verify_result({'name': 'Evandro'}, {'name': 'Evandro'})
def test_get_method_should_return_a_string_as_a_byte(self):
self.verify_result('Evandro', b'Evandro')
def test_get_method_should_return_a_number_as_a_byte(self):
self.verify_result(1234, b'1234')
def verify_lrange_method(self, key, first_datas, last_datas, instance):
self.redis.rpush(key, first_datas)
self.redis.rpush(key, last_datas)
result = self.redis.lrange(key, 0, 1)
is_type_valid = isinstance(result[0], instance)
expected_size = len(result)
self.assertTrue(is_type_valid)
self.assertEqual(expected_size, 2)
self.assertEqual(result, [ first_datas, last_datas ])
def test_lrange_method_should_return_two_serialized_list(self):
self.verify_lrange_method('animals', ['dog', 'cat'], ['elephant', 'alligator'], list)
def test_lrange_method_should_return_two_serialized_dict(self):
self.verify_lrange_method('people', { 'name': 'Evandro' }, { 'name': 'Carmen' }, dict)