2 from django
.conf
import settings
3 from django
.contrib
.gis
.geos
import GEOSGeometry
4 from django
.contrib
.gis
.geoip
import GeoIP
, GeoIPException
5 from django
.utils
import unittest
7 # Note: Requires use of both the GeoIP country and city datasets.
8 # The GEOIP_DATA path should be the only setting set (the directory
9 # should contain links or the actual database files 'GeoIP.dat' and
11 class GeoIPTest(unittest
.TestCase
):
13 def test01_init(self
):
14 "Testing GeoIP initialization."
15 g1
= GeoIP() # Everything inferred from GeoIP path
16 path
= settings
.GEOIP_PATH
17 g2
= GeoIP(path
, 0) # Passing in data path explicitly.
18 g3
= GeoIP
.open(path
, 0) # MaxMind Python API syntax.
20 for g
in (g1
, g2
, g3
):
21 self
.assertEqual(True, bool(g
._country
))
22 self
.assertEqual(True, bool(g
._city
))
24 # Only passing in the location of one database.
25 city
= os
.path
.join(path
, 'GeoLiteCity.dat')
26 cntry
= os
.path
.join(path
, 'GeoIP.dat')
27 g4
= GeoIP(city
, country
='')
28 self
.assertEqual(None, g4
._country
)
29 g5
= GeoIP(cntry
, city
='')
30 self
.assertEqual(None, g5
._city
)
32 # Improper parameters.
33 bad_params
= (23, 'foo', 15.23)
34 for bad
in bad_params
:
35 self
.assertRaises(GeoIPException
, GeoIP
, cache
=bad
)
36 if isinstance(bad
, basestring
):
40 self
.assertRaises(e
, GeoIP
, bad
, 0)
42 def test02_bad_query(self
):
43 "Testing GeoIP query parameter checking."
44 cntry_g
= GeoIP(city
='<foo>')
45 # No city database available, these calls should fail.
46 self
.assertRaises(GeoIPException
, cntry_g
.city
, 'google.com')
47 self
.assertRaises(GeoIPException
, cntry_g
.coords
, 'yahoo.com')
49 # Non-string query should raise TypeError
50 self
.assertRaises(TypeError, cntry_g
.country_code
, 17)
51 self
.assertRaises(TypeError, cntry_g
.country_name
, GeoIP
)
53 def test03_country(self
):
54 "Testing GeoIP country querying methods."
55 g
= GeoIP(city
='<foo>')
57 fqdn
= 'www.google.com'
60 for query
in (fqdn
, addr
):
61 for func
in (g
.country_code
, g
.country_code_by_addr
, g
.country_code_by_name
):
62 self
.assertEqual('US', func(query
))
63 for func
in (g
.country_name
, g
.country_name_by_addr
, g
.country_name_by_name
):
64 self
.assertEqual('United States', func(query
))
65 self
.assertEqual({'country_code' : 'US', 'country_name' : 'United States'},
68 def test04_city(self
):
69 "Testing GeoIP city querying methods."
70 g
= GeoIP(country
='<foo>')
74 for query
in (fqdn
, addr
):
75 # Country queries should still work.
76 for func
in (g
.country_code
, g
.country_code_by_addr
, g
.country_code_by_name
):
77 self
.assertEqual('US', func(query
))
78 for func
in (g
.country_name
, g
.country_name_by_addr
, g
.country_name_by_name
):
79 self
.assertEqual('United States', func(query
))
80 self
.assertEqual({'country_code' : 'US', 'country_name' : 'United States'},
83 # City information dictionary.
85 self
.assertEqual('USA', d
['country_code3'])
86 self
.assertEqual('Houston', d
['city'])
87 self
.assertEqual('TX', d
['region'])
88 self
.assertEqual(713, d
['area_code'])
90 self
.failIf(not isinstance(geom
, GEOSGeometry
))
91 lon
, lat
= (-95.4010, 29.7079)
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], 4)
96 self
.assertAlmostEqual(lat
, tup
[1], 4)
98 def test05_unicode_response(self
):
99 "Testing that GeoIP strings are properly encoded, see #16553."
101 d
= g
.city('62.224.93.23')
102 self
.assertEqual(u
'Sch\xf6mberg', d
['city'])
104 def test06_unicode_query(self
):
105 "Testing that GeoIP accepts unicode string queries, see #17059."
107 d
= g
.country(u
'whitehouse.gov')
108 self
.assertEqual(u
'US', d
['country_code'])
112 s
= unittest
.TestSuite()
113 s
.addTest(unittest
.makeSuite(GeoIPTest
))
116 def run(verbosity
=1):
117 unittest
.TextTestRunner(verbosity
=verbosity
).run(suite())