Beispiel #1
0
class CacheInterfacesTests(TestCase):
    def setUp(self):
        self._segment_changes_file_name = join(dirname(__file__), 'segmentChanges.json')
        self._split_changes_file_name = join(dirname(__file__), 'splitChanges.json')

        self._redis = get_redis({'redisPrefix': 'test'})
        self._redis_split_cache = RedisSplitCache(self._redis)
        self._redis_segment_cache = RedisSegmentCache(self._redis)

    def test_split_cache_interface(self):

        with open(self._split_changes_file_name) as f:
            self._json = load(f)
            split_definition = self._json['splits'][0]
            split_name = split_definition['name']

        #Add and get Split
        self._redis_split_cache.add_split(split_name, split_definition)
        self.assertEqual(split_definition['name'], self._redis_split_cache.get_split(split_name).name)
        self.assertEqual(split_definition['killed'], self._redis_split_cache.get_split(split_name).killed)
        self.assertEqual(split_definition['seed'], self._redis_split_cache.get_split(split_name).seed)

        #Remove Split
        self._redis_split_cache.remove_split(split_name)
        self.assertIsNone(self._redis_split_cache.get_split(split_name))

        #Change Number
        self._redis_split_cache.set_change_number(1212)
        self.assertEqual(1212, self._redis_split_cache.get_change_number())
class RedisSplitCacheTests(TestCase, MockUtilsMixin):
    def setUp(self):
        self.decode_mock = self.patch('splitio.redis_support.decode')
        self.encode_mock = self.patch('splitio.redis_support.encode')
        self.some_split_name = mock.MagicMock()
        self.some_split_name_str = 'some_split_name'
        self.some_split = mock.MagicMock()
        self.some_change_number = mock.MagicMock()
        self.some_redis = mock.MagicMock()
        self.a_split_cache = RedisSplitCache(self.some_redis)

    def test_set_change_number_sets_change_number_key(self):
        """Test that set_change_number sets the change number key"""
        self.a_split_cache.set_change_number(self.some_change_number)
        self.some_redis.set.assert_called_once_with('SPLITIO.splits.till',
                                                    self.some_change_number,
                                                    None)

    def test_get_change_number_gets_segment_change_number_key(self):
        """Test that get_change_number gets the change number key"""
        self.some_redis.get.return_value = '1234'
        result = self.a_split_cache.get_change_number()
        self.assertEqual(int(self.some_redis.get.return_value), result)
        self.assertIsInstance(result, int)
        self.some_redis.get.assert_called_once_with('SPLITIO.splits.till')

    def test_get_change_number_returns_default_value_if_not_set(self):
        """Test that get_change_number returns -1 if the value is not set"""
        self.some_redis.get.return_value = None
        self.assertEqual(-1, self.a_split_cache.get_change_number())

    def test_add_split_sets_split_key_with_pickled_split(self):
        """Test that add_split sets the split key with pickled split"""
        self.a_split_cache.add_split(self.some_split_name_str, self.some_split)
        self.encode_mock.assert_called_once_with(self.some_split)
        self.some_redis.set.assert_called_once_with(
            'SPLITIO.split.some_split_name', self.encode_mock.return_value)

    def test_get_split_returns_none_if_not_cached(self):
        """Test that if a split is not cached get_split returns None"""
        self.some_redis.get.return_value = None
        self.assertEqual(
            None, self.a_split_cache.get_split(self.some_split_name_str))
        self.some_redis.get.assert_called_once_with(
            'SPLITIO.split.some_split_name')

    def test_remove_split_deletes_split_key(self):
        """Test that remove_split deletes the split key"""
        self.a_split_cache.remove_split(self.some_split_name_str)
        self.some_redis.delete.assert_called_once_with(
            'SPLITIO.split.some_split_name')