blob: e9508bc9485fbe378a5e127546cb6cbd31aff960 [file] [log] [blame]
Wei-Yu Chen49950b92021-11-08 19:19:18 +08001"""
2Copyright 2020 The Magma Authors.
3
4This source code is licensed under the BSD-style license found in the
5LICENSE file in the root directory of this source tree.
6
7Unless required by applicable law or agreed to in writing, software
8distributed under the License is distributed on an "AS IS" BASIS,
9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10See the License for the specific language governing permissions and
11limitations under the License.
12"""
13from unittest import TestCase, main
14
15import fakeredis
16from common.redis.containers import RedisFlatDict, RedisHashDict
17from common.redis.serializers import (
18 RedisSerde,
19 get_proto_deserializer,
20 get_proto_serializer,
21)
22from orc8r.protos.service303_pb2 import LogVerbosity
23
24
25class RedisDictTests(TestCase):
26 """
27 Tests for the RedisHashDict and RedisFlatDict containers
28 """
29
30 def setUp(self):
31 client = fakeredis.FakeStrictRedis()
32 # Use arbitrary orc8r proto to test with
33 self._hash_dict = RedisHashDict(
34 client,
35 "unittest",
36 get_proto_serializer(),
37 get_proto_deserializer(LogVerbosity),
38 )
39
40 serde = RedisSerde(
41 'log_verbosity',
42 get_proto_serializer(),
43 get_proto_deserializer(LogVerbosity),
44 )
45 self._flat_dict = RedisFlatDict(client, serde)
46
47 def test_hash_insert(self):
48 expected = LogVerbosity(verbosity=0)
49 expected2 = LogVerbosity(verbosity=1)
50
51 # insert proto
52 self._hash_dict['key1'] = expected
53 version = self._hash_dict.get_version("key1")
54 actual = self._hash_dict['key1']
55 self.assertEqual(1, version)
56 self.assertEqual(expected, actual)
57
58 # update proto
59 self._hash_dict['key1'] = expected2
60 version2 = self._hash_dict.get_version("key1")
61 actual2 = self._hash_dict['key1']
62 self.assertEqual(2, version2)
63 self.assertEqual(expected2, actual2)
64
65 def test_missing_version(self):
66 missing_version = self._hash_dict.get_version("key2")
67 self.assertEqual(0, missing_version)
68
69 def test_hash_delete(self):
70 expected = LogVerbosity(verbosity=2)
71 self._hash_dict['key3'] = expected
72
73 actual = self._hash_dict['key3']
74 self.assertEqual(expected, actual)
75
76 self._hash_dict.pop('key3')
77 self.assertRaises(KeyError, self._hash_dict.__getitem__, 'key3')
78
79 def test_flat_insert(self):
80 expected = LogVerbosity(verbosity=5)
81 expected2 = LogVerbosity(verbosity=1)
82
83 # insert proto
84 self._flat_dict['key1'] = expected
85 version = self._flat_dict.get_version("key1")
86 actual = self._flat_dict['key1']
87 self.assertEqual(1, version)
88 self.assertEqual(expected, actual)
89
90 # update proto
91 self._flat_dict["key1"] = expected2
92 version2 = self._flat_dict.get_version("key1")
93 actual2 = self._flat_dict["key1"]
94 actual3 = self._flat_dict.get("key1")
95 self.assertEqual(2, version2)
96 self.assertEqual(expected2, actual2)
97 self.assertEqual(expected2, actual3)
98
99 def test_flat_missing_version(self):
100 missing_version = self._flat_dict.get_version("key2")
101 self.assertEqual(0, missing_version)
102
103 def test_flat_bad_key(self):
104 expected = LogVerbosity(verbosity=2)
105 self.assertRaises(
106 ValueError, self._flat_dict.__setitem__,
107 'bad:key', expected,
108 )
109 self.assertRaises(
110 ValueError, self._flat_dict.__getitem__,
111 'bad:key',
112 )
113 self.assertRaises(
114 ValueError, self._flat_dict.__delitem__,
115 'bad:key',
116 )
117
118 def test_flat_delete(self):
119 expected = LogVerbosity(verbosity=2)
120 self._flat_dict['key3'] = expected
121
122 actual = self._flat_dict['key3']
123 self.assertEqual(expected, actual)
124
125 del self._flat_dict['key3']
126 self.assertRaises(
127 KeyError, self._flat_dict.__getitem__,
128 'key3',
129 )
130 self.assertEqual(None, self._flat_dict.get('key3'))
131
132 def test_flat_clear(self):
133 expected = LogVerbosity(verbosity=2)
134 self._flat_dict['key3'] = expected
135
136 actual = self._flat_dict['key3']
137 self.assertEqual(expected, actual)
138
139 self._flat_dict.clear()
140 self.assertEqual(0, len(self._flat_dict.keys()))
141
142 def test_flat_garbage_methods(self):
143 expected = LogVerbosity(verbosity=2)
144 expected2 = LogVerbosity(verbosity=3)
145
146 key = "k1"
147 key2 = "k2"
148 bad_key = "bad_key"
149 self._flat_dict[key] = expected
150 self._flat_dict[key2] = expected2
151
152 self._flat_dict.mark_as_garbage(key)
153 is_garbage = self._flat_dict.is_garbage(key)
154 self.assertTrue(is_garbage)
155 is_garbage2 = self._flat_dict.is_garbage(key2)
156 self.assertFalse(is_garbage2)
157
158 self.assertEqual([key], self._flat_dict.garbage_keys())
159 self.assertEqual([key2], self._flat_dict.keys())
160
161 self.assertIsNone(self._flat_dict.get(key))
162 self.assertEqual(expected2, self._flat_dict.get(key2))
163
164 deleted = self._flat_dict.delete_garbage(key)
165 not_deleted = self._flat_dict.delete_garbage(key2)
166 self.assertTrue(deleted)
167 self.assertFalse(not_deleted)
168
169 self.assertIsNone(self._flat_dict.get(key))
170 self.assertEqual(expected2, self._flat_dict.get(key2))
171
172 with self.assertRaises(KeyError):
173 self._flat_dict.is_garbage(bad_key)
174 with self.assertRaises(KeyError):
175 self._flat_dict.mark_as_garbage(bad_key)
176
177
178if __name__ == "__main__":
179 main()