"""Check highlight projection model layer.""" import pytest from product_digital_marketing.models import highlight_projection 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 PRODUCT_ID = 345 TERRITORY_ID = 123 @pytest.fixture def create_data(): """highlight_projection create data for testing.""" return { 'items': [ { 'territory_id': 1, 'highlight': 'For kids', 'apple_projection': 25.5, 'spotify_projection': None, 'downloads_projection': 90.0, 'updated_by': 'ALW:123456' }, { 'territory_id': 10, 'highlight': 'For parents', 'apple_projection': 45, 'spotify_projection': 32.5, 'downloads_projection': 80.7, 'updated_by': 'ALW:123456', 'priority': 'A' } ] } def test_to_dict(): """Check that highlight projection to_dict works.""" highlight_projection_obj = \ highlight_projection_factory.HighlightProjectionFactory( product_id=PRODUCT_ID, territory_id=TERRITORY_ID, spotify_projection=20.5, apple_projection=50, downloads_projection=70.5, highlight='frosted tips', priority='A' ) assert highlight_projection_obj.to_dict() == { 'id': highlight_projection_obj.pk, 'product_id': PRODUCT_ID, 'territory_id': TERRITORY_ID, 'spotify_projection': 20.5, 'apple_projection': 50, 'downloads_projection': 70.5, 'highlight': 'frosted tips', 'priority': 'A' } @db.test_schema def test_create_highlight_projections_highlight_not_found(create_data): """Check create highlight_projection fails if highlight does not exist.""" response = highlight_projection.upsert_marketing_highlight_projections( PRODUCT_ID, create_data, 'alw:!23') assert response.status == 404 @db.test_schema def test_create_highlight_projections(create_data): """Check create highlight projections works.""" 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' ) db.seed_models([highlight, territory]) response = highlight_projection.upsert_marketing_highlight_projections( PRODUCT_ID, create_data, 'alw:123') assert response.status == 201 assert response.message == { 'items': [ { 'id': 1, 'product_id': PRODUCT_ID, 'territory_id': 1, 'spotify_projection': None, 'apple_projection': 25.5, 'downloads_projection': 90.0, 'highlight': 'For kids', 'priority': None } ] } @db.test_schema def test_create_highlight_projections_update_existing_projections(create_data): """Check create highlight projections works.""" 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]) response = highlight_projection.upsert_marketing_highlight_projections( PRODUCT_ID, create_data, 'alw:123') assert response.status == 201 assert response.message == { 'items': [ { 'id': 2, 'product_id': PRODUCT_ID, 'territory_id': 1, 'spotify_projection': None, 'apple_projection': 25.5, 'downloads_projection': 90.0, 'highlight': 'For kids', 'priority': None }, { 'id': 1, 'product_id': PRODUCT_ID, 'territory_id': 10, 'spotify_projection': 32.5, 'apple_projection': 45, 'downloads_projection': 80.7, 'highlight': 'For parents', 'priority': 'A' } ] } @db.test_schema def test_delete_highlight_projection(): """Check delete highlight projection works.""" highlight_projection_obj = \ highlight_projection_factory.HighlightProjectionFactory( product_id=PRODUCT_ID, territory_id=TERRITORY_ID, spotify_projection=1, apple_projection=9000, downloads_projection=9001, highlight='frosted tips', priority='A' ) db.seed_models([highlight_projection_obj]) result = highlight_projection.delete_highlight_projection( PRODUCT_ID, highlight_projection_obj.pk) assert result.status == 200 @db.test_schema def test_delete_highlight_projection_missed(): """Check delete highlight projection with missed projection.""" result = highlight_projection.delete_highlight_projection( PRODUCT_ID, 42) assert result.status == 404 @db.test_schema def test_delete_highlight_projection_wrong_product_id(): """Check delete highlight projection with wrong product_id.""" highlight_projection_obj = \ highlight_projection_factory.HighlightProjectionFactory( product_id=PRODUCT_ID, territory_id=TERRITORY_ID, spotify_projection=1, apple_projection=9000, downloads_projection=9001, highlight='frosted tips', priority='A' ) db.seed_models([highlight_projection_obj]) result = highlight_projection.delete_highlight_projection( PRODUCT_ID + 1, highlight_projection_obj.pk) assert result.status == 404