"""Unit tests for ShowFamily model.""" from oto import status import pytest from podcast.connectors import mysql from podcast.models import show_family from podcast.utils import exc from tests.utils import db_operations def test_get_show_family_by_id(mock_current_admin_user): """Test get show family by id being fetched.""" result = show_family.get_show_family_by_id(1) assert result['id'] == 1 assert result['title'] == 'show family title 1' assert result['is_deleted'] is False def test_get_show_family_by_id_not_found(): """Test get show family by id with not found response.""" with pytest.raises(exc.OwsError) as err: show_family.get_show_family_by_id(9999) assert err.value.status == status.NOT_FOUND def test_get_show_family_by_id_for_deleted(): """Test get show family by id for deleted show family.""" with pytest.raises(exc.OwsError) as err: show_family.get_show_family_by_id(3) assert err.value.status == status.NOT_FOUND def test_create_show_family(mock_current_admin_user): """Test that a show family is being created successfully.""" created_show_family = show_family.create_show_family( {'network_id': '1', 'title': 'title', 'created_by': 1, 'updated_by': 1}) assert created_show_family['id'] == 5 assert created_show_family['created_by'] == 1 assert created_show_family['title'] == 'title' def test_get_show_families(): """Test show_family model returns data alphabetically when seeded.""" result = show_family.get_show_families(limit=0, offset=0, show_family_ids=[1, 2]) assert len(result['items']) == 2 assert result['pagination']['total_records'] == 2 assert result['items'][0]['id'] == 2 assert result['items'][0]['title'] == 'second show family' assert result['items'][0]['is_deleted'] is False assert result['items'][1]['id'] == 1 assert result['items'][1]['title'] == 'show family title 1' assert result['items'][1]['is_deleted'] is False def test_get_show_families_with_limit_and_offset(): """Test show_family model returns data for show_family ids with limit and offset.""" result = show_family.get_show_families(limit=10, offset=1, show_family_ids=[1, 2]) assert len(result['items']) == 1 assert result['pagination']['total_records'] == 2 assert result['items'][0]['id'] == 1 assert result['items'][0]['title'] == 'show family title 1' def test_get_show_families_with_no_show_family_ids(): """Test show_family model returns none if no show_family_ids.""" result_podcast = show_family.get_show_families(limit=0, offset=0, show_family_ids=[]) assert len(result_podcast['items']) == 0 assert result_podcast['pagination']['total_records'] == 0 def test_get_show_families_by_show_family_ids(mock_current_admin_user): """Test show family model returns data for show family ids.""" limit = 0 offset = 0 result_show_family = show_family.get_show_families(limit, offset, show_family_ids=[2]) assert len(result_show_family['items']) == 1 assert result_show_family['pagination']['total_records'] == 1 assert result_show_family['items'][0]['title'] == db_operations.show_family_data[1]['title'] def test_get_show_family_ids_by_network_ids(): """Test can get_show_family_ids_by_network_ids.""" result = show_family.get_show_family_ids_by_network_ids([1, 2, 4]) assert result == [1, 2, 4] def test_get_show_family_ids_by_network_ids_no_show_families(): """Test can get_show_family_ids_by_network_ids for network ids with no show families.""" result = show_family.get_show_family_ids_by_network_ids([3]) assert result == [] def test_delete_show_family(mock_current_admin_user): """Test delete show family.""" show_family_id = 1 with mysql.pod_db_session() as session: result = show_family.delete_show_family(show_family_id, session) assert result['id'] == show_family_id assert result['updated_by'] == 2 assert result['is_deleted'] is True def test_delete_show_family_not_found(): """Test delete show family not found.""" show_family_id = 5 with mysql.pod_db_session() as session: with pytest.raises(exc.OwsError) as err: show_family.delete_show_family(show_family_id, session) assert err.value.status == status.NOT_FOUND assert err.value.message == 'Show Family not found' def test_update_show_family(mock_current_admin_user): """Test that a show family can be updated.""" show_family_id = 1 data = dict( id=1, title='update title' ) with mysql.pod_db_session() as session: updated_show_family = show_family.update_show_family(show_family_id, data, session) assert updated_show_family['title'] == data['title'] assert updated_show_family['updated_by'] == 2 def test_update_show_family_not_found(mock_current_admin_user): """Test that a show family can be updated.""" show_family_id = 10 data = dict( id=10, title='update title' ) with mysql.pod_db_session() as session: with pytest.raises(exc.OwsError) as err: show_family.update_show_family(show_family_id, data, session) assert err.value.status == status.NOT_FOUND assert err.value.message == 'Show Family not found'