1# -*- coding: utf-8 -*- 2from __future__ import unicode_literals 3from django.test import TestCase 4from django.utils import translation 5 6from django_countries import countries, Countries, CountryTuple 7from django_countries.tests import custom_countries 8 9 10EXPECTED_COUNTRY_COUNT = 249 11FIRST_THREE_COUNTRIES = [ 12 ('AF', 'Afghanistan'), 13 ('AX', 'Åland Islands'), 14 ('AL', 'Albania'), 15] 16 17 18class BaseTest(TestCase): 19 20 def setUp(self): 21 del countries.countries 22 23 def tearDown(self): 24 del countries.countries 25 26 27class TestCountriesObject(BaseTest): 28 29 def test_countries_len(self): 30 self.assertEqual(len(countries), EXPECTED_COUNTRY_COUNT) 31 32 def test_countries_sorted(self): 33 self.assertEqual(list(countries)[:3], FIRST_THREE_COUNTRIES) 34 35 def test_countries_namedtuple(self): 36 country = list(countries)[0] 37 first_country = FIRST_THREE_COUNTRIES[0] 38 self.assertEqual(country.code, first_country[0]) 39 self.assertEqual(country.name, first_country[1]) 40 self.assertIsInstance(country, CountryTuple) 41 42 def test_countries_limit(self): 43 with self.settings( 44 COUNTRIES_ONLY={'NZ': 'New Zealand', 'NV': 'Neverland'}): 45 self.assertEqual(list(countries), [ 46 ('NV', 'Neverland'), 47 ('NZ', 'New Zealand'), 48 ]) 49 self.assertEqual(len(countries), 2) 50 51 def test_countries_limit_codes(self): 52 with self.settings(COUNTRIES_ONLY=['NZ', ('NV', 'Neverland')]): 53 self.assertEqual(list(countries), [ 54 ('NV', 'Neverland'), 55 ('NZ', 'New Zealand'), 56 ]) 57 self.assertEqual(len(countries), 2) 58 59 def test_countries_custom_removed_len(self): 60 with self.settings(COUNTRIES_OVERRIDE={'AU': None}): 61 self.assertEqual(len(countries), EXPECTED_COUNTRY_COUNT - 1) 62 63 def test_countries_custom_added_len(self): 64 with self.settings(COUNTRIES_OVERRIDE={'XX': 'Neverland'}): 65 self.assertEqual(len(countries), EXPECTED_COUNTRY_COUNT + 1) 66 67 def test_countries_getitem(self): 68 countries[0] 69 70 def test_countries_slice(self): 71 sliced = countries[10:20:2] 72 self.assertEqual(len(sliced), 5) 73 74 def test_countries_custom_ugettext_evaluation(self): 75 76 class FakeLazyUGetText(object): 77 78 def __bool__(self): # pragma: no cover 79 raise ValueError("Can't evaluate lazy_ugettext yet") 80 81 __nonzero__ = __bool__ 82 83 with self.settings(COUNTRIES_OVERRIDE={'AU': FakeLazyUGetText()}): 84 countries.countries 85 86 def test_ioc_countries(self): 87 from ..ioc_data import check_ioc_countries 88 check_ioc_countries(verbosity=0) 89 90 def test_initial_iter(self): 91 # Use a new instance so nothing is cached 92 dict(Countries()) 93 94 def test_flags(self): 95 from ..data import check_flags 96 check_flags(verbosity=0) 97 98 def test_common_names(self): 99 from ..data import check_common_names 100 check_common_names() 101 102 def test_alpha2(self): 103 self.assertEqual(countries.alpha2('NZ'), 'NZ') 104 self.assertEqual(countries.alpha2('nZ'), 'NZ') 105 self.assertEqual(countries.alpha2('Nzl'), 'NZ') 106 self.assertEqual(countries.alpha2(554), 'NZ') 107 self.assertEqual(countries.alpha2('554'), 'NZ') 108 109 def test_alpha2_invalid(self): 110 self.assertEqual(countries.alpha2('XX'), '') 111 112 def test_alpha2_override(self): 113 with self.settings(COUNTRIES_OVERRIDE={'AU': None}): 114 self.assertEqual(countries.alpha2('AU'), '') 115 116 def test_alpha2_override_new(self): 117 with self.settings(COUNTRIES_OVERRIDE={'XX': 'Neverland'}): 118 self.assertEqual(countries.alpha2('XX'), 'XX') 119 120 def test_fetch_by_name(self): 121 code = countries.by_name('United States of America') 122 self.assertEqual(code, 'US') 123 124 def test_fetch_by_name_case_insensitive(self): 125 code = countries.by_name('United states of America') 126 self.assertEqual(code, 'US') 127 128 def test_fetch_by_name_old(self): 129 code = countries.by_name('Czech Republic') 130 self.assertEqual(code, 'CZ') 131 132 def test_fetch_by_name_old_case_insensitive(self): 133 code = countries.by_name('Czech republic') 134 self.assertEqual(code, 'CZ') 135 136 def test_fetch_by_name_i18n(self): 137 code = countries.by_name('Estados Unidos', language='es') 138 self.assertEqual(code, 'US') 139 140 def test_fetch_by_name_no_match(self): 141 self.assertEqual(countries.by_name('Neverland'), '') 142 143 144class CountriesFirstTest(BaseTest): 145 146 def test_countries_first(self): 147 with self.settings(COUNTRIES_FIRST=['NZ', 'AU']): 148 self.assertEqual( 149 list(countries)[:5], 150 [ 151 ('NZ', 'New Zealand'), 152 ('AU', 'Australia'), 153 ] + FIRST_THREE_COUNTRIES) 154 155 def test_countries_first_break(self): 156 with self.settings(COUNTRIES_FIRST=['NZ', 'AU'], 157 COUNTRIES_FIRST_BREAK='------'): 158 self.assertEqual( 159 list(countries)[:6], 160 [ 161 ('NZ', 'New Zealand'), 162 ('AU', 'Australia'), 163 ('', '------'), 164 ] + FIRST_THREE_COUNTRIES) 165 166 def test_countries_first_some_valid(self): 167 with self.settings(COUNTRIES_FIRST=['XX', 'NZ', 'AU'], 168 COUNTRIES_FIRST_BREAK='------'): 169 countries_list = list(countries) 170 self.assertEqual( 171 countries_list[:6], 172 [ 173 ('NZ', 'New Zealand'), 174 ('AU', 'Australia'), 175 ('', '------'), 176 ] + FIRST_THREE_COUNTRIES) 177 self.assertEqual(len(countries_list), EXPECTED_COUNTRY_COUNT + 1) 178 179 def test_countries_first_no_valid(self): 180 with self.settings(COUNTRIES_FIRST=['XX'], 181 COUNTRIES_FIRST_BREAK='------'): 182 countries_list = list(countries) 183 self.assertEqual(countries_list[:3], FIRST_THREE_COUNTRIES) 184 self.assertEqual(len(countries_list), EXPECTED_COUNTRY_COUNT) 185 186 def test_countries_first_repeat(self): 187 with self.settings(COUNTRIES_FIRST=['NZ', 'AU'], 188 COUNTRIES_FIRST_REPEAT=True): 189 countries_list = list(countries) 190 self.assertEqual(len(countries_list), EXPECTED_COUNTRY_COUNT + 2) 191 sorted_codes = [item[0] for item in countries_list[2:]] 192 sorted_codes.index('NZ') 193 sorted_codes.index('AU') 194 195 def test_countries_first_len(self): 196 with self.settings(COUNTRIES_FIRST=['NZ', 'AU', 'XX']): 197 self.assertEqual(len(countries), EXPECTED_COUNTRY_COUNT + 2) 198 199 def test_countries_first_break_len(self): 200 with self.settings(COUNTRIES_FIRST=['NZ', 'AU', 'XX'], 201 COUNTRIES_FIRST_BREAK='------'): 202 self.assertEqual(len(countries), EXPECTED_COUNTRY_COUNT + 3) 203 204 def test_countries_first_break_len_no_valid(self): 205 with self.settings(COUNTRIES_FIRST=['XX'], 206 COUNTRIES_FIRST_BREAK='------'): 207 self.assertEqual(len(countries), EXPECTED_COUNTRY_COUNT) 208 209 def test_sorted_countries_first_english(self): 210 with self.settings( 211 COUNTRIES_FIRST=['GB', 'AF', 'DK'], COUNTRIES_FIRST_SORT=True): 212 countries_list = list(countries) 213 sorted_codes = [item[0] for item in countries_list[:3]] 214 self.assertEqual(['AF', 'DK', 'GB'], sorted_codes) 215 216 def test_unsorted_countries_first_english(self): 217 with self.settings( 218 COUNTRIES_FIRST=['GB', 'AF', 'DK'], 219 COUNTRIES_FIRST_SORT=False): 220 countries_list = list(countries) 221 unsorted_codes = [item[0] for item in countries_list[:3]] 222 self.assertEqual(['GB', 'AF', 'DK'], unsorted_codes) 223 224 def test_sorted_countries_first_arabic(self): 225 with self.settings( 226 COUNTRIES_FIRST=['GB', 'AF', 'DK'], COUNTRIES_FIRST_SORT=True): 227 lang = translation.get_language() 228 translation.activate('eo') 229 try: 230 countries_list = list(countries) 231 sorted_codes = [item[0] for item in countries_list[:3]] 232 self.assertEqual(['AF', 'GB', 'DK'], sorted_codes) 233 finally: 234 translation.activate(lang) 235 236 237class TestCountriesCustom(BaseTest): 238 239 def test_countries_limit(self): 240 fantasy_countries = custom_countries.FantasyCountries() 241 self.assertEqual(list(fantasy_countries), [ 242 ('NV', 'Neverland'), 243 ('NZ', 'New Zealand'), 244 ]) 245 self.assertEqual(len(fantasy_countries), 2) 246