"""Tests for ows-pricing model.""" import json from flexmock import flexmock from owsrequest import test_utils from owsrequest import request import requests from label_copy_export.constants import ows_services from label_copy_export.models import ows_pricing def test_get_orchard_pricing_tiers_error_response(monkeypatch): """Ensure that 404 response doesn't break the flow. Test that error message is propagated in response.errors. """ pricing_family_id = 19 error_response_data = {'text': 'not found'} call_spec = { 'service': ows_services.OWS_PRICING, 'path': '/pricing-family/{}/orchard-pricing-tier'.format( pricing_family_id), 'status': 404, 'json': error_response_data } mocked_process = test_utils.mock_ows_requests([call_spec]) monkeypatch.setattr( request, 'process', mocked_process, raising=False) result = ows_pricing.get_orchard_pricing_tiers(pricing_family_id) assert not result assert json.loads(result.errors) == error_response_data assert result.status == call_spec['status'] def test_get_orchard_pricing_tiers_response_with_valid_data(monkeypatch): """Test that with valid data the result will be successful.""" pricing_family_id = 2 expected_result = [ {'pricing_family_id': 2, 'name': 'Deluxe Four', 'orchard_pricing_tier_id': 8, 'sort_order': 1, 'is_default': False}, {'pricing_family_id': 2, 'name': 'Deluxe Three', 'orchard_pricing_tier_id': 9, 'sort_order': 2, 'is_default': False}, {'pricing_family_id': 2, 'name': 'Front Plus', 'orchard_pricing_tier_id': 12, 'sort_order': 5, 'is_default': False}, {'pricing_family_id': 2, 'name': 'Front 2', 'orchard_pricing_tier_id': 13, 'sort_order': 6, 'is_default': False}, {'pricing_family_id': 2, 'name': 'Front 1', 'orchard_pricing_tier_id': 14, 'sort_order': 7, 'is_default': False}, {'pricing_family_id': 2, 'name': 'Mid/Front', 'orchard_pricing_tier_id': 15, 'sort_order': 8, 'is_default': True}] valid_response_data = {'items': expected_result} call_spec = { 'service': ows_services.OWS_PRICING, 'path': '/pricing-family/{}/orchard-pricing-tier'.format( pricing_family_id), 'status': 200, 'json': valid_response_data } mocked_process = test_utils.mock_ows_requests([call_spec]) monkeypatch.setattr( request, 'process', mocked_process, raising=False) result = ows_pricing.get_orchard_pricing_tiers(pricing_family_id) assert result assert result.message == expected_result def test_get_orchard_pricing_tier_for_product_error_response(monkeypatch): """Ensure that not successful response doesn't break the flow. Test that error message is propagated in response.errors. """ product_id = 1 pricing_family_id = 19 error_response_data = {'text': 'not found'} call_spec = { 'service': ows_services.OWS_PRICING, 'path': '/product/{}/pricing-family/{}/orchard_pricing_tier'.format( product_id, pricing_family_id), 'status': 404, 'json': error_response_data } mocked_process = test_utils.mock_ows_requests([call_spec]) monkeypatch.setattr( request, 'process', mocked_process, raising=False) result = ows_pricing.get_orchard_pricing_tier_for_product( product_id, pricing_family_id) assert not result assert json.loads(result.errors) == error_response_data assert result.status == call_spec['status'] def test_get_orchard_pricing_tier_for_product_empty_response(): """Ensure that empty pricing response doesn't break the flow. Test that default physical pricing tier is returned in case of empty successful response from ows-pricing. """ product_id = 1 pricing_family_id = 19 empty_response = flexmock(requests.models.Response()) empty_response.status_code = 200 (flexmock(empty_response).should_receive('text') .and_return('')) flexmock(request).should_receive('process').and_return(empty_response) result = ows_pricing.get_orchard_pricing_tier_for_product( product_id, pricing_family_id) assert result assert result.message == ows_services.DEFAULT_PHYSICAL_PRICING_TIER def test_get_orchard_pricing_tier_for_product_response_with_valid_data( monkeypatch): """Test get pricing for product with valid data for successful result.""" product_id = 1 pricing_family_id = 2 valid_response_data = { 'product_orchard_pricing_tier_id': 3665911, 'product_id': 2142250, 'pricing_family_id': 4, 'orchard_pricing_tier_id': 45} call_spec = { 'service': ows_services.OWS_PRICING, 'path': '/product/{}/pricing-family/{}/orchard_pricing_tier'.format( product_id, pricing_family_id), 'status': 200, 'json': valid_response_data } mocked_process = test_utils.mock_ows_requests([call_spec]) monkeypatch.setattr( request, 'process', mocked_process, raising=False) result = ows_pricing.get_orchard_pricing_tier_for_product( product_id, pricing_family_id) assert result assert result.message == valid_response_data