-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_module.py
117 lines (85 loc) · 3.71 KB
/
test_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import datetime
from os import path
import pytest
from pysyge import pysyge
DIR_CURRENT = path.dirname(__file__)
DATABASE_CITY_FILE = path.join(DIR_CURRENT, 'SxGeoCity.dat') # 2020.08.17
BASE_IP = '77.88.55.80' # Yandex
def test_quirks():
geodata = pysyge.GeoLocator(DATABASE_CITY_FILE, pysyge.MODE_MEMORY)
result = geodata.get_location('49.206.213.75', detailed=True)
assert result['city'] == 'Хайдарабад'
assert result['country_iso'] == 'IN'
assert result['tz'] == ''
assert result['info']['country']['name_en'] == 'India'
result = geodata.get_location('1.0.74.0')
assert result['city'] == 'Хацукаити'
result = geodata.get_location('24.231.175.176')
assert result['city'] == 'Spruce'
result = geodata.get_location('109.207.230.244')
assert result['city'] == 'Калининград'
geodata = pysyge.GeoLocator(DATABASE_CITY_FILE, pysyge.MODE_BATCH)
result = geodata.get_location('109.252.119.71', detailed=True)
assert result['info']['city']['name_en'] == 'Moscow'
class TestGeoLocatorBasicCheck:
def test_file_not_found(self):
with pytest.raises(IOError):
pysyge.GeoLocator('nosuchfile.dat')
def test_wrong_file(self):
with pytest.raises(pysyge.GeoLocatorException):
pysyge.GeoLocator(path.join(DIR_CURRENT, 'test_module.py'))
def test_db_version(self):
geodata = pysyge.GeoLocator(DATABASE_CITY_FILE)
assert geodata.get_db_version() >= 21
def test_db_date(self):
geodata = pysyge.GeoLocator(DATABASE_CITY_FILE)
assert isinstance(geodata.get_db_date(), datetime.datetime)
def test_invalid_ips(self):
geodata = pysyge.GeoLocator(DATABASE_CITY_FILE)
assert geodata.get_location('127.0.0.1') == {}
assert geodata.get_location('80.8qeqw') == {}
def assert_location(location, detailed=False):
assert 'city' in location
assert 'country_id' in location
assert 'country_iso' in location
assert 'lon' in location
assert 'lat' in location
assert 'fips' in location
assert 'region_id' in location
assert location['city'] == 'Москва'
assert location['country_id'] == 185
assert location['country_iso'] == 'RU'
assert location['lon'] == 37.61556
assert location['lat'] == 55.75222
assert location['fips'] == '0'
if detailed:
assert 'region' in location
assert 'tz' in location
assert location['region_id'] == 524894
assert location['region'] == 'Москва'
else:
assert 'region' not in location
assert 'tz' not in location
assert location['region_id'] == 0
class TestGeoLocatorFileModeCheck:
def test_location_basic(self):
geodata = pysyge.GeoLocator(DATABASE_CITY_FILE)
location = geodata.get_location(BASE_IP)
assert_location(location)
def test_location_detailed(self):
geodata = pysyge.GeoLocator(DATABASE_CITY_FILE)
location = geodata.get_location(BASE_IP, detailed=True)
assert_location(location, detailed=True)
locations = geodata.get_locations([BASE_IP, '100.20.30.40'])
assert locations[0]['country_iso'] == 'RU'
assert locations[1]['country_iso'] == 'US'
@pytest.mark.parametrize('mode', [pysyge.MODE_MEMORY, pysyge.MODE_BATCH])
def test_location_basic(mode):
geodata = pysyge.GeoLocator(DATABASE_CITY_FILE, mode)
location = geodata.get_location(BASE_IP)
assert_location(location)
@pytest.mark.parametrize('mode', [pysyge.MODE_MEMORY, pysyge.MODE_BATCH])
def test_location_detailed(mode):
geodata = pysyge.GeoLocator(DATABASE_CITY_FILE, mode)
location = geodata.get_location(BASE_IP, detailed=True)
assert_location(location, detailed=True)