def test_pop_with_reverse_and_default_block_rpops(self): redis = MagicMock() key = MagicMock() encoding = MagicMock() redis_list = RedisList(redis, key) result = redis_list.pop(reverse=True, encoding=encoding) redis.rpop.assert_called_once_with(key, encoding=encoding) self.assertEqual(result, redis.rpop.return_value)
def test_push_with_values_and_reverse_rpushes(self): redis = MagicMock() key = MagicMock() redis_list = RedisList(redis, key) values = [MagicMock() for _ in range(8)] result = redis_list.push(*values, reverse=True) redis.rpush.assert_called_once_with(key, *values) self.assertEqual(result, redis.rpush.return_value)
def test_remove_removes(self): redis = MagicMock() key = MagicMock() redis_list = RedisList(redis, key) value = MagicMock() count = MagicMock() result = redis_list.remove(value, count) redis.lrem.assert_called_once_with(key, count, value) self.assertEqual(result, redis.lrem.return_value)
def test_dequeue_with_block_and_default_timeout_brpops_with_zero_timeout( self): redis = MagicMock() key = MagicMock() encoding = MagicMock() redis_list = RedisList(redis, key) result = redis_list.dequeue(block=True, encoding=encoding) redis.brpop.assert_called_once_with(key, timeout=0, encoding=encoding) self.assertEqual(result, redis.brpop.return_value)
async def test_enumerate_with_batch_size_zero_gets_the_list_at_once(self): items = [MagicMock() for _ in range(12)] redis = AsyncMock() redis.lrange.return_value = items key = MagicMock() redis_list = RedisList(redis, key) result = [item async for item in redis_list.enumerate()] self.assertEqual(result, items) redis.lrange.assert_awaited_once_with(key, 0, -1, encoding='utf-8')
def test_requeue_with_block_default_timeout_brpoplpushes_with_default_timeout( self): redis = MagicMock() key = MagicMock() redis_list = RedisList(redis, key) result = redis_list.requeue(block=True) redis.brpoplpush.assert_called_once_with(key, key, timeout=0, encoding='utf-8') self.assertEqual(result, redis.brpoplpush.return_value)
def test_move_without_block_rpoplpushes(self): redis = MagicMock() key = MagicMock() redis_list = RedisList(redis, key) destination_key = MagicMock() encoding = MagicMock() result = redis_list.move(destination_key, block=False, encoding=encoding) redis.rpoplpush.assert_called_once_with(key, destination_key, encoding=encoding) self.assertEqual(result, redis.rpoplpush.return_value)
def test_dequeue_with_block_brpops(self): redis = MagicMock() key = MagicMock() timeout = MagicMock() encoding = MagicMock() redis_list = RedisList(redis, key) result = redis_list.dequeue(block=True, timeout_seconds=timeout, encoding=encoding) redis.brpop.assert_called_once_with(key, timeout=timeout, encoding=encoding) self.assertEqual(result, redis.brpop.return_value)
def test_get_range_works_correctly(self): redis = MagicMock() key = MagicMock() redis_list = RedisList(redis, key) start = MagicMock() stop = MagicMock() encoding = MagicMock() result = redis_list.get_range(start, stop, encoding=encoding) redis.lrange.assert_called_once_with(key, start, stop, encoding=encoding) self.assertEqual(result, redis.lrange.return_value)
async def test_enqueue_with_none_value_does_nothing(self): key = MagicMock() redis_list = RedisList(None, key) result = await redis_list.enqueue(None) self.assertIsNone(result)
async def test_push_with_none_value_reverse_does_nothing(self): key = MagicMock() redis_list = RedisList(None, key) result = await redis_list.push(None, reverse=True) self.assertIsNone(result)
async def test_enumerate_with_stop_and_no_batch_size_gets_all_at_once( self): items = [MagicMock() for _ in range(10)] redis = AsyncMock() stop = 6 redis.lrange.return_value = items[:stop + 1] key = MagicMock() encoding = MagicMock() redis_list = RedisList(redis, key) result = [ item async for item in redis_list.enumerate(stop=stop, encoding=encoding) ] self.assertEqual(result, items[:stop + 1]) redis.lrange.assert_awaited_once_with(key, 0, stop, encoding=encoding)
async def test_find_index_when_value_not_present_returns_none(self): redis = AsyncMock() redis.lrange.return_value = ['test', 'this'] key = MagicMock() redis_list = RedisList(redis, key) result = await redis_list.find_index('me') self.assertIsNone(result)
def test_move_with_block_brpoplpushes(self): redis = MagicMock() key = MagicMock() redis_list = RedisList(redis, key) destination_key = MagicMock() timeout = MagicMock() encoding = MagicMock() result = redis_list.move(destination_key, block=True, timeout_seconds=timeout, encoding=encoding) redis.brpoplpush.assert_called_once_with(key, destination_key, timeout=timeout, encoding=encoding) self.assertEqual(result, redis.brpoplpush.return_value)
async def test_enumerate_gets_correct_batches(self): items = [MagicMock() for _ in range(9)] redis = AsyncMock() redis.lrange.side_effect = lambda _, start, stop, **__: items[start: stop + 1] key = MagicMock() encoding = MagicMock() redis_list = RedisList(redis, key) result = [ item async for item in redis_list.enumerate(batch_size=5, encoding=encoding) ] self.assertEqual(result, items) redis.lrange.assert_has_awaits([ call(key, 0, 4, encoding=encoding), call(key, 5, 9, encoding=encoding) ])
async def test_find_index_with_non_zero_start_adds_start_to_index(self): redis = AsyncMock() redis.lrange.return_value = ['test', 'this', 'for', 'me'] key = MagicMock() redis_list = RedisList(redis, key) start = 5 result = await redis_list.find_index('me', start=start) self.assertEqual(result, 8)
async def test_find_index_uses_correct_defaults(self): redis = AsyncMock() redis.lrange.return_value = ['test', 'this', 'for', 'me'] key = MagicMock() redis_list = RedisList(redis, key) result = await redis_list.find_index('this') redis.lrange.assert_awaited_once_with(key, 0, -1, encoding='utf-8') self.assertEqual(result, 1)
async def test_enumerate_with_start_and_stop_returns_correct_result(self): items = [MagicMock() for _ in range(11)] redis = AsyncMock() redis.lrange.side_effect = lambda _, start, stop, **__: items[start: stop + 1] key = MagicMock() encoding = MagicMock() redis_list = RedisList(redis, key) result = [ item async for item in redis_list.enumerate( start=3, stop=9, batch_size=5, encoding=encoding) ] self.assertEqual(result, items[3:10]) redis.lrange.assert_has_awaits([ call(key, 3, 7, encoding=encoding), call(key, 8, 9, encoding=encoding) ])
async def test_find_index_with_stop_uses_stop(self): redis = AsyncMock() redis.lrange.side_effect = [['test', 'this', 'for', 'me'], ['because']] key = MagicMock() redis_list = RedisList(redis, key) start = 5 stop = 9 result = await redis_list.find_index('something', start=start, stop=stop, batch_size=4) self.assertIsNone(result)
async def test_find_index_with_stop_finds_result(self): redis = AsyncMock() redis.lrange.side_effect = [['test', 'this', 'for', 'me'], ['because', 'something', 'happened']] key = MagicMock() redis_list = RedisList(redis, key) start = 5 stop = 12 result = await redis_list.find_index('something', start=start, stop=stop, batch_size=4) self.assertEqual(result, 10)
async def test_find_index_when_value_present_returns_index(self): redis = AsyncMock() redis.lrange.side_effect = [['test', 'this'], ['for', 'me']] key = MagicMock() redis_list = RedisList(redis, key) start = 0 stop = 3 batch_size = 2 encoding = MagicMock() result = await redis_list.find_index('for', start=start, stop=stop, batch_size=batch_size, encoding=encoding) self.assertEqual(result, 2) redis.lrange.assert_has_awaits([ call(key, 0, 1, encoding=encoding), call(key, 2, 3, encoding=encoding) ])
def test_init_succeeds(self): redis_list = RedisList(MagicMock(), MagicMock()) self.assertIsInstance(redis_list, RedisList)