-
-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathexportcsv_test.go
More file actions
145 lines (126 loc) · 3.36 KB
/
exportcsv_test.go
File metadata and controls
145 lines (126 loc) · 3.36 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
package goatcounter_test
import (
"compress/gzip"
"os"
"strings"
"testing"
"time"
"zgo.at/goatcounter/v2"
"zgo.at/goatcounter/v2/gctest"
"zgo.at/zdb"
"zgo.at/zstd/zjson"
"zgo.at/zstd/ztest"
)
func TestCSVExport(t *testing.T) {
ctx := gctest.DB(t)
var site goatcounter.Site
site.Defaults(ctx)
site.Code = "gctest2"
site.Settings.Collect.Set(goatcounter.CollectHits)
ctx = gctest.Site(ctx, t, &site, nil)
ctx = goatcounter.WithSite(ctx, &site)
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.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.CreateCSV(ctx, 0)
if err != nil {
t.Fatal(err)
}
defer fp.Close()
export.RunCSV(ctx, fp, false)
want := strings.ReplaceAll(`{
"id": 1,
"site_id": 2,
"start_from_hit_id": 0,
"last_hit_id": 4,
"path": "%(ANY)goatcounter-export-gctest2-%(YEAR)%(MONTH)%(DAY)T%(ANY)Z-0.csv.gz",
"created_at": "%(YEAR)-%(MONTH)-%(DAY)T%(ANY)Z",
"finished_at": "%(YEAR)-%(MONTH)-%(DAY)T%(ANY)Z",
"num_rows": 4,
"size": "0.1",
"hash": "sha256-8a426c2bebfa343e2dc20e154b67297e981ac3399202369485d6ede82979205b",
"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.ImportCSV(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)
}
})
}