"""Integration tests for episodes.""" import datetime from decimal import Decimal from tests.integration.consts import api from tests.integration.consts import schema from tests.integration.test_helpers import api as api_calls from tests.integration.test_helpers import utils def test_update_episode(podcast_fixture): """Update episode test case.""" podcast_id = podcast_fixture['id'] episode_id = api_calls.episode.create(podcast_id).json()['id'] new_payload = { 'title': 'New title for the episode', 'description': 'Description for the episode', 'draft': True } response = api_calls.episode.update(podcast_id, episode_id, new_payload) response_body = utils.validate_response_status(response) expected_body = { 'id': episode_id, 'podcast_id': podcast_id, 'title': 'New title for the episode', 'season_id': None, 'description': 'Description for the episode', 'season_number': None, 'episode_number': None, 'episode_type': None, 'trailer_type': None, 'content': None, 'megaphone_id': None, 'uuid': None, 'original_audio_filename': None, 'planned_pre_roll_count': 0, 'planned_mid_roll_count': 0, 'planned_post_roll_count': 0, 'created_date': None, 'updated_date': None, 'draft': True, 'status': 'draft', 'published_date': None, 'insertion_points': [], 'external_id': None, 'is_reviewed': True, 'apple_id': None } assert utils.pattern_matched( api.UUID_REGEX, response_body['megaphone_id']), \ 'Expected megaphone_id to match patter, was {}'.format( response_body['megaphone_id']) fields_to_omit = [ 'uuid', 'created_date', 'updated_date', 'megaphone_id', 'created_by', 'updated_by', 'megaphone_uid'] response_body = utils.omit_keys(response_body, fields_to_omit) expected_body = utils.omit_keys(expected_body, fields_to_omit) assert response_body == expected_body, \ 'Expected published episode to be {}, was {}'.format( expected_body, response_body ) def test_create_episode(podcast_fixture): """Create episode test case.""" response = api_calls.episode.create(podcast_fixture['id']) response_body = utils.validate_response_status(response) utils.assert_has_keys(response_body, schema.EPISODE_FIELDS) def test_delete_episode(podcast_fixture): """Test for delete episode endpoint.""" podcast_id = podcast_fixture['id'] episode_id = api_calls.episode.create(podcast_id).json()['id'] response = api_calls.episode.delete(podcast_id, episode_id) utils.validate_response_status(response) response = api_calls.episode.get_by_id(podcast_id, episode_id) utils.validate_response_status( response, expect_status=api.STATUS_NOT_FOUND ) def test_create_planned_inventory(podcast_fixture): """Create planned inventory endpoint test.""" today = datetime.datetime.today() payload_dates = [ today + datetime.timedelta(days=1), today + datetime.timedelta(days=2), today + datetime.timedelta(days=3) ] date_format = '%Y-%m-%dT%H:%M:%S.000Z' payload = { 'dates': [ payload_dates[0].strftime(date_format), payload_dates[1].strftime(date_format), payload_dates[2].strftime(date_format) ], 'pre_rolls': 1, 'post_rolls': 2, 'mid_rolls': 3, 'title_prefix': 'Api tests planning inventory' } response_body = utils.validate_response_status( api_calls.episode.create_planned_inventory( podcast_fixture['id'], payload ) ) assert response_body['podcast_id'] == podcast_fixture['id'], \ 'Expected podcast_id to be {}, was {}'.format( podcast_fixture['id'], response_body['podcast_id']) assert len(response_body['items']) == len(payload['dates']), \ 'Expected {} episodes, was {}'.format( len(payload['dates']), len(response_body['items'])) for ind, item in enumerate(response_body['items']): utils.assert_has_keys(item, schema.EPISODE_FIELDS) assert item['title'] == payload['title_prefix'] + \ payload_dates[ind].strftime('%m/%d/%Y') assert item['planned_pre_roll_count'] == payload['pre_rolls'] assert item['planned_mid_roll_count'] == payload['mid_rolls'] assert item['planned_post_roll_count'] == payload['post_rolls'] assert item['published_date'] == payload['dates'][ind] assert item['status'] == 'draft' assert item['draft'] is True def test_create_insertion_points(podcast_fixture): """Create insertion points endpoint test.""" episode = utils.validate_response_status( api_calls.episode.create(podcast_fixture['id']) ) episode = utils.validate_response_status( api_calls.episode.update( podcast_fixture['id'], episode['id'], { 'title': 'New title', 'description': 'New description', 'draft': True }) ) payload = [ { 'point_type': 'pre', 'timecode': '0', 'count': 1 }, { 'point_type': 'mid', 'timecode': '55.555', 'count': 1 }, { 'point_type': 'post', 'timecode': '0', 'count': 1 } ] response_body = utils.validate_response_status( api_calls.episode.create_insertion_points( podcast_fixture['id'], episode['id'], payload) ) assert len(response_body['items']) == len(payload), \ 'Expected to get {} items, was {}'.format( len(payload), len(response_body['items'])) for ind, item in enumerate(response_body['items']): utils.assert_has_keys(item, schema.INSERTION_POINT_FIELDS) assert item['episode_id'] == episode['id'] assert item['point_type'] == payload[ind]['point_type'] assert Decimal(item['timecode']) == Decimal(payload[ind]['timecode']) assert item['count'] == payload[ind]['count']