From 8fa4f16a96eea245003c7965df4e7c4d76195fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Gru=CC=88ndger?= Date: Thu, 12 Nov 2015 08:38:43 +0100 Subject: [PATCH] Fixed get_country_code --- ogn/ognutils.py | 5 ++++- tests/test_ognutils.py | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ogn/ognutils.py b/ogn/ognutils.py index 4188615..49940cb 100644 --- a/ogn/ognutils.py +++ b/ogn/ognutils.py @@ -42,5 +42,8 @@ def get_ddb(csvfile=None): def get_country_code(latitude, longitude): geolocator = Nominatim() location = geolocator.reverse("%f, %f" % (latitude, longitude)) - country_code = location.raw["address"]["country_code"] + try: + country_code = location.raw["address"]["country_code"] + except KeyError: + country_code = None return country_code diff --git a/tests/test_ognutils.py b/tests/test_ognutils.py index 43f92a9..9810a19 100644 --- a/tests/test_ognutils.py +++ b/tests/test_ognutils.py @@ -27,3 +27,9 @@ class TestStringMethods(unittest.TestCase): longitude = 11.0 country_code = get_country_code(latitude, longitude) self.assertEquals(country_code, 'de') + + def test_get_country_code_bad(self): + latitude = 0.0002274 + longitude = -0.0009119 + country_code = get_country_code(latitude, longitude) + self.assertEqual(country_code, None)