PageRenderTime 143ms CodeModel.GetById 80ms app.highlight 14ms RepoModel.GetById 47ms app.codeStats 0ms

/django/contrib/gis/tests/test_geoip.py

https://code.google.com/p/mango-py/
Python | 103 lines | 94 code | 4 blank | 5 comment | 2 complexity | 55b1889fc0e51fc0876bce6dabdd4bcf MD5 | raw file
  1import os, unittest
  2from django.db import settings
  3from django.contrib.gis.geos import GEOSGeometry
  4from django.contrib.gis.utils import GeoIP, GeoIPException
  5
  6# Note: Requires use of both the GeoIP country and city datasets.
  7# The GEOIP_DATA path should be the only setting set (the directory
  8# should contain links or the actual database files 'GeoIP.dat' and
  9# 'GeoLiteCity.dat'.
 10class GeoIPTest(unittest.TestCase):
 11
 12    def test01_init(self):
 13        "Testing GeoIP initialization."
 14        g1 = GeoIP() # Everything inferred from GeoIP path
 15        path = settings.GEOIP_PATH
 16        g2 = GeoIP(path, 0) # Passing in data path explicitly.
 17        g3 = GeoIP.open(path, 0) # MaxMind Python API syntax.
 18
 19        for g in (g1, g2, g3):
 20            self.assertEqual(True, bool(g._country))
 21            self.assertEqual(True, bool(g._city))
 22
 23        # Only passing in the location of one database.
 24        city = os.path.join(path, 'GeoLiteCity.dat')
 25        cntry = os.path.join(path, 'GeoIP.dat')
 26        g4 = GeoIP(city, country='')
 27        self.assertEqual(None, g4._country)
 28        g5 = GeoIP(cntry, city='')
 29        self.assertEqual(None, g5._city)
 30
 31        # Improper parameters.
 32        bad_params = (23, 'foo', 15.23)
 33        for bad in bad_params:
 34            self.assertRaises(GeoIPException, GeoIP, cache=bad)
 35            if isinstance(bad, basestring):
 36                e = GeoIPException
 37            else:
 38                e = TypeError
 39            self.assertRaises(e, GeoIP, bad, 0)
 40
 41    def test02_bad_query(self):
 42        "Testing GeoIP query parameter checking."
 43        cntry_g = GeoIP(city='<foo>')
 44        # No city database available, these calls should fail.
 45        self.assertRaises(GeoIPException, cntry_g.city, 'google.com')
 46        self.assertRaises(GeoIPException, cntry_g.coords, 'yahoo.com')
 47
 48        # Non-string query should raise TypeError
 49        self.assertRaises(TypeError, cntry_g.country_code, 17)
 50        self.assertRaises(TypeError, cntry_g.country_name, GeoIP)
 51
 52    def test03_country(self):
 53        "Testing GeoIP country querying methods."
 54        g = GeoIP(city='<foo>')
 55
 56        fqdn = 'www.google.com'
 57        addr = '12.215.42.19'
 58
 59        for query in (fqdn, addr):
 60            for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
 61                self.assertEqual('US', func(query))
 62            for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
 63                self.assertEqual('United States', func(query))
 64            self.assertEqual({'country_code' : 'US', 'country_name' : 'United States'},
 65                             g.country(query))
 66
 67    def test04_city(self):
 68        "Testing GeoIP city querying methods."
 69        g = GeoIP(country='<foo>')
 70
 71        addr = '130.80.29.3'
 72        fqdn = 'chron.com'
 73        for query in (fqdn, addr):
 74            # Country queries should still work.
 75            for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
 76                self.assertEqual('US', func(query))
 77            for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
 78                self.assertEqual('United States', func(query))
 79            self.assertEqual({'country_code' : 'US', 'country_name' : 'United States'},
 80                             g.country(query))
 81
 82            # City information dictionary.
 83            d = g.city(query)
 84            self.assertEqual('USA', d['country_code3'])
 85            self.assertEqual('Houston', d['city'])
 86            self.assertEqual('TX', d['region'])
 87            self.assertEqual(713, d['area_code'])
 88            geom = g.geos(query)
 89            self.failIf(not isinstance(geom, GEOSGeometry))
 90            lon, lat = (-95.3670, 29.7523)
 91            lat_lon = g.lat_lon(query)
 92            lat_lon = (lat_lon[1], lat_lon[0])
 93            for tup in (geom.tuple, g.coords(query), g.lon_lat(query), lat_lon):
 94                self.assertAlmostEqual(lon, tup[0], 4)
 95                self.assertAlmostEqual(lat, tup[1], 4)
 96
 97def suite():
 98    s = unittest.TestSuite()
 99    s.addTest(unittest.makeSuite(GeoIPTest))
100    return s
101
102def run(verbosity=2):
103    unittest.TextTestRunner(verbosity=verbosity).run(suite())