-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
locations_test.go
75 lines (64 loc) · 1.8 KB
/
locations_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
// Copyright © Martin Tournoij – This file is part of GoatCounter and published
// under the terms of a slightly modified EUPL v1.2 license, which can be found
// in the LICENSE file or at https://license.goatcounter.com
package goatcounter_test
import (
"fmt"
"testing"
. "zgo.at/goatcounter/v2"
"zgo.at/goatcounter/v2/gctest"
"zgo.at/zdb"
"zgo.at/zstd/ztest"
)
func TestLocations(t *testing.T) {
ctx := gctest.DB(t)
run := func() {
{
var l Location
err := l.Lookup(ctx, "51.171.91.33")
if err != nil {
t.Fatal(err)
}
out := fmt.Sprintf("%#v", l)
want := `goatcounter.Location{ID:2, Country:"IE", Region:"", CountryName:"Ireland", RegionName:"", ISO3166_2:"IE"}`
if out != want {
t.Error(out)
}
}
{
var l Location
err := l.ByCode(ctx, "US-TX")
if err != nil {
t.Fatal(err)
}
out := fmt.Sprintf("%#v", l)
want := `goatcounter.Location{ID:3, Country:"US", Region:"TX", CountryName:"United States", RegionName:"", ISO3166_2:"US-TX"}`
if out != want {
t.Error(out)
}
}
out := zdb.DumpString(ctx, `select * from locations`)
want := `
location_id iso_3166_2 country region country_name region_name
1 (unknown)
2 IE IE Ireland
3 US-TX US TX United States
4 US US United States`
if d := ztest.Diff(out, want, ztest.DiffNormalizeWhitespace); d != "" {
t.Error(d)
}
}
// Run it multiple times, since it should always give the same resuts.
run()
run()
ctx = NewContext(zdb.MustGetDB(ctx)) // Reset cache
run()
}
func BenchmarkLocationsByCode(b *testing.B) {
ctx := gctest.DB(b)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
(&Location{}).ByCode(ctx, "US-TX")
}
}