Permalink
Browse files

gis: Fixed #6196 in GeoIP refactor. Added unit tests, improved path s…

…etup, and made mostly compatible w/existing MaxMind Python API.

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6918 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 40a7292 commit 9270d47d80a4ffa09659d26ae71559c20384ffed @jbronn jbronn committed Dec 14, 2007
@@ -3,9 +3,12 @@
from unittest import TestSuite, TextTestRunner
from django.contrib.gis.gdal import HAS_GDAL
try:
- from django.contrib.gis.tests.utils import mysql, oracle
+ from django.contrib.gis.tests.utils import mysql, oracle, postgis
except:
- mysql, oracle = (False, False)
+ mysql, oracle, postgis = (False, False, False)
+from django.contrib.gis.utils import HAS_GEOIP
+from django.conf import settings
+if not settings._target: settings.configure()
# Tests that require use of a spatial database (e.g., creation of models)
test_models = ['geoapp']
@@ -20,8 +23,9 @@
# TODO: There is a problem with the `syncdb` SQL for the LayerMapping
# tests on Oracle.
test_models += ['distapp']
- else:
+ elif postgis:
test_models += ['distapp', 'layermap']
+
test_suite_names += [
'test_gdal_driver',
'test_gdal_ds',
@@ -33,6 +37,10 @@
else:
print >>sys.stderr, "GDAL not available - no GDAL tests will be run."
+if HAS_GEOIP:
+ if hasattr(settings, 'GEOIP_PATH'):
+ test_suite_names.append('test_geoip')
+
def suite():
"Builds a test suite for the GIS package."
s = TestSuite()
@@ -77,7 +85,6 @@ def run_tests(module_list, verbosity=1, interactive=True):
Finally, the tests may be run by invoking `./manage.py test`.
"""
- from django.conf import settings
from django.contrib.gis.db.backend import create_spatial_db
from django.db import connection
from django.test.utils import destroy_test_db
@@ -86,10 +93,13 @@ def run_tests(module_list, verbosity=1, interactive=True):
old_debug = settings.DEBUG
old_name = copy(settings.DATABASE_NAME)
old_installed = copy(settings.INSTALLED_APPS)
+ new_installed = copy(settings.INSTALLED_APPS)
# Want DEBUG to be set to False.
settings.DEBUG = False
+ from django.db.models import loading
+
# Creating the test suite, adding the test models to INSTALLED_APPS, and
# adding the model test suites to our suite package.
test_suite = suite()
@@ -99,16 +109,18 @@ def run_tests(module_list, verbosity=1, interactive=True):
test_module_name = 'tests_mysql'
else:
test_module_name = 'tests'
- settings.INSTALLED_APPS.append(module_name)
+ new_installed.append(module_name)
+
+ # Getting the test suite
tsuite = getattr(__import__('django.contrib.gis.tests.%s' % test_model, globals(), locals(), [test_module_name]), test_module_name)
test_suite.addTest(tsuite.suite())
-
+
# Resetting the loaded flag to take into account what we appended to
# the INSTALLED_APPS (since this routine is invoked through
# django/core/management, it caches the apps; this ensures that syncdb
# will see our appended models)
- from django.db.models import loading
- loading._loaded = False
+ settings.INSTALLED_APPS = new_installed
+ loading.cache.loaded = False
# Creating the test spatial database.
create_spatial_db(test=True, verbosity=verbosity)
@@ -0,0 +1,104 @@
+import os, unittest
+from django.db import settings
+from django.contrib.gis.geos import GEOSGeometry
+from django.contrib.gis.utils import GeoIP, GeoIPException
+
+# Note: Requires use of both the GeoIP country and city datasets.
+# The GEOIP_DATA path should be the only setting set (the directory
+# should contain links or the actual database files 'GeoIP.dat' and
+# 'GeoLiteCity.dat'.
+class GeoIPTest(unittest.TestCase):
+
+ def test01_init(self):
+ "Testing GeoIP initialization."
+ g1 = GeoIP() # Everything inferred from GeoIP path
+ path = settings.GEOIP_PATH
+ g2 = GeoIP(path, 0) # Passing in data path explicitly.
+ g3 = GeoIP.open(path, 0) # MaxMind Python API syntax.
+
+ for g in (g1, g2, g3):
+ self.assertEqual(True, bool(g._country))
+ self.assertEqual(True, bool(g._city))
+
+ # Only passing in the location of one database.
+ city = os.path.join(path, 'GeoLiteCity.dat')
+ cntry = os.path.join(path, 'GeoIP.dat')
+ g4 = GeoIP(city, country='')
+ self.assertEqual(None, g4._country)
+ g5 = GeoIP(cntry, city='')
+ self.assertEqual(None, g5._city)
+
+ # Improper parameters.
+ bad_params = (23, 'foo', 15.23)
+ for bad in bad_params:
+ self.assertRaises(GeoIPException, GeoIP, cache=bad)
+ if isinstance(bad, basestring):
+ e = GeoIPException
+ else:
+ e = TypeError
+ self.assertRaises(e, GeoIP, bad, 0)
+
+ def test02_bad_query(self):
+ "Testing GeoIP query parameter checking."
+ cntry_g = GeoIP(city='<foo>')
+ # No city database available, these calls should fail.
+ self.assertRaises(GeoIPException, cntry_g.city, 'google.com')
+ self.assertRaises(GeoIPException, cntry_g.coords, 'yahoo.com')
+
+ # Non-string query should raise TypeError
+ self.assertRaises(TypeError, cntry_g.country_code, 17)
+ self.assertRaises(TypeError, cntry_g.country_name, GeoIP)
+
+ def test03_country(self):
+ "Testing GeoIP country querying methods."
+ g = GeoIP(city='<foo>')
+
+ fqdn = 'www.google.com'
+ addr = '12.215.42.19'
+
+ for query in (fqdn, addr):
+ for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
+ self.assertEqual('US', func(query))
+ for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
+ self.assertEqual('United States', func(query))
+ self.assertEqual({'country_code' : 'US', 'country_name' : 'United States'},
+ g.country(query))
+
+ def test04_city(self):
+ "Testing GeoIP city querying methods."
+ g = GeoIP(country='<foo>')
+
+ addr = '130.80.29.3'
+ fqdn = 'chron.com'
+ for query in (fqdn, addr):
+ # Country queries should still work.
+ for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
+ self.assertEqual('US', func(query))
+ for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
+ self.assertEqual('United States', func(query))
+ self.assertEqual({'country_code' : 'US', 'country_name' : 'United States'},
+ g.country(query))
+
+ # City information dictionary.
+ d = g.city(query)
+ self.assertEqual('USA', d['country_code3'])
+ self.assertEqual('Houston', d['city'])
+ self.assertEqual('TX', d['region'])
+ self.assertEqual('77002', d['postal_code'])
+ self.assertEqual(713, d['area_code'])
+ geom = g.geos(query)
+ self.failIf(not isinstance(geom, GEOSGeometry))
+ lon, lat = (-95.366996765, 29.752300262)
+ lat_lon = g.lat_lon(query)
+ lat_lon = (lat_lon[1], lat_lon[0])
+ for tup in (geom.tuple, g.coords(query), g.lon_lat(query), lat_lon):
+ self.assertAlmostEqual(lon, tup[0], 9)
+ self.assertAlmostEqual(lat, tup[1], 9)
+
+def suite():
+ s = unittest.TestSuite()
+ s.addTest(unittest.makeSuite(GeoIPTest))
+ return s
+
+def run(verbosity=2):
+ unittest.TextTestRunner(verbosity=verbosity).run(suite())
@@ -15,7 +15,7 @@
# Attempting to import the GeoIP class.
try:
- from django.contrib.gis.utils.geoip import GeoIP
+ from django.contrib.gis.utils.geoip import GeoIP, GeoIPException
HAS_GEOIP = True
except:
HAS_GEOIP = False
Oops, something went wrong. Retry.

0 comments on commit 9270d47

Please sign in to comment.