"""Tests for Change Detection logic.""" from unittest.mock import MagicMock from unittest.mock import call from oto import response from pricing.constants import pricing_family from pricing.logic import change_detection from pricing.logic import product_store_pricing from pricing.models import ows_product_physical from pricing.models import product_store_effective_price_code import pytest @pytest.fixture def enable_both_stores_for_ows_and_effective(): """Fixture for enabling both stores for everything.""" return [ { 'store_id': 696, 'write_to_ows_pricing': True, 'write_to_effective_price_code': True }, { 'store_id': 738, 'write_to_ows_pricing': True, 'write_to_effective_price_code': True } ] @pytest.fixture def enable_both_stores_for_effective_not_ows(): """Fixture for enabling both stores for everything.""" return [ { 'store_id': 696, 'write_to_ows_pricing': False, 'write_to_effective_price_code': True }, { 'store_id': 738, 'write_to_ows_pricing': False, 'write_to_effective_price_code': True } ] @pytest.fixture def pricing_response(): """Fixture for a product store pricing response.""" return response.Response({ 'items': [ { 'price_code': 'Test Price Code', 'store_pricing_tier_name': 'Tier' } ] }) @pytest.fixture def invalid_pricing_response(): """Fixture for an invalid product store pricing response.""" return response.create_error_response(400, 'An error occurred') def test_write_pricing_changes( monkeypatch, pricing_response, enable_both_stores_for_ows_and_effective): """Test that price changes are written to both ows and effective prices.""" monkeypatch.setattr( product_store_pricing, 'get_product_store_pricing', MagicMock(return_value=pricing_response)) monkeypatch.setattr( ows_product_physical, 'add_change_history', MagicMock()) monkeypatch.setattr( product_store_effective_price_code, 'update_product_store_effective_price_code', MagicMock()) change_detection.STORES = enable_both_stores_for_ows_and_effective product_id = 1 pricing_family_id = pricing_family.PHYSICAL_PRICING_FAMILY_ID change_detection.write_pricing_changes( product_id, pricing_family_id) call1 = call(product_id, 696, 'Test Price Code') call2 = call(product_id, 738, 'Test Price Code') calls = [call1, call2] ows_product_physical.add_change_history.assert_has_calls(calls) data = { 'price_code': 'Test Price Code', 'store_pricing_tier_name': 'Tier' } call1 = call(product_id, 696, data) call2 = call(product_id, 738, data) calls = [call1, call2] product_store_effective_price_code.\ update_product_store_effective_price_code.assert_has_calls( calls) def test_write_physical_pricing_changes_only_effective( monkeypatch, pricing_response, enable_both_stores_for_effective_not_ows): """Test that price changes are written to effective price codes.""" monkeypatch.setattr( product_store_pricing, 'get_product_store_pricing', MagicMock(return_value=pricing_response)) monkeypatch.setattr( ows_product_physical, 'add_change_history', MagicMock()) monkeypatch.setattr( product_store_effective_price_code, 'update_product_store_effective_price_code', MagicMock()) change_detection.STORES = enable_both_stores_for_effective_not_ows product_id = 1 pricing_family_id = pricing_family.PHYSICAL_PRICING_FAMILY_ID change_detection.write_pricing_changes( product_id, pricing_family_id) ows_product_physical.add_change_history.assert_not_called() data = { 'price_code': 'Test Price Code', 'store_pricing_tier_name': 'Tier' } call1 = call(product_id, 696, data) call2 = call(product_id, 738, data) calls = [call1, call2] product_store_effective_price_code.\ update_product_store_effective_price_code.assert_has_calls( calls) def test_write_physical_pricing_changes_handles_no_price_code( monkeypatch, invalid_pricing_response, enable_both_stores_for_effective_not_ows): """Test that no price code means no writing changes.""" monkeypatch.setattr( product_store_pricing, 'get_product_store_pricing', MagicMock(return_value=invalid_pricing_response)) monkeypatch.setattr( ows_product_physical, 'add_change_history', MagicMock()) monkeypatch.setattr( product_store_effective_price_code, 'update_product_store_effective_price_code', MagicMock()) change_detection.STORES = enable_both_stores_for_effective_not_ows product_id = 1 pricing_family_id = pricing_family.PHYSICAL_PRICING_FAMILY_ID change_detection.write_pricing_changes( product_id, pricing_family_id) ows_product_physical.add_change_history.assert_not_called() call1 = call(product_id, 696, {'price_code': 'No Price Assigned'}) call2 = call(product_id, 738, {'price_code': 'No Price Assigned'}) calls = [call1, call2] product_store_effective_price_code.\ update_product_store_effective_price_code.assert_has_calls( calls) def test_write_non_physical_pricing_changes( monkeypatch): """Test that no price code means no writing changes.""" monkeypatch.setattr( product_store_pricing, 'get_product_store_pricing', MagicMock(return_value=pricing_response)) monkeypatch.setattr( ows_product_physical, 'add_change_history', MagicMock()) monkeypatch.setattr( product_store_effective_price_code, 'update_product_store_effective_price_code', MagicMock()) change_detection.STORES = enable_both_stores_for_ows_and_effective product_id = 1 pricing_family_id = pricing_family.FILM_PRICING_FAMILY_ID change_detection.write_pricing_changes( product_id, pricing_family_id) ows_product_physical.add_change_history.assert_not_called() product_store_effective_price_code.\ update_product_store_effective_price_code.assert_not_called()