1import gettext
2import re
3import pycountry
4import pycountry.db
5import pytest
6
7
8@pytest.fixture(autouse=True, scope='session')
9def logging():
10    import logging
11    logging.basicConfig(level=logging.DEBUG)
12
13
14def test_country_list():
15    assert len(pycountry.countries) == 249
16    assert isinstance(list(pycountry.countries)[0], pycountry.db.Data)
17
18
19def test_country_fuzzy_search():
20    results = pycountry.countries.search_fuzzy(u'England')
21    assert len(results) == 1
22    assert results[0] == pycountry.countries.get(alpha_2='GB')
23
24    # Match alternative names exactly and thus GB ends up with Wales
25    # before Australia.
26    results = pycountry.countries.search_fuzzy(u'Wales')
27    assert len(results) == 2
28    assert results[0] == pycountry.countries.get(alpha_2='GB')
29    assert results[1] == pycountry.countries.get(alpha_2='AU')
30
31    # Match with accents removed, first a country with a partial match in the
32    # country name, then a country with multiple subdivision partial matches,
33    # and then a country with a single subdivision match.
34    results = pycountry.countries.search_fuzzy(u'Cote')
35    assert len(results) == 3
36    assert results[0] == pycountry.countries.get(alpha_2='CI')
37    assert results[1] == pycountry.countries.get(alpha_2='FR')
38    assert results[2] == pycountry.countries.get(alpha_2='HN')
39
40    # A somewhat carefully balanced point system allows for a (bias-based)
41    # graceful sorting of common substrings being used in multiple matches:
42    results = pycountry.countries.search_fuzzy(u'New')
43    assert results[0] == pycountry.countries.get(alpha_2='NC')
44    assert results[1] == pycountry.countries.get(alpha_2='NZ')
45    assert results[2] == pycountry.countries.get(alpha_2='PG')
46    assert results[3] == pycountry.countries.get(alpha_2='GB')
47    assert results[4] == pycountry.countries.get(alpha_2='US')
48    assert results[5] == pycountry.countries.get(alpha_2='CA')
49    assert results[6] == pycountry.countries.get(alpha_2='AU')
50    assert results[7] == pycountry.countries.get(alpha_2='MH')
51
52    # bug #34, likely about capitalization that was broken
53    results = pycountry.countries.search_fuzzy(u'united states of america')
54    assert len(results) == 1
55    assert results[0] == pycountry.countries.get(alpha_2='US')
56
57
58def test_historic_country_fuzzy_search():
59    results = pycountry.historic_countries.search_fuzzy(u'burma')
60    assert len(results) == 1
61    assert results[0] == pycountry.historic_countries.get(alpha_4='BUMM')
62
63
64def test_germany_has_all_attributes():
65    germany = pycountry.countries.get(alpha_2='DE')
66    assert germany.alpha_2 == u'DE'
67    assert germany.alpha_3 == u'DEU'
68    assert germany.numeric == u'276'
69    assert germany.name == u'Germany'
70    assert germany.official_name == u'Federal Republic of Germany'
71
72
73def test_subdivisions_directly_accessible():
74    assert len(pycountry.subdivisions) == 4883
75    assert isinstance(list(pycountry.subdivisions)[0], pycountry.db.Data)
76
77    de_st = pycountry.subdivisions.get(code='DE-ST')
78    assert de_st.code == u'DE-ST'
79    assert de_st.name == u'Sachsen-Anhalt'
80    assert de_st.type == u'State'
81    assert de_st.parent is None
82    assert de_st.parent_code is None
83    assert de_st.country is pycountry.countries.get(alpha_2='DE')
84
85
86def test_subdivisions_have_subdivision_as_parent():
87    al_bu = pycountry.subdivisions.get(code='AL-BU')
88    assert al_bu.code == u'AL-BU'
89    assert al_bu.name == u'Bulqiz\xeb'
90    assert al_bu.type == u'District'
91    assert al_bu.parent_code == u'AL-09'
92    assert al_bu.parent is pycountry.subdivisions.get(code='AL-09')
93    assert al_bu.parent.name == u'Dib\xebr'
94
95
96def test_query_subdivisions_of_country():
97    assert len(pycountry.subdivisions.get(country_code='DE')) == 16
98    assert len(pycountry.subdivisions.get(country_code='US')) == 57
99
100
101def test_scripts():
102    assert len(pycountry.scripts) == 182
103    assert isinstance(list(pycountry.scripts)[0], pycountry.db.Data)
104
105    latin = pycountry.scripts.get(name='Latin')
106    assert latin.alpha_4 == u'Latn'
107    assert latin.name == u'Latin'
108    assert latin.numeric == u'215'
109
110
111def test_currencies():
112    assert len(pycountry.currencies) == 170
113    assert isinstance(list(pycountry.currencies)[0], pycountry.db.Data)
114
115    argentine_peso = pycountry.currencies.get(alpha_3='ARS')
116    assert argentine_peso.alpha_3 == u'ARS'
117    assert argentine_peso.name == u'Argentine Peso'
118    assert argentine_peso.numeric == u'032'
119
120
121def test_languages():
122    assert len(pycountry.languages) == 7847
123    assert isinstance(list(pycountry.languages)[0], pycountry.db.Data)
124
125    aragonese = pycountry.languages.get(alpha_2='an')
126    assert aragonese.alpha_2 == u'an'
127    assert aragonese.alpha_3 == u'arg'
128    assert aragonese.name == u'Aragonese'
129
130    bengali = pycountry.languages.get(alpha_2='bn')
131    assert bengali.name == u'Bengali'
132    assert bengali.common_name == u'Bangla'
133
134    # this tests the slow search path in lookup()
135    bengali2 = pycountry.languages.lookup('bAngLa')
136    assert bengali2 == bengali
137
138
139def test_language_families():
140    assert len(pycountry.language_families) == 115
141    assert isinstance(list(pycountry.language_families)[0], pycountry.db.Data)
142
143    aragonese = pycountry.languages.get(alpha_3='arg')
144    assert aragonese.alpha_3 == u'arg'
145    assert aragonese.name == u'Aragonese'
146
147
148def test_locales():
149    german = gettext.translation(
150        'iso3166', pycountry.LOCALES_DIR, languages=['de'])
151    german.install()
152    assert __builtins__['_']('Germany') == 'Deutschland'
153
154
155def test_removed_countries():
156    ussr = pycountry.historic_countries.get(alpha_3='SUN')
157    assert isinstance(ussr, pycountry.db.Data)
158    assert ussr.alpha_4 == u'SUHH'
159    assert ussr.alpha_3 == u'SUN'
160    assert ussr.name == u'USSR, Union of Soviet Socialist Republics'
161    assert ussr.withdrawal_date == u'1992-08-30'
162
163
164def test_repr():
165    assert re.match("Country\\(alpha_2=u?'DE', "
166                    "alpha_3=u?'DEU', "
167                    "name=u?'Germany', "
168                    "numeric=u?'276', "
169                    "official_name=u?'Federal Republic of Germany'\\)",
170                    repr(pycountry.countries.get(alpha_2='DE')))
171
172
173def test_dir():
174    germany = pycountry.countries.get(alpha_2='DE')
175    for n in 'alpha_2', 'alpha_3', 'name', 'numeric', 'official_name':
176        assert n in dir(germany)
177
178
179def test_get():
180    c = pycountry.countries
181    with pytest.raises(TypeError):
182        c.get(alpha_2='DE', alpha_3='DEU')
183    assert c.get(alpha_2='DE') == c.get(alpha_3='DEU')
184    assert c.get(alpha_2='Foo') is None
185    tracer = object()
186    assert c.get(alpha_2='Foo', default=tracer) is tracer
187
188
189def test_lookup():
190    c = pycountry.countries
191    g = c.get(alpha_2='DE')
192    assert g == c.get(alpha_2='de')
193    assert g == c.lookup('de')
194    assert g == c.lookup('DEU')
195    assert g == c.lookup('276')
196    assert g == c.lookup('germany')
197    assert g == c.lookup('Federal Republic of Germany')
198    # try a generated field
199    bqaq = pycountry.historic_countries.get(alpha_4='BQAQ')
200    assert bqaq == pycountry.historic_countries.lookup('atb')
201    german = pycountry.languages.get(alpha_2='de')
202    assert german == pycountry.languages.lookup('De')
203    euro = pycountry.currencies.get(alpha_3='EUR')
204    assert euro == pycountry.currencies.lookup('euro')
205    latin = pycountry.scripts.get(name='Latin')
206    assert latin == pycountry.scripts.lookup('latn')
207    al_bu = pycountry.subdivisions.get(code='AL-BU')
208    assert al_bu == pycountry.subdivisions.lookup('al-bu')
209    with pytest.raises(LookupError):
210        pycountry.countries.lookup('bogus country')
211    with pytest.raises(LookupError):
212        pycountry.countries.lookup(12345)
213    with pytest.raises(LookupError):
214        pycountry.countries.get(alpha_2=12345)
215
216
217def test_subdivision_parent():
218    s = pycountry.subdivisions
219    sd = s.get(code='CV-BV')
220    assert sd.parent_code == 'CV-B'
221    assert sd.parent is s.get(code=sd.parent_code)
222
223
224def test_subdivision_missing_code_raises_keyerror():
225    s = pycountry.subdivisions
226    assert s.get(code='US-ZZ') is None
227
228
229def test_subdivision_empty_list():
230    s = pycountry.subdivisions
231    assert len(s.get(country_code='DE')) == 16
232    assert len(s.get(country_code='JE')) == 0
233    assert s.get(country_code='FOOBAR') is None
234
235
236def test_has_version_attribute():
237    assert pycountry.__version__ != 'n/a'
238    assert len(pycountry.__version__) >= 5
239    assert '.' in pycountry.__version__
240