def __init__(self, queue=None): self.queue = queue or list() redis = mock.MagicMock(aioredis.Redis) redis.rpush.side_effect = utils.make_coro(result=lambda key, item: self.queue.append(item)) redis.rpop.side_effect = utils.make_coro(result=lambda key: self.queue.pop()) redis.lpush.side_effect = utils.make_coro(result=lambda key, item: self.queue.insert(0, item)) redis.llen.side_effect = utils.make_coro(result=lambda key: len(self.queue)) super(MockQueue, self).__init__(redis, None)
async def test_process_posts(): posts = [utils.reddit_post()] posts_queue = MockQueue(list(map(json.dumps, posts))) messages_queue = MockQueue() with mock.patch('newsbot.queues.posts_queue') as posts_queue_patch: posts_queue_patch.side_effect = utils.make_coro(posts_queue) with mock.patch('newsbot.queues.messages_queue') as messages_queue_patch: messages_queue_patch.side_effect = utils.make_coro(messages_queue) await services.process_posts() assert not len(posts_queue.queue) assert len(messages_queue.queue)
async def test_process_messages_too_many_requests(): post = utils.reddit_post() messages = services.process_reddit_post(post) messages_queue = MockQueue([json.dumps(messages)]) telegram_session = connections.get_telegram_session() with mock.patch('newsbot.queues.messages_queue') as messages_queue_patch: messages_queue_patch.side_effect = utils.make_coro(messages_queue) with mock.patch.object(telegram_session, 'process_message') as process_message_mock: process_message_mock.side_effect = utils.make_coro(connections.TelegramTooManyRequests()) await services.process_messages() assert await messages_queue.size() == 1 assert await messages_queue.get() == messages
async def test_process_messages(): post = utils.reddit_post() messages = services.process_reddit_post(post) messages_queue = MockQueue([json.dumps(messages)]) telegram_session = connections.get_telegram_session() with mock.patch('newsbot.queues.messages_queue') as messages_queue_patch: messages_queue_patch.side_effect = utils.make_coro(messages_queue) with mock.patch.object(telegram_session, 'process_message') as process_message_mock: process_message_mock.side_effect = utils.make_coro({'ok': True}) await services.process_messages() assert not len(messages_queue.queue)
async def test_process_post_imgur(): post = utils.reddit_post(domain='imgur.com') with mock.patch('newsbot.services.process_imgur_post') as process_imgur_post_mock: process_imgur_post_mock.side_effect = utils.make_coro() messages = await services.process_post(post) process_imgur_post_mock.assert_called_with(post)
async def test_gather_posts(reddit_session): posts = [utils.reddit_post()] posts_queue = MockQueue() settings.CONFIG['subreddits'] = {'/r/test': {}} with mock.patch('newsbot.queues.posts_queue') as posts_queue_patch: posts_queue_patch.side_effect = utils.make_coro(posts_queue) with mock.patch.object(reddit_session, 'get_posts') as get_posts_mock: get_posts_mock.side_effect = utils.make_async_gen(*posts) with mock.patch('newsbot.services.filter_post') as filter_post_mock: filter_post_mock.side_effect = utils.make_coro(False) with mock.patch('newsbot.connections._reddit_session', new=reddit_session): await services.gather_posts() assert len(posts_queue.queue) == len(posts)
async def test_get_imgur_images_image(imgur_session): url = 'https://imgur.com/test' imgur_images = [{'foo': 'bar'}] with mock.patch.object(imgur_session, 'get_imgur_image') as patch: patch.side_effect = utils.make_coro(result=imgur_images[0]) images = await utils.list_async_gen(imgur_session.get_imgur_images(url) ) patch.assert_called_with('test') assert images == imgur_images