forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memstore_test.go
166 lines (144 loc) · 4.48 KB
/
memstore_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
// 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 (
"context"
"testing"
. "zgo.at/goatcounter/v2"
"zgo.at/goatcounter/v2/gctest"
"zgo.at/zdb"
"zgo.at/zstd/zint"
"zgo.at/zstd/ztime"
)
func TestMemstore(t *testing.T) {
ctx := gctest.DB(t)
var site Site
site.Defaults(ctx)
site.Settings.Collect.Set(CollectHits)
ctx = gctest.Site(ctx, t, &site, nil)
ctx = WithSite(ctx, &site)
for i := 0; i < 2000; i++ {
Memstore.Append(gen(ctx))
}
_, err := Memstore.Persist(ctx)
if err != nil {
t.Fatal(err)
}
var count int
err = zdb.Get(ctx, &count, `select count(*) from hits`)
if err != nil {
t.Fatal(err)
}
if count != 2000 {
t.Errorf("wrong count; wanted 2000 but have %d", count)
}
}
func gen(ctx context.Context) Hit {
s := MustGetSite(ctx)
return Hit{
Site: s.ID,
Session: TestSession,
Path: "/test",
Ref: "https://example.com/test",
UserAgentHeader: "test",
}
}
func TestNextUUID(t *testing.T) {
want := `11223344556677-8899aabbccddef01
11223344556677-8899aabbccddef02
11223344556677-8899aabbccddef03
11223344556677-8899aabbccddeeff`
t.Run("", func(t *testing.T) {
gctest.DB(t)
have := Memstore.SessionID().Format(16) + "\n" +
Memstore.SessionID().Format(16) + "\n" +
Memstore.SessionID().Format(16) + "\n" +
TestSession.Format(16)
if have != want {
t.Errorf("wrong:\n%s", have)
}
})
t.Run("", func(t *testing.T) {
gctest.DB(t)
have := Memstore.SessionID().Format(16) + "\n" +
Memstore.SessionID().Format(16) + "\n" +
Memstore.SessionID().Format(16) + "\n" +
TestSession.Format(16)
if have != want {
t.Errorf("wrong after reset:\n%s", have)
}
})
}
func TestMemstoreCollect(t *testing.T) {
all := func() zint.Bitflag16 {
s := SiteSettings{}
s.Defaults(context.Background())
s.Collect.Set(CollectHits)
return s.Collect
}()
tests := []struct {
collect zint.Bitflag16
collectRegions Strings
want string
}{
{all, Strings{}, `
session bot path ref ref_scheme size location first_visit
00112233445566778899aabbccddeeff 0 /test example.com h 5,6,7.0 NL 0
00112233445566778899aabbccddeeff 0 /other xxx c 5,6,7.0 ID-BA 1
`},
{CollectNothing, Strings{}, `
session bot path ref ref_scheme size location first_visit
`},
{all ^ CollectLocationRegion, Strings{}, `
session bot path ref ref_scheme size location first_visit
00112233445566778899aabbccddeeff 0 /test example.com h 5,6,7.0 NL 0
00112233445566778899aabbccddeeff 0 /other xxx c 5,6,7.0 ID 1
`},
{all, Strings{"US"}, `
session bot path ref ref_scheme size location first_visit
00112233445566778899aabbccddeeff 0 /test example.com h 5,6,7.0 NL 0
00112233445566778899aabbccddeeff 0 /other xxx c 5,6,7.0 ID 1
`},
{all, Strings{"ID"}, `
session bot path ref ref_scheme size location first_visit
00112233445566778899aabbccddeeff 0 /test example.com h 5,6,7.0 NL 0
00112233445566778899aabbccddeeff 0 /other xxx c 5,6,7.0 ID-BA 1
`},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
ctx := gctest.DB(t)
ztime.SetNow(t, "2020-06-18")
site := Site{Settings: SiteSettings{
Collect: tt.collect,
CollectRegions: tt.collectRegions,
}}
ctx = gctest.Site(ctx, t, &site, nil)
gctest.StoreHits(ctx, t, false, Hit{
Site: site.ID,
Path: "/test",
Ref: "https://example.com",
Location: "NL",
Size: Floats{5, 6, 7},
}, Hit{
Site: site.ID,
Path: "/other",
Query: "ref=xxx",
Location: "ID-BA",
Size: Floats{5, 6, 7},
FirstVisit: true,
})
have := zdb.DumpString(ctx, `
select session, bot, paths.path, refs.ref, refs.ref_scheme, sizes.size, location, first_visit
from hits
join paths using (path_id)
left join refs using (ref_id)
left join sizes using (size_id)
`)
if d := zdb.Diff(have, tt.want); d != "" {
t.Error(d)
}
})
}
}