"""Check highlight model layer.""" from product_digital_marketing.models import highlight from tests.unit.factories import highlight_factory from tests.unit.factories import highlight_projection_factory from tests.unit.test_utils import db PRODUCT_ID = 345 TERRITORY1_ID = 100 TERRITORY2_ID = 200 TERRITORY3_ID = 300 def test_to_dict(): """Check that highlight to_dict works.""" highlight_obj = 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', ) assert highlight_obj.to_dict() == { 'global_highlight': 'Some general drivers', 'apple_highlight': 'crazy apple drivers', 'spotify_highlight': 'awesome spotify drivers', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, 'projections': [] } @db.test_schema def test_get_highlight(): """Check that get highlight works.""" highlight_obj = 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', ) db.seed_models([highlight_obj]) response = highlight.get_marketing_highlight_by_product_id(PRODUCT_ID) assert response assert response.message == { 'global_highlight': 'Some general drivers', 'apple_highlight': 'crazy apple drivers', 'spotify_highlight': 'awesome spotify drivers', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, 'projections': [] } @db.test_schema def test_get_highlight_with_projections(): """Check that get highlight works.""" highlight_obj = 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', ) db.seed_models([highlight_obj]) highlight_projection1 = \ highlight_projection_factory.HighlightProjectionFactory( product_id=PRODUCT_ID, territory_id=TERRITORY1_ID, spotify_projection=1, apple_projection=2, downloads_projection=3, highlight='Cool highlight 1', priority='A' ) highlight_projection2 = \ highlight_projection_factory.HighlightProjectionFactory( product_id=PRODUCT_ID, territory_id=TERRITORY2_ID, spotify_projection=4, apple_projection=5, downloads_projection=6, highlight='Cool highlight 2', priority='B' ) highlight_projection3 = \ highlight_projection_factory.HighlightProjectionFactory( product_id=PRODUCT_ID, territory_id=TERRITORY3_ID, spotify_projection=7, apple_projection=8, downloads_projection=9, highlight='Cool highlight 3', priority=None ) db.seed_models([ highlight_projection1, highlight_projection2, highlight_projection3 ]) response = highlight.get_marketing_highlight_by_product_id(PRODUCT_ID) assert response assert response.message == { 'global_highlight': 'Some general drivers', 'apple_highlight': 'crazy apple drivers', 'spotify_highlight': 'awesome spotify drivers', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, 'projections': [ { 'id': highlight_projection1.pk, 'product_id': PRODUCT_ID, 'territory_id': TERRITORY1_ID, 'spotify_projection': 1, 'apple_projection': 2, 'downloads_projection': 3, 'highlight': 'Cool highlight 1', 'priority': 'A' }, { 'id': highlight_projection2.pk, 'product_id': PRODUCT_ID, 'territory_id': TERRITORY2_ID, 'spotify_projection': 4, 'apple_projection': 5, 'downloads_projection': 6, 'highlight': 'Cool highlight 2', 'priority': 'B' }, { 'id': highlight_projection3.pk, 'product_id': PRODUCT_ID, 'territory_id': TERRITORY3_ID, 'spotify_projection': 7, 'apple_projection': 8, 'downloads_projection': 9, 'highlight': 'Cool highlight 3', 'priority': None } ] } # Ensures that projections are sorted by their primary key. projection_ids = [ projection['id'] for projection in response.message['projections'] ] assert projection_ids == sorted(projection_ids) @db.test_schema def test_get_highlight_not_found(): """Check that get highlight works.""" response = highlight.get_marketing_highlight_by_product_id(42) assert not response assert response.status == 404 @db.test_schema def test_upsert_marketing_highlight(): """Check that put highlight works.""" highlight_data = { 'global_highlight': 'For kids', 'apple_highlight': 'I drive an apple', 'spotify_highlight': 'Spot that fy', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, } response = highlight.upsert_marketing_highlight(PRODUCT_ID, highlight_data, 'alw:123') assert response assert response.message == { 'global_highlight': 'For kids', 'apple_highlight': 'I drive an apple', 'spotify_highlight': 'Spot that fy', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, 'projections': [] } @db.test_schema def test_upsert_marketing_highlight_already_exists(): """Check that put lyricss works when it already exists.""" highlight_obj = 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='alw:123', ) db.seed_models([highlight_obj]) highlight_data = { 'global_highlight': 'For kids', 'apple_highlight': 'I drive an apple', 'spotify_highlight': 'Spot that fy', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, } response = highlight.upsert_marketing_highlight(PRODUCT_ID, highlight_data, 'alw:123') assert response assert response.message == { 'global_highlight': 'For kids', 'apple_highlight': 'I drive an apple', 'spotify_highlight': 'Spot that fy', 'apple_total': 300, 'spotify_total': None, 'downloads_total': 100500, 'projections': [] }