"""Term license utility functions unit tests.""" from datetime import date from decimal import Decimal from pytest import fixture from api.utils import term_license as util @fixture def term_licenses(): """Util specific term license fixture. Returns: list: raw term license data. """ return [ # executed { 'contractStatus': 'Executed', 'licenseRights': ['svod'], 'storeName': 'Netflix', 'value': 300}, { 'contractStatus': 'Executed', 'licenseRights': ['svod'], 'storeName': 'Something Else', 'value': 400}, # projected { 'contractStatus': 'Projected', 'licenseRights': ['svod', 'airline'], 'storeName': 'Netflix', 'value': 200}, { 'contractStatus': 'Projected', 'licenseRights': ['est', 'svod'], 'storeName': 'Nobody will know', 'value': 100}, { 'contractStatus': 'Projected', 'licenseRights': ['est'], 'storeName': 'Nobody will know', 'value': 100}] @fixture def term_licenses_breakdown(): """Finalized processed breakdown data.""" return { 'actuals': { 'term_license_netflix(airline,svod)': Decimal('0'), 'term_license_something_else(svod)': Decimal('0'), 'term_license_nobody_will_know(est,svod)': Decimal('0')}, 'projected': { 'term_license_netflix(airline,svod)': Decimal('500'), 'term_license_something_else(svod)': Decimal('400'), 'term_license_nobody_will_know(est,svod)': Decimal('200')}, 'term_licenses_labels': { 'term_license_netflix(airline,svod)': 'Term License - Netflix (Airline, SVOD)', 'term_license_something_else(svod)': 'Term License - Something Else (SVOD)', 'term_license_nobody_will_know(est,svod)': 'Term License - Nobody will know (EST, SVOD)'}} def test_group_breakdown(term_licenses, term_licenses_breakdown): """Test grouping of term licenses based on rights.""" breakdowns = { 'actuals': {'term_license': Decimal(0)}, 'projected': {'term_license': Decimal(0)}} util.group_breakdown(breakdowns, term_licenses) assert breakdowns == term_licenses_breakdown def test_store_slug(): """Test svod_slug conversion function.""" cases = ( ('FOO', 'term_license_foo'), ('Foo Bar', 'term_license_foo_bar'), ('Foo Bar 123', 'term_license_foo_bar'), ('Foo - Bar', 'term_license_foo_bar'), ('Foo - Bar(baz/test) ', 'term_license_foo_bar_baz_test')) for case, expected in cases: assert util.store_slug(case) == expected def test_breakdown_key(): """Test the breakdown_key function.""" cases = ( ( 'term_license_netflix', ['svod'], 'term_license_netflix(svod)'), ( 'term_license_amc_networks_int_l_sundancetv', ['fvod', 'broadcast', 'svod'], 'term_license_amc_networks_int_l_sundancetv(broadcast,fvod,svod)'), ( 'term_license_iflix', ['svod'], 'term_license_iflix(svod)'), ) for key, rights, expected in cases: assert util.breakdown_key(key, rights) == expected def test_breakdown_label(): """Test the breakdown_label function.""" cases = ( ( 'Netflix', ['svod'], 'Term License - Netflix (SVOD)'), ( "AMC Networks Int'l (SundanceTV)", ['fvod', 'broadcast', 'svod'], "Term License - AMC Networks Int'l (SundanceTV) " '(Broadcast, FVOD, SVOD)'), ( 'iflix', ['svod'], 'Term License - iflix (SVOD)'), ) for store, rights, expected in cases: assert util.breakdown_label(store, rights) == expected def test_fill_tl_breakdown_projections(): """Test the fill_tl_breakdown_projections function.""" labels = {'foo': 'Foo', 'bar': 'Bar'} breakdown = { 'actuals': { 'foo': Decimal(12), 'bar': Decimal(31)}, 'projected': { 'foo': Decimal(0), 'bar': None}} expected = { 'foo': Decimal(0), 'bar': Decimal(31)} result = util.fill_tl_breakdown_projections(breakdown, labels) assert expected == result['projected'] def test_fill_tl_projections(): """Test the fill_tl_projections function.""" revenue = { 'raw': [ { 'transaction_type_id': 14, 'amount': Decimal(18), 'data_point': date(2016, 1, 1), 'store_id': 333 }, { 'transaction_type_id': 14, 'amount': Decimal(23), 'data_point': date(2016, 1, 1), 'store_id': 111 }, { 'transaction_type_id': 40, 'amount': Decimal(223), 'data_point': date(2016, 1, 1), 'store_id': 444 }] } term_license = { 'raw': [ {'storeId': '111'}, {'storeId': 222}], 'projections': { date(2016, 1, 1): Decimal(2), date(2016, 1, 2): Decimal(4)}} expected = { date(2016, 1, 1): Decimal(20), date(2016, 1, 2): Decimal(4)} result = util.fill_tl_projections(revenue, term_license) assert expected == result