def test_regions(): with mock.patch('tests.test_geoip.geoip._GEOIP_IPV4_GEO') as mock_db: geoip.region_by_addr(IPv4Address('127.0.0.1')) assert mock_db.record_by_addr.called_with('127.0.0.1') with mock.patch('tests.test_geoip.geoip._GEOIP_IPV6_GEO') as mock_db: geoip.region_by_addr(IPv6Address('::1')) assert mock_db.record_by_addr.called_with('::1')
def region(self): """ If :func:`~lars.geoip.init_databases` has been called with a region-level (or lower) GeoIP IPv6 database, returns the region of the address. """ return geoip.region_by_addr(self)
def test_init_db(): with mock.patch('tests.test_geoip.geoip.pygeoip.GeoIP') as mock_class: geoip.init_databases('mock.dat') assert mock_class.called_with('mock.dat', pygeoip.MEMORY_CACHE) # Ensure when the IPv6 database isn't initialized we get value errors # when requesting geoip info on IPv6 addresses # None, not errors (as the IPv6 database is optional) assert geoip._GEOIP_IPV6_GEO is None assert geoip._GEOIP_IPV4_ISP is None assert geoip._GEOIP_IPV6_ISP is None assert geoip._GEOIP_IPV4_ORG is None assert geoip._GEOIP_IPV6_ORG is None with pytest.raises(ValueError): geoip.country_code_by_addr(IPv6Address('::1')) with pytest.raises(ValueError): geoip.region_by_addr(IPv6Address('::1')) with pytest.raises(ValueError): geoip.city_by_addr(IPv6Address('::1')) with pytest.raises(ValueError): geoip.coords_by_addr(IPv6Address('::1')) with pytest.raises(ValueError): geoip.isp_by_addr(IPv4Address('127.0.0.1')) with pytest.raises(ValueError): geoip.org_by_addr(IPv4Address('127.0.0.1')) # Test loading no databases with pytest.raises(ValueError): geoip.init_databases() # Test loading every other database mock_class.reset_mock() geoip.init_databases(None, 'isp_v4.dat', 'org_v4.dat', 'geo_v6.dat', 'isp_v6.dat', 'org_v6.dat', memcache=False) assert mock_class.call_count == 5 assert mock_class.mock_calls == [ mock.call('isp_v4.dat', 0), mock.call('org_v4.dat', 0), mock.call('geo_v6.dat', 0), mock.call('isp_v6.dat', 0), mock.call('org_v6.dat', 0), ]
def test_init_db(): with mock.patch('tests.test_geoip.geoip.pygeoip.GeoIP') as mock_class: geoip.init_databases('mock.dat') assert mock_class.called_with('mock.dat', pygeoip.MEMORY_CACHE) # Ensure when the IPv6 database isn't initialized we get value errors # when requesting geoip info on IPv6 addresses # None, not errors (as the IPv6 database is optional) assert geoip._GEOIP_IPV6_GEO is None assert geoip._GEOIP_IPV4_ISP is None assert geoip._GEOIP_IPV6_ISP is None assert geoip._GEOIP_IPV4_ORG is None assert geoip._GEOIP_IPV6_ORG is None with pytest.raises(ValueError): geoip.country_code_by_addr(IPv6Address('::1')) with pytest.raises(ValueError): geoip.region_by_addr(IPv6Address('::1')) with pytest.raises(ValueError): geoip.city_by_addr(IPv6Address('::1')) with pytest.raises(ValueError): geoip.coords_by_addr(IPv6Address('::1')) with pytest.raises(ValueError): geoip.isp_by_addr(IPv4Address('127.0.0.1')) with pytest.raises(ValueError): geoip.org_by_addr(IPv4Address('127.0.0.1')) # Test loading no databases with pytest.raises(ValueError): geoip.init_databases() # Test loading every other database mock_class.reset_mock() geoip.init_databases( None, 'isp_v4.dat', 'org_v4.dat', 'geo_v6.dat', 'isp_v6.dat', 'org_v6.dat', memcache=False) assert mock_class.call_count == 5 assert mock_class.mock_calls == [ mock.call('isp_v4.dat', 0), mock.call('org_v4.dat', 0), mock.call('geo_v6.dat', 0), mock.call('isp_v6.dat', 0), mock.call('org_v6.dat', 0), ]