-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoLocate_test.go
185 lines (172 loc) · 4.26 KB
/
geoLocate_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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package address_parse_china
import (
"github.com/ChrisMuir/address-parse-china/city"
"github.com/ChrisMuir/address-parse-china/county"
"github.com/ChrisMuir/address-parse-china/models"
"github.com/ChrisMuir/address-parse-china/province"
"reflect"
"testing"
)
func TestGeoLocate(t *testing.T) {
tests := []struct {
input []string
expected []models.GeoLocation
}{
{
input: []string{"大连市甘井子区南关岭街道姚工街101号"},
expected: []models.GeoLocation{
{
Address: "大连市甘井子区南关岭街道姚工街101号",
Province: "辽宁",
ProvinceCode: 21,
City: "大连",
CityCode: 2102,
County: "甘井子",
CountyCode: 210211,
},
},
},
{
input: []string{"大连市甘井子区南关岭街道姚工街101号", "浙江省杭州市余杭区径山镇小古城村"},
expected: []models.GeoLocation{
{
Address: "大连市甘井子区南关岭街道姚工街101号",
Province: "辽宁",
ProvinceCode: 21,
City: "大连",
CityCode: 2102,
County: "甘井子",
CountyCode: 210211,
},
{
Address: "浙江省杭州市余杭区径山镇小古城村",
Province: "浙江",
ProvinceCode: 33,
City: "杭州",
CityCode: 3301,
County: "余杭",
CountyCode: 330110,
},
},
},
{
input: []string{"我喜欢猫"},
expected: []models.GeoLocation{
{
Address: "我喜欢猫",
Province: "",
ProvinceCode: 0,
City: "",
CityCode: 0,
County: "",
CountyCode: 0,
},
},
},
{
input: []string{"I like cats"},
expected: []models.GeoLocation{
{
Address: "I like cats",
Province: "",
ProvinceCode: 0,
City: "",
CityCode: 0,
County: "",
CountyCode: 0,
},
},
},
}
for _, tt := range tests {
resp := GeoLocate(tt.input)
if !reflect.DeepEqual(resp, tt.expected) {
t.Errorf("from GeoLocate(), expected %v, instead got %v, on input %v", tt.expected, resp, tt.input)
}
}
}
func BenchmarkGeoLocate(b *testing.B) {
locs := []string{
"大连市甘井子区南关岭街道姚工街101号",
}
for i := 0; i < b.N; i++ {
GeoLocate(locs)
}
}
func TestGetProvinceData(t *testing.T) {
resp := GetProvinceData()
// Check len
expectedLen := 31
if len(resp) != expectedLen {
t.Errorf("Province data expected len %v, instead got %v", expectedLen, len(resp))
}
// Check one expected value
expectedVal := province.Province{
"上海",
31,
}
seen := false
for _, currProv := range resp {
if currProv.ProvinceName == expectedVal.ProvinceName && currProv.ProvinceCode == expectedVal.ProvinceCode {
seen = true
break
}
}
if !seen {
t.Errorf("Province data expected to see value %v, but value not found in pkg data", expectedVal)
}
}
func TestGetCityData(t *testing.T) {
resp := GetCityData()
// Check len
expectedLen := 341
if len(resp) != expectedLen {
t.Errorf("City data expected len %v, instead got %v", expectedLen, len(resp))
}
// Check one expected value
expectedVal := city.City{
"晋城",
1405,
14,
}
seen := false
for _, currCity := range resp {
if currCity.CityName == expectedVal.CityName &&
currCity.CityCode == expectedVal.CityCode &&
currCity.ProvinceCode == expectedVal.ProvinceCode {
seen = true
break
}
}
if !seen {
t.Errorf("City data expected to see value %v, but value not found in pkg data", expectedVal)
}
}
func TestGetCountyDataData(t *testing.T) {
resp := GetCountyData()
// Check len
expectedLen := 2978
if len(resp) != expectedLen {
t.Errorf("County data expected len %v, instead got %v", expectedLen, len(resp))
}
// Check one expected value
expectedVal := county.County{
"河北唐山海港经济开发",
130274,
1302,
13,
}
seen := false
for _, currCounty := range resp {
if currCounty.CountyName == expectedVal.CountyName &&
currCounty.CountyCode == expectedVal.CountyCode &&
currCounty.CityCode == expectedVal.CityCode &&
currCounty.ProvinceCode == expectedVal.ProvinceCode {
seen = true
break
}
}
if !seen {
t.Errorf("County data expected to see value %v, but value not found in pkg data", expectedVal)
}
}