forked from abclogin/dadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geoip_test.go
80 lines (74 loc) · 1.87 KB
/
geoip_test.go
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
package dadata_test
import (
"reflect"
"testing"
. "gopkg.in/webdeskltd/dadata.v2"
)
func TestDaData_DetectAddressByIP(t *testing.T) {
daData := newGeoIPDetector()
tests := []struct {
name string
ip string
wantCity string
wantErr bool
}{
{"wrong ip", "xxx.999.ss.23", "", true},
{"google proxy", "8.8.8.8", "", true},
{"some msk ip", "85.235.162.70", "Москва", false},
{"some belgorod ip", "217.118.95.255", "Орёл", false},
{"some vladivostok ip", "80.83.239.112", "Владивосток", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := daData.GeoIP(tt.ip)
if (err != nil) != tt.wantErr {
t.Errorf("DaData.GeoIP() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.wantErr {
// after wantErr it's make no sense check got - want
return
}
if got == nil {
return
}
if !reflect.DeepEqual(got.Location.Data.City, tt.wantCity) {
t.Errorf("DaData.GeoIP() = %v, want %v", got.Location.Data.City, tt.wantCity)
}
})
}
}
func TestDaData_GeolocateAddresses(t *testing.T) {
tt := []struct {
name string
query GeolocateRequest
wantStreet string
wantErr bool
}{
{
"ГБОУ Школа №2120",
GeolocateRequest{Lat: 55.6010979, Lon: 37.3593894},
"77000006000003600",
false,
},
{
"МФЮА",
GeolocateRequest{Lat: 55.722853, Lon: 37.692247},
"77000000000001300",
false,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
daData := instance()
got, err := daData.GeolocateAddress(tc.query)
if (err != nil) != tc.wantErr {
t.Errorf("DaData.GeolocateAddress() error = %v, wantErr %v", err, tc.wantErr)
return
}
kladr := got[0].Data.StreetKladrID
if !reflect.DeepEqual(kladr, tc.wantStreet) {
t.Errorf("DaData.GeolocateAddress() = %#v, want %#v", kladr, tc.wantStreet)
}
})
}
}