Skip to content

Commit df442be

Browse files
committed
Added test case.
1 parent 5841dcd commit df442be

7 files changed

+80
-7
lines changed

IP2Location.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import socket
44
import re
55
import json
6+
import os
67

78
if sys.version < '3':
89
import urllib, httplib
@@ -127,6 +128,10 @@ def __init__(self, filename=None,mode='FILE_IO'):
127128
128129
'''
129130
self.mode = mode
131+
132+
if os.path.isfile(filename) == False:
133+
raise ValueError("The database file does not seem to exist.")
134+
130135
if filename:
131136
self.open(filename)
132137

@@ -272,8 +277,8 @@ def find(self, addr):
272277
addr: IPv4 or IPv6 address as a string
273278
274279
Returns IP2LocationRecord or None if address not found in file
275-
276-
This function will be obselete in future.
280+
281+
This function will be obselete in future.
277282
'''
278283
return self._get_record(addr)
279284

data/IP2LOCATION-LITE-DB1.BIN

1.92 MB
Binary file not shown.

data/IP2LOCATION-LITE-DB1.IPV6.BIN

5.01 MB
Binary file not shown.

sample.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import os
88
import IP2Location
99

10-
'''
10+
"""
1111
Cache the database into memory to accelerate lookup speed.
1212
WARNING: Please make sure your system have sufficient RAM to use this feature.
13-
'''
13+
"""
1414
# database = IP2Location.IP2Location(os.path.join("data", "IPV6-COUNTRY.BIN"), "SHARED_MEMORY")
1515

1616
# Default file I/O lookup
@@ -42,8 +42,8 @@
4242

4343
# Web Service
4444
ws = IP2Location.IP2LocationWebService("demo","WS24",True)
45-
rec = ws.lookup('8.8.8.8', ['continent', 'country', 'region', 'city', 'geotargeting', 'country_groupings', 'time_zone_info'], 'en')
45+
rec = ws.lookup("8.8.8.8", ["continent", "country", "region", "city", "geotargeting", "country_groupings", "time_zone_info"], "en")
4646
print (rec)
47-
print ('\n')
48-
print ('Credit Remaining: {}\n'.format(ws.getcredit()))
47+
print ("\n")
48+
print ("Credit Remaining: {}\n".format(ws.getcredit()))
4949

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
py_modules=['IP2Location'],
1515
url="https://github.com/ip2location/ip2location-python",
1616
packages=setuptools.find_packages(),
17+
tests_require=[pytest>=4.6.11],
1718
classifiers=[
1819
"Development Status :: 5 - Production/Stable",
1920
"Intended Audience :: Developers",

tests/test_database.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import pytest
4+
5+
import os, IP2Location
6+
7+
ipv4database = os.path.join("data", "IP2LOCATION-LITE-DB1.BIN")
8+
9+
ipv6database = os.path.join("data", "IP2LOCATION-LITE-DB1.IPV6.BIN")
10+
11+
def testinvaliddatabase():
12+
try:
13+
database = IP2Location.IP2Location(os.path.join("data", "NULL.BIN"))
14+
except ValueError as e:
15+
assert "The database file does not seem to exist." == str(e)
16+
17+
def testipv4countrycode():
18+
database = IP2Location.IP2Location(ipv4database)
19+
rec = database.get_all("8.8.8.8")
20+
assert rec.country_short == "US", "Test failed because country code not same."
21+
22+
def testipv4countryname():
23+
database = IP2Location.IP2Location(ipv4database)
24+
rec = database.get_all("8.8.8.8")
25+
assert rec.country_long == "United States of America", "Test failed because country name not same."
26+
27+
def testipv4unsupportedfield():
28+
database = IP2Location.IP2Location(ipv4database)
29+
rec = database.get_all("8.8.8.8")
30+
assert rec.city == None, "Test failed because city name not same."
31+
32+
def testipv6countrycode():
33+
database = IP2Location.IP2Location(ipv6database)
34+
rec = database.get_all("2001:4860:4860::8888")
35+
assert rec.country_short == "US", "Test failed because country code not same."
36+
37+
def testipv6countryname():
38+
database = IP2Location.IP2Location(ipv6database)
39+
rec = database.get_all("2001:4860:4860::8888")
40+
assert rec.country_long == "United States of America", "Test failed because country name not same."
41+
42+
def testipv6unsupportedfield():
43+
database = IP2Location.IP2Location(ipv6database)
44+
rec = database.get_all("2001:4860:4860::8888")
45+
assert rec.city == None, "Test failed because city name not same."

tests/test_webservice.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import pytest
4+
5+
import IP2Location
6+
7+
apikey = "demo"
8+
package = "WS24"
9+
usessl = True
10+
addons = ["continent", "country", "region", "city", "geotargeting", "country_groupings", "time_zone_info"]
11+
language = "en"
12+
13+
ws = IP2Location.IP2LocationWebService(apikey,package,usessl)
14+
15+
def testcountrycode():
16+
# ws = IP2Location.IP2LocationWebService(apikey,package,usessl)
17+
rec = ws.lookup("8.8.8.8", addons, language)
18+
assert rec['country_code'] == "US", "Test failed because country code not same."
19+
20+
def testgetcredit():
21+
credit = ws.getcredit()
22+
assert str(credit).isdigit() == True, "Test failed because it is no a digit value."

0 commit comments

Comments
 (0)