blob: a4128cecbb5699b2e6544bf3e65fbc3e332c29b1 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2020 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4"""Tests for the Redis utility module."""
5from __future__ import print_function
6from __future__ import division
7from __future__ import absolute_import
8
9import fakeredis
10import unittest
11
12from framework import redis_utils
13from proto import features_pb2
14
15
16class RedisHelperTest(unittest.TestCase):
17
18 def testFormatRedisKey(self):
19 redis_key = redis_utils.FormatRedisKey(111)
20 self.assertEqual('111', redis_key)
21 redis_key = redis_utils.FormatRedisKey(222, prefix='foo:')
22 self.assertEqual('foo:222', redis_key)
23 redis_key = redis_utils.FormatRedisKey(333, prefix='bar')
24 self.assertEqual('bar:333', redis_key)
25
26 def testCreateRedisClient(self):
27 self.assertIsNone(redis_utils.connection_pool)
28 redis_client_1 = redis_utils.CreateRedisClient()
29 self.assertIsNotNone(redis_client_1)
30 self.assertIsNotNone(redis_utils.connection_pool)
31 redis_client_2 = redis_utils.CreateRedisClient()
32 self.assertIsNotNone(redis_client_2)
33 self.assertIsNot(redis_client_1, redis_client_2)
34
35 def testConnectionVerification(self):
36 server = fakeredis.FakeServer()
37 client = None
38 self.assertFalse(redis_utils.VerifyRedisConnection(client))
39 server.connected = True
40 client = fakeredis.FakeRedis(server=server)
41 self.assertTrue(redis_utils.VerifyRedisConnection(client))
42 server.connected = False
43 self.assertFalse(redis_utils.VerifyRedisConnection(client))
44
45 def testSerializeDeserializeInt(self):
46 serialized_int = redis_utils.SerializeValue(123)
47 self.assertEqual('123', serialized_int)
48 self.assertEquals(123, redis_utils.DeserializeValue(serialized_int))
49
50 def testSerializeDeserializeStr(self):
51 serialized = redis_utils.SerializeValue('123')
52 self.assertEqual('"123"', serialized)
53 self.assertEquals('123', redis_utils.DeserializeValue(serialized))
54
55 def testSerializeDeserializePB(self):
56 features = features_pb2.Hotlist.HotlistItem(
57 issue_id=7949, rank=0, adder_id=333, date_added=1525)
58 serialized = redis_utils.SerializeValue(
59 features, pb_class=features_pb2.Hotlist.HotlistItem)
60 self.assertIsInstance(serialized, str)
61 deserialized = redis_utils.DeserializeValue(
62 serialized, pb_class=features_pb2.Hotlist.HotlistItem)
63 self.assertIsInstance(deserialized, features_pb2.Hotlist.HotlistItem)
64 self.assertEquals(deserialized, features)