forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_test.go
145 lines (126 loc) · 3.45 KB
/
export_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
// 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 (
"compress/gzip"
"os"
"strings"
"testing"
"time"
"zgo.at/blackmail"
"zgo.at/goatcounter/v2"
"zgo.at/goatcounter/v2/gctest"
"zgo.at/zdb"
"zgo.at/zstd/zjson"
"zgo.at/zstd/ztest"
)
func TestExport(t *testing.T) {
blackmail.DefaultMailer = blackmail.NewMailer(blackmail.ConnectWriter)
ctx := gctest.DB(t)
dump := func() string {
return zdb.DumpString(ctx, `
select
hits.site_id,
paths.path,
paths.title,
paths.event,
browsers.name || ' ' || browsers.version as browser,
systems.name || ' ' || systems.version as system,
-- hits.session,
hits.bot,
hits.ref,
hits.ref_scheme as ref_s,
hits.size,
hits.location as loc,
hits.first_visit as first,
hits.created_at
from hits
join paths using (path_id)
join browsers using (browser_id)
join systems using (system_id)
order by hit_id asc`)
}
d1 := time.Date(2019, 6, 18, 0, 0, 0, 0, time.UTC)
d2 := time.Date(2019, 6, 19, 0, 0, 0, 0, time.UTC)
gctest.StoreHits(ctx, t, false, []goatcounter.Hit{
{Path: "/asd", CreatedAt: d1, UserAgentHeader: "Mozilla/5.0 (X11; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0", Title: "Page asd"},
{Path: "/zxc", CreatedAt: d1, UserAgentHeader: "Mozilla/5.0 (X11; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0", Title: "Page zxc"},
{Path: "event", CreatedAt: d2, Event: true},
{Path: "bot-event", CreatedAt: d2, Event: true, Bot: 1},
{
Path: "/asd",
CreatedAt: d2,
UserAgentHeader: "Mozilla/5.0 (X11; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0",
Title: "Other",
Location: "ID",
Size: goatcounter.Floats{1024, 768, 1},
Ref: "https://example.com/p",
},
}...)
initial := dump()
var export goatcounter.Export
defer func() {
if export.Path != "" {
os.Remove(export.Path)
}
}()
t.Run("export", func(t *testing.T) {
fp, err := export.Create(ctx, 0)
if err != nil {
t.Fatal(err)
}
defer fp.Close()
export.Run(ctx, fp, false)
want := strings.ReplaceAll(`{
"id": 1,
"site_id": 1,
"start_from_hit_id": 0,
"last_hit_id": 5,
"path": "%(ANY)goatcounter-export-gctest-%(YEAR)%(MONTH)%(DAY)T%(ANY)Z-0.csv.gz",
"created_at": "%(YEAR)-%(MONTH)-%(DAY)T%(ANY)Z",
"finished_at": null,
"num_rows": 5,
"size": "0.1",
"hash": "sha256-7fb7060000c3e8a1e05bc9f6156fc5571218a234b0a62b4ad6d67a529ad13707",
"error": null
}`, "\t", "")
got := string(zjson.MustMarshalIndent(export, "", ""))
if d := ztest.DiffMatch(got, want); d != "" {
t.Fatal(d)
}
var exports goatcounter.Exports
err = exports.List(ctx)
if err != nil {
t.Fatal(err)
}
if len(exports) != 1 {
t.Fatal("exports.List()")
}
})
t.Run("import", func(t *testing.T) {
fp, err := os.Open(export.Path)
if err != nil {
t.Fatal(err)
}
defer fp.Close()
gzfp, err := gzip.NewReader(fp)
if err != nil {
t.Fatal(err)
}
defer gzfp.Close()
goatcounter.Import(ctx, gzfp, true, false, func(hit goatcounter.Hit, final bool) {
if !final {
goatcounter.Memstore.Append(hit)
}
})
_, err = goatcounter.Memstore.Persist(ctx)
if err != nil {
t.Fatal(err)
}
out := dump()
if d := ztest.Diff(out, initial); d != "" {
t.Error(d)
}
})
}