"""Test the Country object.""" from unittest.mock import patch import pytest from abacus_common_data.country import Country def test_get_orchard_country_id(): """Test getting the orchard country ID.""" test_country = Country('USA') assert test_country.orchard_id == 1 def test_country_get_list(): """Test get all countries method.""" country_list = Country.get_list() assert len(country_list) == 251 assert all(isinstance(item, Country) for item in country_list) def test_country_get_by_alpha2(): """Test get country by ISO-3166 alpha-2 code.""" country = Country('US') assert country.alpha2 == 'US' assert country.alpha3 == 'USA' assert country.numeric == '840' assert country.name == 'United States of America' assert country.region == 'Americas' def test_country_get_by_alpha3(): """Test get country by ISO-3166 alpha-3 code.""" country = Country('USA') assert country.alpha2 == 'US' assert country.alpha3 == 'USA' assert country.name == 'United States of America' assert country.region == 'Americas' assert country.numeric == '840' def test_country_get_by_numeric(): """Test get country by ISO-3166 numeric code.""" country = Country('840') assert country.alpha2 == 'US' assert country.alpha3 == 'USA' assert country.name == 'United States of America' assert country.region == 'Americas' assert country.numeric == '840' def test_country_raises_keyerror(): """Test that KeyError is raised for invalid code.""" with pytest.raises(KeyError) as err: Country('NOTEXISTS') assert str(err.value) == "'Country with code NOTEXISTS was not found'" def test_country_get_countries_by_region_name(): """Test get countries by region name.""" countries = Country.get_countries_by_region_name('Americas') assert len(countries) == 57 assert all(isinstance(item, Country) for item in countries) def test_country_get_countries_by_region_name_keyerror(): """Test get countries by region name raises KeyError.""" with pytest.raises(KeyError) as err: Country.get_countries_by_region_name('NOTEXISTS') assert str(err.value) == "'Region with name NOTEXISTS was not found'" def test_country_to_dict(): """Test country to_dict method.""" country_dict = Country('USA').to_dict() assert country_dict == { 'alpha2': 'US', 'alpha3': 'USA', 'numeric': '840', 'name': 'United States of America', 'region': 'Americas' } @patch('abacus_common_data.country.Country._build_indexes') def test_countries_are_loaded_once(build_indexes_mock): """Test that with multiple invokes json is loaded only once.""" Country._country_list = [] Country('USA') Country('UKR') Country('GEO') assert build_indexes_mock.call_count == 1 def test_build_indexes(): """Test that indexes are build properly.""" Country._country_list = [] Country._indexes = {} countries_feed = [ { 'name': 'Afghanistan', 'alpha-2': 'AF', 'alpha-3': 'AFG', 'country-code': '004', 'iso_3166-2': 'ISO 3166-2:AF', 'region': 'Asia', 'sub-region': 'Southern Asia', 'intermediate-region': '', 'region-code': '142', 'sub-region-code': '034', 'intermediate-region-code': '' }, { 'name': 'Ă…land Islands', 'alpha-2': 'AX', 'alpha-3': 'ALA', 'country-code': '248', 'iso_3166-2': 'ISO 3166-2:AX', 'region': 'Europe', 'sub-region': 'Northern Europe', 'intermediate-region': '', 'region-code': '150', 'sub-region-code': '154', 'intermediate-region-code': '' }, { 'name': 'Albania', 'alpha-2': 'AL', 'alpha-3': 'ALB', 'country-code': '008', 'iso_3166-2': 'ISO 3166-2:AL', 'region': 'Europe', 'sub-region': 'Southern Europe', 'intermediate-region': '', 'region-code': '150', 'sub-region-code': '039', 'intermediate-region-code': '' } ] Country._country_list = countries_feed Country._build_indexes(fields=['alpha-2', 'region']) assert Country._indexes == { 'alpha-2': { 'AF': [countries_feed[0]], 'AX': [countries_feed[1]], 'AL': [countries_feed[2]] }, 'region': { 'Asia': [countries_feed[0]], 'Europe': [countries_feed[1], countries_feed[2]] } } @patch('abacus_common_data.country.Country._init_countries_data') def test_init_countries_data(init_countries_mock): """Test _init_countries_data method.""" Country._indexes = {'alpha-3': {'USA': [{'name': 'USA'}]}} Country('USA') assert init_countries_mock.call_count == 1