1# -*- coding: utf-8 -*- 2import unittest 3from nose.tools import raises 4 5import pygeoip 6from tests.config import COUNTRY_DB_PATH, COUNTRY_V6_DB_PATH 7 8 9class TestGeoIPCountryFunctions(unittest.TestCase): 10 def setUp(self): 11 self.gi = pygeoip.GeoIP(COUNTRY_DB_PATH) 12 self.gi6 = pygeoip.GeoIP(COUNTRY_V6_DB_PATH) 13 14 def testCountryCodeByAddr(self): 15 us_code = self.gi.country_code_by_addr('64.17.254.216') 16 it_code = self.gi.country_code_by_addr('78.26.70.208') 17 jp6_code = self.gi6.country_code_by_addr('2001:200::') 18 19 self.assertEqual(us_code, 'US') 20 self.assertEqual(it_code, 'IT') 21 self.assertEqual(jp6_code, 'JP') 22 23 def testCountryNameByAddr(self): 24 us_name = self.gi.country_name_by_addr('64.17.254.216') 25 it_name = self.gi.country_name_by_addr('78.26.70.208') 26 jp6_name = self.gi6.country_name_by_addr('2001:200::') 27 28 self.assertEqual(us_name, 'United States') 29 self.assertEqual(it_name, 'Italy') 30 self.assertEqual(jp6_name, 'Japan') 31 32 @raises(pygeoip.GeoIPError) 33 def testOpen4With6(self): 34 data = self.gi.country_code_by_addr('2001:200::') 35 raise ValueError(data) 36 37 @raises(pygeoip.GeoIPError) 38 def testOpen6With4(self): 39 data = self.gi6.country_code_by_addr('78.26.70.208') 40 raise ValueError(data) 41 42 @raises(pygeoip.GeoIPError) 43 def testOrgByAddr(self): 44 self.gi.org_by_addr('78.26.70.208') 45 46 @raises(pygeoip.GeoIPError) 47 def testRecordByAddr(self): 48 self.gi.record_by_addr('78.26.70.208') 49 50 @raises(pygeoip.GeoIPError) 51 def testRegionByAddr(self): 52 self.gi.region_by_addr('78.26.70.208') 53 54 @raises(pygeoip.GeoIPError) 55 def testTimeZoneByAddr(self): 56 self.gi.time_zone_by_addr('78.26.70.208') 57