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