Repository navigation
Expand file tree
/
Copy pathdata_test.go
More file actions
201 lines (186 loc) · 4.46 KB
/
Copy pathdata_test.go
File metadata and controls
201 lines (186 loc) · 4.46 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
package localtimezone
import (
"encoding/csv"
"os"
"strconv"
"testing"
"time"
"github.com/uber/h3-go/v4"
)
// 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 := NewLocalTimeZone()
data, err := generateTestCases()
if err != nil {
t.Errorf("cannot get test data: %v", err)
}
for _, tc := range data {
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 TestTzNamesPresent(t *testing.T) {
client := NewLocalTimeZone()
z, ok := client.(*localTimeZone)
if !ok {
t.Error("error when initializing client")
}
cache := z.data.Load()
if len(cache.tzNames) == 0 {
t.Error("expected timezone names in cache")
}
for _, name := range cache.tzNames {
if name == "" {
t.Error("unexpected empty timezone name")
}
}
}
func TestZonesValidLoadLocation(t *testing.T) {
t.Parallel()
client := NewLocalTimeZone()
z, ok := client.(*localTimeZone)
if !ok {
t.Error("error when initializing client")
}
// Every timezone name embedded in the data must be loadable by time.LoadLocation.
for _, name := range z.data.Load().tzNames {
if _, err := time.LoadLocation(name); err != nil {
t.Errorf("timezone %q is not valid for time.LoadLocation: %v", name, err)
}
}
// The nautical fallback zones are generated at lookup time rather than stored
// in the data, so verify they are loadable across the full longitude range too.
for lon := -180.0; lon <= 180.0; lon += 7.5 {
name := nauticalZone(h3.NewLatLng(0, lon))
if _, err := time.LoadLocation(name); err != nil {
t.Errorf("nautical timezone %q is not valid for time.LoadLocation: %v", name, err)
}
}
}
func TestCellsPresent(t *testing.T) {
client := NewLocalTimeZone()
z, ok := client.(*localTimeZone)
if !ok {
t.Error("error when initializing client")
}
cache := z.data.Load()
if len(cache.cells) == 0 {
t.Error("expected cells in cache")
}
if len(cache.cells) != len(cache.tzIdx) {
t.Errorf("cells and tzIdx length mismatch: %d vs %d", len(cache.cells), len(cache.tzIdx))
}
// Verify cells are sorted
for i := 1; i < len(cache.cells); i++ {
if cache.cells[i] < cache.cells[i-1] {
t.Errorf("cells not sorted at index %d", i)
break
}
}
}
func BenchmarkGetZone(b *testing.B) {
client := NewLocalTimeZone()
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) {
n := 0
for b.Loop() {
tc := data[n%len(data)]
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) {
n := 0
for b.Loop() {
tc := data[n%len(data)]
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++
}
})
}