-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_test.go
188 lines (177 loc) · 3.85 KB
/
data_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
186
187
188
package localtimezone
import (
"encoding/csv"
"os"
"strconv"
"testing"
)
// city and lat/lon data is from Pareto Software LLC, SimpleMaps.com
// https://simplemaps.com/data/world-cities
type TimezoneTestCase struct {
City string
Lat float64
Lon float64
ExpectedZone string
}
func generateTestCases() ([]TimezoneTestCase, error) {
f, err := os.Open("test/testdata.csv")
if err != nil {
return nil, err
}
reader := csv.NewReader(f)
rawData, err := reader.ReadAll()
if err != nil {
return nil, err
}
err = f.Close()
if err != nil {
return nil, err
}
var data []TimezoneTestCase
for _, line := range rawData {
lat, err := strconv.ParseFloat(line[1], 64)
if err != nil {
return nil, err
}
lon, err := strconv.ParseFloat(line[2], 64)
if err != nil {
return nil, err
}
tc := TimezoneTestCase{
City: line[0],
Lat: lat,
Lon: lon,
ExpectedZone: line[3],
}
data = append(data, tc)
}
return data, nil
}
func TestData(t *testing.T) {
t.Parallel()
z, err := NewLocalTimeZone()
if err != nil {
t.Errorf("cannot initialize timezone client: %v", err)
}
data, err := generateTestCases()
if err != nil {
t.Errorf("cannot get test data: %v", err)
}
for _, tc := range data {
tc := tc // Remove race condition over test fields
t.Run(tc.City, func(t *testing.T) {
t.Parallel()
point := Point{
Lon: tc.Lon,
Lat: tc.Lat,
}
tzids, err := z.GetZone(point)
if err != nil {
t.Errorf("unexpected err %v", err)
}
if len(tzids) < 1 {
t.Error("cannot find a timezone")
}
if tc.ExpectedZone != tzids[0] {
t.Errorf("expected zone %s; got %s", tc.ExpectedZone, tzids[0])
}
tzid, err := z.GetOneZone(point)
if err != nil {
t.Errorf("unexpected err %v", err)
}
if tc.ExpectedZone != tzid {
t.Errorf("expected zone %s; got %s", tc.ExpectedZone, tzid)
}
})
}
}
func TestTzidPresent(t *testing.T) {
client, err := NewLocalTimeZone()
if err != nil {
t.Errorf("cannot initialize timezone client: %v", err)
}
z, ok := client.(*localTimeZone)
if !ok {
t.Error("error when initializing client")
}
z.mu.RLock()
defer z.mu.RUnlock()
_, ok = z.tzData["id"]
if ok {
t.Error("unexpected feature with empty tzid")
}
}
func TestPolygons(t *testing.T) {
client, err := NewLocalTimeZone()
if err != nil {
t.Errorf("cannot initialize timezone client: %v", err)
}
z, ok := client.(*localTimeZone)
if !ok {
t.Error("error when initializing client")
}
z.mu.RLock()
defer z.mu.RUnlock()
for tzid, d := range z.tzData {
if d.polygon != nil {
for _, ring := range *d.polygon {
if len(ring) < 4 {
t.Errorf("expected polygon %s ring to have >= 4 coordinates, instead has %d", tzid, len(ring))
}
}
}
}
}
func BenchmarkGetZone(b *testing.B) {
client, err := NewLocalTimeZone()
if err != nil {
b.Errorf("cannot initialize timezone client: %v", err)
}
data, err := generateTestCases()
if err != nil {
b.Errorf("cannot initialize test cases: %v", err)
}
// Ensure client has finished loading data
_, err = client.GetZone(Point{0, 0})
if err != nil {
b.Errorf("cannot initialize timezone client: %v", err)
}
b.Run("GetZone on large cities", func(b *testing.B) {
Loop:
for n := 0; n < b.N; {
for _, tc := range data {
if n > b.N {
break Loop
}
point := Point{
Lon: tc.Lon,
Lat: tc.Lat,
}
_, err = client.GetZone(point)
if err != nil {
b.Errorf("point %v did not return a zone", point)
}
n++
}
}
})
b.Run("GetOneZone on large cities", func(b *testing.B) {
Loop:
for n := 0; n < b.N; {
for _, tc := range data {
if n > b.N {
break Loop
}
point := Point{
Lon: tc.Lon,
Lat: tc.Lat,
}
_, err = client.GetOneZone(point)
if err != nil {
b.Errorf("point %v did not return a zone", point)
}
n++
}
}
})
}