|
1 import os, unittest |
|
2 from django.db import settings |
|
3 from django.contrib.gis.geos import GEOSGeometry |
|
4 from 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'. |
|
10 class 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('77002', d['postal_code']) |
|
88 self.assertEqual(713, d['area_code']) |
|
89 geom = g.geos(query) |
|
90 self.failIf(not isinstance(geom, GEOSGeometry)) |
|
91 lon, lat = (-95.366996765, 29.752300262) |
|
92 lat_lon = g.lat_lon(query) |
|
93 lat_lon = (lat_lon[1], lat_lon[0]) |
|
94 for tup in (geom.tuple, g.coords(query), g.lon_lat(query), lat_lon): |
|
95 self.assertAlmostEqual(lon, tup[0], 9) |
|
96 self.assertAlmostEqual(lat, tup[1], 9) |
|
97 |
|
98 def suite(): |
|
99 s = unittest.TestSuite() |
|
100 s.addTest(unittest.makeSuite(GeoIPTest)) |
|
101 return s |
|
102 |
|
103 def run(verbosity=2): |
|
104 unittest.TextTestRunner(verbosity=verbosity).run(suite()) |