"""Unit tests for highlight logic.""" import pytest from product_digital_marketing.constants import error from product_digital_marketing.logic import highlight_logic from tests.unit.factories import highlight_factory from tests.unit.factories import highlight_projection_factory from tests.unit.factories import territory_factory from tests.unit.test_utils import db @pytest.fixture def product_id(): """Create a marketing highlight id for testing.""" return 123 @pytest.fixture def target_product_id(): """Create a marketing highlight id for testing.""" return 456 @db.test_schema def test_copy_marketing_source_not_found(product_id, target_product_id): """Test that model layer copy marketing with non existing source.""" result = highlight_logic.copy_marketing( product_id, target_product_id) assert result.status == 404 @db.test_schema def test_copy_marketing_target_found(product_id, target_product_id): """Test that model layer copy marketing with existing target.""" source_highlight = highlight_factory.HighlightFactory( product_id=product_id, global_highlight='Some general drivers', spotify_highlight='awesome spotify drivers', apple_highlight='crazy apple drivers', spotify_total=None, apple_total=300, downloads_total=100500, updated_by='OA:111', ) target_highlight = highlight_factory.HighlightFactory( product_id=target_product_id, global_highlight='Some general drivers', spotify_highlight='awesome spotify drivers', apple_highlight='crazy apple drivers', spotify_total=None, apple_total=300, downloads_total=100500, updated_by='OA:111', ) db.seed_models([source_highlight, target_highlight]) result = highlight_logic.copy_marketing( product_id, target_product_id) assert result.status == 400 assert result.errors == { 'code': error.ERROR_MESSAGE_COPY_TARGET_EXISTS, 'message': error.BAD_REQUEST_CODE } @db.test_schema def test_copy_marketing_success(product_id, target_product_id): """Test that model layer copy marketing with existing target.""" highlight = highlight_factory.HighlightFactory( product_id=product_id, global_highlight='Some general drivers', spotify_highlight='awesome spotify drivers', apple_highlight='crazy apple drivers', spotify_total=None, apple_total=300, downloads_total=100500 ) territory_factory.TerritoryFactory.reset_sequence() territory = territory_factory.TerritoryFactory( name='Lego Island', category='continent' ) highlight_projection_factory.HighlightProjectionFactory.reset_sequence() projection = \ highlight_projection_factory.HighlightProjectionFactory( product_id=product_id, territory_id=10, spotify_projection=1, apple_projection=9000, downloads_projection=9001, highlight='Cool highlight 1', priority='A' ) db.seed_models([highlight, territory, projection]) result = highlight_logic.copy_marketing( product_id, target_product_id) assert result.status == 201 assert result.message == { 'global_highlight': 'Some general drivers', 'spotify_highlight': 'awesome spotify drivers', 'apple_highlight': 'crazy apple drivers', 'spotify_total': None, 'apple_total': None, 'downloads_total': None, 'projections': [ { 'id': projection.pk + 1, 'product_id': target_product_id, 'territory_id': 10, 'spotify_projection': None, 'apple_projection': None, 'downloads_projection': None, 'highlight': 'Cool highlight 1', 'priority': 'A' } ] }