import pytest from unittest.mock import Mock from labelaudit.consts import label_audit as consts from labelaudit.logic import asset_search from labelaudit.tasks import asset_search_tasks from labelaudit.logic import audit_release from labelaudit.logic import release from labelaudit.logic.result import Result audit_release_id = 123 release_id = 456 orchard_upc = 'not-a-real-upc' ioda_upc = 'not-real-either' releases = [ { 'youtubeAuditReleaseId': 1, 'youtubeAuditId': 1, 'releaseId': 12, 'assetCmsAccountId': None, 'isCmsAccountAsset': None, 'isYoutubeAsset': None, 'auditStatus': 'pending', 'numAssets': None, 'error': None, 'lastUpdated': '2015-05-26 11:56:49' } ] @pytest.fixture def no_assets(): return { 'kind': 'youtubePartner#assetSnippetList', 'pageInfo': {'totalResults': 0} } @pytest.fixture def single_asset(): return { 'kind': 'youtubePartner#assetSnippetList', 'items': [{ 'kind': 'youtubePartner#assetSnippet', 'id': 'A820245425510328', 'type': 'sound_recording', 'title': 'Cigarette', 'isrc': 'GBJVC1500004' }], 'pageInfo': { 'totalResults': 1 } } @pytest.fixture def updated_release(): return { 'youtubeAuditRelease': { 'youtubeAuditReleaseId': 85177, 'isCmsAccountAsset': 'Y', 'isYoutubeAsset': 'Y', 'numAssets': None, 'auditStatus': 'pending', 'error': None, 'releaseId': 1369359, 'youtubeAuditId': 7, 'assetCmsAccountId': 9, 'lastUpdated': '2015-06-27 19:53:10' } } @pytest.fixture def updated_release_error(): return { 'fault': { 'id': 'Not found' } } @pytest.fixture def ioda_owner(): return consts.CmsAccounts.get('ioda').content_owner def mock_search(monkeypatch, fake_response): search_by_upc = Mock(return_value=fake_response) monkeypatch.setattr(asset_search, 'search_by_upc', search_by_upc) return search_by_upc def mock_updated_release(monkeypatch): updated_release_result = Result(message=updated_release, status=200) updated_release_mock = Mock(return_value=updated_release_result) monkeypatch.setattr( audit_release, 'update_audit_release', updated_release_mock) def mock_updated_release_error(monkeypatch): updated_release_mock = Mock(side_effect=Exception('test exception')) monkeypatch.setattr( audit_release, 'update_audit_release', updated_release_mock) def mock_orchard_upc(monkeypatch, upc): search_result = Result(message=upc, status=200) search_result_mock = Mock(return_value=search_result) monkeypatch.setattr(release, 'fetch_orchard_upc', search_result_mock) def mock_ioda_upc(monkeypatch, upc): search_result = Result(message=upc, status=200) search_result_mock = Mock(return_value=search_result) monkeypatch.setattr(release, 'fetch_ioda_upc', search_result_mock) def mock_next_step(monkeypatch, next_func_name): expected_next = Mock() monkeypatch.setattr(asset_search_tasks, next_func_name, expected_next) return expected_next def mock_asset_count(monkeypatch, asset_count=0): mock_count_assets = Mock(return_value=asset_count) monkeypatch.setattr(asset_search, 'count_my_assets', mock_count_assets) return mock_count_assets def test_process_releases(monkeypatch): orchard_my_assets = mock_next_step(monkeypatch, 'orchard_my_assets') asset_search_tasks.process_releases(releases) orchard_my_assets.delay.assert_called_with(1, 12) def test_orchard_my_assets_search_params(monkeypatch, single_asset): mock_orchard_upc(monkeypatch, orchard_upc) mock_updated_release(monkeypatch) mock_count_assets = mock_asset_count(monkeypatch, 1) asset_search_tasks.orchard_my_assets(audit_release_id, release_id) mock_count_assets.assert_called_with( orchard_upc, asset_search.OWNER_ORCHARD) def test_orchard_my_assets_no_results_not_ioda(monkeypatch): mock_orchard_upc(monkeypatch, orchard_upc) mock_ioda_upc(monkeypatch, None) mock_asset_count(monkeypatch, 0) orchard_all_assets = mock_next_step(monkeypatch, 'orchard_all_assets') mock_updated_release(monkeypatch) asset_search_tasks.orchard_my_assets(audit_release_id, release_id) orchard_all_assets.delay.assert_called_with( audit_release_id, orchard_upc, None) def test_orchard_my_assets_results_not_ioda(monkeypatch): mock_orchard_upc(monkeypatch, orchard_upc) mock_ioda_upc(monkeypatch, None) mock_updated_release(monkeypatch) mock_asset_count(monkeypatch, 1) result = asset_search_tasks.orchard_my_assets(audit_release_id, release_id) assert result is True def test_orchard_my_assets_no_results_ioda(monkeypatch): mock_orchard_upc(monkeypatch, None) mock_ioda_upc(monkeypatch, ioda_upc) mock_search(monkeypatch, no_assets) ioda_my_assets = mock_next_step(monkeypatch, 'ioda_my_assets') mock_updated_release(monkeypatch) asset_search_tasks.orchard_my_assets(audit_release_id, release_id) ioda_my_assets.delay.assert_called_with( audit_release_id, None, ioda_upc) def test_ioda_my_assets_search_params(monkeypatch, ioda_owner): mock_updated_release(monkeypatch) mock_count_assets = mock_asset_count(monkeypatch, 1) asset_search_tasks.ioda_my_assets( audit_release_id, orchard_upc, ioda_upc) mock_count_assets.assert_called_with(ioda_upc, ioda_owner) def test_ioda_my_assets_no_results(monkeypatch, no_assets): mock_search(monkeypatch, no_assets) orchard_all_assets = mock_next_step(monkeypatch, 'orchard_all_assets') mock_updated_release(monkeypatch) asset_search_tasks.ioda_my_assets(audit_release_id, orchard_upc, ioda_upc) orchard_all_assets.delay.assert_called_with( audit_release_id, orchard_upc, ioda_upc) def test_ioda_my_assets_results(monkeypatch): mock_updated_release(monkeypatch) mock_asset_count(monkeypatch, 1) result = asset_search_tasks.ioda_my_assets( audit_release_id, orchard_upc, ioda_upc) assert result is True def test_orchard_all_assets_search_params(monkeypatch, single_asset): search_by_upc = mock_search(monkeypatch, single_asset) mock_updated_release(monkeypatch) asset_search_tasks.orchard_all_assets( audit_release_id, orchard_upc, ioda_upc) search_by_upc.assert_called_with( orchard_upc, asset_search.OWNER_ORCHARD, False) def test_orchard_all_assets_no_results_not_ioda(monkeypatch, no_assets): mock_search(monkeypatch, no_assets) mock_updated_release(monkeypatch) result = asset_search_tasks.orchard_all_assets( audit_release_id, orchard_upc, None) assert result is False def test_orchard_all_assets_no_results_ioda(monkeypatch, no_assets): mock_search(monkeypatch, no_assets) ioda_all_assets = mock_next_step(monkeypatch, 'ioda_all_assets') mock_updated_release(monkeypatch) asset_search_tasks.orchard_all_assets( audit_release_id, orchard_upc, ioda_upc) ioda_all_assets.delay.assert_called_with(audit_release_id, ioda_upc) def test_orchard_all_assets_results(monkeypatch, single_asset): mock_search(monkeypatch, single_asset) mock_updated_release(monkeypatch) result = asset_search_tasks.orchard_all_assets( audit_release_id, orchard_upc, ioda_upc) assert result is True def test_ioda_all_assets_search_params(monkeypatch, single_asset): search_by_upc = mock_search(monkeypatch, single_asset) mock_updated_release(monkeypatch) asset_search_tasks.ioda_all_assets(audit_release_id, ioda_upc) search_by_upc.assert_called_with(ioda_upc, asset_search.OWNER_IODA, False) def test_ioda_all_assets_no_results(monkeypatch, no_assets): mock_search(monkeypatch, no_assets) mock_updated_release(monkeypatch) result = asset_search_tasks.ioda_all_assets(audit_release_id, ioda_upc) assert result is False def test_ioda_all_assets_results(monkeypatch, single_asset): mock_search(monkeypatch, single_asset) mock_updated_release(monkeypatch) result = asset_search_tasks.ioda_all_assets(audit_release_id, ioda_upc) assert result is True def test_orchard_my_assets_exception(monkeypatch): mock_orchard_upc(monkeypatch, orchard_upc) mock_asset_count(monkeypatch, 1) mock_updated_release_error(monkeypatch) with pytest.raises(Exception) as excinfo: asset_search_tasks.orchard_my_assets( audit_release_id, release_id) assert 'test exception' in str(excinfo.value) def test_orchard_my_assets_no_results_not_ioda_exception( monkeypatch, no_assets): mock_orchard_upc(monkeypatch, orchard_upc) mock_ioda_upc(monkeypatch, None) mock_search(monkeypatch, no_assets) mock_next_step(monkeypatch, 'orchard_all_assets') mock_updated_release_error(monkeypatch) with pytest.raises(Exception) as excinfo: asset_search_tasks.orchard_my_assets(audit_release_id, release_id) assert 'test exception' in str(excinfo.value) def test_orchard_my_assets_results_ioda_exception(monkeypatch): mock_orchard_upc(monkeypatch, orchard_upc) mock_asset_count(monkeypatch, 1) mock_updated_release_error(monkeypatch) with pytest.raises(Exception) as excinfo: asset_search_tasks.ioda_my_assets( audit_release_id, orchard_upc, ioda_upc) assert 'test exception' in str(excinfo.value) def test_ioda_my_assets_no_results_exception(monkeypatch, no_assets): mock_search(monkeypatch, no_assets) mock_next_step(monkeypatch, 'orchard_all_assets') mock_updated_release_error(monkeypatch) with pytest.raises(Exception) as excinfo: asset_search_tasks.ioda_my_assets( audit_release_id, orchard_upc, ioda_upc) assert 'test exception' in str(excinfo.value) def test_ioda_my_assets_results_exception(monkeypatch): mock_asset_count(monkeypatch, 1) mock_updated_release_error(monkeypatch) with pytest.raises(Exception) as excinfo: asset_search_tasks.ioda_my_assets( audit_release_id, orchard_upc, ioda_upc) assert 'test exception' in str(excinfo.value) def test_orchard_all_assets_no_results_exception(monkeypatch, no_assets): mock_search(monkeypatch, no_assets) mock_updated_release_error(monkeypatch) with pytest.raises(Exception) as excinfo: asset_search_tasks.orchard_all_assets( audit_release_id, orchard_upc, None) assert 'test exception' in str(excinfo.value) def test_orchard_all_assets_results_exception(monkeypatch, single_asset): mock_search(monkeypatch, single_asset) mock_updated_release_error(monkeypatch) with pytest.raises(Exception) as excinfo: asset_search_tasks.orchard_all_assets( audit_release_id, orchard_upc, ioda_upc) assert 'test exception' in str(excinfo.value) def test_ioda_all_assets_no_results_exception(monkeypatch, no_assets): mock_search(monkeypatch, no_assets) mock_updated_release_error(monkeypatch) with pytest.raises(Exception) as excinfo: asset_search_tasks.ioda_all_assets(audit_release_id, ioda_upc) assert 'test exception' in str(excinfo.value) def test_ioda_all_assets_results_exception(monkeypatch, single_asset): mock_search(monkeypatch, single_asset) mock_updated_release_error(monkeypatch) with pytest.raises(Exception) as excinfo: asset_search_tasks.ioda_all_assets(audit_release_id, ioda_upc) assert 'test exception' in str(excinfo.value)