"""Tests for logic utils.""" import copy import pytest from sales_goals.utils import logic_utils MARKET_GOALS_IN_US = [ { 'target_market_goal_id': 1, 'sales_goal_id': 1234, 'store_id': 1, 'label_goal_value': 1, 'distribution_goal_value': 2, 'country_id': 1, 'country_name': 'United States' }, { 'target_market_goal_id': 2, 'sales_goal_id': 1234, 'store_id': 8, 'label_goal_value': 1, 'distribution_goal_value': 2, 'country_id': 1, 'country_name': 'United States' }, { 'target_market_goal_id': 3, 'sales_goal_id': 1234, 'store_id': 7, 'label_goal_value': 1, 'distribution_goal_value': 2, 'country_id': 1, 'country_name': 'United States' }, { 'target_market_goal_id': 4, 'sales_goal_id': 1234, 'store_id': 4, 'label_goal_value': 1, 'distribution_goal_value': 2, 'country_id': 1, 'country_name': 'United States' }, { 'target_market_goal_id': 5, 'sales_goal_id': 1234, 'store_id': 5, 'label_goal_value': 1, 'distribution_goal_value': 2, 'country_id': 1, 'country_name': 'United States' } ] MARKET_GOALS_IN_UK = [ { 'target_market_goal_id': 5, 'sales_goal_id': 1234, 'store_id': 1, 'label_goal_value': 1, 'distribution_goal_value': 2, 'country_id': 3, 'country_name': 'United Kingdom' }, { 'target_market_goal_id': 5, 'sales_goal_id': 1234, 'store_id': 2, 'label_goal_value': 1, 'distribution_goal_value': 2, 'country_id': 3, 'country_name': 'United Kingdom' }, { 'target_market_goal_id': 5, 'sales_goal_id': 1234, 'store_id': 4, 'label_goal_value': 1, 'distribution_goal_value': 2, 'country_id': 3, 'country_name': 'United Kingdom' } ] def test_compose_totals(): """Expect the compose_total function return correct sum result.""" result = logic_utils.compose_totals(MARKET_GOALS_IN_US) assert result == {'label_total': 5, 'distribution_total': 10} def test_compose_totals_with_none_values(): """Expect the compose_total function return correct sum result. Expect the compose_total function return correct sum result if None values are passed. """ test_market_goals = copy.deepcopy(MARKET_GOALS_IN_US) for goal in test_market_goals: goal['label_goal_value'] = None result = logic_utils.compose_totals(test_market_goals) assert result == {'label_total': 0, 'distribution_total': 10} @pytest.mark.parametrize( 'user_id, expected_result', [ ('oa:23487', False), ('alw:76543', True), (None, False)]) def test_is_workstation_user(user_id, expected_result): """Expect function given the user_id return correct boolean.""" result = logic_utils.is_workstation_user(user_id) assert result == expected_result def test_compose_market_goals_result_for_oa_user(): """Expect function compose the market goals correctly for oa user.""" user_id = 'oa:23487' countries_hide_retailers = {2, 3, 8} result = logic_utils.compose_market_goals_result( MARKET_GOALS_IN_US, countries_hide_retailers, user_id) assert result == MARKET_GOALS_IN_US def test_compose_market_goals_result_for_alw_user(): """Expect function compose the market goals correctly for alw user.""" user_id = 'alw:76543' countries_hide_retailers = {2, 3, 8} market_goals = copy.deepcopy(MARKET_GOALS_IN_US) market_goals.extend(MARKET_GOALS_IN_UK) # since the UK is the country with hide_retailers=True, there should be # only Other store with id=4 and only label_goal_value returned expected_target_mkt_goals_for_uk = [{ 'target_market_goal_id': 5, 'sales_goal_id': 1234, 'store_id': 4, 'label_goal_value': 1, 'country_id': 3, 'country_name': 'United Kingdom' }] expected_result = copy.deepcopy(MARKET_GOALS_IN_US) expected_result.extend(expected_target_mkt_goals_for_uk) result = logic_utils.compose_market_goals_result( market_goals, countries_hide_retailers, user_id) assert result == expected_result