diff --git a/django/contrib/gis/geoip2/base.py b/django/contrib/gis/geoip2/base.py index 23f72a58cc..1d5a46653c 100644 --- a/django/contrib/gis/geoip2/base.py +++ b/django/contrib/gis/geoip2/base.py @@ -92,6 +92,9 @@ class GeoIP2: if os.path.isfile(city_db): self._city = geoip2.database.Reader(city_db, mode=cache) self._city_file = city_db + + if not self._reader: + raise GeoIP2Exception('Could not load a database from %s.' % path) elif os.path.isfile(path): # Otherwise, some detective work will be needed to figure out # whether the given database path is for the GeoIP country or city diff --git a/tests/gis_tests/test_geoip2.py b/tests/gis_tests/test_geoip2.py index 9ad039bbba..158b014abc 100644 --- a/tests/gis_tests/test_geoip2.py +++ b/tests/gis_tests/test_geoip2.py @@ -1,10 +1,10 @@ import os -import unittest from unittest import mock, skipUnless from django.conf import settings from django.contrib.gis.geoip2 import HAS_GEOIP2 from django.contrib.gis.geos import GEOSGeometry +from django.test import SimpleTestCase if HAS_GEOIP2: from django.contrib.gis.geoip2 import GeoIP2, GeoIP2Exception @@ -18,7 +18,7 @@ if HAS_GEOIP2: HAS_GEOIP2 and getattr(settings, "GEOIP_PATH", None), "GeoIP is required along with the GEOIP_PATH setting." ) -class GeoIPTest(unittest.TestCase): +class GeoIPTest(SimpleTestCase): addr = '128.249.1.1' fqdn = 'tmc.edu' @@ -53,6 +53,12 @@ class GeoIPTest(unittest.TestCase): with self.assertRaises(e): GeoIP2(bad, 0) + def test_no_database_file(self): + invalid_path = os.path.join(os.path.dirname(__file__), 'data') + msg = 'Could not load a database from %s.' % invalid_path + with self.assertRaisesMessage(GeoIP2Exception, msg): + GeoIP2(invalid_path) + def test02_bad_query(self): "GeoIP query parameter checking." cntry_g = GeoIP2(city='')