File: test_lookup.py

package info (click to toggle)
python-iso3166 2.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 120 kB
  • sloc: python: 577; makefile: 4
file content (84 lines) | stat: -rw-r--r-- 1,989 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-

from typing import List, Union
import pytest
import iso3166
from iso3166 import countries


def check_lookup(
    alpha2: str,
    matching_keys: List[Union[int, str]],
    missing_keys: List[Union[int, str]],
) -> None:
    for k in matching_keys:
        assert countries[k].alpha2 == alpha2
        assert countries.get(k).alpha2 == alpha2
        assert k in countries

    for k in missing_keys:
        with pytest.raises(KeyError):
            countries.get(k)

        with pytest.raises(KeyError):
            countries[k]

        assert countries.get(k, None) is None


def test_length() -> None:
    assert len(countries) == len(iso3166._records)


def test_empty_string() -> None:
    check_lookup("US", ["us", "US"], [""])


def test_alpha2() -> None:
    check_lookup("US", ["us", "US"], ["zz"])


def test_alpha3() -> None:
    check_lookup("US", ["usa", "USA"], ["zzz"])


def test_name() -> None:
    check_lookup(
        "US",
        ["united states of america", "United STates of America"],
        ["zzzzz"],
    )


def test_numeric() -> None:
    check_lookup("US", [840, "840"], [111, "111"])

    with pytest.raises(KeyError):
        countries.get("000")


def test_alt_name() -> None:
    check_lookup("TW", ["taiwan", "Taiwan, province of china"], ["zzzzz"])
    check_lookup("PS", ["palestine", "palestine, state of"], ["zzzz"])


def test_none_default() -> None:
    assert countries.get("NOTUS", None) is None


def test_data() -> None:
    assert len(list(countries)) > 0

    for country in countries:
        assert len(country.alpha2) == 2
        assert country.alpha2.upper() == country.alpha2

        assert len(country.alpha3) == 3
        assert country.alpha3.upper() == country.alpha3

        assert len(country.numeric) == 3
        assert country.numeric == ("%03d" % int(country.numeric))
        assert int(country.numeric) > 0

        assert len(country.name) > 3
        assert len(country.apolitical_name) > 3