"""Tests for Track model.""" from unittest.mock import MagicMock from oto.response import create_error_response from ows_product_physical.constant import error from ows_product_physical.models import track def test_track_get_publishing_obligation_by_product_id_success(monkeypatch): """Test get publishing obligations by product id success.""" expected_result = { 'items': [ { 'us_publishing_obligation': 'ControlledByYourLabel', 'track_id': 1, 'publisher_names': ['Publisher 1'], 'third_party_publisher': 'Y' }, { 'us_publishing_obligation': None, 'track_id': 2, 'publisher_names': ['Publisher 2'], 'third_party_publisher': 'N' } ], 'pagination': { 'offset': 0, 'type': 'standard', 'total_records': 2, 'limit': 2 } } monkeypatch.setattr( track, '_get_publishing_obligations', value=MagicMock( return_value=expected_result)) resp = track.get_publishing_obligations(1) assert resp.status == 200 assert track._get_publishing_obligations.called def test_get_publishing_obligation_by_product_id_db_fail(monkeypatch): """Assert fetch product by id fail exception was captured.""" expected_error_response = create_error_response( code=error.INTERNAL_ERROR, message='mysql error', status=500) monkeypatch.setattr( track, '_get_publishing_obligations', value=MagicMock( return_value=expected_error_response)) resp = track.get_publishing_obligations(1) assert resp.status == 500 def test_track_get_publishing_obligation( db_fixture, multiple_tracks_for_update, minimal_product_for_tracks): """Assert publishing obligations retrieved from database by product_id.""" track.process_tracks( minimal_product_for_tracks['product_id'], minimal_product_for_tracks['upc'], multiple_tracks_for_update) expected_message = { 'items': [ { 'track_name': 'First track', 'track_number': 1, 'publisher_names': [], 'us_publishing_obligation': 'Composition', 'track_id': 1, 'third_party_publisher': 'Y' }, { 'track_name': 'Second track', 'track_number': 2, 'publisher_names': [], 'us_publishing_obligation': 'ControlledByYourLabel', 'track_id': 2, 'third_party_publisher': 'Y' }, { 'track_name': 'Third Track', 'track_number': 3, 'publisher_names': [], 'us_publishing_obligation': 'PublicDomain', 'track_id': 3, 'third_party_publisher': 'N' } ] } result = track.get_publishing_obligations( minimal_product_for_tracks['product_id']) assert result.message == expected_message def test_track_get_publishing_obligation_with_real_db_data(db_with_data): """Assert get_publishing_obligations method returns correct result.""" expected_result = { 'items': [ { 'track_number': 1, 'track_name': 'First track', 'us_publishing_obligation': 'ControlledByYourLabel', 'track_id': 1, 'publisher_names': ['Publisher 1'], 'third_party_publisher': 'Y' }, { 'track_name': 'Second track', 'track_number': 2, 'us_publishing_obligation': None, 'track_id': 2, 'publisher_names': ['Publisher 2'], 'third_party_publisher': 'N' } ] } # get publishing obligations for the track existing in test database result = track.get_publishing_obligations(1) assert result.message == expected_result def test_track_get_publishing_obligation_without_db_data(db_with_data): """Assert get_publishing_obligations method returns correct result.""" expected_not_found_response_message = ( error.PUBLISHING_OBLIGATION_NOT_FOUND_MESSAGE) # get for the product without publishing obligations result = track.get_publishing_obligations(2) assert result.errors['message'] == expected_not_found_response_message assert result.status == 404 def test_track_get_publishing_obligation_non_existing_product(db_with_data): """Assert get_publishing_obligations method returns correct result.""" expected_not_found_response_message = ( error.PUBLISHING_OBLIGATION_NOT_FOUND_MESSAGE) # get for the product which doesn't exist in test database result = track.get_publishing_obligations(22222) assert result.errors['message'] == expected_not_found_response_message assert result.status == 404