"""Test for countries logic module.""" import copy from flexmock import flexmock from oto import response import pytest from sales_goals import config from sales_goals.logic import countries from sales_goals.models import ows_territories TEST_SALES_GOALS_RESULT = [ { 'name': 'First Country', 'country_id': 1, 'hide_retailers': False, 'stores': [ { 'name': 'Test Store', 'store_id': 1 }, { 'name': 'Another Test Store', 'store_id': 2 }, { 'name': 'Other', 'store_id': 3 } ] }, { 'name': 'Second Country', 'country_id': 2, 'hide_retailers': True, 'stores': [ { 'name': 'Test Store', 'store_id': 1 }, { 'name': 'Other', 'store_id': 3 } ] }, { 'name': 'Third Country', 'country_id': 3, 'hide_retailers': True, 'stores': [ { 'name': 'Test Store', 'store_id': 1 }, { 'name': 'Other', 'store_id': 3 } ] }, { 'name': 'Fourth Country', 'country_id': 4, 'hide_retailers': True, 'stores': [ { 'name': 'Test Store', 'store_id': 1 }, { 'name': 'Other', 'store_id': 3 } ] }, { 'name': 'Fifth Country', 'country_id': 5, 'hide_retailers': True, 'stores': [ { 'name': 'Test Store', 'store_id': 1 }, { 'name': 'Other', 'store_id': 3 } ] }, { 'name': 'Sixth Country', 'country_id': 6, 'hide_retailers': True, 'stores': [ { 'name': 'Test Store', 'store_id': 1 }, { 'name': 'Other', 'store_id': 3 } ] }, { 'name': 'Seventh Country', 'country_id': 7, 'hide_retailers': True, 'stores': [ { 'name': 'Test Store', 'store_id': 1 }, { 'name': 'Other', 'store_id': 3 } ] }, { 'name': 'Eighth Country', 'country_id': 8, 'hide_retailers': True, 'stores': [ { 'name': 'Test Store', 'store_id': 1 }, { 'name': 'Other', 'store_id': 3 } ] }, { 'name': 'Ninth Country', 'country_id': 9, 'hide_retailers': True, 'stores': [ { 'name': 'Test Store', 'store_id': 1 }, { 'name': 'Other', 'store_id': 3 } ] }, { 'name': 'Tenth Country', 'country_id': 10, 'hide_retailers': True, 'stores': [ { 'name': 'Test Store', 'store_id': 1 }, { 'name': 'Other', 'store_id': 3 } ] }, ] COUNTRIES_LIST_TO_FILTER = [ {'name': 'United States', 'country_id': 1}, {'name': 'Australia', 'country_id': 8}, {'name': 'Canada', 'country_id': 2}, {'name': 'United Kingdom', 'country_id': 3}, {'name': 'BeNeLux & Nordics', 'country_id': 1000}, {'name': 'Southern Europe', 'country_id': 1001}, {'name': 'Eastern Europe', 'country_id': 1002}, {'name': 'Asia ex Japan', 'country_id': 1003}, {'name': 'Japan', 'country_id': 1004}, {'name': 'GSA', 'country_id': 1005}, {'name': 'France', 'country_id': 1006} ] def test_get_countries(): """Test that function returns all countries in correct format.""" items_quantity = 13 result = countries.get_countries() assert isinstance(result.message, dict) assert len(result.message['items']) == items_quantity def test_get_countries_with_test_yml_alw_user(): """Test get countries for alw user.""" user_id = 'alw:12345' expected_result = copy.deepcopy(TEST_SALES_GOALS_RESULT) for country in expected_result: if country['hide_retailers'] is True: country['stores'] = [{'name': 'Other', 'store_id': 3}] flexmock(config, COUNTRY_DATA_PATH='tests/spec/test.yml') result = countries.get_countries(user_id) assert result.message['items'] == expected_result def test_get_countries_with_test_yml_oa_user(): """Test get countries for oa user.""" user_id = 'oa:12345' flexmock(config, COUNTRY_DATA_PATH='tests/spec/test.yml') result = countries.get_countries(user_id) assert result.message['items'] == TEST_SALES_GOALS_RESULT @pytest.mark.parametrize('country_id, items_quantity', ([1, 10], [2, 1])) def test_get_stores_available_in_country( country_id, items_quantity): """Test that function returns all stores in correct format.""" result = countries.get_stores_available_in_country(country_id) assert isinstance(result.message, dict) assert len(result.message['items']) == items_quantity def test_get_countries_with_hidden_retailers(): """Expect function return the correct set of country_ids.""" result = countries.get_countries_with_hidden_retailers() assert result == {129, 2, 3, 6, 1000, 1001, 1002, 8, 1003, 1005, 9, 1007, 1004, 1006, 1008} def test_get_territories(feature_engine): """Test that function returns all countries in correct format.""" (flexmock(ows_territories) .should_receive('get_territories') .and_return(response.Response( [{'country_id': 1}, {'country_id': 50}] ))) result = countries.get_territories() assert isinstance(result.message, dict) assert len(result.message['items']) == 17