Wei-Yu Chen | ad55cb8 | 2022-02-15 20:07:01 +0800 | [diff] [blame^] | 1 | # SPDX-FileCopyrightText: 2020 The Magma Authors. |
| 2 | # SPDX-FileCopyrightText: 2022 Open Networking Foundation <support@opennetworking.org> |
| 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 5 | |
Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame] | 6 | from unittest import TestCase, main |
| 7 | |
| 8 | import fakeredis |
| 9 | from common.redis.containers import RedisFlatDict, RedisHashDict |
| 10 | from common.redis.serializers import ( |
| 11 | RedisSerde, |
| 12 | get_proto_deserializer, |
| 13 | get_proto_serializer, |
| 14 | ) |
| 15 | from orc8r.protos.service303_pb2 import LogVerbosity |
| 16 | |
| 17 | |
| 18 | class RedisDictTests(TestCase): |
| 19 | """ |
| 20 | Tests for the RedisHashDict and RedisFlatDict containers |
| 21 | """ |
| 22 | |
| 23 | def setUp(self): |
| 24 | client = fakeredis.FakeStrictRedis() |
| 25 | # Use arbitrary orc8r proto to test with |
| 26 | self._hash_dict = RedisHashDict( |
| 27 | client, |
| 28 | "unittest", |
| 29 | get_proto_serializer(), |
| 30 | get_proto_deserializer(LogVerbosity), |
| 31 | ) |
| 32 | |
| 33 | serde = RedisSerde( |
| 34 | 'log_verbosity', |
| 35 | get_proto_serializer(), |
| 36 | get_proto_deserializer(LogVerbosity), |
| 37 | ) |
| 38 | self._flat_dict = RedisFlatDict(client, serde) |
| 39 | |
| 40 | def test_hash_insert(self): |
| 41 | expected = LogVerbosity(verbosity=0) |
| 42 | expected2 = LogVerbosity(verbosity=1) |
| 43 | |
| 44 | # insert proto |
| 45 | self._hash_dict['key1'] = expected |
| 46 | version = self._hash_dict.get_version("key1") |
| 47 | actual = self._hash_dict['key1'] |
| 48 | self.assertEqual(1, version) |
| 49 | self.assertEqual(expected, actual) |
| 50 | |
| 51 | # update proto |
| 52 | self._hash_dict['key1'] = expected2 |
| 53 | version2 = self._hash_dict.get_version("key1") |
| 54 | actual2 = self._hash_dict['key1'] |
| 55 | self.assertEqual(2, version2) |
| 56 | self.assertEqual(expected2, actual2) |
| 57 | |
| 58 | def test_missing_version(self): |
| 59 | missing_version = self._hash_dict.get_version("key2") |
| 60 | self.assertEqual(0, missing_version) |
| 61 | |
| 62 | def test_hash_delete(self): |
| 63 | expected = LogVerbosity(verbosity=2) |
| 64 | self._hash_dict['key3'] = expected |
| 65 | |
| 66 | actual = self._hash_dict['key3'] |
| 67 | self.assertEqual(expected, actual) |
| 68 | |
| 69 | self._hash_dict.pop('key3') |
| 70 | self.assertRaises(KeyError, self._hash_dict.__getitem__, 'key3') |
| 71 | |
| 72 | def test_flat_insert(self): |
| 73 | expected = LogVerbosity(verbosity=5) |
| 74 | expected2 = LogVerbosity(verbosity=1) |
| 75 | |
| 76 | # insert proto |
| 77 | self._flat_dict['key1'] = expected |
| 78 | version = self._flat_dict.get_version("key1") |
| 79 | actual = self._flat_dict['key1'] |
| 80 | self.assertEqual(1, version) |
| 81 | self.assertEqual(expected, actual) |
| 82 | |
| 83 | # update proto |
| 84 | self._flat_dict["key1"] = expected2 |
| 85 | version2 = self._flat_dict.get_version("key1") |
| 86 | actual2 = self._flat_dict["key1"] |
| 87 | actual3 = self._flat_dict.get("key1") |
| 88 | self.assertEqual(2, version2) |
| 89 | self.assertEqual(expected2, actual2) |
| 90 | self.assertEqual(expected2, actual3) |
| 91 | |
| 92 | def test_flat_missing_version(self): |
| 93 | missing_version = self._flat_dict.get_version("key2") |
| 94 | self.assertEqual(0, missing_version) |
| 95 | |
| 96 | def test_flat_bad_key(self): |
| 97 | expected = LogVerbosity(verbosity=2) |
| 98 | self.assertRaises( |
| 99 | ValueError, self._flat_dict.__setitem__, |
| 100 | 'bad:key', expected, |
| 101 | ) |
| 102 | self.assertRaises( |
| 103 | ValueError, self._flat_dict.__getitem__, |
| 104 | 'bad:key', |
| 105 | ) |
| 106 | self.assertRaises( |
| 107 | ValueError, self._flat_dict.__delitem__, |
| 108 | 'bad:key', |
| 109 | ) |
| 110 | |
| 111 | def test_flat_delete(self): |
| 112 | expected = LogVerbosity(verbosity=2) |
| 113 | self._flat_dict['key3'] = expected |
| 114 | |
| 115 | actual = self._flat_dict['key3'] |
| 116 | self.assertEqual(expected, actual) |
| 117 | |
| 118 | del self._flat_dict['key3'] |
| 119 | self.assertRaises( |
| 120 | KeyError, self._flat_dict.__getitem__, |
| 121 | 'key3', |
| 122 | ) |
| 123 | self.assertEqual(None, self._flat_dict.get('key3')) |
| 124 | |
| 125 | def test_flat_clear(self): |
| 126 | expected = LogVerbosity(verbosity=2) |
| 127 | self._flat_dict['key3'] = expected |
| 128 | |
| 129 | actual = self._flat_dict['key3'] |
| 130 | self.assertEqual(expected, actual) |
| 131 | |
| 132 | self._flat_dict.clear() |
| 133 | self.assertEqual(0, len(self._flat_dict.keys())) |
| 134 | |
| 135 | def test_flat_garbage_methods(self): |
| 136 | expected = LogVerbosity(verbosity=2) |
| 137 | expected2 = LogVerbosity(verbosity=3) |
| 138 | |
| 139 | key = "k1" |
| 140 | key2 = "k2" |
| 141 | bad_key = "bad_key" |
| 142 | self._flat_dict[key] = expected |
| 143 | self._flat_dict[key2] = expected2 |
| 144 | |
| 145 | self._flat_dict.mark_as_garbage(key) |
| 146 | is_garbage = self._flat_dict.is_garbage(key) |
| 147 | self.assertTrue(is_garbage) |
| 148 | is_garbage2 = self._flat_dict.is_garbage(key2) |
| 149 | self.assertFalse(is_garbage2) |
| 150 | |
| 151 | self.assertEqual([key], self._flat_dict.garbage_keys()) |
| 152 | self.assertEqual([key2], self._flat_dict.keys()) |
| 153 | |
| 154 | self.assertIsNone(self._flat_dict.get(key)) |
| 155 | self.assertEqual(expected2, self._flat_dict.get(key2)) |
| 156 | |
| 157 | deleted = self._flat_dict.delete_garbage(key) |
| 158 | not_deleted = self._flat_dict.delete_garbage(key2) |
| 159 | self.assertTrue(deleted) |
| 160 | self.assertFalse(not_deleted) |
| 161 | |
| 162 | self.assertIsNone(self._flat_dict.get(key)) |
| 163 | self.assertEqual(expected2, self._flat_dict.get(key2)) |
| 164 | |
| 165 | with self.assertRaises(KeyError): |
| 166 | self._flat_dict.is_garbage(bad_key) |
| 167 | with self.assertRaises(KeyError): |
| 168 | self._flat_dict.mark_as_garbage(bad_key) |
| 169 | |
| 170 | |
| 171 | if __name__ == "__main__": |
| 172 | main() |