"""Test for sales_goals logic module.""" import copy from flexmock import flexmock from oto import response import pytest from sales_goals.constants import error from sales_goals.constants import models from sales_goals.constants import ows_services from sales_goals.logic import countries from sales_goals.logic import sales_goals from sales_goals.models import ows_product from sales_goals.models import sales_goal as sales_goal_model from sales_goals.models import sales_goals_for_country from sales_goals.models import store from sales_goals.models import target_market_goal TEST_COUNTRY_ID = 1 @pytest.fixture def sales_goal_model_fetch_response(): """Function that returns valid model fetch response of sales goals. Returns: dict: sales goals data """ return { 'sales_goal_id': 1, 'product_id': 1, 'first_week_estimate': 0, 'updated_by': 'oa:1234', 'updated_at': '2017-04-13 12:33:33.652345'} @pytest.fixture def target_market_goal_model_fetch_response(): """Function that returns valid model fetch response of target market goals. Returns: list: target market goals data """ return [{ 'target_market_goal_id': '1', 'sales_goal_id': 1, 'store_id': 1, 'label_goal_value': 1, 'distribution_goal_value': 1, 'country_id': 1, 'country_name': 'USA'}, { 'target_market_goal_id': '2', 'sales_goal_id': 1, 'store_id': 4, 'label_goal_value': 1, 'distribution_goal_value': 1, 'country_id': 2, 'country_name': 'Canada'} ] SALES_GOAL_TO_UPDATE = response.Response(sales_goal_model_fetch_response) @pytest.fixture def store_model_fetch_response(): """Function that returns valid fetch response of store model. Returns: list: stores data """ return [{ 'store_id': 1, 'name': 'Amazon'}] @pytest.fixture def valid_post_product_country_request_data(): """Fixture returns valid POST goals/{productId}{countryId} request data.""" return { 'goals': [ { 'store': { 'store_id': 1, 'name': 'Amazon', }, 'label_goal_value': 9999, 'distribution_goal_value': 9999}]} @pytest.fixture def sales_goal_data_for_patch(): """Function that returns valid data to update the sales goal. Contains target_market_goals for all the stores for certain country. Returns: dict: sales goal data """ return { 'first_week_estimate': 33, 'notes': 'Notes to test update', 'target_market_goals': [{ 'country_id': 1, 'goals': [{ 'target_market_goal_id': 123, 'label_goal_value': 20, 'distribution_goal_value': 30 }], }] } @pytest.fixture def goal_data_for_country(): """Function that returns valid data to update the sales goal in country. Returns: dict: sales goal data """ return { 'country_id': 1, 'goals': [ { 'store': { 'name': 'Target', 'store_id': 7 }, 'target_market_goal_id': 123, 'label_goal_value': 20, 'distribution_goal_value': 30 } ], } @pytest.fixture def goal_data_for_country_with_multiple_target_market_goals(): """Function that returns valid data to update the sales goal in country. Returns goal data for country with multiple target_market_goals. Returns: dict: sales goal data """ return { 'country_id': 1, 'goals': [ { 'store': { 'name': 'Amazon', 'store_id': 1 }, 'target_market_goal_id': 123, 'label_goal_value': 20, 'distribution_goal_value': 30 }, { 'store': { 'name': 'HMV', 'store_id': 2 }, 'target_market_goal_id': 124, 'label_goal_value': 200, 'distribution_goal_value': 100 }, { 'store': { 'name': 'Other', 'store_id': 4 }, 'target_market_goal_id': 125, 'label_goal_value': 40, 'distribution_goal_value': 50 } ], } @pytest.fixture def market_goal_data(): """Return valid data to create target market goals in country. Returns: list: target market goals. """ return [ { 'store': {'store_id': 1, 'name': 'Amazon'}, 'label_goal_value': 200, 'distribution_goal_value': 300 }, { 'store': {'store_id': 2, 'name': 'HMV'}, 'label_goal_value': 37, 'distribution_goal_value': 33 } ] @pytest.fixture def existing_target_mkt_goals_to_compose_data_for_create(): """Return valid data of existing target market goals. Returns: list: target market goals data. """ return [ { 'target_market_goal_id': 123, 'sales_goal_id': 1, 'store_id': 1, 'label_goal_value': 30, 'distribution_goal_value': 40, 'country_id': 1, 'country_name': 'USA' }, { 'target_market_goal_id': 124, 'sales_goal_id': 1, 'store_id': 2, 'label_goal_value': 10, 'distribution_goal_value': 20, 'country_id': 1, 'country_name': 'USA' }] @pytest.fixture(scope='function') def fetch_by_product_id_successful_resp(): """Return valid fetch_by_product_id response. Returns: dict: sales goals data. """ return { 'sales_goals': { 'updated_by': 'oa:1234', 'first_week_estimate': 0, 'notes': None, 'sales_goal_id': 1, 'updated_at': '2017-04-13 12:33:33.652345', 'product_id': 1 }, 'target_market_goals': [ { 'goals': [ { 'store': { 'name': 'Amazon', 'store_id': 1 }, 'target_market_goal_id': '1', 'distribution_goal_value': 1, 'label_goal_value': 1 } ], 'country': {'name': 'USA', 'country_id': 1} }, { 'goals': [ { 'store': { 'name': 'Other', 'store_id': 4 }, 'target_market_goal_id': '1', 'distribution_goal_value': 1, 'label_goal_value': 1 }, { 'store': { 'name': 'Other', 'store_id': 4 }, 'target_market_goal_id': '1', 'distribution_goal_value': 1, 'label_goal_value': 1 }, ], 'country': {'name': 'Canada', 'country_id': 2} } ] } @pytest.fixture def fetch_by_product_id_resp_without_countries_hidden_retailers(): """Return valid fetch_by_product_id response. Return valid fetch_by_product_id response without the countries with hide_retailers equal to True. Returns: dict: sales goals data. """ return { 'sales_goals': { 'updated_by': 'oa:1234', 'first_week_estimate': 0, 'notes': None, 'sales_goal_id': 1, 'updated_at': '2017-04-13 12:33:33.652345', 'product_id': 1 }, 'target_market_goals': [ { 'goals': [ { 'store': { 'name': 'Amazon', 'store_id': 1 }, 'target_market_goal_id': '1', 'distribution_goal_value': 1, 'label_goal_value': 1 } ], 'country': { 'name': 'USA', 'country_id': 1 } }, ] } @pytest.mark.parametrize( 'account_type, account_id, product_ownership_status', [ ('', '', None), ('vendor', '567', 200)]) @pytest.mark.parametrize( 'request_body, notes', [ ({'sales_goals': {}}, None), ({'sales_goals': { 'notes': 'Test notes'}}, 'Test notes')]) def test_create_for_product_id_success( account_type, account_id, product_ownership_status, request_body, notes): """Test create_for_product_id for successful result.""" product_id = 343434 user_id = 'alw:4567' sales_goals_created = { 'sales_goal_id': 12, 'product_id': 343434, 'updated_by': 'alw:4567', 'updated_at': '2017-04-13 12:33:33.652345' } if account_type and account_id: (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=product_ownership_status)) .once()) (flexmock(sales_goal_model) .should_receive('create') .with_args( product_id=product_id, updated_by=user_id, notes=notes, hmv_notes=None) .and_return(response.Response(sales_goals_created)) .once()) result = sales_goals.create_for_product_id( product_id=product_id, sales_goal_data=request_body, account_type=account_type, account_id=account_id, user_id=user_id) assert result assert result.message == sales_goals_created def test_create_for_product_id_ownership_validation_fails(): """Test create_for_product_id ownership validation fails.""" product_id = 343434 user_id = 'alw:4567' account_type = 'vendor' account_id = '454' request_body = {'sales_goals': {}} ownership_validation_failure_message = 'Ownership validation failed' (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return( response.create_error_response( status=400, code=ows_services.OWNERSHIP_VALIDATION_ERROR, message=ownership_validation_failure_message)) .once()) (flexmock(sales_goal_model) .should_call('create') .never()) result = sales_goals.create_for_product_id( product_id=product_id, sales_goal_data=request_body, account_type=account_type, account_id=account_id, user_id=user_id) assert not result assert result.errors['message'] == ownership_validation_failure_message assert result.errors['code'] == ows_services.OWNERSHIP_VALIDATION_ERROR def test_create_for_product_id_creation_fails(): """Test create_for_product_id product creation fails.""" product_id = 343434 user_id = 'alw:4567' account_type = 'vendor' account_id = '454' request_body = {'sales_goals': {}} expected_error_message = ( 'Something went wrong while creating the sales goal') sales_goals_creation_result = response.create_fatal_response( expected_error_message) (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=200)) .once()) (flexmock(sales_goal_model) .should_receive('create') .with_args( product_id=product_id, notes=None, hmv_notes=None, updated_by=user_id) .and_return(sales_goals_creation_result) .once()) result = sales_goals.create_for_product_id( product_id=product_id, sales_goal_data=request_body, account_type=account_type, account_id=account_id, user_id=user_id) assert not result @pytest.mark.parametrize( 'goal, market_goals, stores', [ ({'sales_goal_id': 1234, 'first_week_estimate': 50, 'updated_at': '2017-04-13 12:33:33.652345', 'updated_by': 'oa:1234'}, [], []), ({'sales_goal_id': 1234, 'first_week_estimate': 50, 'updated_at': '2017-04-13 12:33:33.652345', 'updated_by': 'oa:1234'}, [{'target_market_goal_id': 1, 'sales_goal_id': 1234, 'store_id': 1, 'label_goal_value': 1, 'distribution_goal_value': 1, 'country_id': 1, 'country_name': 'USA'}, {'target_market_goal_id': 2, 'sales_goal_id': 1234, 'store_id': 8, 'label_goal_value': 1, 'distribution_goal_value': 1, 'country_id': 1, 'country_name': 'USA'}, ], [{'store_id': 1, 'name': 'Amazon'}, {'store_id': 8, 'name': 'Walmart'}, ]), ({'sales_goal_id': 1234, 'first_week_estimate': 50, 'updated_at': '2017-04-13 12:33:33.652345', 'updated_by': 'oa:1234'}, [{'target_market_goal_id': 1, 'sales_goal_id': 1234, 'store_id': 1, 'label_goal_value': 1, 'distribution_goal_value': 1, 'country_id': 1, 'country_name': 'USA'}, {'target_market_goal_id': 2, 'sales_goal_id': 1234, 'store_id': 8, 'label_goal_value': 1, 'distribution_goal_value': 1, 'country_id': 1, 'country_name': 'USA'}, ], [{'store_id': 1, 'name': 'Amazon'}, {'store_id': 8, 'name': 'Walmart'}, ]) ]) def test_format_fetched_goal_data( goal, market_goals, stores): """Test format fetched goal data utils function.""" total_data = {} countries_map = { arr['country_id']: arr['country_name'] for arr in market_goals} expected_result = { 'sales_goals': { 'sales_goal_id': goal['sales_goal_id'], 'first_week_estimate': goal['first_week_estimate'], 'updated_at': goal['updated_at'], 'updated_by': goal['updated_by'] } } target_goal = {} if market_goals: target_goal['first_week_estimate'] = None target_goal['country'] = { 'country_id': TEST_COUNTRY_ID, 'name': countries_map[TEST_COUNTRY_ID]} formatted_goals = [{ 'target_market_goal_id': market_goals[idx][ 'target_market_goal_id'], 'store': { 'store_id': market_goals[idx]['store_id'], 'name': [ store_data['name'] for store_data in stores if store_data['store_id'] == market_goals[idx]['store_id']][0], }, 'label_goal_value': market_goals[idx]['label_goal_value'], 'distribution_goal_value': market_goals[idx][ 'distribution_goal_value']} for idx, el in enumerate(market_goals)] total_data = [{ 'country_id': TEST_COUNTRY_ID, 'label_total': 1, 'distribution_total': 2}] target_goal['goals'] = { 'first_week_estimate': None, 'items': sales_goals._sort_target_mkt_goals(formatted_goals), 'total': {'label_total': 1, 'distribution_total': 2}} expected_target_goal_data = [] if target_goal: expected_target_goal_data.append(target_goal) expected_result['target_market_goals'] = expected_target_goal_data result = sales_goals.format_fetched_goal_data( goal, market_goals, stores, total_data) assert result == expected_result @pytest.mark.parametrize( 'account_type, account_id, product_ownership_status', [ ('', '', None), ('vendor', '567', 200)]) @pytest.mark.parametrize( 'model_target_goals_resp, model_store_resp, total_data', [ ([], [], []), ( [ { 'target_market_goal_id': '1', 'sales_goal_id': 1, 'store_id': 1, 'label_goal_value': 1, 'distribution_goal_value': 1, 'country_id': 1, 'country_name': 'USA' }, { 'target_market_goal_id': '2', 'sales_goal_id': 1, 'store_id': 4, 'label_goal_value': 1, 'distribution_goal_value': 1, 'country_id': 2, 'country_name': 'Canada' } ], [{'store_id': 1, 'name': 'Amazon'}], [ { 'country_id': 1, 'label_total': 1, 'distribution_total': 1 }, { 'distribution_total': 1, 'label_total': 1, 'country_id': 2 } ], ) ] ) def test_fetch_by_product_id_success( account_type, account_id, product_ownership_status, sales_goal_model_fetch_response, model_target_goals_resp, model_store_resp, fetch_by_product_id_successful_resp, total_data): """Test fetch_by_product_id success.""" model_target_goals_resp_copy = copy.deepcopy(model_target_goals_resp) user_id = 'alw:12345' if account_type and account_id: (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(sales_goal_model_fetch_response['product_id'])) .and_return(response.Response(status=product_ownership_status)) .once()) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(sales_goal_model_fetch_response['product_id']) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_model_fetch_response['sales_goal_id']) .and_return(response.Response(model_target_goals_resp_copy)) .once()) if model_target_goals_resp_copy: store_ids = list( {m_goal['store_id'] for m_goal in model_target_goals_resp_copy}) (flexmock(store) .should_receive('fetch_by_store_ids') .with_args(store_ids) .and_return(response.Response(model_store_resp)) .once()) sales_goals_for_country_response = None (flexmock(sales_goals_for_country) .should_receive('fetch_sales_goals_for_country') .and_return(sales_goals_for_country_response) .once()) (flexmock(sales_goals) .should_receive('format_fetched_goal_data') .with_args( sales_goal_model_fetch_response, model_target_goals_resp_copy, model_store_resp, total_data, sales_goals_for_country_response) .and_return(fetch_by_product_id_successful_resp) .once()) result = sales_goals.fetch_by_product_id( sales_goal_model_fetch_response['product_id'], account_type, account_id, user_id) assert result.status == 200 assert result.message == fetch_by_product_id_successful_resp def test_fetch_by_product_id_success_for_alw_user( sales_goal_model_fetch_response, target_market_goal_model_fetch_response, store_model_fetch_response, fetch_by_product_id_successful_resp ): """Expect fetch_by_product_id doesn't return notes for alw user.""" total_data = [ {'country_id': 1, 'label_total': 1, 'distribution_total': 1}, {'distribution_total': 1, 'label_total': 1, 'country_id': 2}] fetch_by_product_id_resp = fetch_by_product_id_successful_resp fetch_by_product_id_resp_copy = copy.deepcopy(fetch_by_product_id_resp) account_type = 'vendor' account_id = '567' (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(sales_goal_model_fetch_response['product_id'])) .and_return(response.Response(status=200)) .once()) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(sales_goal_model_fetch_response['product_id']) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_model_fetch_response['sales_goal_id']) .and_return(response.Response(target_market_goal_model_fetch_response)) .once()) if target_market_goal_model_fetch_response: store_ids = list( {m_goal['store_id'] for m_goal in target_market_goal_model_fetch_response}) (flexmock(store) .should_receive('fetch_by_store_ids') .with_args(store_ids) .and_return(response.Response(store_model_fetch_response)) .once()) sales_goals_for_country_response = None (flexmock(sales_goals_for_country) .should_receive('fetch_sales_goals_for_country') .and_return(sales_goals_for_country_response) .once()) (flexmock(sales_goals) .should_receive('format_fetched_goal_data') .with_args( sales_goal_model_fetch_response, target_market_goal_model_fetch_response, store_model_fetch_response, total_data, sales_goals_for_country_response) .and_return(fetch_by_product_id_resp) .once()) result = sales_goals.fetch_by_product_id( sales_goal_model_fetch_response['product_id'], account_type, account_id, 'alw:12345') del fetch_by_product_id_resp_copy['sales_goals']['notes'] assert result.status == 200 assert result.message == fetch_by_product_id_resp_copy @pytest.mark.parametrize( 'account_type, account_id, product_ownership_status', [ ('vendor', '567', 403)]) def test_fetch_by_product_id_ownership_validation_failed( account_type, account_id, product_ownership_status): """Test fetch_by_product_id product ownership validation fails.""" product_id = 1 (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=product_ownership_status)) .once()) result = sales_goals.fetch_by_product_id( product_id, account_type, account_id) assert result.status == 403 @pytest.mark.parametrize( 'sales_goals_hidden_retailers_enabled', [True, False]) def test_fetch_by_product_id_sales_goal_not_found( sales_goal_model_fetch_response, sales_goals_hidden_retailers_enabled): """Test fetch_by_product_id product sales goal fetching fails.""" (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(sales_goal_model_fetch_response['product_id']) .and_return(response.create_not_found_response()) .once()) result = sales_goals.fetch_by_product_id( sales_goal_model_fetch_response['product_id'], '', '') assert result.status == 404 def test_fetch_by_product_id_target_goals_internal_error( sales_goal_model_fetch_response): """Test fetch_by_product_id product target goals fetching fails.""" error_msg = 'Failed fetching target goals' (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(sales_goal_model_fetch_response['product_id']) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_model_fetch_response['sales_goal_id']) .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals.fetch_by_product_id( sales_goal_model_fetch_response['product_id'], '', '') assert result.status == 500 assert result.errors['message'] == error_msg def test_fetch_by_product_id_stores_internal_error( sales_goal_model_fetch_response, target_market_goal_model_fetch_response): """Test fetch_by_product_id product stores fetching fails.""" error_msg = 'Failed fetching stores data' (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(sales_goal_model_fetch_response['product_id']) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_model_fetch_response['sales_goal_id']) .and_return(response.Response(target_market_goal_model_fetch_response)) .once()) store_ids = list( {m_goal['store_id'] for m_goal in target_market_goal_model_fetch_response}) (flexmock(store) .should_receive('fetch_by_store_ids') .with_args(store_ids) .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals.fetch_by_product_id( sales_goal_model_fetch_response['product_id'], '', '') assert result.status == 500 assert result.errors['message'] == error_msg def test_create_target_market_goals_for_sales_goal_success( valid_post_product_country_request_data, target_market_goal_model_fetch_response): """Test function _create_target_market_goals_for_sales_goal success.""" sales_goal_id = 1 user_id = 'oa:12345' country_id = 1 country_name = 'USA' store_id = valid_post_product_country_request_data[ 'goals'][0]['store']['store_id'] market_goals = valid_post_product_country_request_data['goals'] (flexmock(target_market_goal) .should_receive('create') .and_return(response.Response(target_market_goal_model_fetch_response)) .once()) result = sales_goals._create_target_market_goals_for_sales_goal( sales_goal_id, country_id, country_name, user_id, market_goals) assert result assert result.message['store_ids'] == [store_id] assert result.message['created_goals'] == [ target_market_goal_model_fetch_response] def test_create_target_market_goals_for_sales_goal_failed( valid_post_product_country_request_data): """Test function _create_target_market_goals_for_sales_goal failure.""" error_msg = 'Failed to create goal' sales_goal_id = 1 user_id = 'oa:12345' country_id = 1 country_name = 'USA' market_goals = valid_post_product_country_request_data['goals'] (flexmock(target_market_goal) .should_receive('create') .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals._create_target_market_goals_for_sales_goal( sales_goal_id, country_id, country_name, user_id, market_goals) assert not result assert result.errors['message'] == error_msg def test_create_distr_target_market_goals_for_alw_user_failed( valid_post_product_country_request_data): """Test function _create_target_market_goals_for_sales_goal failure.""" error_msg = ( 'User alw:12345 can not perform actions on distribution value: ' 'distribution_goal_value') sales_goal_id = 1 user_id = 'alw:12345' country_id = 1 country_name = 'USA' market_goals = valid_post_product_country_request_data['goals'] (flexmock(target_market_goal) .should_receive('create') .never()) result = sales_goals._create_target_market_goals_for_sales_goal( sales_goal_id, country_id, country_name, user_id, market_goals) assert not result assert result.errors['message'] == error_msg def test_format_create_target_market_goal_response(): """Test for format_create_target_market_goal_response.""" update_data = { 'updated_by': 'oa:34765', 'updated_at': '2017-08-11T12:22:35' } goals = [{ 'store_id': 1, 'name': 'Amazon', 'target_market_goal_id': 1, 'label_goal_value': 20, 'distribution_goal_value': 30}] total_data = {'label_total': 100, 'distribution_total': 999} stores_map = {1: 'Amazon'} expected_result = { 'updated_by': 'oa:34765', 'updated_at': '2017-08-11T12:22:35', 'goals': { 'first_week_estimate': None, 'items': [{ 'target_market_goal_id': goals[0]['target_market_goal_id'], 'store': { 'store_id': goals[0]['store_id'], 'name': goals[0]['name']}, 'label_goal_value': goals[0]['label_goal_value'], 'distribution_goal_value': goals[0][ 'distribution_goal_value']}], 'total': {'label_total': 100, 'distribution_total': 999} }, } result = sales_goals._format_create_target_market_goal_response( goals, stores_map, update_data, total_data, ) assert result == expected_result @pytest.mark.parametrize( 'fetch_by_sales_goal_id_response', [ (response.Response([{ 'target_market_goal_id': 123, 'sales_goal_id': 1, 'store_id': 1, 'label_goal_value': 1, 'distribution_goal_value': 1, 'country_id': 1, 'country_name': 'USA' }] )), (response.create_not_found_response()) ] ) @pytest.mark.parametrize( 'first_week_estimate', [0, 33] ) @pytest.mark.parametrize( 'notes', ['Test notes', ''] ) def test_update_for_product_id_success( sales_goal_data_for_patch, sales_goal_model_fetch_response, sales_goal_data_to_dict, first_week_estimate, fetch_by_sales_goal_id_response, notes, fetch_by_product_id_successful_resp): """Test update_for_product_id success.""" account_type = 'vendor' account_id = '567' product_id = 3 user_id = 'oa:2345' product_ownership_status = 200 sales_goal_id = sales_goal_model_fetch_response.get('sales_goal_id') sales_goal_data_for_patch['first_week_estimate'] = first_week_estimate sales_goal_data_for_patch['notes'] = notes if account_type and account_id: (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=product_ownership_status)) .once()) sales_goal_to_update = response.Response(sales_goal_model_fetch_response) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(sales_goal_to_update) .once()) (flexmock(sales_goals_for_country) .should_receive('upsert_sales_goals_for_country')) (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_id) .and_return(fetch_by_sales_goal_id_response) .once()) (flexmock(sales_goals) .should_receive('_update_target_market_goals_in_country') .and_return(response.Response()) .once()) (flexmock(sales_goal_model) .should_receive('update') .with_args( sales_goal_id=sales_goal_id, values={ 'first_week_estimate': sales_goal_data_for_patch.get( 'first_week_estimate'), 'notes': sales_goal_data_for_patch.get('notes'), 'updated_by': user_id}) .and_return(response.Response(sales_goal_data_to_dict)) .once()) fetch_by_id_result = { 'sales_goals': { 'updated_by': 'alw:2345', 'first_week_estimate': first_week_estimate, 'notes': notes, 'sales_goal_id': 165, 'updated_at': '2017-04-14 12:33:33.652345', 'product_id': 8765432 }, 'target_market_goals': [ { 'goals': [ { 'store': { 'name': 'Amazon', 'store_id': 1 }, 'target_market_goal_id': 123, 'label_goal_value': 20, 'distribution_goal_value': 30 } ], 'country': {'name': 'USA', 'country_id': 1} } ] } (flexmock(sales_goals) .should_receive('fetch_by_product_id') .with_args(product_id, user_id=user_id) .and_return(response.Response(fetch_by_id_result)) .once()) result = sales_goals.update_for_product_id( product_id, sales_goal_data_for_patch, account_type, account_id, user_id) assert result assert result.message == fetch_by_id_result def test_update_for_product_id_fails_if_target_market_goal_editing_restriction( sales_goal_data_for_patch, sales_goal_model_fetch_response): """Test update_for_product_id fails because of editing restriction. Test update_for_product_id fails in case the distribution_goal_value is provided and editing is performed by label user. """ product_id = 3 user_id = 'alw:2345' (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .never()) sales_goal_to_update = response.Response(sales_goal_model_fetch_response) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(sales_goal_to_update) .once()) existing_target_market_goals = [{ 'target_market_goal_id': 123, 'sales_goal_id': 1, 'store_id': 1, 'label_goal_value': 1, 'distribution_goal_value': 1, 'country_id': 1, 'country_name': 'USA' }] (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(1) .and_return(response.Response(existing_target_market_goals)) .once()) (flexmock(sales_goals) .should_receive('_update_target_market_goals') .with_args(sales_goal_data_for_patch['target_market_goals'][0]) .and_return(response.Response()) .never()) result = sales_goals.update_for_product_id( product_id=product_id, sales_goal_data=sales_goal_data_for_patch, user_id=user_id) assert not result assert result.errors['message'] == ( 'User alw:2345 can not perform actions on distribution value: ' 'distribution_goal_value') def test_check_access_for_user_to_edit_target_market_goal_denied(): """Check access for label user to edit distribution goal denied.""" distribution_goal_value = 432 user_id = 'alw:223' result = sales_goals.check_access_for_user_to_edit_target_market_goal( distribution_goal_value, user_id) assert not result assert result.errors['code'] == 'permission_denied' @pytest.mark.parametrize( 'distribution_goal_value, user_id', [ (432, 'oa:6565'), (0, 'alw:223') ]) def test_check_access_for_user_to_edit_target_market_goal_allowed( distribution_goal_value, user_id): """Check access for user to edit distribution goal allowed.""" result = sales_goals.check_access_for_user_to_edit_target_market_goal( distribution_goal_value, user_id) assert result @pytest.mark.parametrize( 'account_type, account_id, product_ownership_status', [ ('vendor', '567', 403)]) def test_update_for_product_id_ownership_validation_failed( account_type, account_id, product_ownership_status, sales_goal_data_for_patch): """Test update_for_product_id product ownership validation fails.""" product_id = 1 (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=product_ownership_status)) .once()) result = sales_goals.update_for_product_id( product_id, sales_goal_data_for_patch, account_type, account_id) assert result.status == 403 @pytest.mark.parametrize( 'account_type, account_id, product_ownership_status', [ ('', '', None), ('vendor', '567', 200)]) def test_create_target_market_goal_success( account_type, account_id, product_ownership_status, sales_goal_model_fetch_response, valid_countries_get_response, store_model_fetch_response, target_market_goal_model_fetch_response, valid_post_product_country_request_data, ): """Test create_target_market_goal success.""" product_id = 1 country_id = 1 request_data = valid_post_product_country_request_data user_id = sales_goal_model_fetch_response['updated_by'] expected_result = { 'updated_at': '2017-04-13 12:33:33.652345', 'updated_by': user_id, 'goals': [ { 'target_market_goal_id': target_market_goal_model_fetch_response[0][ 'target_market_goal_id'], 'store': { 'store_id': valid_post_product_country_request_data[ 'goals'][0]['store']['store_id'], 'name': valid_post_product_country_request_data[ 'goals'][0]['store']['name']}, 'label_goal_value': valid_post_product_country_request_data[ 'goals'][0]['label_goal_value'], 'distribution_goal_value': valid_post_product_country_request_data[ 'goals'][0]['distribution_goal_value']}]} if account_type and account_id: (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=product_ownership_status)) .once()) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(countries) .should_receive('get_countries') .and_return(response.Response(valid_countries_get_response)) .once()) (flexmock(sales_goals) .should_receive('_get_country_name_by_id') .with_args( valid_countries=valid_countries_get_response['items'], country_id=country_id) .and_return(response.Response( valid_countries_get_response['items'][0]['name'])) .once()) (flexmock(sales_goals) .should_receive('_validate_store_in_country_goal_data') .with_args( valid_countries=valid_countries_get_response['items'], country_id=country_id, market_goal_data=request_data[models.GOALS]) .and_return(response.Response()) .once()) store_ids = [ goal['store_id'] for goal in target_market_goal_model_fetch_response] create_target_market_goals_for_sales_goal_response = { 'created_goals': target_market_goal_model_fetch_response, 'store_ids': store_ids} (flexmock(sales_goals) .should_receive('_create_or_update_target_market_goal') .and_return(response.Response( create_target_market_goals_for_sales_goal_response)) .once()) (flexmock(store) .should_receive('fetch_by_store_ids') .and_return(response.Response(store_model_fetch_response)) .once()) (flexmock(sales_goal_model) .should_receive('update') .and_return(response.Response()) .once()) expected_update_data = { 'updated_by': user_id, 'updated_at': sales_goal_model_fetch_response['updated_at'] } (flexmock(sales_goals) .should_receive('_compose_update_data') .and_return(response.Response(expected_update_data)) .once()) (flexmock(sales_goals) .should_receive('_format_create_target_market_goal_response') .and_return(expected_result) .once()) result = sales_goals.create_target_market_goal( product_id=product_id, country_id=country_id, market_goal_data=request_data, account_id=account_id, account_type=account_type, user_id=user_id) assert result.status == 200 assert isinstance(result.message['goals'], list) assert result.message == expected_result @pytest.mark.parametrize('user_type', ('alw', 'oa')) def test_create_target_market_goal_hidden_retailer_flag_enabled( sales_goal_model_fetch_response, valid_countries_get_response, store_model_fetch_response, target_market_goal_model_fetch_response, valid_post_product_country_request_data, user_type): """Test create_target_market_goal.""" product_id = 1 country_id = 1 request_data = valid_post_product_country_request_data user_id = '{user_type}:1234'.format(user_type=user_type) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(countries) .should_receive('get_countries') .and_return(response.Response(valid_countries_get_response)) .once()) (flexmock(sales_goals) .should_receive('_get_country_name_by_id') .with_args( valid_countries=valid_countries_get_response['items'], country_id=country_id) .and_return(response.Response( valid_countries_get_response['items'][0]['name'])) .once()) (flexmock(sales_goals) .should_receive('_validate_store_in_country_goal_data') .with_args( valid_countries=valid_countries_get_response['items'], country_id=country_id, market_goal_data=request_data[models.GOALS]) .and_return(response.Response()) .once()) label_goal = 9999 goal_create_response = response.Response( {'store_ids': [1], 'created_goals': [ {'label_goal_value': label_goal, 'country_id': 1, 'country_name': 'United States', 'sales_goal_id': 1, 'store_id': 1, 'target_market_goal_id': 95, 'distribution_goal_value': 9999}]}) (flexmock(sales_goals) .should_receive('_create_or_update_target_market_goal') .and_return(goal_create_response) .once()) (flexmock(store) .should_receive('fetch_by_store_ids') .and_return(response.Response(store_model_fetch_response)) .once()) (flexmock(sales_goal_model) .should_receive('update') .and_return(response.Response()) .once()) expected_update_data = { 'updated_by': user_id, 'updated_at': sales_goal_model_fetch_response['updated_at'] } (flexmock(sales_goals) .should_receive('_compose_update_data') .and_return(response.Response(expected_update_data)) .once()) result = sales_goals.create_target_market_goal( product_id=product_id, country_id=country_id, market_goal_data=request_data, user_id=user_id) assert result.status == 200 assert isinstance(result.message['goals'], dict) assert isinstance(result.message['goals']['total'], dict) assert result.message['goals']['total']['label_total'] == label_goal def test_create_target_market_goal_ownership_validation_failed( sales_goal_model_fetch_response, valid_post_product_country_request_data): """Test create_target_market_goal if ownership validation failed.""" error_msg = 'get_product_ownership_by_account returned error' account_type = 'vendor' account_id = '1' product_id = 1 country_id = 1 request_data = valid_post_product_country_request_data user_id = sales_goal_model_fetch_response['updated_by'] (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals.create_target_market_goal( product_id=product_id, country_id=country_id, market_goal_data=request_data, account_id=account_id, account_type=account_type, user_id=user_id) assert result.status == 500 assert result.errors['message'] == error_msg def test_create_target_market_goal_fetch_goal_by_product_id_failed( sales_goal_model_fetch_response, valid_post_product_country_request_data): """Test create_target_market_goal fetch_goal_by_product_id failed.""" error_msg = 'fetch_by_product_id returned error' product_id = 1 country_id = 1 request_data = valid_post_product_country_request_data user_id = sales_goal_model_fetch_response['updated_by'] (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals.create_target_market_goal( product_id=product_id, country_id=country_id, market_goal_data=request_data, user_id=user_id) assert result.status == 500 assert result.errors['message'] == error_msg def test_create_target_market_goal_get_sales_goals_country_name_failed( sales_goal_model_fetch_response, valid_countries_get_response, valid_post_product_country_request_data): """Test create_target_market_goal _get_country_name_by_id failed.""" error_msg = '_get_country_name_by_id returned error' product_id = 1 country_id = 1 request_data = valid_post_product_country_request_data user_id = sales_goal_model_fetch_response['updated_by'] (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(countries) .should_receive('get_countries') .and_return(response.Response(valid_countries_get_response)) .once()) (flexmock(sales_goals) .should_receive('_get_country_name_by_id') .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals.create_target_market_goal( product_id=product_id, country_id=country_id, market_goal_data=request_data, user_id=user_id) assert result.status == 500 assert result.errors['message'] == error_msg def test_create_target_market_goal_create_target_market_goal_failed( sales_goal_model_fetch_response, valid_countries_get_response, valid_post_product_country_request_data ): """Test create_target_market_goal create target market goal failed.""" error_msg = 'create target market goal returned error' product_id = 1 country_id = 1 request_data = valid_post_product_country_request_data user_id = sales_goal_model_fetch_response['updated_by'] (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(countries) .should_receive('get_countries') .and_return(response.Response(valid_countries_get_response)) .once()) (flexmock(sales_goals) .should_receive('_get_country_name_by_id') .with_args( valid_countries=valid_countries_get_response['items'], country_id=country_id) .and_return(response.Response( valid_countries_get_response['items'][0]['name'])) .once()) (flexmock(sales_goals) .should_receive('_validate_store_in_country_goal_data') .with_args( valid_countries=valid_countries_get_response['items'], country_id=country_id, market_goal_data=request_data[models.GOALS]) .and_return(response.Response()) .once()) (flexmock(sales_goals) .should_receive('_create_or_update_target_market_goal') .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals.create_target_market_goal( product_id=product_id, country_id=country_id, market_goal_data=request_data, user_id=user_id) assert result.status == 500 assert result.errors['message'] == error_msg def test_create_target_market_goal_fetch_by_store_ids_failed( sales_goal_model_fetch_response, valid_countries_get_response, valid_post_product_country_request_data, target_market_goal_model_fetch_response): """Test create_target_market_goal fetch_by_store_ids failed.""" error_msg = 'fetch_by_store_ids returned error' product_id = 1 country_id = 1 request_data = valid_post_product_country_request_data user_id = sales_goal_model_fetch_response['updated_by'] (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(countries) .should_receive('get_countries') .and_return(response.Response(valid_countries_get_response)) .once()) (flexmock(sales_goals) .should_receive('_get_country_name_by_id') .with_args( valid_countries=valid_countries_get_response['items'], country_id=country_id) .and_return(response.Response( valid_countries_get_response['items'][0]['name'])) .once()) (flexmock(sales_goals) .should_receive('_validate_store_in_country_goal_data') .with_args( valid_countries=valid_countries_get_response['items'], country_id=country_id, market_goal_data=request_data[models.GOALS]) .and_return(response.Response()) .once()) store_ids = [ goal['store_id'] for goal in target_market_goal_model_fetch_response] create_target_market_goals_for_sales_goal_response = { 'created_goals': target_market_goal_model_fetch_response, 'store_ids': store_ids} (flexmock(sales_goals) .should_receive('_create_or_update_target_market_goal') .and_return(response.Response( create_target_market_goals_for_sales_goal_response)) .once()) (flexmock(store) .should_receive('fetch_by_store_ids') .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals.create_target_market_goal( product_id=product_id, country_id=country_id, market_goal_data=request_data, user_id=user_id) assert result.status == 500 assert result.errors['message'] == error_msg def test_create_target_market_goal_update_sales_goal_failed( sales_goal_model_fetch_response, valid_countries_get_response, valid_post_product_country_request_data, target_market_goal_model_fetch_response, store_model_fetch_response ): """Test create_target_market_goal update sales_goal failed.""" error_msg = 'sales_goal update returned error' product_id = 1 country_id = 1 request_data = valid_post_product_country_request_data user_id = sales_goal_model_fetch_response['updated_by'] (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(countries) .should_receive('get_countries') .and_return(response.Response(valid_countries_get_response)) .once()) (flexmock(sales_goals) .should_receive('_get_country_name_by_id') .with_args( valid_countries=valid_countries_get_response['items'], country_id=country_id) .and_return(response.Response( valid_countries_get_response['items'][0]['name'])) .once()) (flexmock(sales_goals) .should_receive('_validate_store_in_country_goal_data') .with_args( valid_countries=valid_countries_get_response['items'], country_id=country_id, market_goal_data=request_data[models.GOALS]) .and_return(response.Response()) .once()) store_ids = [ goal['store_id'] for goal in target_market_goal_model_fetch_response] create_target_market_goals_for_sales_goal_response = { 'created_goals': target_market_goal_model_fetch_response, 'store_ids': store_ids} (flexmock(sales_goals) .should_receive('_create_or_update_target_market_goal') .and_return(response.Response( create_target_market_goals_for_sales_goal_response)) .once()) (flexmock(store) .should_receive('fetch_by_store_ids') .and_return(response.Response(store_model_fetch_response)) .once()) (flexmock(sales_goal_model) .should_receive('update') .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals.create_target_market_goal( product_id=product_id, country_id=country_id, market_goal_data=request_data, user_id=user_id) assert result.status == 500 assert result.errors['message'] == error_msg def test_create_target_market_goal_store_validation_failed( sales_goal_model_fetch_response, valid_countries_get_response, valid_post_product_country_request_data): """Expect to get Not Found Error if store in country doesn't exist.""" product_id = 1 country_id = 1 error_msg = (error.ERROR_MESSAGE_WRONG_STORE_ID_WAS_PROVIDED .format(country_id=country_id)) request_data = valid_post_product_country_request_data user_id = sales_goal_model_fetch_response['updated_by'] (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(countries) .should_receive('get_countries') .and_return(response.Response(valid_countries_get_response)) .once()) (flexmock(sales_goals) .should_receive('_get_country_name_by_id') .with_args( valid_countries=valid_countries_get_response['items'], country_id=country_id) .and_return(response.Response( valid_countries_get_response['items'][0]['name'])) .once()) (flexmock(sales_goals) .should_receive('_validate_store_in_country_goal_data') .and_return(response.create_not_found_response( message=error_msg)) .once()) result = sales_goals.create_target_market_goal( product_id=product_id, country_id=country_id, market_goal_data=request_data, user_id=user_id) assert result.status == 404 assert result.errors['message'] == error_msg def test__get_country_name_by_id_success(valid_countries_get_response): """Test _get_country_name_by_id for successful result.""" valid_countries = valid_countries_get_response['items'] country_id = valid_countries_get_response['items'][0]['country_id'] expected_result = valid_countries_get_response['items'][0]['name'] result = sales_goals._get_country_name_by_id( country_id=country_id, valid_countries=valid_countries) assert result assert result.message == expected_result def test__get_country_name_by_id_invalid_country_id( valid_countries_get_response): """Expect to get error response when invalid_country_id is provided.""" non_existing_country_id = 100 valid_countries = valid_countries_get_response['items'] result = sales_goals._get_country_name_by_id( valid_countries=valid_countries, country_id=non_existing_country_id) assert result.status == 404 assert result.errors['message'] == ( error.ERROR_MESSAGE_COUNTRY_DO_NOT_EXIST.format( country_id=non_existing_country_id)) @pytest.mark.parametrize( 'account_type, account_id, product_ownership_status', [ ('', '', None), ('vendor', '567', 200)]) @pytest.mark.parametrize( 'goals_deletion_status', [ (200), (404), ]) def test_delete_target_market_goal_success( account_type, account_id, product_ownership_status, sales_goal_model_fetch_response, goals_deletion_status): """Test delete_target_market_goal for successful result.""" product_id = sales_goal_model_fetch_response['product_id'] sales_goal_id = sales_goal_model_fetch_response['sales_goal_id'] country_id = 1 if account_type and account_id: (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=product_ownership_status)) .once()) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) update_data = { 'updated_by': 'oa:123', 'updated_at': '2017-07-13 12:33:33.652345' } (flexmock(sales_goals) .should_receive('_compose_update_data') .and_return(response.Response(message=update_data)) .once()) (flexmock(target_market_goal) .should_receive('delete_by_sales_goal_id_and_country_id') .with_args(sales_goal_id=sales_goal_id, country_id=country_id) .and_return(response.Response(status=goals_deletion_status)) .once()) (flexmock(sales_goal_model) .should_receive('update') .and_return(response.Response()) .once()) result = sales_goals.delete_target_market_goal( product_id, country_id, account_type, account_id) assert result assert result.message == update_data def test_delete_target_market_goal_ownership_validation_failed( sales_goal_model_fetch_response): """Test delete_target_market_goal for successful validation failed.""" error_msg = 'ownership validation failed' product_id = sales_goal_model_fetch_response['product_id'] country_id = 1 account_id = '1234' account_type = 'vendor' (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals.delete_target_market_goal( product_id, country_id, account_type, account_id) assert not result assert result.errors['message'] == error_msg def test_delete_target_market_goal_sales_goal_fetch_failed( sales_goal_model_fetch_response): """Test delete_target_market_goal for data fetching failed.""" error_msg = 'Sales goal fetching failed' product_id = sales_goal_model_fetch_response['product_id'] country_id = 1 (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals.delete_target_market_goal( product_id, country_id) assert not result assert result.errors['message'] == error_msg def test_delete_target_market_goal_goal_deletion_failed( sales_goal_model_fetch_response): """Test delete_target_market_goal for goal deletion failed.""" error_msg = 'Driver deletion failed' product_id = sales_goal_model_fetch_response['product_id'] sales_goal_id = sales_goal_model_fetch_response['sales_goal_id'] country_id = 1 (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(sales_goal_model) .should_receive('update') .and_return(response.Response()) .once()) update_data = { 'updated_by': 'oa:123', 'updated_at': '2017-07-13 12:33:33.652345' } (flexmock(sales_goals) .should_receive('_compose_update_data') .and_return(response.Response(message=update_data)) .once()) (flexmock(target_market_goal) .should_receive('delete_by_sales_goal_id_and_country_id') .with_args(sales_goal_id=sales_goal_id, country_id=country_id) .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals.delete_target_market_goal(product_id, country_id) assert not result assert result.errors['message'] == error_msg def test_delete_target_market_goal_update_failed( sales_goal_model_fetch_response): """Test delete_target_market_goal for goal update failed.""" error_msg = 'Sales goal update failed' product_id = sales_goal_model_fetch_response['product_id'] country_id = 1 (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.Response(sales_goal_model_fetch_response)) .once()) (flexmock(sales_goal_model) .should_receive('update') .and_return(response.create_fatal_response(error_msg)) .once()) result = sales_goals.delete_target_market_goal(product_id, country_id) assert not result assert result.errors['message'] == error_msg def test_helper_update_target_market_goals_success(goal_data_for_country): """Test _update_target_market_goals for success.""" target_market_goals_update_kwargs = { 'target_market_goal_id': goal_data_for_country['goals'][0]['target_market_goal_id'], 'label_goal_value': goal_data_for_country['goals'][0]['label_goal_value'], 'distribution_goal_value': goal_data_for_country['goals'][0]['distribution_goal_value'] } update_response = { 'sales_goal_id': 1, 'country_id': 1, 'country_name': 'Target', 'store_id': 7, 'target_market_goal_id': 123, 'label_goal_value': 20, 'distribution_goal_value': 30 } (flexmock(target_market_goal) .should_receive('update') .with_args(**target_market_goals_update_kwargs) .and_return(response.Response(update_response)) .once()) expected_result = { 'store_ids': [update_response['store_id']], 'created_goals': [update_response] } result = sales_goals._update_target_market_goals(goal_data_for_country) assert result.message == expected_result def test_helper_update_target_market_goals_success_if_multiple_target_goals( goal_data_for_country_with_multiple_target_market_goals): """Test _update_target_market_goals for success. Test _update_target_market_goals with multiple target market goals in goal_data_for_country. """ for target_market_goal_data in ( goal_data_for_country_with_multiple_target_market_goals['goals']): target_market_goals_update_kwargs = { 'target_market_goal_id': target_market_goal_data['target_market_goal_id'], 'label_goal_value': target_market_goal_data['label_goal_value'], 'distribution_goal_value': target_market_goal_data['distribution_goal_value'] } update_response = { 'sales_goal_id': 1, 'country_id': 1, 'country_name': 'USA', 'store_id': target_market_goal_data['store']['store_id'], 'target_market_goal_id': target_market_goal_data[ 'target_market_goal_id'], 'label_goal_value': target_market_goal_data['label_goal_value'], 'distribution_goal_value': target_market_goal_data[ 'distribution_goal_value'] } (flexmock(target_market_goal) .should_receive('update') .with_args(**target_market_goals_update_kwargs) .and_return(response.Response(update_response)) .once()) expected_result = { 'created_goals': [ { 'sales_goal_id': 1, 'country_id': 1, 'distribution_goal_value': 30, 'target_market_goal_id': 123, 'label_goal_value': 20, 'store_id': 1, 'country_name': 'USA' }, { 'sales_goal_id': 1, 'country_id': 1, 'distribution_goal_value': 100, 'target_market_goal_id': 124, 'label_goal_value': 200, 'store_id': 2, 'country_name': 'USA' }, { 'sales_goal_id': 1, 'country_id': 1, 'distribution_goal_value': 50, 'target_market_goal_id': 125, 'label_goal_value': 40, 'store_id': 4, 'country_name': 'USA' } ], 'store_ids': [1, 2, 4] } result = sales_goals._update_target_market_goals( goal_data_for_country_with_multiple_target_market_goals) assert result.message == expected_result @pytest.mark.parametrize( 'failure_response, response_status', [ (response.create_fatal_response(), 500), (response.create_error_response( code='path_update_error', message='Something went wrong during patch update'), 400)]) def test_helper_update_target_market_goals_failure( goal_data_for_country, failure_response, response_status): """Test update_target_market_goals for failure.""" target_market_goals_update_kwargs = { 'target_market_goal_id': goal_data_for_country['goals'][0]['target_market_goal_id'], 'label_goal_value': goal_data_for_country['goals'][0]['label_goal_value'], 'distribution_goal_value': goal_data_for_country['goals'][0]['distribution_goal_value'] } (flexmock(target_market_goal) .should_receive('update') .with_args(**target_market_goals_update_kwargs) .and_return(failure_response) .once()) result = sales_goals._update_target_market_goals(goal_data_for_country) assert not result assert result.status == response_status def test_update_for_product_id_success_without_target_market_goals( sales_goal_model_fetch_response, sales_goal_data_to_dict): """Test update_for_product_id success if no target mkt goals provided.""" account_type = 'vendor' account_id = '567' product_id = 3 user_id = 'alw:2345' product_ownership_status = 200 sales_goal_kwargs_for_patch = {'first_week_estimate': 100} sales_goal_id = sales_goal_model_fetch_response.get('sales_goal_id') if account_type and account_id: (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=product_ownership_status)) .once()) sales_goal_to_update = response.Response(sales_goal_model_fetch_response) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(sales_goal_to_update) .once()) (flexmock(sales_goals) .should_receive('_update_target_market_goals') .and_return(response.Response()) .never()) (flexmock(sales_goal_model) .should_receive('update') .with_args( sales_goal_id=sales_goal_id, values={ 'first_week_estimate': sales_goal_kwargs_for_patch[ 'first_week_estimate'], 'updated_by': user_id}) .and_return(response.Response(sales_goal_data_to_dict)) .once()) fetch_by_id_result = { 'sales_goals': { 'updated_by': 'alw:2345', 'first_week_estimate': 100, 'sales_goal_id': sales_goal_id, 'updated_at': '2017-04-14 12:33:33.652345', 'product_id': 8765432 } } (flexmock(sales_goals) .should_receive('fetch_by_product_id') .with_args(product_id, user_id=user_id) .and_return(response.Response(fetch_by_id_result)) .once()) result = sales_goals.update_for_product_id( product_id, sales_goal_kwargs_for_patch, account_type, account_id, user_id) assert result assert result.message == fetch_by_id_result def test__compose_update_data_success(): """Test compose update data success.""" user_id = 'oa:123445' product_id = 333 update_date = '2017-04-14 12:33:33.652345' fetch_by_id_result = { 'updated_by': user_id, 'first_week_estimate': 100, 'sales_goal_id': 22, 'updated_at': update_date, 'product_id': product_id } (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.Response(fetch_by_id_result)) .once()) result = sales_goals._compose_update_data(user_id, product_id) expected_result = { 'updated_by': user_id, 'updated_at': update_date } assert result.message == expected_result def test__compose_update_data_failure(): """Test compose update data fails if fetch_by_product_id returns error.""" user_id = 'oa:123445' product_id = 333 (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.create_not_found_response()) .once()) result = sales_goals._compose_update_data(user_id, product_id) assert not result def test__update_target_market_goals_in_country_success(goal_data_for_country): """Test _update_target_market_goals_in_country success result.""" user_id = 'oa:223344' goals_in_country = goal_data_for_country.get(models.GOALS) (flexmock(sales_goals) .should_receive('check_access_for_user_to_edit_target_market_goal') .with_args(goals_in_country[0]['distribution_goal_value'], user_id) .and_return(response.Response()) .once()) (flexmock(sales_goals) .should_receive('_update_target_market_goals') .with_args(goal_data_for_country) .and_return(response.Response()) .once()) existing_target_market_goals = [{ 'target_market_goal_id': 1, 'sales_goal_id': 1, 'store_id': 7, 'label_goal_value': 44, 'distribution_goal_value': 55, 'country_id': 1, 'country_name': 'USA' }] result = sales_goals._update_target_market_goals_in_country( user_id, goals_in_country, goal_data_for_country, existing_target_market_goals) assert result @pytest.mark.parametrize( 'check_access_times_called, check_access_response,' 'update_target_market_goals_times_called,' 'update_target_market_goals_response', [ (1, response.Response(), 1, response.create_error_response( code='test_error_code', message='Test error message')), (1, response.create_error_response( code='test_error_code', message='Test error message'), 0, response.Response()), ] ) def test__update_target_market_goals_in_country_fails( goal_data_for_country, check_access_times_called, check_access_response, update_target_market_goals_times_called, update_target_market_goals_response, target_market_goal_model_fetch_response ): """Test _update_target_market_goals_in_country failure result.""" user_id = 'oa:223344' goals_in_country = goal_data_for_country.get(models.GOALS) (flexmock(sales_goals) .should_receive('check_access_for_user_to_edit_target_market_goal') .with_args(goals_in_country[0]['distribution_goal_value'], user_id) .and_return(check_access_response) .times(check_access_times_called)) (flexmock(sales_goals) .should_receive('_update_target_market_goals') .with_args(goal_data_for_country) .and_return(update_target_market_goals_response) .times(update_target_market_goals_times_called)) result = sales_goals._update_target_market_goals_in_country( user_id, goals_in_country, goal_data_for_country, target_market_goal_model_fetch_response) assert not result @pytest.mark.parametrize( 'existing_target_market_goals, expected_result', [ ( [ { 'target_market_goal_id': 543, 'sales_goal_id': 12345, 'store_id': 1, 'label_goal_value': 50, 'distribution_goal_value': 30, 'country_id': 1, 'country_name': 'USA' }, { 'target_market_goal_id': 544, 'sales_goal_id': 12345, 'store_id': 2, 'label_goal_value': 50, 'distribution_goal_value': 40, 'country_id': 1, 'country_name': 'USA' }, { 'target_market_goal_id': 545, 'sales_goal_id': 12345, 'store_id': 4, 'label_goal_value': 10, 'distribution_goal_value': 35, 'country_id': 1, 'country_name': 'USA' }, ], [ { 'label_goal_value': 20, 'store': { 'name': 'Amazon', 'store_id': 1 }, 'target_market_goal_id': 543 }, { 'label_goal_value': 3, 'distribution_goal_value': 2, 'store': { 'name': 'Other', 'store_id': 4 }, 'target_market_goal_id': 545 } ] ), ( [], [ { 'store': {'store_id': 1, 'name': 'Amazon'}, 'label_goal_value': 20, 'distribution_goal_value': 30, 'target_market_goal_id': 543, }, { 'store': {'store_id': 2, 'name': 'HMV'}, 'label_goal_value': 50, 'distribution_goal_value': 40, 'target_market_goal_id': 544 }, { 'store': {'store_id': 4, 'name': 'Other'}, 'label_goal_value': 3, 'distribution_goal_value': 2, 'target_market_goal_id': 545 } ] ) ] ) def test__filter_changed_target_market_goals( existing_target_market_goals, expected_result): """Test for _filter_changed_target_market_goals method.""" goals_in_country = [ { 'store': {'store_id': 1, 'name': 'Amazon'}, 'label_goal_value': 20, 'distribution_goal_value': 30, 'target_market_goal_id': 543, }, { 'store': {'store_id': 2, 'name': 'HMV'}, 'label_goal_value': 50, 'distribution_goal_value': 40, 'target_market_goal_id': 544 }, { 'store': {'store_id': 4, 'name': 'Other'}, 'label_goal_value': 3, 'distribution_goal_value': 2, 'target_market_goal_id': 545 } ] result = sales_goals._filter_changed_target_market_goals( goals_in_country, existing_target_market_goals) assert result == expected_result def test__sort_target_mkt_goals(): """Test _sort_target_mkt_goals returns correct result.""" goals_to_sort = [ { 'store': {'store_id': 8, 'name': 'Walmart'}, 'label_goal_value': 1, 'target_market_goal_id': 2, 'distribution_goal_value': 1 }, { 'store': {'store_id': 1, 'name': 'Amazon'}, 'label_goal_value': 1, 'target_market_goal_id': 1, 'distribution_goal_value': 1 }, { 'store': {'store_id': 4, 'name': 'Other'}, 'label_goal_value': 1, 'target_market_goal_id': 4, 'distribution_goal_value': 1 }, { 'store': {'store_id': 5, 'name': 'AEC'}, 'label_goal_value': 1, 'target_market_goal_id': 5, 'distribution_goal_value': 1 }, { 'store': {'store_id': 7, 'name': 'Target'}, 'label_goal_value': 1, 'target_market_goal_id': 3, 'distribution_goal_value': 1 } ] expected_result = [ { 'store': {'store_id': 5, 'name': 'AEC'}, 'label_goal_value': 1, 'target_market_goal_id': 5, 'distribution_goal_value': 1 }, { 'store': {'store_id': 1, 'name': 'Amazon'}, 'label_goal_value': 1, 'target_market_goal_id': 1, 'distribution_goal_value': 1 }, { 'store': {'store_id': 7, 'name': 'Target'}, 'label_goal_value': 1, 'target_market_goal_id': 3, 'distribution_goal_value': 1 }, { 'store': {'store_id': 8, 'name': 'Walmart'}, 'label_goal_value': 1, 'target_market_goal_id': 2, 'distribution_goal_value': 1 }, { 'store': {'store_id': 4, 'name': 'Other'}, 'label_goal_value': 1, 'target_market_goal_id': 4, 'distribution_goal_value': 1 } ] result = sales_goals._sort_target_mkt_goals(goals_to_sort) assert result == expected_result @pytest.mark.parametrize( 'fetch_by_product_id_response, fetch_by_product_id_response_times_called, ' 'fetch_by_sales_goal_id_response, fetch_by_sales_goal_id_times_called', [ (response.create_fatal_response(), 1, '', 0), (response.Response( { 'sales_goal_id': 1, 'product_id': 1, 'first_week_estimate': 0, 'updated_by': 'oa:1234', 'updated_at': '2017-04-13 12:33:33.652345' } ), 1, response.create_fatal_response(), 1) ]) def test_update_for_product_id_prepare_data_methods_fatal_response( sales_goal_data_for_patch, fetch_by_product_id_response, fetch_by_product_id_response_times_called, fetch_by_sales_goal_id_response, fetch_by_sales_goal_id_times_called, sales_goal_model_fetch_response): """Test update_for_product_id fatal response cases.""" product_id = 3 user_id = 'oa:2345' account_type = 'vendor' account_id = '44' sales_goal_id = sales_goal_model_fetch_response.get('sales_goal_id') (flexmock(sales_goals_for_country) .should_receive('upsert_sales_goals_for_country')) (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=200)) .once()) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(fetch_by_product_id_response) .times(fetch_by_product_id_response_times_called)) (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_id) .and_return(fetch_by_sales_goal_id_response) .times(fetch_by_sales_goal_id_times_called)) (flexmock(sales_goals) .should_receive('_update_target_market_goals_in_country') .never()) result = sales_goals.update_for_product_id( product_id, sales_goal_data_for_patch, account_type, account_id, user_id) assert result.status == 500 @pytest.mark.parametrize( '_update_target_mkt_goals_in_country_response, ' '_update_target_mkt_goals_in_country_times_called, ' 'update_response, update_times_called', [ (response.create_fatal_response(), 1, '', 0), (response.Response(), 1, response.create_fatal_response(), 1), ]) def test_update_for_product_id_update_methods_fatal_response( sales_goal_data_for_patch, _update_target_mkt_goals_in_country_response, _update_target_mkt_goals_in_country_times_called, update_response, update_times_called, sales_goal_model_fetch_response): """Test update_for_product_id fatal response cases.""" account_type = 'vendor' account_id = '567' product_id = 3 user_id = 'oa:2345' sales_goal_id = sales_goal_model_fetch_response.get('sales_goal_id') (flexmock(sales_goals_for_country) .should_receive('upsert_sales_goals_for_country')) (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=200)) .once()) sales_goal_to_update = response.Response(sales_goal_model_fetch_response) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(sales_goal_to_update) .once()) existing_target_market_goals = [{ 'target_market_goal_id': 123, 'sales_goal_id': 1, 'store_id': 1, 'label_goal_value': 1, 'distribution_goal_value': 1, 'country_id': 1, 'country_name': 'USA' }] (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_id) .and_return(response.Response(existing_target_market_goals)) .once()) (flexmock(sales_goals) .should_receive('_update_target_market_goals_in_country') .and_return(_update_target_mkt_goals_in_country_response) .times(_update_target_mkt_goals_in_country_times_called)) (flexmock(sales_goal_model) .should_receive('update') .with_args( sales_goal_id=sales_goal_id, values={ 'first_week_estimate': sales_goal_data_for_patch.get( 'first_week_estimate'), 'notes': sales_goal_data_for_patch.get('notes'), 'updated_by': user_id}) .and_return(update_response) .times(update_times_called)) (flexmock(sales_goals) .should_receive('fetch_by_product_id') .never()) result = sales_goals.update_for_product_id( product_id, sales_goal_data_for_patch, account_type, account_id, user_id) assert result.status == 500 @pytest.mark.parametrize( 'fetch_by_product_id_response, fetch_by_product_id_response_times_called, ' 'fetch_by_sales_goal_id_response, fetch_by_sales_goal_id_times_called', [ ( response.create_error_response(code='test_code', message='test_message'), 1, '', 0 ), ( response.Response( { 'sales_goal_id': 1, 'product_id': 1, 'first_week_estimate': 0, 'updated_by': 'oa:1234', 'updated_at': '2017-04-13 12:33:33.652345' } ), 1, response.create_error_response(code='test_code', message='test_message'), 1 ) ] ) def test_update_for_product_id_prepare_data_methods_error_response( sales_goal_data_for_patch, fetch_by_product_id_response, fetch_by_product_id_response_times_called, fetch_by_sales_goal_id_response, fetch_by_sales_goal_id_times_called, sales_goal_model_fetch_response): """Test update_for_product_id error response cases.""" product_id = 3 user_id = 'oa:2345' account_type = 'vendor' account_id = '44' sales_goal_id = sales_goal_model_fetch_response.get('sales_goal_id') (flexmock(sales_goals_for_country) .should_receive('upsert_sales_goals_for_country')) (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=200)) .once()) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(fetch_by_product_id_response) .times(fetch_by_product_id_response_times_called)) (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_id) .and_return(fetch_by_sales_goal_id_response) .times(fetch_by_sales_goal_id_times_called)) (flexmock(sales_goals) .should_receive('_update_target_market_goals_in_country') .never()) result = sales_goals.update_for_product_id( product_id, sales_goal_data_for_patch, account_type, account_id, user_id) assert result.status == 400 @pytest.mark.parametrize( '_update_target_mkt_goals_in_country_response, ' '_update_target_mkt_goals_in_country_times_called, ' 'update_response, update_times_called', [ (response.create_error_response( code='test_code', message='test_message'), 1, '', 0), (response.Response(), 1, response.create_error_response( code='test_code', message='test_message'), 1), ]) def test_update_for_product_id_update_methods_error_response( sales_goal_data_for_patch, _update_target_mkt_goals_in_country_response, _update_target_mkt_goals_in_country_times_called, update_response, update_times_called, sales_goal_model_fetch_response): """Test update_for_product_id error response cases.""" account_type = 'vendor' account_id = '567' product_id = 3 user_id = 'oa:2345' sales_goal_id = sales_goal_model_fetch_response.get('sales_goal_id') (flexmock(sales_goals_for_country) .should_receive('upsert_sales_goals_for_country')) (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=200)) .once()) sales_goal_to_update = response.Response(sales_goal_model_fetch_response) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(sales_goal_to_update) .once()) existing_target_market_goals = [{ 'target_market_goal_id': 123, 'sales_goal_id': 1, 'store_id': 1, 'label_goal_value': 1, 'distribution_goal_value': 1, 'country_id': 1, 'country_name': 'USA' }] (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_id) .and_return(response.Response(existing_target_market_goals)) .once()) (flexmock(sales_goals) .should_receive('_update_target_market_goals_in_country') .and_return(_update_target_mkt_goals_in_country_response) .times(_update_target_mkt_goals_in_country_times_called)) (flexmock(sales_goal_model) .should_receive('update') .with_args( sales_goal_id=sales_goal_id, values={ 'first_week_estimate': sales_goal_data_for_patch.get( 'first_week_estimate'), 'notes': sales_goal_data_for_patch.get('notes'), 'updated_by': user_id}) .and_return(update_response) .times(update_times_called)) (flexmock(sales_goals) .should_receive('fetch_by_product_id') .never()) result = sales_goals.update_for_product_id( product_id, sales_goal_data_for_patch, account_type, account_id, user_id) assert result.status == 400 def test_update_for_product_id_fetch_by_product_id_not_found_response( sales_goal_data_for_patch): """Test update_for_product_id if sales goal for product is not found.""" product_id = 3 user_id = 'oa:2345' account_type = 'vendor' account_id = '44' (flexmock(ows_product) .should_receive('get_product_ownership_by_account') .with_args( account_type, account_id, str(product_id)) .and_return(response.Response(status=200)) .once()) (flexmock(sales_goal_model) .should_receive('fetch_by_product_id') .with_args(product_id) .and_return(response.create_not_found_response()) .once()) (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .never()) result = sales_goals.update_for_product_id( product_id, sales_goal_data_for_patch, account_type, account_id, user_id) assert result.status == 404 def test__prepare_target_mkt_goals_for_update(): """Test _prepare_target_mkt_goals_for_update composes data correctly.""" market_goal_data = [ { 'store': {'store_id': 1, 'name': 'Amazon'}, 'label_goal_value': 200, 'distribution_goal_value': 300 }, { 'store': {'store_id': 2, 'name': 'HMV'}, 'label_goal_value': 37, 'distribution_goal_value': 33 }, { 'store': {'store_id': 4, 'name': 'Other'}, 'label_goal_value': 37, 'distribution_goal_value': 33 } ] country_id = 1 existing_target_market_goals = [ { 'target_market_goal_id': 11, 'sales_goal_id': 1, 'store_id': 1, 'label_goal_value': 10, 'distribution_goal_value': 20, 'country_id': 1, 'country_name': 'USA' }, { 'target_market_goal_id': 12, 'sales_goal_id': 1, 'store_id': 2, 'label_goal_value': 11, 'distribution_goal_value': 21, 'country_id': 1, 'country_name': 'USA' }, { 'target_market_goal_id': 13, 'sales_goal_id': 1, 'store_id': 4, 'label_goal_value': 4, 'distribution_goal_value': 5, 'country_id': 1, 'country_name': 'USA' } ] expected_result = [ { 'store': {'store_id': 1, 'name': 'Amazon'}, 'label_goal_value': 200, 'distribution_goal_value': 300, 'target_market_goal_id': 11 }, { 'store': {'store_id': 2, 'name': 'HMV'}, 'label_goal_value': 37, 'distribution_goal_value': 33, 'target_market_goal_id': 12 }, { 'store': {'store_id': 4, 'name': 'Other'}, 'label_goal_value': 37, 'distribution_goal_value': 33, 'target_market_goal_id': 13 } ] result = sales_goals._prepare_target_mkt_goals_for_update( market_goal_data, country_id, existing_target_market_goals) assert result == expected_result def test__create_or_update_target_market_goal_successful_update( market_goal_data, existing_target_mkt_goals_to_compose_data_for_create): """Test target mkt goals exist and update performed instead of creation.""" sales_goal_id = 1 country_id = 1 country_name = 'USA' user_id = 1 prepared_target_mkt_goals = [ { 'store': {'store_id': 1, 'name': 'Amazon'}, 'label_goal_value': 200, 'distribution_goal_value': 300, 'target_market_goal_id': 123 }, { 'store': {'store_id': 2, 'name': 'HMV'}, 'label_goal_value': 37, 'distribution_goal_value': 33, 'target_market_goal_id': 124 } ] (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_id) .and_return(response.Response( existing_target_mkt_goals_to_compose_data_for_create)) .once()) (flexmock(sales_goals) .should_call('_prepare_target_mkt_goals_for_update') .with_args( market_goal_data, country_id, existing_target_mkt_goals_to_compose_data_for_create) .and_return(prepared_target_mkt_goals) .once()) (flexmock(sales_goals) .should_receive('_update_target_market_goals_in_country') .and_return(response.Response()) .once()) result = sales_goals._create_or_update_target_market_goal( sales_goal_id, country_id, country_name, user_id, market_goal_data) assert result def test__create_or_update_target_market_goal_successful_creation( market_goal_data): """Test target mkt goals successful creation.""" sales_goal_id = 1 country_id = 1 country_name = 'USA' user_id = 1 (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_id) .and_return( response.create_not_found_response( error.ERROR_MESSAGE_TARGET_MARKET_GOALS_DO_NOT_EXIST.format( sales_goal_id=sales_goal_id))) .once()) (flexmock(sales_goals) .should_receive('_prepare_target_mkt_goals_for_update') .never()) (flexmock(sales_goals) .should_receive('_create_target_market_goals_for_sales_goal') .with_args( sales_goal_id=sales_goal_id, country_id=country_id, country_name=country_name, user_id=user_id, market_goals_data=market_goal_data) .and_return(response.Response()) .once()) result = sales_goals._create_or_update_target_market_goal( sales_goal_id, country_id, country_name, user_id, market_goal_data) assert result def test__create_or_update_target_market_goal_update_fails( market_goal_data, existing_target_mkt_goals_to_compose_data_for_create): """Test target mkt goals exist and update fails.""" sales_goal_id = 1 country_id = 1 country_name = 'USA' user_id = 1 prepared_target_mkt_goals = [ { 'store': {'store_id': 1, 'name': 'Amazon'}, 'label_goal_value': 200, 'distribution_goal_value': 300, 'target_market_goal_id': 123 }, { 'store': {'store_id': 2, 'name': 'HMV'}, 'label_goal_value': 37, 'distribution_goal_value': 33, 'target_market_goal_id': 124 } ] (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_id) .and_return(response.Response( existing_target_mkt_goals_to_compose_data_for_create)) .once()) (flexmock(sales_goals) .should_call('_prepare_target_mkt_goals_for_update') .with_args( market_goal_data, country_id, existing_target_mkt_goals_to_compose_data_for_create) .and_return(prepared_target_mkt_goals) .once()) (flexmock(sales_goals) .should_receive('_update_target_market_goals_in_country') .and_return(response.create_error_response( message='test error message', code='test_error_code')) .once()) result = sales_goals._create_or_update_target_market_goal( sales_goal_id, country_id, country_name, user_id, market_goal_data) assert not result assert result.errors['message'] == 'test error message' def test__create_or_update_target_market_goal_create_fails(market_goal_data): """Test target mkt goals creation fails.""" sales_goal_id = 1 country_id = 1 country_name = 'USA' user_id = 1 (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_id) .and_return( response.create_not_found_response( error.ERROR_MESSAGE_TARGET_MARKET_GOALS_DO_NOT_EXIST.format( sales_goal_id=sales_goal_id))) .once()) (flexmock(sales_goals) .should_receive('_prepare_target_mkt_goals_for_update') .never()) (flexmock(sales_goals) .should_receive('_create_target_market_goals_for_sales_goal') .with_args( sales_goal_id=sales_goal_id, country_id=country_id, country_name=country_name, user_id=user_id, market_goals_data=market_goal_data) .and_return(response.create_error_response( message='test error message', code='test_error_code')) .once()) result = sales_goals._create_or_update_target_market_goal( sales_goal_id, country_id, country_name, user_id, market_goal_data) assert not result assert result.errors['message'] == 'test error message' def test__create_target_market_goal_if_goals_found_in_another_country( market_goal_data, existing_target_mkt_goals_to_compose_data_for_create): """Test target mkt goals exist but in another country, then create.""" sales_goal_id = 1 country_id = 3 country_name = 'USA' user_id = 1 (flexmock(target_market_goal) .should_receive('fetch_by_sales_goal_id') .with_args(sales_goal_id) .and_return(response.Response( existing_target_mkt_goals_to_compose_data_for_create)) .once()) (flexmock(sales_goals) .should_receive('_prepare_target_mkt_goals_for_update') .never()) (flexmock(sales_goals) .should_receive('_create_target_market_goals_for_sales_goal') .with_args( sales_goal_id=sales_goal_id, country_id=country_id, country_name=country_name, user_id=user_id, market_goals_data=market_goal_data) .and_return(response.Response()) .once()) result = sales_goals._create_or_update_target_market_goal( sales_goal_id, country_id, country_name, user_id, market_goal_data) assert result def test__compose_stores_map_success(store_model_fetch_response): """Expect for successful response with stores mapping.""" create_mkt_goals_response_message = { 'created_goals': ['first', 'second', 'third'], 'store_ids': [1, 2, 3]} (flexmock(store) .should_receive('fetch_by_store_ids') .and_return(response.Response(store_model_fetch_response)) .once()) result = sales_goals._compose_stores_map(create_mkt_goals_response_message) expected_result = {1: 'Amazon'} assert result assert result.message == expected_result def test__compose_stores_map_if_no_store_ids(store_model_fetch_response): """Expect for successful response if there are no store_ids passed.""" create_mkt_goals_response_message = { 'created_goals': [], 'store_ids': []} (flexmock(store) .should_receive('fetch_by_store_ids') .never()) result = sales_goals._compose_stores_map(create_mkt_goals_response_message) assert result assert result.message == {} def test__compose_stores_map_stores_not_found(): """Expect for Not Found response if stores were not found.""" create_mkt_goals_response_message = { 'created_goals': [{'value': 'test_goal_value'}], 'store_ids': [1, 2, 3]} (flexmock(store) .should_receive('fetch_by_store_ids') .and_return(response.create_not_found_response()) .once()) result = sales_goals._compose_stores_map(create_mkt_goals_response_message) assert result.status == 404 def test__validate_store_in_country_goal_data( valid_countries_get_response, market_goal_data): """Expect successful response if valid stores for country are passed.""" result = sales_goals._validate_store_in_country_goal_data( valid_countries=valid_countries_get_response['items'], country_id=1, market_goal_data=market_goal_data) assert result def test__validate_store_in_country_goal_data_with_invalid_store( valid_countries_get_response, market_goal_data): """Expect Not Found response if invalid store for country is passed.""" invalid_store_for_country = { 'store': {'store_id': 100, 'name': 'Unknown Store'}, 'label_goal_value': 200, 'distribution_goal_value': 300 } country_id = 1 market_goal_data_with_invalid_store = copy.deepcopy(market_goal_data) market_goal_data_with_invalid_store.append(invalid_store_for_country) result = sales_goals._validate_store_in_country_goal_data( valid_countries=valid_countries_get_response['items'], country_id=country_id, market_goal_data=market_goal_data_with_invalid_store) assert result.status == 404 assert ( result.errors['message'] == error.ERROR_MESSAGE_WRONG_STORE_ID_WAS_PROVIDED .format(country_id=country_id)) def test_add_missing_market_goals_data(): """Test _add_missing_market_goals_data on US stores.""" country_id = 1 stores_in_country_count = 10 market_goals_list = [ { 'distribution_goal_value': 0, 'store': { 'name': 'Other', 'store_id': 4}, 'label_goal_value': 0 } ] result = sales_goals._add_missing_market_goals_data( market_goals_list, country_id) assert len(result) == stores_in_country_count