def test_unknown_country(self): r = maximum_country_radius('AA') self.assertTrue(r is None) self.assertFalse('AA' in _radius_cache) r = maximum_country_radius('AAA') self.assertTrue(r is None) self.assertFalse('AAA' in _radius_cache)
def test_alpha3(self): r = maximum_country_radius('USA') self.assertEqual(r, self.usa_radius) cached = _radius_cache['USA'] self.assertEqual(r, cached) r = maximum_country_radius('usa') self.assertEqual(r, self.usa_radius) self.assertFalse('usa' in _radius_cache)
def test_malformed_country(self): r = maximum_country_radius(None) self.assertTrue(r is None) r = maximum_country_radius(42) self.assertTrue(r is None) r = maximum_country_radius('A') self.assertTrue(r is None) r = maximum_country_radius('-#1-') self.assertTrue(r is None)
def geoip_accuracy(country_code, city=False): """ Returns the best accuracy guess for the given GeoIP record. :param country_code: A two-letter ISO country code. :type country_code: str :param city: Do we have a city record or a country record. :type city: bool :returns: An accuracy guess in meters. :rtype: int """ accuracy = None if country_code: accuracy = maximum_country_radius(country_code) if accuracy is None: # No country code or no successful radius lookup accuracy = GEOIP_COUNTRY_ACCURACY if city: # Use country radius as an upper bound for city radius # for really small countries accuracy = min(GEOIP_CITY_ACCURACY, accuracy) return accuracy
def radius_from_geoip(record): """ Returns the best accuracy guess in meters for the given GeoIP record and whether or not the record included city data. """ accuracy = None if 'country_code3' in record and record['country_code3']: accuracy = maximum_country_radius(record['country_code3']) elif 'country_code' in record and record['country_code']: accuracy = maximum_country_radius(record['country_code']) if accuracy is None: # No country code or no successful radius lookup accuracy = GEOIP_COUNTRY_ACCURACY city = False if 'city' in record and record['city']: # Use country radius as an upper bound for city radius # for really small countries accuracy = min(GEOIP_CITY_ACCURACY, accuracy) city = True return (accuracy, city)
def geoip_accuracy(country_code, city=False): """ Returns the best accuracy guess for the given GeoIP record. :param country_code: A two-letter ISO country code. :param city: A boolean indicating whether or not we have a city record. :returns: An accuracy guess in meters. :rtype: int """ accuracy = None if country_code: accuracy = maximum_country_radius(country_code) if accuracy is None: # No country code or no successful radius lookup accuracy = GEOIP_COUNTRY_ACCURACY if city: # Use country radius as an upper bound for city radius # for really small countries accuracy = min(GEOIP_CITY_ACCURACY, accuracy) return accuracy
"city": True, "country_code": "GB", "country_name": "United Kingdom", "ip": "81.2.69.192", "latitude": 51.5142, "longitude": -0.0931, "accuracy": GEOIP_CITY_ACCURACY, }, "Bhutan": { "city": False, "country_code": "BT", "country_name": "Bhutan", "ip": "67.43.156.1", "latitude": 27.5, "longitude": 90.5, "accuracy": maximum_country_radius("BT"), }, } GB_LAT = 51.5 GB_LON = -0.1 GB_MCC = 234 GB_MNC = 30 def _make_db(uri=SQLURI): return Database(uri) def _make_redis(uri=REDIS_URI): return redis_client(uri)
'city': True, 'country_code': 'GB', 'country_name': 'United Kingdom', 'ip': '81.2.69.192', 'latitude': 51.5142, 'longitude': -0.0931, 'accuracy': GEOIP_CITY_ACCURACY, }, 'Bhutan': { 'city': False, 'country_code': 'BT', 'country_name': 'Bhutan', 'ip': '67.43.156.1', 'latitude': 27.5, 'longitude': 90.5, 'accuracy': maximum_country_radius('BT'), }, } CANADA_MCC = 302 USA_MCC = 310 ATT_MNC = 150 FREMONT_LAT = 37.5079 FREMONT_LON = -121.96 BRAZIL_MCC = 724 VIVO_MNC = 11 SAO_PAULO_LAT = -23.54 SAO_PAULO_LON = -46.64 PORTO_ALEGRE_LAT = -30.032 PORTO_ALEGRE_LON = -51.22
def test_small_countries(self): r = maximum_country_radius('LI') self.assertEqual(r, self.li_radius) r = maximum_country_radius('VAT') self.assertEqual(r, self.vat_radius)
PARIS_LON, PORTO_ALEGRE_LAT, PORTO_ALEGRE_LON, SAO_PAULO_LAT, SAO_PAULO_LON, USA_MCC, VIVO_MNC, ) from ichnaea.service import locate GB_IP = '192.168.0.1' GB_LAT = 51.5 GB_LON = -0.1 GB_MCC = 234 GB_RADIUS = maximum_country_radius('GB') class TestSearchAllSources(DBTestCase): @classmethod def setUpClass(cls): DBTestCase.setUpClass() cls.geoip_db = GeoIPMock({ FREMONT_IP: { 'latitude': FREMONT_LAT, 'longitude': FREMONT_LON, 'country_code': 'US', 'city': True, }, GB_IP: {