def test_execute_images_task(test_app, clean_db, mock_session_ctx, session_object_mock, mocker, set_side_effect, target, side_effect, side_effect_target, task_status, image_status, no_img): load_fixture_file('ImageTask__01.json') tasks = models.ImageTask.objects.all() task = tasks[0] _html_images = get_image_dicts(exclude=('status', 'storage_url', 'tasks')) mocker.patch.object(models.utils, 'get_images_from_html', return_value=_html_images) mocker.patch.object(models.utils, 'write_to_storage') mocker.patch.object(models.utils, 'get_media_path', return_value='/media') set_side_effect(locals(), target, side_effect_target, side_effect) raises = side_effect and side_effect is not FileExistsError if raises: with pytest.raises((side_effect, models.TaskException)): execute_images_task(task.to_json()) else: execute_images_task(task.to_json()) task.reload() assert task.status == task_status assert len(task.images) == no_img for img in task.images: assert img.status == image_status
def test_task_get_images(test_app, clean_db, mocker, session_object_mock, set_side_effect, target, side_effect, side_effect_target, status): load_fixture_file('ImageTask__01.json') task = models.ImageTask.objects.first() _html_images = get_image_dicts(exclude=('status', 'storage_url', 'tasks')) mocker.patch.object(models.utils, 'get_images_from_html', return_value=_html_images) set_side_effect(locals(), target, side_effect_target, side_effect) if side_effect: with pytest.raises(side_effect): run_with_asyncio(task.get_images)(session_object_mock) else: run_with_asyncio(task.get_images)(session_object_mock) task.reload() if side_effect: assert len(task.images) == 0 else: assert len(task.images) == len(_html_images) assert set(img.src for img in task.images) == set(img['src'] for img in _html_images)
def test_task_download_images(test_app, clean_db, session_object_mock, mocker, set_side_effect, target, side_effect, side_effect_target, task_status, image_status): load_fixture_file('ImageTask__01.json') load_fixture_file('Image__01.json') task = models.ImageTask.objects.first() images = models.Image.objects.all() initial_statuses = {img.id: img.status for img in images} task.update(images=images) storage_url = '/media/file.png' mocker.patch.object(models.utils, 'write_to_storage') mocker.patch.object(models.utils, 'get_storage_path_and_url', return_value=('x', storage_url)) set_side_effect(locals(), target, side_effect_target, side_effect) raises = side_effect and side_effect is not FileExistsError if raises: with pytest.raises(Exception): run_with_asyncio(task.download_images)(session_object_mock) else: run_with_asyncio(task.download_images)(session_object_mock) task.reload() assert task.status == task_status for img in task.images: assert img.status == \ image_status if initial_statuses[img.id] in [StatusEnum.WAITING, StatusEnum.ERROR] \ else initial_statuses[img.id]
def test_execute_text_task(test_app, clean_db, session_object_mock, mock_session_ctx, set_side_effect, target, side_effect, side_effect_target, status): load_fixture_file('TextTask__01.json') task = models.TextTask.objects.first() text = """Testing — aiohttp 3.6.2 documentation Testing¶ Testing aiohttp web servers¶ aiohttp provides plugin for pytest making writing web server tests extremely easy, it also provides test framework agnostic utilities for testing with other frameworks such as unittest. Before starting to write your tests, you may also be interested on reading how to write testable services that interact with the loop. For using pytest plugin please install pytest-aiohttp library: $ pip install pytest-aiohttp""" set_side_effect(locals(), target, side_effect_target, side_effect) if side_effect: with pytest.raises(side_effect): execute_text_task(task.to_json()) else: execute_text_task(task.to_json()) task.reload() assert task.text == (text if not side_effect else None) assert task.status == status
def test_image_download_image(test_app, clean_db, mocker, session_object_mock, target, set_side_effect, side_effect, side_effect_target, status): load_fixture_file('ImageTask__01.json') load_fixture_file('Image__01.json') storage_url = '/media/file.png' mocker.patch.object(models.utils, 'write_to_storage') mocker.patch.object(models.utils, 'get_storage_path_and_url', return_value=('x', storage_url)) set_side_effect(locals(), target, side_effect_target, side_effect) task = models.ImageTask.objects.first() image = models.Image.objects.first() raises = side_effect and side_effect is not FileExistsError if raises: with pytest.raises(side_effect): run_with_asyncio(image.download_image)(task, session_object_mock) else: run_with_asyncio(image.download_image)(task, session_object_mock) image.reload() assert image.status == status assert image.storage_url == (None if raises else storage_url)
def test_api_get_images(client, clean_db): load_fixture_file('ImageTask__01.json') load_fixture_file('Image__01.json') images = models.Image.objects.all() image_tasks = models.ImageTask.objects.all() image_tasks[0].update(images=images) images.update(tasks=[image_tasks[0]]) response = client.get('/api/images_tasks/invalid_id/images/invalid_id') assert response.status_code == 404 response = client.get(f'/api/images_tasks/{image_tasks[0].pk}/images/') assert response.status_code == 200 response = client.get( f"/api/images_tasks/{image_tasks[0].pk}/images/{images[0].pk}") assert response.status_code == 404 response = client.get( f"/api/images_tasks/{image_tasks[0].pk}/images/{images[2].pk}") assert response.status_code == 302 assert images[2]['storage_url'] in response.location response = client.get( f"/api/images_tasks/{image_tasks[1].pk}/images/{images[0].pk}") assert response.status_code == 404
def test_api_post_images_task(client, clean_db, mock_execute_text_task, mock_execute_images_task, mock, model, endpoint, fixture_file): load_fixture_file(fixture_file) mock = { 'mock_execute_images_task': mock_execute_images_task, 'mock_execute_text_task': mock_execute_text_task, }[mock] url = 'http://www.google.pl' initial_no_tasks = model.objects.count() response = client.post(f'/api/{endpoint}/', json={'wrong_param': url}) assert response.status_code == 400 assert response.json[ 'message'] == "Request need to contain 'url' parameter" assert not mock.delay.called assert model.objects.count() == initial_no_tasks response = client.post(f'/api/{endpoint}/', json={'url': url}) assert response.status_code == 201 assert response.json['url'] == url assert response.json['status'] == 'waiting' assert mock.delay.called assert model.objects.count() == initial_no_tasks + 1
def test_api_get_text(client, clean_db): load_fixture_file('TextTask__01.json') tasks = models.TextTask.objects.all() response = client.get('/api/text_tasks/invalid_id/text') assert response.status_code == 404 response = client.get(f'/api/text_tasks/{tasks[0].pk}/text') assert response.status_code == 404 response = client.get(f'/api/text_tasks/{tasks[1].pk}/text') assert response.status_code == 200 assert response.json['text'] == tasks[1]['text']
def test_api_get_task(client, model, exclude, endpoint, fixture_file, clean_db): load_fixture_file(fixture_file) tasks = model.objects.exclude(*exclude).all() response = client.get(f'/api/{endpoint}/invalid_id') assert response.status_code == 404 for task in tasks: response = client.get(f"/api/{endpoint}/{task.pk}") assert response.status_code == 200 assert response.json == mongo_dumps_loads(task) response = client.get(f'/api/{endpoint}/') assert response.status_code == 200 assert response.json == mongo_dumps_loads(tasks)