forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemstore_test.go
156 lines (134 loc) · 4.45 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
// 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)
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 got %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)
got := Memstore.SessionID().Format(16) + "\n" +
Memstore.SessionID().Format(16) + "\n" +
Memstore.SessionID().Format(16) + "\n" +
TestSession.Format(16)
if got != want {
t.Errorf("wrong:\n%s", got)
}
})
t.Run("", func(t *testing.T) {
gctest.DB(t)
got := Memstore.SessionID().Format(16) + "\n" +
Memstore.SessionID().Format(16) + "\n" +
Memstore.SessionID().Format(16) + "\n" +
TestSession.Format(16)
if got != want {
t.Errorf("wrong after reset:\n%s", got)
}
})
}
func TestMemstoreCollect(t *testing.T) {
all := func() zint.Bitflag16 {
s := SiteSettings{}
s.Defaults(context.Background())
return s.Collect
}()
tests := []struct {
collect zint.Bitflag16
collectRegions Strings
want string
}{
{all, Strings{}, `
user_agent_id session bot ref ref_scheme size location first_visit
1 00112233445566778899aabbccddeeff 0 example.com h 5,6,7 NL 0
1 00112233445566778899aabbccddeeff 0 xxx c 5,6,7 ID-BA 1
`},
{CollectNothing, Strings{}, `
user_agent_id session bot ref ref_scheme size location first_visit
NULL 00000000000000000000000000000000 0 NULL 1
NULL 00000000000000000000000000000000 0 NULL 1
`},
{all ^ CollectLocationRegion, Strings{}, `
user_agent_id session bot ref ref_scheme size location first_visit
1 00112233445566778899aabbccddeeff 0 example.com h 5,6,7 NL 0
1 00112233445566778899aabbccddeeff 0 xxx c 5,6,7 ID 1
`},
{all, Strings{"US"}, `
user_agent_id session bot ref ref_scheme size location first_visit
1 00112233445566778899aabbccddeeff 0 example.com h 5,6,7 NL 0
1 00112233445566778899aabbccddeeff 0 xxx c 5,6,7 ID 1
`},
{all, Strings{"ID"}, `
user_agent_id session bot ref ref_scheme size location first_visit
1 00112233445566778899aabbccddeeff 0 example.com h 5,6,7 NL 0
1 00112233445566778899aabbccddeeff 0 xxx c 5,6,7 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,
})
got := zdb.DumpString(ctx, `select user_agent_id, session, bot, ref, ref_scheme, size, location, first_visit from hits`)
if d := zdb.Diff(got, tt.want); d != "" {
t.Error(d)
}
})
}
}