"""Integration tests.""" import logging import time import pytest from pytest_lazyfixture import lazy_fixture from tests.integration.consts import api as api_consts from tests.integration.test_helpers import api as api_calls from tests.integration.test_helpers import utils def wait_for_asset_status(asset_filename, expected_status, max_tries, wait_before_try): """Wait for asset status.""" for tries in range(max_tries): time.sleep(wait_before_try) status_data = api_calls.asset.get_status(asset_filename).json() logging.debug(status_data) status = status_data.get('status') logging.info(f'Got status {status}') if status == expected_status: logging.info( f'Got expected status: {status} ' f'description: {status_data.get("description")}' ) is_ok = True break elif status and '_error' in status: logging.error( f'Got non-expecting error status: {status}' f' description: {status_data.get("description")}' ) is_ok = False break else: logging.error( f"Didn't get expected status for {max_tries * wait_before_try}" f' seconds. The last status: {status_data}' ) is_ok = False return is_ok @pytest.mark.parametrize('object_fixture, original_filename, asset_type, expected_status', [ # podcast artwork (lazy_fixture('podcast_object_fixture'), 'sample_image_2999x2999.jpg', 'artwork', 'validation_error'), (lazy_fixture('podcast_object_fixture'), 'sample_image_4000x4000.jpg', 'artwork', 'encoding_completed'), # podcast's episode artwork (lazy_fixture('episode_object_fixture'), 'sample_image_2999x2999.jpg', 'artwork', 'validation_error'), (lazy_fixture('episode_object_fixture'), 'sample_image_4000x4000.jpg', 'artwork', 'encoding_completed'), # podcast's episode audio (lazy_fixture('episode_object_fixture'), '2-seconds-of-silence.wav', 'audio', 'validation_error'), (lazy_fixture('episode_object_fixture'), 'sample_audio.wav', 'audio', 'encoding_completed', ), ]) def test_asset_pipeline(object_fixture, original_filename, asset_type, expected_status): """Post asset file to pipeline.""" object_type, object_id = object_fixture logging.info(f'object_type={object_type}, object_id={object_id}') upload_token_data = utils.validate_response_status( api_calls.asset.get_upload_token() ) utils.put_asset_to_s3(original_filename, upload_token_data, object_type, object_id) status_validation_started = time.time() try: assert wait_for_asset_status( upload_token_data['filename'], expected_status, api_consts.MAX_TRIES, api_consts.WAIT_BEFORE_TRY) finally: status_validation_finished = time.time() logging.info( f'It took: {status_validation_finished - status_validation_started:.1f} ' f'seconds') api_calls.asset.delete(object_id, object_type, asset_type)