"""Unit tests for podcast model.""" from oto import status import pytest from podcast.connectors import mysql from podcast.models import episode from podcast.models import podcast from podcast.utils import exc from tests.utils import db_operations def test_get_podcasts(mock_current_admin_user): """Test podcast model returns data alphabetically when seeded.""" limit = 0 offset = 0 result_podcast = podcast.get_podcasts(limit, offset, podcast_ids=[1, 2]) assert len(result_podcast['items']) == 2 assert result_podcast['pagination']['total_records'] == 2 result_podcast_1 = result_podcast['items'][0] result_podcast_2 = result_podcast['items'][1] assert result_podcast_1['id'] == db_operations.podcast_data[1]['id'] assert result_podcast_1['title'] == db_operations.podcast_data[1]['title'] assert result_podcast_2['id'] == db_operations.podcast_data[0]['id'] assert result_podcast_2['title'] == db_operations.podcast_data[0]['title'] def test_get_podcasts_podcast_ids(mock_current_admin_user): """Test podcast model returns data for podcast ids.""" limit = 0 offset = 0 result_podcast = podcast.get_podcasts(limit, offset, podcast_ids=[2]) assert len(result_podcast['items']) == 1 assert result_podcast['pagination']['total_records'] == 1 result_podcast_1 = result_podcast['items'][0] assert result_podcast_1['id'] == db_operations.podcast_data[1]['id'] assert result_podcast_1['title'] == db_operations.podcast_data[1]['title'] assert result_podcast_1['show_family_id'] == db_operations.podcast_data[1]['show_family_id'] def test_get_podcasts_with_apple_and_youtube_feed_ids(mock_current_admin_user): """Test podcast model does not return podcasts with feed_type in (public-rss & private-rss).""" limit = 0 offset = 0 result_podcast = podcast.get_podcasts(limit, offset, podcast_ids=[3, 4]) assert len(result_podcast['items']) == 0 def test_get_podcasts_with_no_podcasts_access(mock_current_podcast_level_user): """Test podcast model returns none if no podcast aacess.""" limit = 0 offset = 0 result_podcast = podcast.get_podcasts(limit, offset, podcast_ids=[]) assert len(result_podcast['items']) == 0 assert result_podcast['pagination']['total_records'] == 0 def test_get_podcast(): """Test podcast model returns data when seeded.""" result_podcast = podcast.get_podcast_by_id(1) assert result_podcast['id'] == db_operations.podcast_data[0]['id'] assert result_podcast['title'] == db_operations.podcast_data[0]['title'] assert result_podcast['categories'] == db_operations.podcast_data[0]['categories'] assert result_podcast['show_family_id'] == db_operations.podcast_data[0]['show_family_id'] def test_get_podcast_ids_by_network_ids(): """Test can get podcasts ids by network ids. Returns podcasts with feed_type in (public-rss , private-rss) """ podcasts = podcast.get_podcast_ids_by_network_ids([1, 2]) assert len(podcasts) == 2 def test_get_podcasts_by_network_ids(): """Test can get podcasts by network ids.""" results = podcast.get_podcasts_by_network_ids(network_ids=[1]) assert len(results['items']) == 2 assert results['items'][0]['id'] == db_operations.podcast_data[0]['id'] assert results['items'][1]['id'] == db_operations.podcast_data[2]['id'] def test_get_podcasts_limit_offset(mock_current_admin_user): """Test podcast model returns limited data when seeded.""" limit = 1 offset = 0 result_podcast = podcast.get_podcasts(limit, offset, podcast_ids=[1, 2]) assert len(result_podcast['items']) == 1 def test_create_podcast(mock_current_admin_user): """Test that a podcast is being created successfully.""" created_podcast = podcast.create_podcast({'slug': 'testslug'}) assert created_podcast['id'] == 5 assert created_podcast['created_by'] == 2 assert created_podcast['slug'] == 'testslug' def test_create_podcast_without_slug(mock_current_admin_user): """Test that a podcast is being created successfully.""" created_podcast = podcast.create_podcast({'title': 'showwithoutslug'}) assert created_podcast['id'] == 5 assert created_podcast['slug'] is None def test_update_podcast(mock_current_admin_user): """Test that a podcast can be updated.""" podcast_id = 1 data = dict( id=1, title='title 1', description='description 1', ) updated_podcast = podcast.update_podcast(podcast_id, data) assert updated_podcast['title'] == data['title'] assert updated_podcast['updated_by'] == 2 assert updated_podcast['description'] == data['description'] def test_update_podcast_empty_body(): """Test that a podcast requires fields to be updated.""" podcast_id = 1 data = {} with pytest.raises(exc.OwsError) as err: podcast.update_podcast(podcast_id, data) assert err.value.status == 400 def test_delete_podcast(mock_current_admin_user): """Test delete podcast.""" podcast_id = 1 with mysql.pod_db_session() as session: result = podcast.delete_podcast(podcast_id, session) assert result['id'] == podcast_id result_podcasts = podcast.get_podcasts(podcast_ids=[1, 2]) assert len(result_podcasts['items']) == 1 def test_delete_podcast_podcast_id_not_found(): """Test delete podcast.""" podcast_id = None with mysql.pod_db_session() as session: with pytest.raises(exc.OwsError) as err: podcast.delete_podcast(podcast_id, session) assert err.value.status == 400 def test_delete_podcast_not_found(): """Test delete podcast not found.""" podcast_id = 5 with mysql.pod_db_session() as session: with pytest.raises(exc.OwsError) as err: podcast.delete_podcast(podcast_id, session) assert err.value.status == status.NOT_FOUND def test_get_podcast_by_id_not_found(): """Test get podcast by id with not found response.""" podcast_id = 99 with pytest.raises(exc.OwsError) as err: podcast.get_podcast_by_id(podcast_id) assert err.value.status == status.NOT_FOUND def test_soft_delete_doesnt_fetch_rows(mock_current_admin_user): """Test with is_deleted=True rows are not fetched.""" podcast_id = 1 total_podcasts = len(db_operations.podcast_data) result = podcast.get_podcast_by_id(podcast_id) assert result['id'] == podcast_id result = podcast.get_podcasts(podcast_ids=[1, 2, 3, 4]) assert len(result['items']) == total_podcasts - 2 podcast.update_podcast(podcast_id, {'is_deleted': True}) with pytest.raises(exc.OwsError) as err: podcast.get_podcast_by_id(podcast_id) assert err.value.status == 404 result = podcast.get_podcasts(podcast_ids=[1, 2, 3, 4]) assert len(result['items']) == total_podcasts - 3 def test_double_delete_podcast_return404(mock_current_admin_user): """Test that double deletion return 404.""" podcast_id = 1 with mysql.pod_db_session() as session: podcast.delete_podcast(podcast_id, session) with pytest.raises(exc.OwsError) as err: podcast.delete_podcast(podcast_id, session) assert err.value.status == status.NOT_FOUND def test_delete_podcast_with_episodes(mock_current_admin_user): """Test that with podcast episodes are deleted as well.""" podcast_id = 1 episodes = episode.get_episodes(podcast_id) assert len(episodes['items']) == 3 with mysql.pod_db_session() as session: podcast.delete_podcast(podcast_id, session) episodes = episode.get_episodes(podcast_id) assert len(episodes['items']) == 0 def test_get_podcasts_by_ids(): """Test can get many podcasts by ids.""" results = podcast.get_podcasts_by_ids([1, 2]) assert len(results['items']) == 2 def test_get_podcasts_by_mp_ids(): """Test can get many podcasts by ids.""" results = podcast.get_podcasts_by_megaphone_ids([ '6bf5bea4-a1d4-11e6-8dea-b334e2aa4710', '6bf5bea4-a1d4-11e6-8dea-b334e2aa4720']) assert len(results['items']) == 2 def test_search_podcasts(): """Test can search on podcasts.""" podcasts = podcast.search_podcasts('alpha', [1, 2]) assert len(podcasts) == 1 result_podcast = podcasts[0] assert result_podcast['id'] == db_operations.podcast_data[1]['id'] assert result_podcast['title'] == db_operations.podcast_data[1]['title'] def test_search_podcasts_find_none(): """Test can search on podcasts.""" podcasts = podcast.search_podcasts('alpha', [3]) assert podcasts == [] def test_get_podcast_by_megaphone_id(db_fixture): """Test can get podcast by megaphone id.""" result_podcast = podcast.get_podcast_by_megaphone_id('6bf5bea4-a1d4-11e6-8dea-b334e2aa4720') assert result_podcast['id'] == db_operations.podcast_data[1]['id'] assert result_podcast['title'] == db_operations.podcast_data[1]['title'] assert result_podcast['megaphone_id'] == db_operations.podcast_data[1]['megaphone_id'] def test_get_podcast_favorited_users(): """Test get only active favorited users for podcasts.""" result_favorited_users = podcast.get_podcast_by_id(1, True).favorited_users assert len(result_favorited_users) == 1 favorited_user = result_favorited_users[0].to_dict() assert favorited_user['active'] is True def test_get_podcast_ids_by_ids_and_network_ids(): """Test get podcast ids by podcast and network ids.""" response = podcast.get_podcast_ids_by_ids_and_network_ids([1], [2]) assert len(response) == 2 assert response == [1, 2] def test_get_podcast_ids_by_ids_with_apple_youtube_feeds_and_network_ids(): """Test get podcast ids by podcast with feed_types apple and youtube and network ids.""" response = podcast.get_podcast_ids_by_ids_and_network_ids([3, 4], [2]) assert len(response) == 1 assert response == [2] def test_get_podcast_ids_by_show_family_ids_and_network_ids(): """Test get podcast ids by show family ids and network ids.""" response = podcast.get_podcast_ids_by_show_family_ids_and_network_ids([1], [2]) assert response == [1, 2, 3, 4] def test_get_podcast_ids_by_incorrect_show_family_ids_and_network_ids(): """Test get podcast ids by incorrect show family ids and network ids.""" response = podcast.get_podcast_ids_by_show_family_ids_and_network_ids([10], [20]) assert response == [] def test_get_podcast_with_seasons(): """Test get podcast with seasons.""" result_podcast = podcast.get_podcast_by_id(1) assert len(result_podcast['seasons']) == 3 assert result_podcast['seasons'] == [ {'id': 1, 'podcast_id': 1, 'number': 1, 'name': 's1', 'external_id': 'pod_1_season_1'}, {'id': 2, 'podcast_id': 1, 'number': 2, 'name': 's2', 'external_id': 'pod_1_season_2'}, {'id': 3, 'podcast_id': 1, 'number': 3, 'name': 's3', 'external_id': 'pod_1_season_3'} ] def test_create_podcast_channel_id(mock_current_admin_user): """Test that a podcast is being created successfully with channel id.""" created_podcast = podcast.create_podcast({'slug': 'slug', 'feed_type': 'apple-subscription', 'channel_id': '123'}) assert created_podcast['id'] == 5 assert created_podcast['feed_type'] == 'apple-subscription' assert created_podcast['channel_id'] == '123' def test_get_feeds_by_show_family_id(): """Test can get feeds(podcasts) by show family id.""" result = podcast.get_feeds_by_show_family_id(1) assert len(result['items']) == 2 assert result['items'][1].get('seasons') is None def test_get_feeds_count_for_show_family_ids(): """Test get feeds count for show family ids.""" result = podcast.get_feeds_count_for_show_family_ids([1, 2]) assert result == {1: 2, 2: 2} def test_get_feeds_by_show_family_id_no_podcasts(): """Test can get feeds(podcasts) by show family id when no podcasts.""" result = podcast.get_feeds_by_show_family_id(100) assert result['items'] == [] def test_get_earliest_feeds_by_show_family_ids(): """Test get_earliest_feeds_by_show_family_ids.""" result = podcast.get_earliest_feeds_by_show_family_ids([1, 2]) assert len(result['items']) == 2 assert result['items'][0]['id'] == 1 assert result['items'][0]['title'] == 'title' assert result['items'][0].get('seasons') is None assert result['items'][1]['id'] == 2 assert result['items'][1]['title'] == 'alpha title 1' assert result['items'][1].get('seasons') is None def test_get_earliest_feeds_by_show_family_ids_no_podcasts(): """Test get_earliest_feeds_by_show_family_ids when no podcasts.""" result = podcast.get_earliest_feeds_by_show_family_ids([700, 999]) assert result['items'] == [] def test_get_public_and_private_rss_podcasts_by_network_ids_and_show_family_ids_network_access(): """Test get_public_and_private_rss_podcasts_by_network_ids_and_show_family_ids having network access.""" result = podcast.get_public_and_private_rss_podcasts_by_network_ids_and_show_family_ids([1, 2]) assert len(result['items']) == 2 assert result['items'][0]['id'] == 1 assert result['items'][0]['title'] == 'title' assert result['items'][1]['id'] == 2 assert result['items'][1]['title'] == 'alpha title 1' def test_get_public_and_private_rss_podcasts_by_network_ids_and_show_family_ids_show_families_access(): """Test get_public_and_private_rss_podcasts_by_network_ids_and_show_family_ids having show-families access.""" result = podcast.get_public_and_private_rss_podcasts_by_network_ids_and_show_family_ids([], [1, 2]) assert len(result['items']) == 2 assert result['items'][0]['id'] == 1 assert result['items'][0]['title'] == 'title' assert result['items'][1]['id'] == 2 assert result['items'][1]['title'] == 'alpha title 1' def test_get_public_and_private_rss_podcasts_by_network_ids_and_show_family_ids_no_podcasts(): """Test get_public_and_private_rss_podcasts_by_network_ids_and_show_family_ids when no podcasts.""" result = podcast.get_public_and_private_rss_podcasts_by_network_ids_and_show_family_ids([3], [3]) assert result['items'] == []