Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pygeoip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pygeoip
Commits
c4688541
Commit
c4688541
authored
Nov 16, 2013
by
William Tisäter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use smaller test databases and optimize tests
parent
23e94c00
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
98 additions
and
308 deletions
+98
-308
DEVELOPER.md
+1
-1
README.md
+9
-9
tests/config.py
+2
-1
tests/test_asnum.py
+5
-23
tests/test_cache.py
+6
-9
tests/test_city.py
+12
-78
tests/test_country.py
+18
-56
tests/test_isp.py
+2
-20
tests/test_memory.py
+2
-2
tests/test_netspeed.py
+4
-10
tests/test_org.py
+3
-21
tests/test_region.py
+7
-40
tests/test_thread.py
+23
-18
tests/test_tz.py
+3
-19
tests/test_unicode.py
+1
-1
No files found.
DEVELOPER.md
View file @
c4688541
...
...
@@ -12,7 +12,7 @@ Running the tests requires a couple of sample databases found on the
link below.
Maxmind sample databases for testing can be downloaded here:
https://www.defunct.cc/maxmind-geoip-samples.tar.gz (
58
MB)
https://www.defunct.cc/maxmind-geoip-samples.tar.gz (
17
MB)
Extract the tarball in the tests directory and run
`tox`
from the root directory.
...
...
README.md
View file @
c4688541
...
...
@@ -111,12 +111,12 @@ For more information, [check out the full API documentation](http://packages.pyt
## Supported Databases
| Type | IPv4 | IPv6 | Details
|
| -------------- |:----:|:----:| -------
-------
|
| Country | ✓
| ✓
|
[
MaxMind Country product page
](
http://www.maxmind.com/en/country
)
|
| City | ✓
| ✓
|
[
MaxMind City product page
](
http://www.maxmind.com/en/city
)
|
| Organization | ✓
|
|
[
MaxMind Organization product page
](
http://www.maxmind.com/en/organization
)
|
| ISP | ✓
|
|
[
MaxMind ISP product page
](
http://www.maxmind.com/en/isp
)
|
| Region | ✓
|
|
[
MaxMind Region product page
](
http://www.maxmind.com/en/geolocation_landing
)
|
| ASN | ✓
| ✓
|
[
MaxMind ASN product page
](
http://dev.maxmind.com/geoip/legacy/geolite
)
|
| Netspeed | ✓
|
|
[
MaxMind Netspeed product page
](
http://www.maxmind.com/en/netspeed
)
|
| Type | IPv4 | IPv6 | Details |
| -------------- |:----:|:----:| ------- |
| Country | ✓
| ✓
|
[
MaxMind Country product page
](
http://www.maxmind.com/en/country
)
|
| City | ✓
| ✓
|
[
MaxMind City product page
](
http://www.maxmind.com/en/city
)
|
| Organization | ✓
|
|
[
MaxMind Organization product page
](
http://www.maxmind.com/en/organization
)
|
| ISP | ✓
|
|
[
MaxMind ISP product page
](
http://www.maxmind.com/en/isp
)
|
| Region | ✓
|
|
[
MaxMind Region product page
](
http://www.maxmind.com/en/geolocation_landing
)
|
| ASN | ✓
| ✓
|
[
MaxMind ASN product page
](
http://dev.maxmind.com/geoip/legacy/geolite
)
|
| Netspeed | ✓
|
|
[
MaxMind Netspeed product page
](
http://www.maxmind.com/en/netspeed
)
|
tests/config.py
View file @
c4688541
...
...
@@ -9,5 +9,6 @@ CITY_DB_PATH = path.join(DATA_DIR, 'GeoLiteCity.dat')
ORG_DB_PATH
=
path
.
join
(
DATA_DIR
,
'GeoIPOrg.dat'
)
ASNUM_DB_PATH
=
path
.
join
(
DATA_DIR
,
'GeoIPASNum.dat'
)
ISP_DB_PATH
=
path
.
join
(
DATA_DIR
,
'GeoIPISP.dat'
)
NETSPEED_DB_PATH
=
path
.
join
(
DATA_DIR
,
'GeoIPNetSpeedRev0.dat'
)
NETSPEED_DB_PATH
=
path
.
join
(
DATA_DIR
,
'GeoIPNetSpeed.dat'
)
NETSPEEDCELL_DB_PATH
=
path
.
join
(
DATA_DIR
,
'GeoIPNetSpeedCell.dat'
)
CORRUPT_DB_PATH
=
path
.
join
(
DATA_DIR
,
'../config.py'
)
tests/test_asnum.py
View file @
c4688541
...
...
@@ -8,34 +8,16 @@ from tests.config import ASNUM_DB_PATH
class
TestGeoIPASNumFunctions
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
us_as
=
'AS15169 Google Inc.'
self
.
us_ip
=
'64.233.161.99'
self
.
us_hostname
=
'google.com'
self
.
gb_as
=
'AS2818 BBC Internet Services, UK'
self
.
gb_ip
=
'212.58.253.68'
self
.
gb_hostname
=
'bbc.com'
self
.
gia
=
pygeoip
.
GeoIP
(
ASNUM_DB_PATH
)
self
.
gi
=
pygeoip
.
GeoIP
(
ASNUM_DB_PATH
)
def
testOrgByAddr
(
self
):
gb_as
=
self
.
gia
.
asn_by_addr
(
self
.
gb_ip
)
us_as
=
self
.
gia
.
asn_by_addr
(
self
.
us_ip
)
self
.
assertEqual
(
gb_as
,
self
.
gb_as
)
self
.
assertEqual
(
us_as
,
self
.
us_as
)
def
testOrgByName
(
self
):
gb_as
=
self
.
gia
.
asn_by_name
(
self
.
gb_hostname
)
us_as
=
self
.
gia
.
asn_by_name
(
self
.
us_hostname
)
self
.
assertEqual
(
gb_as
,
self
.
gb_as
)
self
.
assertEqual
(
us_as
,
self
.
us_as
)
asn
=
self
.
gi
.
asn_by_addr
(
'64.17.254.216'
)
self
.
assertEqual
(
asn
,
'AS33224'
)
@raises
(
pygeoip
.
GeoIPError
)
def
testCodeByAddr
(
self
):
self
.
gi
a
.
country_code_by_addr
(
self
.
us_ip
)
self
.
gi
.
country_code_by_addr
(
'64.17.254.216'
)
@raises
(
pygeoip
.
GeoIPError
)
def
testNameByAddr
(
self
):
self
.
gi
a
.
country_name_by_addr
(
self
.
us_ip
)
self
.
gi
.
country_name_by_addr
(
'64.17.254.216'
)
tests/test_cache.py
View file @
c4688541
...
...
@@ -7,18 +7,15 @@ from tests.config import COUNTRY_DB_PATH
class
TestGeoIPCacheMethods
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
us_ip
=
'64.233.161.99'
self
.
us_code
=
'US'
self
.
gi_mmap
=
pygeoip
.
GeoIP
(
COUNTRY_DB_PATH
,
MMAP_CACHE
)
self
.
gi_memory
=
pygeoip
.
GeoIP
(
COUNTRY_DB_PATH
,
MEMORY_CACHE
)
self
.
gi_standard
=
pygeoip
.
GeoIP
(
COUNTRY_DB_PATH
,
STANDARD
)
def
testCountryCodeByAddr
(
self
):
code_mmap
=
self
.
gi_mmap
.
country_code_by_addr
(
self
.
us_ip
)
code_memory
=
self
.
gi_memory
.
country_code_by_addr
(
self
.
us_ip
)
code_standard
=
self
.
gi_standard
.
country_code_by_addr
(
self
.
us_ip
)
code_mmap
=
self
.
gi_mmap
.
country_code_by_addr
(
'64.17.254.216'
)
code_memory
=
self
.
gi_memory
.
country_code_by_addr
(
'64.17.254.216'
)
code_standard
=
self
.
gi_standard
.
country_code_by_addr
(
'64.17.254.216'
)
self
.
assertEqual
(
code_mmap
,
self
.
us_code
)
self
.
assertEqual
(
code_memory
,
self
.
us_code
)
self
.
assertEqual
(
code_standard
,
self
.
us_code
)
self
.
assertEqual
(
code_mmap
,
'US'
)
self
.
assertEqual
(
code_memory
,
'US'
)
self
.
assertEqual
(
code_standard
,
'US'
)
tests/test_city.py
View file @
c4688541
...
...
@@ -7,18 +7,6 @@ from tests.config import CITY_DB_PATH
class
TestGeoIPCityFunctions
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
us_hostname
=
'google.com'
self
.
us_ip
=
'64.233.161.99'
self
.
gb_hostname
=
'bbc.com'
self
.
gb_ip
=
'212.58.253.68'
self
.
us_code
=
'US'
self
.
gb_code
=
'GB'
self
.
us_name
=
'United States'
self
.
gb_name
=
'United Kingdom'
self
.
us_record_data
=
{
'city'
:
'Mountain View'
,
'region_code'
:
'CA'
,
...
...
@@ -51,58 +39,28 @@ class TestGeoIPCityFunctions(unittest.TestCase):
'time_zone'
:
'Europe/London'
}
self
.
us_region_data
=
{
'region_code'
:
'CA'
,
'country_code'
:
'US'
}
self
.
gb_region_data
=
{
'region_code'
:
'N7'
,
'country_code'
:
'GB'
}
self
.
gic
=
pygeoip
.
GeoIP
(
CITY_DB_PATH
)
self
.
gic_mem
=
pygeoip
.
GeoIP
(
CITY_DB_PATH
,
pygeoip
.
MEMORY_CACHE
)
self
.
gic_mmap
=
pygeoip
.
GeoIP
(
CITY_DB_PATH
,
pygeoip
.
MMAP_CACHE
)
def
testCountryCodeByName
(
self
):
us_code
=
self
.
gic
.
country_code_by_name
(
self
.
us_hostname
)
gb_code
=
self
.
gic
.
country_code_by_name
(
self
.
gb_hostname
)
self
.
assertEqual
(
us_code
,
self
.
us_code
)
self
.
assertEqual
(
gb_code
,
self
.
gb_code
)
def
testCountryCodeByAddr
(
self
):
us_code
=
self
.
gic
.
country_code_by_addr
(
self
.
us_ip
)
gb_code
=
self
.
gic
.
country_code_by_addr
(
self
.
gb_ip
)
self
.
assertEqual
(
us_code
,
self
.
us_code
)
self
.
assertEqual
(
gb_code
,
self
.
gb_code
)
def
testCountryNameByName
(
self
):
us_name
=
self
.
gic
.
country_name_by_name
(
self
.
us_hostname
)
gb_name
=
self
.
gic
.
country_name_by_name
(
self
.
gb_hostname
)
self
.
assertEqual
(
us_name
,
self
.
us_name
)
self
.
assertEqual
(
gb_name
,
self
.
gb_name
)
code
=
self
.
gic
.
country_code_by_addr
(
'66.92.181.240'
)
self
.
assertEqual
(
code
,
'US'
)
def
testCountryNameByAddr
(
self
):
us_name
=
self
.
gic
.
country_name_by_addr
(
self
.
us_ip
)
gb_name
=
self
.
gic
.
country_name_by_addr
(
self
.
gb_ip
)
self
.
assertEqual
(
us_name
,
self
.
us_name
)
self
.
assertEqual
(
gb_name
,
self
.
gb_name
)
us_name
=
self
.
gic
.
country_name_by_addr
(
'64.233.161.99'
)
gb_name
=
self
.
gic
.
country_name_by_addr
(
'212.58.253.68'
)
def
testRegionByName
(
self
):
us_region_data
=
self
.
gic
.
region_by_name
(
self
.
us_hostname
)
gb_region_data
=
self
.
gic
.
region_by_name
(
self
.
gb_hostname
)
self
.
assertEqual
(
us_region_data
,
self
.
us_region_data
)
self
.
assertEqual
(
gb_region_data
,
self
.
gb_region_data
)
self
.
assertEqual
(
us_name
,
'United States'
)
self
.
assertEqual
(
gb_name
,
'United Kingdom'
)
def
testRegionByAddr
(
self
):
us_region
=
self
.
gic
.
region_by_addr
(
self
.
us_ip
)
gb_region
=
self
.
gic
.
region_by_addr
(
self
.
gb_ip
)
self
.
assertEqual
(
us_region
,
self
.
us_region_data
)
self
.
assertEqual
(
gb_region
,
self
.
gb_region_data
)
region
=
self
.
gic
.
region_by_addr
(
'66.92.181.240'
)
self
.
assertEqual
(
region
,
{
'region_code'
:
'CA'
,
'country_code'
:
'US'
})
def
testCacheMethods
(
self
):
mem_record
=
self
.
gic_mem
.
record_by_addr
(
self
.
us_ip
)
mmap_record
=
self
.
gic_mmap
.
record_by_addr
(
self
.
us_ip
)
mem_record
=
self
.
gic_mem
.
record_by_addr
(
'64.233.161.99'
)
mmap_record
=
self
.
gic_mmap
.
record_by_addr
(
'64.233.161.99'
)
self
.
assertEqual
(
mem_record
[
'city'
],
self
.
us_record_data
[
'city'
])
self
.
assertEqual
(
mmap_record
[
'city'
],
self
.
us_record_data
[
'city'
])
...
...
@@ -113,31 +71,7 @@ class TestGeoIPCityFunctions(unittest.TestCase):
'country_name'
,
'time_zone'
)
almost_equal_keys
=
(
'longitude'
,
'latitude'
)
us_record
=
self
.
gic
.
record_by_addr
(
self
.
us_ip
)
for
key
,
value
in
us_record
.
items
():
if
key
in
equal_keys
:
test_value
=
self
.
us_record_data
[
key
]
self
.
assertEqual
(
value
,
test_value
,
'Key:
%
s'
%
key
)
elif
key
in
almost_equal_keys
:
test_value
=
self
.
us_record_data
[
key
]
self
.
assertAlmostEqual
(
value
,
test_value
,
3
,
'Key:
%
s'
%
key
)
gb_record
=
self
.
gic
.
record_by_addr
(
self
.
gb_ip
)
for
key
,
value
in
gb_record
.
items
():
if
key
in
equal_keys
:
test_value
=
self
.
gb_record_data
[
key
]
self
.
assertEqual
(
value
,
test_value
,
'Key:
%
s'
%
key
)
elif
key
in
almost_equal_keys
:
test_value
=
self
.
gb_record_data
[
key
]
self
.
assertAlmostEqual
(
value
,
test_value
,
3
,
'Key:
%
s'
%
key
)
def
testRecordByName
(
self
):
equal_keys
=
(
'city'
,
'region_name'
,
'area_code'
,
'country_code3'
,
'postal_code'
,
'dma_code'
,
'country_code'
,
'country_name'
,
'time_zone'
)
almost_equal_keys
=
(
'longitude'
,
'latitude'
)
us_record
=
self
.
gic
.
record_by_name
(
self
.
us_hostname
)
us_record
=
self
.
gic
.
record_by_addr
(
'64.233.161.99'
)
for
key
,
value
in
us_record
.
items
():
if
key
in
equal_keys
:
test_value
=
self
.
us_record_data
[
key
]
...
...
@@ -146,7 +80,7 @@ class TestGeoIPCityFunctions(unittest.TestCase):
test_value
=
self
.
us_record_data
[
key
]
self
.
assertAlmostEqual
(
value
,
test_value
,
3
,
'Key:
%
s'
%
key
)
gb_record
=
self
.
gic
.
record_by_
name
(
self
.
gb_hostname
)
gb_record
=
self
.
gic
.
record_by_
addr
(
'212.58.253.68'
)
for
key
,
value
in
gb_record
.
items
():
if
key
in
equal_keys
:
test_value
=
self
.
gb_record_data
[
key
]
...
...
tests/test_country.py
View file @
c4688541
# -*- coding: utf-8 -*-
import
socket
import
unittest
from
nose.tools
import
raises
...
...
@@ -9,86 +8,49 @@ from tests.config import COUNTRY_DB_PATH, COUNTRY_V6_DB_PATH
class
TestGeoIPCountryFunctions
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
us_hostname
=
'google.com'
self
.
gb_hostname
=
'bbc.com'
self
.
us_ip
=
'64.233.161.99'
self
.
gb_ip
=
'212.58.253.68'
self
.
ie6_hostname
=
'ipv6.loopia.se'
self
.
ie6_ip
=
'2a00:1450:400f:800::1002'
self
.
ie_code
=
'IE'
self
.
us_code
=
'US'
self
.
gb_code
=
'GB'
self
.
se_code
=
'SE'
self
.
ie_name
=
'Ireland'
self
.
us_name
=
'United States'
self
.
gb_name
=
'United Kingdom'
self
.
se_name
=
'Sweden'
self
.
gi
=
pygeoip
.
GeoIP
(
COUNTRY_DB_PATH
)
self
.
gi6
=
pygeoip
.
GeoIP
(
COUNTRY_V6_DB_PATH
)
def
testCountryCodeByName
(
self
):
us_code
=
self
.
gi
.
country_code_by_name
(
self
.
us_hostname
)
gb_code
=
self
.
gi
.
country_code_by_name
(
self
.
gb_hostname
)
ie6_code
=
self
.
gi6
.
country_code_by_name
(
self
.
ie6_hostname
)
self
.
assertEqual
(
us_code
,
self
.
us_code
)
self
.
assertEqual
(
gb_code
,
self
.
gb_code
)
self
.
assertEqual
(
ie6_code
,
self
.
se_code
)
def
testCountryCodeByAddr
(
self
):
us_code
=
self
.
gi
.
country_code_by_addr
(
self
.
us_ip
)
gb_code
=
self
.
gi
.
country_code_by_addr
(
self
.
gb_ip
)
ie6_code
=
self
.
gi6
.
country_code_by_addr
(
self
.
ie6_ip
)
self
.
assertEqual
(
us_code
,
self
.
us_code
)
self
.
assertEqual
(
gb_code
,
self
.
gb_code
)
self
.
assertEqual
(
ie6_code
,
self
.
ie_code
)
def
testCountryNameByName
(
self
):
us_name
=
self
.
gi
.
country_name_by_name
(
self
.
us_hostname
)
gb_name
=
self
.
gi
.
country_name_by_name
(
self
.
gb_hostname
)
ie6_name
=
self
.
gi6
.
country_name_by_name
(
self
.
ie6_hostname
)
us_code
=
self
.
gi
.
country_code_by_addr
(
'64.17.254.216'
)
it_code
=
self
.
gi
.
country_code_by_addr
(
'78.26.70.208'
)
jp6_code
=
self
.
gi6
.
country_code_by_addr
(
'2001:200::'
)
self
.
assertEqual
(
us_
name
,
self
.
us_name
)
self
.
assertEqual
(
gb_name
,
self
.
gb_name
)
self
.
assertEqual
(
ie6_name
,
self
.
se_name
)
self
.
assertEqual
(
us_
code
,
'US'
)
self
.
assertEqual
(
it_code
,
'IT'
)
self
.
assertEqual
(
jp6_code
,
'JP'
)
def
testCountryNameByAddr
(
self
):
us_name
=
self
.
gi
.
country_name_by_addr
(
self
.
us_ip
)
gb_name
=
self
.
gi
.
country_name_by_addr
(
self
.
gb_ip
)
ie6_name
=
self
.
gi6
.
country_name_by_addr
(
self
.
ie6_ip
)
us_name
=
self
.
gi
.
country_name_by_addr
(
'64.17.254.216'
)
it_name
=
self
.
gi
.
country_name_by_addr
(
'78.26.70.208'
)
jp6_name
=
self
.
gi6
.
country_name_by_addr
(
'2001:200::'
)
self
.
assertEqual
(
us_name
,
self
.
us_name
)
self
.
assertEqual
(
gb_name
,
self
.
gb_name
)
self
.
assertEqual
(
ie6_name
,
self
.
ie_name
)
self
.
assertEqual
(
us_name
,
'United States'
)
self
.
assertEqual
(
it_name
,
'Italy'
)
self
.
assertEqual
(
jp6_name
,
'Japan'
)
@raises
(
pygeoip
.
GeoIPError
)
def
testOpen4With6
(
self
):
data
=
self
.
gi
.
country_code_by_addr
(
self
.
ie6_ip
)
data
=
self
.
gi
.
country_code_by_addr
(
'2001:200::'
)
raise
ValueError
(
data
)
@raises
(
pygeoip
.
GeoIPError
)
def
testOpen6With4
(
self
):
data
=
self
.
gi6
.
country_code_by_addr
(
self
.
gb_ip
)
data
=
self
.
gi6
.
country_code_by_addr
(
'78.26.70.208'
)
raise
ValueError
(
data
)
@raises
(
pygeoip
.
GeoIPError
)
def
testOrgByAddr
(
self
):
self
.
gi
.
org_by_addr
(
self
.
us_ip
)
self
.
gi
.
org_by_addr
(
'78.26.70.208'
)
@raises
(
pygeoip
.
GeoIPError
)
def
testRecordByAddr
(
self
):
self
.
gi
.
record_by_addr
(
self
.
us_ip
)
self
.
gi
.
record_by_addr
(
'78.26.70.208'
)
@raises
(
pygeoip
.
GeoIPError
)
def
testRegionByAddr
(
self
):
self
.
gi
.
region_by_addr
(
self
.
us_ip
)
self
.
gi
.
region_by_addr
(
'78.26.70.208'
)
@raises
(
pygeoip
.
GeoIPError
)
def
testTimeZoneByAddr
(
self
):
self
.
gi
.
time_zone_by_addr
(
self
.
us_ip
)
self
.
gi
.
time_zone_by_addr
(
'78.26.70.208'
)
tests/test_isp.py
View file @
c4688541
...
...
@@ -7,26 +7,8 @@ from tests.config import ISP_DB_PATH
class
TestGeOIPISPFunctions
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
us_isp
=
'Turner Broadcasting System'
self
.
us_ip
=
'157.166.255.18'
self
.
us_hostname
=
'cnn.com'
self
.
se_isp
=
'IP-Only Tele Communication AB'
self
.
se_ip
=
'213.132.112.97'
self
.
se_hostname
=
'blocket.se'
self
.
gi
=
pygeoip
.
GeoIP
(
ISP_DB_PATH
)
def
testISPByAddr
(
self
):
se_isp
=
self
.
gi
.
isp_by_addr
(
self
.
se_ip
)
us_isp
=
self
.
gi
.
isp_by_addr
(
self
.
us_ip
)
self
.
assertEqual
(
se_isp
,
self
.
se_isp
)
self
.
assertEqual
(
us_isp
,
self
.
us_isp
)
def
testISPByName
(
self
):
se_isp
=
self
.
gi
.
isp_by_name
(
self
.
se_hostname
)
us_isp
=
self
.
gi
.
isp_by_name
(
self
.
us_hostname
)
self
.
assertEqual
(
se_isp
,
self
.
se_isp
)
self
.
assertEqual
(
us_isp
,
self
.
us_isp
)
isp
=
self
.
gi
.
isp_by_addr
(
'70.46.123.145'
)
self
.
assertEqual
(
isp
,
'FDN Communications'
)
tests/test_memory.py
View file @
c4688541
...
...
@@ -11,5 +11,5 @@ class TestMemoryCache(unittest.TestCase):
gi
=
pygeoip
.
GeoIP
(
COUNTRY_DB_PATH
,
flags
=
const
.
MEMORY_CACHE
,
cache
=
False
)
self
.
assertEqual
(
gi
.
_type
,
'MEMORY_CACHE'
)
code
=
gi
.
country_code_by_
name
(
'dn.se
'
)
self
.
assertEqual
(
code
,
'
SE
'
)
code
=
gi
.
country_code_by_
addr
(
'64.17.254.216
'
)
self
.
assertEqual
(
code
,
'
US
'
)
tests/test_netspeed.py
View file @
c4688541
...
...
@@ -2,23 +2,17 @@
import
unittest
import
pygeoip
from
tests.config
import
NETSPEED_DB_PATH
from
tests.config
import
NETSPEED_DB_PATH
,
NETSPEEDCELL_DB_PATH
class
TestGeoIP
Org
Functions
(
unittest
.
TestCase
):
class
TestGeoIP
Netspeed
Functions
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
ip
=
'17.172.224.47'
self
.
hostname
=
'apple.com'
self
.
gi
=
pygeoip
.
GeoIP
(
NETSPEED_DB_PATH
)
def
testNetSpeedByAddr
(
self
):
netspeed
=
self
.
gi
.
id_by_addr
(
self
.
ip
)
netspeed
=
self
.
gi
.
id_by_addr
(
'17.172.224.47'
)
self
.
assertEqual
(
netspeed
,
3
)
def
testNetSpeedByAddrWrapper
(
self
):
netspeed
=
self
.
gi
.
netspeed_by_addr
(
self
.
ip
)
self
.
assertEqual
(
netspeed
,
'Corporate'
)
def
testNetSpeedByNameWrapper
(
self
):
netspeed
=
self
.
gi
.
netspeed_by_name
(
self
.
hostname
)
netspeed
=
self
.
gi
.
netspeed_by_addr
(
'17.172.224.47'
)
self
.
assertEqual
(
netspeed
,
'Corporate'
)
tests/test_org.py
View file @
c4688541
...
...
@@ -7,26 +7,8 @@ from tests.config import ORG_DB_PATH
class
TestGeoIPOrgFunctions
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
us_org
=
'APPLE COMPUTER'
self
.
us_ip
=
'17.172.224.47'
self
.
us_hostname
=
'apple.com'
self
.
gb_org
=
'BBC'
self
.
gb_ip
=
'212.58.253.68'
self
.
gb_hostname
=
'bbc.com'
self
.
gio
=
pygeoip
.
GeoIP
(
ORG_DB_PATH
)
self
.
gi
=
pygeoip
.
GeoIP
(
ORG_DB_PATH
)
def
testOrgByAddr
(
self
):
gb_org
=
self
.
gio
.
org_by_addr
(
self
.
gb_ip
)
us_org
=
self
.
gio
.
org_by_addr
(
self
.
us_ip
)
self
.
assertEqual
(
gb_org
,
self
.
gb_org
)
self
.
assertEqual
(
us_org
,
self
.
us_org
)
def
testOrgByName
(
self
):
gb_org
=
self
.
gio
.
org_by_name
(
self
.
gb_hostname
)
us_org
=
self
.
gio
.
org_by_name
(
self
.
us_hostname
)
self
.
assertEqual
(
gb_org
,
self
.
gb_org
)
self
.
assertEqual
(
us_org
,
self
.
us_org
)
org
=
self
.
gi
.
org_by_addr
(
'70.46.123.145'
)
self
.
assertEqual
(
org
,
'DSLAM WAN Allocation'
)
tests/test_region.py
View file @
c4688541
...
...
@@ -7,48 +7,15 @@ from tests.config import REGION_DB_PATH
class
TestGeoIPRegionFunctions
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
se_hostname
=
'dn.se'
self
.
se_region_data
=
{
'region_code'
:
None
,
'country_code'
:
None
}
self
.
gi
=
pygeoip
.
GeoIP
(
REGION_DB_PATH
)
self
.
ca_hostname
=
'cbc.ca'
self
.
ca_region_data
=
{
'region_code'
:
'ON'
,
'country_code'
:
'CA'
}
self
.
us_code
=
'US'
self
.
us_hostname
=
'apple.com'
self
.
us_ip
=
'17.172.224.47'
self
.
us_region_data
=
{
def
testRegionByAddr
(
self
):
region_name
=
self
.
gi
.
region_by_addr
(
'17.172.224.47'
)
self
.
assertEqual
(
region_name
,
{
'region_code'
:
'CA'
,
'country_code'
:
'US'
}
self
.
gir
=
pygeoip
.
GeoIP
(
REGION_DB_PATH
)
def
testRegionByName
(
self
):
region_name
=
self
.
gir
.
region_by_name
(
self
.
us_hostname
)
self
.
assertEqual
(
region_name
,
self
.
us_region_data
)
def
testRegionByAddr
(
self
):
region_name
=
self
.
gir
.
region_by_addr
(
self
.
us_ip
)
self
.
assertEqual
(
region_name
,
self
.
us_region_data
)
def
testCountryCodeByName
(
self
):
us_code
=
self
.
gir
.
country_code_by_name
(
self
.
us_hostname
)
self
.
assertEqual
(
us_code
,
self
.
us_code
)
})
def
testCountryCodeByAddr
(
self
):
us_code
=
self
.
gir
.
country_code_by_addr
(
self
.
us_ip
)
self
.
assertEqual
(
us_code
,
self
.
us_code
)
def
testCanadaRegion
(
self
):
region_name
=
self
.
gir
.
region_by_name
(
self
.
ca_hostname
)
self
.
assertEqual
(
region_name
,
self
.
ca_region_data
)
def
testSwedenRegion
(
self
):
region_name
=
self
.
gir
.
region_by_name
(
self
.
se_hostname
)
self
.
assertEqual
(
region_name
,
self
.
se_region_data
)
us_code
=
self
.
gi
.
country_code_by_addr
(
'17.172.224.47'
)
self
.
assertEqual
(
us_code
,
'US'
)
tests/test_thread.py
View file @
c4688541
# -*- coding: utf-8 -*-
import
sys
import
unittest
import
threading
...
...
@@ -8,32 +9,36 @@ from tests.config import COUNTRY_DB_PATH
class
TestGeoIPThreading
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
us_ip
=
'64.233.161.99'
self
.
gb_ip
=
'212.58.253.68'
self
.
us_code
=
'US'
self
.
gb_code
=
'GB'
self
.
gi
=
pygeoip
.
GeoIP
(
COUNTRY_DB_PATH
)
def
testCountryDatabase
(
self
):
us_thread
=
TestThread
(
'us'
,
self
.
gi
,
self
.
us_ip
,
self
.
us_code
,
self
.
assertEqual
)
gb_thread
=
TestThread
(
'gb'
,
self
.
gi
,
self
.
us_ip
,
self
.
us_code
,
self
.
assertEqual
)
us_thread
.
start
()
gb_thread
.
start
()
us_thread
.
join
()
gb_thread
.
join
()
t1
=
TestThread
(
'us'
,
'64.17.254.216'
,
'US'
,
self
)
t2
=
TestThread
(
'it'
,
'78.26.70.208'
,
'IT'
,
self
)
t1
.
start
()
t2
.
start
()
t1
.
join
()
t2
.
join
()
if
t1
.
exc_info
:
raise
t1
.
exc_info
[
1
]
if
t2
.
exc_info
:
raise
t2
.
exc_info
[
1
]
class
TestThread
(
threading
.
Thread
):
def
__init__
(
self
,
name
,
gi
,
ip
,
code
,
assertEqual
):
def
__init__
(
self
,
name
,
ip
,
code
,
test
):
threading
.
Thread
.
__init__
(
self
,
name
=
name
)
self
.
ip
=
ip
self
.
gi
=
gi
self
.
code
=
code
self
.
assertEqual
=
assertEqual
self
.
test
=
test
self
.
exc_info
=
None
def
run
(
self
):
for
i
in
range
(
1000
):
code
=
self
.
gi
.
country_code_by_addr
(
self
.
ip
)
self
.
assertEqual
(
code
,
self
.
code
)
try
:
for
i
in
range
(
1000
):
code
=
self
.
test
.
gi
.
country_code_by_addr
(
self
.
ip
)
self
.
test
.
assertEqual
(
code
,
self
.
code
)
except
AssertionError
:
self
.
exc_info
=
sys
.
exc_info
()
tests/test_tz.py
View file @
c4688541
...
...
@@ -8,13 +8,7 @@ from tests.config import CITY_DB_PATH
class
TestGeoIPTimeZoneFunctions
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
us_hostname
=
'google.com'
self
.
us_ip
=
'64.233.161.99'
self
.
gb_hostname
=
'bbc.com'
self
.
gb_ip
=
'212.58.253.68'
self
.
gic
=
pygeoip
.
GeoIP
(
CITY_DB_PATH
)
self
.
gi
=
pygeoip
.
GeoIP
(
CITY_DB_PATH
)
def
testTimeZoneInternals
(
self
):
tz
=
pygeoip
.
timezone
.
time_zone_by_country_and_region
(
'XX'
)
...
...
@@ -27,15 +21,5 @@ class TestGeoIPTimeZoneFunctions(unittest.TestCase):
self
.
assertEquals
(
tz
,
None
)
def
testTimeZoneByAddr
(
self
):
us_time_zone
=
self
.
gic
.
time_zone_by_addr
(
self
.
us_ip
)
gb_time_zone
=
self
.
gic
.
time_zone_by_addr
(
self
.
gb_ip
)
self
.
assertEquals
(
us_time_zone
,
'America/Los_Angeles'
)
self
.
assertEquals
(
gb_time_zone
,
'Europe/London'
)
def
testTimeZoneByName
(
self
):
us_time_zone
=
self
.
gic
.
time_zone_by_name
(
self
.
us_hostname
)
gb_time_zone
=
self
.
gic
.
time_zone_by_name
(
self
.
gb_hostname
)
self
.
assertEquals
(
us_time_zone
,
'America/Los_Angeles'
)
self
.
assertEquals
(
gb_time_zone
,
'Europe/London'
)
tz
=
self
.
gi
.
time_zone_by_addr
(
'64.17.254.216'
)
self
.
assertEquals
(
tz
,
'America/Los_Angeles'
)
tests/test_unicode.py
View file @
c4688541
...
...
@@ -2,7 +2,7 @@
import
unittest
import
pygeoip
from
pygeoip.const
import
PY2
,
PY3
from
pygeoip.const
import
PY2
from
tests.config
import
CITY_DB_PATH
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment