"""Tests for Product Store Pricing logic.""" import datetime from ast import literal_eval from unittest.mock import MagicMock from oto import response from pricing.constants import pricing_family from pricing.logic import product_store_pricing from pricing.logic import territories_helper from pricing.models import legacy_database from pricing.models import orchard_pricing_tier as orchard_pricing_tier_model from pricing.models import product_orchard_pricing_tier from pricing.models import product_pricing_override from pricing.models import store_pricing_scheme from pricing.models import track_pricing_override import pytest from tests.utils import territory_pricing_helper @pytest.fixture def not_found_response(): """Fixture for not found response.""" return response.create_not_found_response() @pytest.fixture def error_response(): """Fixture for error response.""" return response.create_error_response(400, 'An error occurred') @pytest.fixture def fixture_store_pricing_scheme_no_tiers(): """Fixture for a store pricing scheme response.""" return response.Response({ 'name': 'A Store Pricing Scheme', 'store_pricing_tiers': [] }) @pytest.fixture def fixture_store_pricing_scheme(): """Fixture for a store pricing scheme response.""" return response.Response({ 'name': 'A Store Pricing Scheme', 'store_pricing_tiers': [{ 'orchard_pricing_tier_id': 456, 'store_pricing_tier_codes': [{ 'applies_worldwide': True, 'price_code': 'Test', 'territory_list_include': False, 'resolution': 'All', 'store_pricing_tier_code_territories': [] }] }] }) @pytest.fixture def fixture_product_orchard_pricing_tier(): """Fixture for a product orchard pricing tier.""" return response.Response({ 'product_id': 123, 'orchard_pricing_tier_id': 456 }) @pytest.fixture def fixture_product_pricing_overrides(): """Fixture for a list of product pricing overrides.""" return response.Response( { 'items': [] }) def monkey(monkeypatch, obj, test_data): """Use monkeypatch to stub out method on obj with test_data.""" monkeypatch.setattr( obj, test_data['model_function'], MagicMock(return_value=response.Response(test_data['data']))) def test_get_product_store_pricing_result_pricing_scheme_not_found( monkeypatch, not_found_response, fixture_product_orchard_pricing_tier, fixture_product_pricing_overrides ): """Test that when things are missing a not found will be returned.""" monkeypatch.setattr( store_pricing_scheme, 'get_by_pricing_family_and_store_id', MagicMock(return_value=not_found_response)) monkeypatch.setattr( product_orchard_pricing_tier, 'get_by_product_id_and_pricing_family_id', MagicMock(return_value=fixture_product_orchard_pricing_tier)) monkeypatch.setattr( product_pricing_override, 'get_by_product_id', MagicMock(return_value=fixture_product_pricing_overrides)) result = product_store_pricing.get_product_store_pricing_result(1, 2, 3) print('debug, ', result, result.message, result.status) assert result.status == 404 def test_get_product_store_pricing_pricing_result_tier_not_found( monkeypatch, fixture_store_pricing_scheme_no_tiers, fixture_product_orchard_pricing_tier, fixture_product_pricing_overrides): """Test that 404 is returned when no matching tier is found in the pricing scheme. This test verifies that when a product has an orchard pricing tier, but there are no matching tiers in the store pricing scheme, a 404 status is returned. """ monkeypatch.setattr( store_pricing_scheme, 'get_by_pricing_family_and_store_id', MagicMock(return_value=fixture_store_pricing_scheme_no_tiers)) monkeypatch.setattr( product_orchard_pricing_tier, 'get_by_product_id_and_pricing_family_id', MagicMock(return_value=fixture_product_orchard_pricing_tier)) monkeypatch.setattr( product_pricing_override, 'get_by_product_id', MagicMock(return_value=fixture_product_pricing_overrides)) result = product_store_pricing.get_product_store_pricing_result(1, 2, 3) assert result.status == 404 def test_get_product_store_pricing_result_error_getting_overrides( monkeypatch, fixture_store_pricing_scheme, fixture_product_orchard_pricing_tier, error_response): """Test that an error getting the overrides will be returned as error.""" monkeypatch.setattr( store_pricing_scheme, 'get_by_pricing_family_and_store_id', MagicMock(return_value=fixture_store_pricing_scheme)) monkeypatch.setattr( product_orchard_pricing_tier, 'get_by_product_id_and_pricing_family_id', MagicMock(return_value=fixture_product_orchard_pricing_tier)) monkeypatch.setattr( product_pricing_override, 'get_by_product_id', MagicMock(return_value=error_response)) result = product_store_pricing.get_product_store_pricing_result(1, 2, 3) assert result.status == 400 def test_get_product_store_pricing_result_empty_legacy_result( monkeypatch, not_found_response, fixture_product_pricing_overrides): """Test that an empty legacy result will be returned as 404.""" monkeypatch.setattr( product_orchard_pricing_tier, 'get_by_product_id_and_pricing_family_id', MagicMock(return_value=not_found_response)) monkeypatch.setattr( store_pricing_scheme, 'get_by_pricing_family_and_store_id', MagicMock(return_value=not_found_response)) monkeypatch.setattr( product_pricing_override, 'get_by_product_id', MagicMock(return_value=fixture_product_pricing_overrides)) monkeypatch.setattr( product_store_pricing, 'get_legacy_pricing', MagicMock(return_value=[])) result = product_store_pricing.get_product_store_pricing_result(2, 3, 4) assert result.status == 404 def test_map_legacy_pricing_rows(monkeypatch): """Test that legacy database rows are mapped correctly.""" product_rows = [ { 'country_code': 'US', 'price_code': '4', 'pricing_tier': 'Back', 'custom_price': None, 'currency_code': None }, { 'country_code': 'CA', 'price_code': '4', 'pricing_tier': 'Back', 'custom_price': None, 'currency_code': None }, { 'country_code': 'GB', 'price_code': '5', 'pricing_tier': 'Front', 'custom_price': None, 'currency_code': None } ] default_rows = [ { 'country_code': 'BE', 'price_code': '3', 'pricing_tier': 'Mid', 'custom_price': None, 'currency_code': None }, { 'country_code': 'FR', 'price_code': '5', 'pricing_tier': 'Front', 'custom_price': None, 'currency_code': None } ] monkeypatch.setattr(territories_helper, 'convert_territories_to_iso', MagicMock(side_effect=lambda value: literal_eval( value))) monkeypatch.setattr(territories_helper, 'get_all_orchard_territories', MagicMock(return_value=['AG'])) pricing_family_id = 1 result = product_store_pricing.map_legacy_pricing_rows( product_rows, default_rows, pricing_family_id) expected_value = [ { 'territories': ['GB'], 'price_code': '5', 'legacy_pricing_tier': 'Front', 'territory_list_include': True, 'applies_worldwide': False, 'resolution': 'All' }, { 'territories': ['FR'], 'price_code': '5', 'legacy_pricing_tier': 'Front', 'territory_list_include': True, 'applies_worldwide': False, 'resolution': 'All' }, { 'territories': ['US'], 'price_code': '4', 'legacy_pricing_tier': 'Back', 'territory_list_include': True, 'applies_worldwide': False, 'resolution': 'All' }, { 'territories': ['CA'], 'price_code': '4', 'legacy_pricing_tier': 'Back', 'territory_list_include': True, 'applies_worldwide': False, 'resolution': 'All' }, { 'territories': ['BE'], 'price_code': '3', 'legacy_pricing_tier': 'Mid', 'territory_list_include': True, 'applies_worldwide': False, 'resolution': 'All' }, { 'territories': ['AG'], 'price_code': '2', 'legacy_pricing_tier': 'Front', 'territory_list_include': True, 'applies_worldwide': False, 'resolution': 'All' } ] territory_pricing_helper.check_pricing_results_match( result, expected_value) def test_get_product_store_pricing_no_legacy_pricing( monkeypatch, not_found_response, fixture_product_pricing_overrides): """Test that no legacy pricing returns not found.""" monkeypatch.setattr(product_orchard_pricing_tier, 'get_by_product_id_and_pricing_family_id', MagicMock(return_value=not_found_response)) monkeypatch.setattr(store_pricing_scheme, 'get_by_pricing_family_and_store_id', MagicMock(return_value=not_found_response)) monkeypatch.setattr(product_store_pricing, 'get_legacy_pricing', MagicMock(return_value=[])) monkeypatch.setattr(product_pricing_override, 'get_by_product_id', MagicMock(return_value=fixture_product_pricing_overrides)) result = product_store_pricing.get_product_store_pricing(1, 1, 1) assert result.status == 404 def test_get_legacy_pricing_no_defaults(monkeypatch): """Test that no legacy pricing defaults returns empty list.""" monkeypatch.setattr(legacy_database, 'get_default_pricing_codes', MagicMock(return_value=[])) pricing_family_id = 1 product_id = 2 store_id = 3 result = product_store_pricing.get_legacy_pricing( pricing_family_id, product_id, store_id) assert result == [] def test_find_matching_new_tier_codes(): """Test finding new store pricing tier codes for legacy pricing element.""" pricing_element = { 'territories': ['US'], 'price_code': '4', 'resolution': 'All', 'legacy_pricing_tier': 'Back', 'source': '' } pricing_tiers = [ { 'name': 'Back', 'orchard_pricing_tier_id': 1, 'store_pricing_tier_codes': [ { 'price_code': '8', 'resolution': 'SD', 'applies_worldwide': False, 'territory_list_include': True, 'territory_set': (['US']) }, { 'price_code': '10', 'resolution': 'SD', 'applies_worldwide': False, 'territory_list_include': True, 'territory_set': (['CA']) }, { 'price_code': '111', 'resolution': 'HD', 'applies_worldwide': False, 'territory_list_include': True, 'territory_set': (['US']) } ] } ] result = product_store_pricing.find_matching_new_tier_codes( pricing_element, pricing_tiers) assert result == [ { 'price_code': '8', 'resolution': 'SD', 'applies_worldwide': False, 'pricing_tier': 'Back', 'territory_list_include': True, 'territory_set': (['US']) }, { 'price_code': '111', 'resolution': 'HD', 'applies_worldwide': False, 'pricing_tier': 'Back', 'territory_list_include': True, 'territory_set': (['US']) } ] def test_find_matching_new_tier_codes_ignore_special_characters(): """Test finding new store pricing tier codes for tier with special char.""" pricing_element = { 'territories': ['US'], 'price_code': '4', 'resolution': 'All', 'legacy_pricing_tier': 'Theatrical / D & D', 'source': '' } pricing_tiers = [ { 'name': 'Theatrical/D%&D', 'orchard_pricing_tier_id': 1, 'store_pricing_tier_codes': [ { 'price_code': '8', 'resolution': 'SD', 'applies_worldwide': False, 'territory_list_include': True, 'territory_set': (['US']) }, { 'price_code': '10', 'resolution': 'SD', 'applies_worldwide': False, 'territory_list_include': True, 'territory_set': (['CA']) }, { 'price_code': '111', 'resolution': 'HD', 'applies_worldwide': False, 'territory_list_include': True, 'territory_set': (['US']) } ] } ] result = product_store_pricing.find_matching_new_tier_codes( pricing_element, pricing_tiers) assert result == [ { 'price_code': '8', 'resolution': 'SD', 'applies_worldwide': False, 'pricing_tier': 'Theatrical/D%&D', 'territory_list_include': True, 'territory_set': (['US']) }, { 'price_code': '111', 'resolution': 'HD', 'applies_worldwide': False, 'pricing_tier': 'Theatrical/D%&D', 'territory_list_include': True, 'territory_set': (['US']) } ] def get_track_prices(): """Test getting the track prices.""" track_pricing_rows = [ { 'id': 123, 'country_code': 'US', 'price_code': '100' }, { 'id': 123, 'country_code': 'CA', 'price_code': '100' }, { 'id': 456, 'country_code': 'US', 'price_code': '100' }, { 'id': 456, 'country_code': 'CA', 'price_code': '99' } ] result = product_store_pricing.get_track_prices(track_pricing_rows) assert result == { 123: { 'US': '100', 'CA': '100' }, 456: { 'US': '100', 'CA': '99' } } def test_get_default_track_prices(monkeypatch): """Test getting the default track prices.""" monkeypatch.setattr(territories_helper, 'get_all_orchard_territories', MagicMock(return_value=['US', 'CA', 'GB'])) default_rows = [ { 'country_code': 'US', 'price_code': '100' }, { 'country_code': 'GB', 'price_code': '99' } ] result = product_store_pricing.get_default_track_prices(default_rows, 3) assert result == { 'US': '100', 'CA': '3', 'GB': '99' } def test_map_override_to_pricing_elements_price_code(): """Test mapping an override with price code to a pricing element.""" product_pricing_override = { 'territory_list_include': True, 'price_code': 'OVERRIDE', 'territories': [ 'FR' ], 'applies_worldwide': False, } pricing_family_id = 1 result = product_store_pricing.map_override_to_pricing_elements( pricing_family_id, product_pricing_override, {}) assert result assert result == [{ 'territory_list_include': True, 'price_code': 'OVERRIDE', 'resolution': 'All', 'territories': [ 'FR' ], 'applies_worldwide': False, 'custom_price': None, 'custom_currency_code': None }] def test_map_override_to_pricing_elements_custom_price(): """Test mapping an override with custom price to a pricing element.""" product_pricing_override = { 'territory_list_include': True, 'custom_price': '123', 'custom_currency_code': 'USD', 'territories': [ 'FR' ], 'applies_worldwide': False, } pricing_family_id = 1 result = product_store_pricing.map_override_to_pricing_elements( pricing_family_id, product_pricing_override, {}) assert result assert result == [{ 'territory_list_include': True, 'custom_price': '123', 'custom_currency_code': 'USD', 'resolution': 'All', 'territories': [ 'FR' ], 'applies_worldwide': False, }] def test_map_override_to_pricing_elements_orchard_pricing_tier(): """Test mapping an override with pricing tier to a pricing element.""" product_pricing_override = { 'territory_list_include': True, 'orchard_pricing_tier_id': 1, 'territories': [ 'FR', 'BE' ], 'applies_worldwide': False, } store_pricing_scheme = { 'store_id': 1, 'store_pricing_tiers': [{ 'store_pricing_tier_id': 15, 'orchard_pricing_tier_id': 1, 'name': 'TIER NAME', 'store_pricing_tier_codes': [ { 'price_code': 'FRENCH', 'territory_set': {'FR'}, 'applies_worldwide': False, 'territory_list_include': True, }, { 'price_code': 'BELGIAN', 'territory_set': {'BE'}, 'applies_worldwide': False, 'territory_list_include': True, } ] }] } pricing_family_id = 1 result = product_store_pricing.map_override_to_pricing_elements( pricing_family_id, product_pricing_override, store_pricing_scheme) assert result assert result == [ { 'territory_list_include': True, 'applies_worldwide': False, 'price_code': 'FRENCH', 'store_pricing_tier_name': 'TIER NAME', 'resolution': 'All', 'territories': [ 'FR' ], 'custom_price': None, 'custom_currency_code': None }, { 'territory_list_include': True, 'applies_worldwide': False, 'price_code': 'BELGIAN', 'store_pricing_tier_name': 'TIER NAME', 'resolution': 'All', 'territories': [ 'BE' ], 'custom_price': None, 'custom_currency_code': None } ] def test_map_override_to_pricing_elements_missing_orchard_pricing_tier(): """Test mapping an override with missing tier to a pricing element.""" product_pricing_override = { 'territory_list_include': True, 'orchard_pricing_tier_id': 1, 'territories': [ 'FR', 'BE' ], 'applies_worldwide': False, } store_pricing_scheme = { 'store_pricing_tiers': [] } pricing_family_id = 1 result = product_store_pricing.map_override_to_pricing_elements( pricing_family_id, product_pricing_override, store_pricing_scheme) assert result == [] def test_map_override_to_pricing_elements_nothing(): """Test mapping an override without price code or tier.""" product_pricing_override = { 'territory_list_include': True, 'territories': [ 'FR' ], 'applies_worldwide': False, } pricing_family_id = 1 result = product_store_pricing.map_override_to_pricing_elements( pricing_family_id, product_pricing_override, {}) assert result == [] def test_apply_product_pricing_override_store_id_mismatch(monkeypatch): """Test overrides for other stores are ignored.""" override = { 'stores': [2] } monkeypatch.setattr( product_store_pricing, 'map_override_to_pricing_elements', MagicMock()) pricing_family_id = 1 store_id = 1 product_store_pricing.apply_product_pricing_override( pricing_family_id, None, override, store_id, None) product_store_pricing.map_override_to_pricing_elements.assert_not_called() def test_apply_product_pricing_override_store_id_match(monkeypatch): """Test overrides for matching store are processed.""" override = { 'store_id': 1 } store_pricing_scheme = { 'store': 'scheme' } monkeypatch.setattr( product_store_pricing, 'map_override_to_pricing_elements', MagicMock(return_value=[])) pricing_family_id = 1 store_id = 1 product_store_pricing.apply_product_pricing_override( pricing_family_id, None, override, store_id, store_pricing_scheme) product_store_pricing.map_override_to_pricing_elements.assert_called_with( pricing_family_id, override, store_pricing_scheme) def test_get_track_store_pricing_result_no_track_listing_returns_not_found( monkeypatch): """Test that missing track listing returns not found.""" pricing_family_id = 3 product_id = 100 store_id = 1 monkeypatch.setattr( legacy_database, 'get_product_tracks', MagicMock(return_value=[])) result = product_store_pricing.get_track_store_pricing_result( pricing_family_id, product_id, store_id) assert result.status == 404 def test_get_track_store_pricing__result_no_legacy_pricing_returns_not_found( monkeypatch): """Test that no legacy track pricing causes not found response.""" pricing_family_id = 3 product_id = 100 store_id = 1 monkeypatch.setattr( legacy_database, 'get_product_tracks', MagicMock(return_value=[{}])) monkeypatch.setattr( product_store_pricing, 'get_legacy_track_pricing', MagicMock(return_value=[])) result = product_store_pricing.get_track_store_pricing_result( pricing_family_id, product_id, store_id) assert result.status == 404 def test_get_track_store_pricing_result_product_pricing_override_failure( monkeypatch): """Test that failure track pricing causes not found response.""" pricing_family_id = 3 product_id = 100 store_id = 1 monkeypatch.setattr( legacy_database, 'get_product_tracks', MagicMock(return_value=[{'id': 1, 'isrc': '12345'}])) monkeypatch.setattr( product_store_pricing, 'get_legacy_track_pricing', MagicMock(return_value=[{}])) monkeypatch.setattr( product_store_pricing, 'get_us_price_code', MagicMock(return_value='100')) monkeypatch.setattr( product_store_pricing, 'mark_items_needing_new_pricing', MagicMock(return_value=False)) monkeypatch.setattr( product_pricing_override, 'get_by_product_id', MagicMock(return_value=None)) result = product_store_pricing.get_track_store_pricing_result( pricing_family_id, product_id, store_id) assert result.status == 400 def test_get_track_store_pricing_result_track_pricing_override_failure( monkeypatch, fixture_product_pricing_overrides): """Test that failure track pricing causes not found response.""" pricing_family_id = 3 product_id = 100 store_id = 1 monkeypatch.setattr( legacy_database, 'get_product_tracks', MagicMock(return_value=[{'id': 1, 'isrc': '12345'}])) monkeypatch.setattr( product_store_pricing, 'get_legacy_track_pricing', MagicMock(return_value=[{}])) monkeypatch.setattr( product_store_pricing, 'get_us_price_code', MagicMock(return_value='100')) monkeypatch.setattr( product_store_pricing, 'mark_items_needing_new_pricing', MagicMock(return_value=False)) monkeypatch.setattr( product_pricing_override, 'get_by_product_id', MagicMock(return_value=fixture_product_pricing_overrides)) monkeypatch.setattr( track_pricing_override, 'get_by_track_id', MagicMock(return_value=None)) result = product_store_pricing.get_track_store_pricing_result( pricing_family_id, product_id, store_id) assert result.status == 400 def test_get_legacy_track_pricing(monkeypatch): """Test that failure track pricing causes not found response.""" pricing_family_id = 3 product_id = 100 store_id = 1 monkeypatch.setattr( product_store_pricing, 'load_legacy_default_price_codes', MagicMock(return_value=None)) result = product_store_pricing.get_legacy_track_pricing( pricing_family_id, product_id, store_id, []) assert result == [] def test_code_applies_to_territory_world(): """Test that world applies to all territories.""" store_pricing_tier_code = { 'applies_worldwide': True } result = product_store_pricing.code_applies_to_territory( store_pricing_tier_code, 'BE') assert result def test_code_applies_to_territory_negative_list_applies(): """Test that a negative list does not apply to territory in list.""" store_pricing_tier_code = { 'applies_worldwide': False, 'territory_list_include': False, 'territory_set': set(['BE']) } result = product_store_pricing.code_applies_to_territory( store_pricing_tier_code, 'BE') assert not result def test_code_applies_to_territory_negative_list_does_not_apply(): """Test that a negative list applies to a territory not in list.""" store_pricing_tier_code = { 'applies_worldwide': False, 'territory_list_include': False, 'territory_set': set(['FR']) } result = product_store_pricing.code_applies_to_territory( store_pricing_tier_code, 'BE') assert result def test_remove_intervals(): """Test that the interval informations are correctly removed.""" pricing_result = [ { 'price_code': '100', 'end_date': datetime.datetime(1900, 1, 1, 0, 0, 0) }, { 'price_code': '101', 'start_date': datetime.datetime(2100, 1, 1, 0, 0, 0) }, { 'price_code': '102', 'end_date': datetime.datetime.now() }, { 'price_code': '200', 'start_date': datetime.datetime(1900, 1, 1, 0, 0, 0), 'end_date': datetime.datetime(2100, 1, 1, 0, 0, 0) }, { 'price_code': '201', 'start_date': datetime.datetime.now() }, ] expected_pricing_result = [ { 'price_code': '200' }, { 'price_code': '201' } ] result_pricing_result = product_store_pricing.remove_intervals( pricing_result) assert result_pricing_result == expected_pricing_result def test_get_default_orchard_pricing_tier_returns_none(monkeypatch): """Test that it will return None if there are no orchard pricing tiers.""" monkeypatch.setattr( orchard_pricing_tier_model, 'get_orchard_pricing_tier_by_pricing_family_id', MagicMock(return_value=None)) result = product_store_pricing.get_default_orchard_pricing_tier(101) assert result is None def test_get_track_store_pricing_returns_errors(monkeypatch): """Test that it will return errors if underlying method raises them .""" monkeypatch.setattr( product_store_pricing, 'get_track_store_pricing_result', MagicMock(return_value=None)) result = product_store_pricing.get_track_store_pricing(1, 2, 3) assert result is None def test_get_us_price_code(): """Test that the US price code is found.""" pricing_result = [ { 'territories': ['US'], 'price_code': 'US PRICE CODE' }, { 'territories': ['CA'], 'price_code': 'CA PRICE CODE' } ] pricing_family_id = 2 result = product_store_pricing.get_us_price_code( pricing_result, pricing_family_id) assert result == 'US PRICE CODE' def test_get_us_price_code_default(): """Test that the if the price code is not found the default is returned.""" pricing_result = [ { 'territories': ['GB'], 'price_code': 'GB PRICE CODE' }, { 'territories': ['CA'], 'price_code': 'CA PRICE CODE' } ] pricing_family_id = 2 result = product_store_pricing.get_us_price_code( pricing_result, pricing_family_id) assert result == '3' def test_get_us_price_code_track(): """Test that the US price code is found for a track.""" pricing_result = [ { 'territories': ['US'], 'track_ids': [1], 'price_code': 'US PRICE CODE TRACK 1' }, { 'territories': ['US'], 'track_ids': [2], 'price_code': 'US PRICE CODE TRACK 2' } ] pricing_family_id = 2 track_id = 1 result = product_store_pricing.get_us_price_code( pricing_result, pricing_family_id, track_id) assert result == 'US PRICE CODE TRACK 1' def test_mark_items_needing_new_pricing(): """Test that the correct items are marked.""" us_price_code = 'US PRICE CODE' pricing_result = [ { 'territories': ['US'], 'price_code': us_price_code }, { 'territories': ['CA'], 'price_code': 'CA PRICE CODE' }, { 'territories': ['GB'], 'price_code': us_price_code } ] expected_pricing_result = [ { 'territories': ['US'], 'price_code': us_price_code }, { 'territories': ['CA'], 'price_code': 'CA PRICE CODE' }, { 'territories': ['GB'], 'price_code': us_price_code, 'need_new_pricing': True } ] need_new_pricing = product_store_pricing.mark_items_needing_new_pricing( pricing_result, us_price_code) assert need_new_pricing assert pricing_result == expected_pricing_result def test_mark_items_needing_new_pricing_false(): """Test that no items are marked.""" us_price_code = 'US PRICE CODE' pricing_result = [ { 'territories': ['US'], 'price_code': us_price_code }, { 'territories': ['CA'], 'price_code': 'CA PRICE CODE' }, { 'territories': ['GB'], 'price_code': 'GB PRICE CODE' } ] expected_pricing_result = [ { 'territories': ['US'], 'price_code': us_price_code }, { 'territories': ['CA'], 'price_code': 'CA PRICE CODE' }, { 'territories': ['GB'], 'price_code': 'GB PRICE CODE' } ] need_new_pricing = product_store_pricing.mark_items_needing_new_pricing( pricing_result, us_price_code) assert not need_new_pricing assert pricing_result == expected_pricing_result def test_mark_items_needing_new_pricing_track(): """Test that the correct items are marked when a track_id is passed in.""" us_price_code = 'US PRICE CODE' pricing_result = [ { 'territories': ['US'], 'track_ids': [1], 'price_code': us_price_code }, { 'territories': ['CA'], 'track_ids': [1], 'price_code': 'CA PRICE CODE' }, { 'territories': ['GB'], 'track_ids': [2], 'price_code': us_price_code }, { 'territories': ['FR'], 'track_ids': [1], 'price_code': us_price_code } ] expected_pricing_result = [ { 'territories': ['US'], 'track_ids': [1], 'price_code': us_price_code }, { 'territories': ['CA'], 'track_ids': [1], 'price_code': 'CA PRICE CODE' }, { 'territories': ['GB'], 'track_ids': [2], 'price_code': us_price_code }, { 'territories': ['FR'], 'track_ids': [1], 'price_code': us_price_code, 'need_new_pricing': True } ] track_id = 1 need_new_pricing = product_store_pricing.mark_items_needing_new_pricing( pricing_result, us_price_code, track_id) assert need_new_pricing assert pricing_result == expected_pricing_result def test_get_store_pricing_tier_with_us_price_code(): """Test that the store pricing tier is found.""" us_price_code = 'US PRICE CODE' pricing_scheme = { 'store_pricing_tiers': [ { 'name': 'FRONT', 'store_pricing_tier_codes': [ { 'territory_set': ['US', 'CA', 'GB'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': us_price_code } ] }, { 'name': 'MID', 'store_pricing_tier_codes': [ { 'territory_set': ['US', 'CA', 'GB'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'ANOTHER PRICE CODE' } ] }, { 'name': 'BACK', 'store_pricing_tier_codes': [ { 'territory_set': ['CA', 'GB'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': us_price_code } ] } ] } result = product_store_pricing.get_store_pricing_tier_with_us_price_code( pricing_scheme, us_price_code) assert result['name'] == 'FRONT' def test_get_store_pricing_tier_with_us_price_code_not_found(): """Test that the store pricing tier is not found.""" us_price_code = 'US PRICE CODE' pricing_scheme = { 'store_pricing_tiers': [ { 'name': 'MID', 'store_pricing_tier_codes': [ { 'territory_set': ['US', 'CA', 'GB'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'ANOTHER PRICE CODE' } ] }, { 'name': 'BACK', 'store_pricing_tier_codes': [ { 'territory_set': ['CA', 'GB'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': us_price_code } ] } ] } result = product_store_pricing.get_store_pricing_tier_with_us_price_code( pricing_scheme, us_price_code) assert not result def test_apply_new_pricing(): """Test that the new pricing is applied.""" pricing_result = [ { 'territories': ['US'], 'price_code': 'US PRICE CODE' }, { 'territories': ['CA'], 'price_code': 'CA PRICE CODE' }, { 'territories': ['GB'], 'price_code': 'US PRICE CODE', 'need_new_pricing': True } ] store_pricing_tier = { 'name': 'FRONT', 'store_pricing_tier_codes': [ { 'territory_set': ['US', 'CA'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'US PRICE CODE' }, { 'territory_set': ['GB'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'STORE PRICE CODE' } ] } expected_pricing_result = [ { 'territories': ['US'], 'price_code': 'US PRICE CODE' }, { 'territories': ['CA'], 'price_code': 'CA PRICE CODE' }, { 'territories': ['GB'], 'price_code': 'STORE PRICE CODE', 'source': 'NewStorePrice', 'store_pricing_tier_name': 'FRONT' } ] product_store_pricing.apply_new_pricing(pricing_result, store_pricing_tier) assert pricing_result == expected_pricing_result def test_apply_new_pricing_track(): """Test that the new pricing is applied when a track_id is passed in.""" pricing_result = [ { 'territories': ['US'], 'track_ids': [1], 'price_code': 'US PRICE CODE' }, { 'territories': ['CA'], 'track_ids': [1], 'price_code': 'CA PRICE CODE' }, { 'territories': ['GB'], 'track_ids': [1], 'price_code': 'US PRICE CODE', 'need_new_pricing': True }, { 'territories': ['GB'], 'track_ids': [2], 'price_code': 'US PRICE CODE', 'need_new_pricing': True } ] store_pricing_tier = { 'name': 'FRONT', 'store_pricing_tier_codes': [ { 'territory_set': ['US', 'CA'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'US PRICE CODE' }, { 'territory_set': ['GB'], 'applies_worldwide': False, 'territory_list_include': True, 'price_code': 'STORE PRICE CODE' } ] } expected_pricing_result = [ { 'territories': ['US'], 'track_ids': [1], 'price_code': 'US PRICE CODE' }, { 'territories': ['CA'], 'track_ids': [1], 'price_code': 'CA PRICE CODE' }, { 'territories': ['GB'], 'track_ids': [1], 'price_code': 'STORE PRICE CODE', 'source': 'NewStorePrice', 'store_pricing_tier_name': 'FRONT' }, { 'territories': ['GB'], 'track_ids': [2], 'price_code': 'US PRICE CODE', 'need_new_pricing': True } ] track_id = 1 product_store_pricing.apply_new_pricing( pricing_result, store_pricing_tier, track_id) assert pricing_result == expected_pricing_result def test_group_track_pricing_result_by_territory(): """Test that the pricing result are grouped correctly.""" pricing_result = [ { 'territories': ['US'], 'price_code': 'US PRICE CODE', 'track_ids': [1] }, { 'territories': ['US'], 'price_code': 'US PRICE CODE', 'track_ids': [2] }, { 'territories': ['CA'], 'price_code': 'US PRICE CODE', 'track_ids': [1] }, { 'territories': ['CA'], 'price_code': 'US PRICE CODE', 'track_ids': [2] }, { 'territories': ['GB'], 'price_code': 'US PRICE CODE', 'track_ids': [1] }, { 'territories': ['GB'], 'price_code': 'ANOTHER PRICE CODE', 'track_ids': [2] } ] expected_pricing_result = [ { 'territories': ['CA', 'GB', 'US'], 'price_code': 'US PRICE CODE', 'track_ids': [1] }, { 'territories': ['CA', 'US'], 'price_code': 'US PRICE CODE', 'track_ids': [2] }, { 'territories': ['GB'], 'price_code': 'ANOTHER PRICE CODE', 'track_ids': [2] } ] result = product_store_pricing.\ group_track_pricing_result_by_territory(pricing_result) territory_pricing_helper.check_pricing_results_match( result, expected_pricing_result) def test_add_ww_territory_should_skip_for_physical(): """Test that WW territory is not inserted for Physical.""" pricing_result = [ ] product_store_pricing.add_ww_territory( product_store_pricing.pricing_family.PHYSICAL_PRICING_FAMILY_ID, pricing_result) assert pricing_result == [] def test_load_pricing_scheme_returns_none_for_film(monkeypatch): """Test that it will return error for non-physical if scheme not found .""" monkeypatch.setattr( store_pricing_scheme, 'get_by_pricing_family_and_store_id', MagicMock(return_value=None)) pricing_family_id = pricing_family.FILM_PRICING_FAMILY_ID store_id = 1 result = product_store_pricing.load_pricing_scheme( pricing_family_id, store_id) assert result is None def test_load_pricing_scheme_returns_default_for_physical(monkeypatch): """Test that it will return default for physical if scheme not found .""" monkeypatch.setattr( store_pricing_scheme, 'get_by_pricing_family_and_store_id', MagicMock(return_value=None)) pricing_family_id = pricing_family.PHYSICAL_PRICING_FAMILY_ID store_id = 1 orchard_pricing_tier = { 'orchard_pricing_tier_id': 1 } result = product_store_pricing.load_pricing_scheme( pricing_family_id, store_id, orchard_pricing_tier) assert result.message