Ejemplo n.º 1
0
    def test_lookup_via_name(self, mocked_pycountry):
        mocked_pycountry.return_value = 'country_object'
        expected_calls = [mock.call(name='United Kingdom')]

        assert country_iso_code('United Kingdom') == 'country_object'
        assert mocked_pycountry.mock_calls == expected_calls
        assert mocked_pycountry.call_count == 1
Ejemplo n.º 2
0
def add_country_details(org_details):
    """If country name is valid attempt to append iso codes and continent.

    Args:
        org_details (dict): organisation details, without iso codes and continent

    Returns:
        (dict): processed org with extra data appended or None if failure
    """
    continent_map = alpha2_to_continent_mapping()

    try:
        country_name = org_details['country']
        country_codes = country_iso_code(country_name)
    except KeyError:
        for c in [
                'country_alpha_2', 'country_alpha_3', 'country_name',
                'country_numeric', 'continent'
        ]:
            org_details[c] = None
    else:
        org_details['country_alpha_2'] = country_codes.alpha_2
        org_details['country_alpha_3'] = country_codes.alpha_3
        org_details['country_name'] = country_codes.name
        org_details['country_numeric'] = country_codes.numeric
        org_details['continent'] = continent_map[country_codes.alpha_2]

    return org_details
Ejemplo n.º 3
0
    def test_lookup_via_common_name(self, mocked_pycountry):
        mocked_pycountry.side_effect = [KeyError(), 'country_object']
        expected_calls = [mock.call(name='United Kingdom'),
                          mock.call(common_name='United Kingdom')
                          ]

        assert country_iso_code('United Kingdom') == 'country_object'
        assert mocked_pycountry.mock_calls == expected_calls
        assert mocked_pycountry.call_count == 2
Ejemplo n.º 4
0
    def test_title_case_is_applied(self, mocked_pycountry):
        expected_calls = [mock.call(name='United Kingdom'),
                          mock.call(name='United Kingdom'),
                          mock.call(name='United Kingdom')]

        country_iso_code('united kingdom')
        country_iso_code('UNITED KINGDOM')
        country_iso_code('United kingdom')
        assert mocked_pycountry.mock_calls == expected_calls
Ejemplo n.º 5
0
    def test_invalid_lookup_raises_keyerror(self, mocked_pycountry):
        mocked_pycountry.side_effect = [KeyError(), KeyError(), KeyError()]

        with pytest.raises(KeyError) as e:
            country_iso_code('Fake Country')
        assert 'Fake Country not found' in str(e.value)