forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ref_test.go
106 lines (91 loc) · 2.32 KB
/
ref_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
// 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 (
"testing"
"time"
. "zgo.at/goatcounter/v2"
"zgo.at/goatcounter/v2/gctest"
"zgo.at/zstd/zjson"
"zgo.at/zstd/ztest"
"zgo.at/zstd/ztime"
)
func TestListRefsByPathID(t *testing.T) {
ctx := gctest.DB(t)
gctest.StoreHits(ctx, t, false,
Hit{Path: "/x", Ref: "http://example.com"},
Hit{Path: "/x", Ref: "http://example.com"},
Hit{Path: "/x", Ref: "http://example.org"},
Hit{Path: "/y", Ref: "http://example.org"})
rng := ztime.NewRange(ztime.Now().Add(-1 * time.Hour)).To(ztime.Now().Add(1 * time.Hour))
var have HitStats
err := have.ListRefsByPathID(ctx, 1, rng, 10, 0)
if err != nil {
t.Fatal(err)
}
want := `{
"more": false,
"stats": [{
"count": 0,
"name": "example.org",
"ref_scheme": "h"
}, {
"count": 0,
"name": "example.com",
"ref_scheme": "h"
}]}`
if d := ztest.Diff(zjson.MustMarshalString(have), want, ztest.DiffJSON); d != "" {
t.Error(d)
}
}
func TestListTopRefs(t *testing.T) {
ctx := gctest.DB(t)
gctest.StoreHits(ctx, t, false,
Hit{Path: "/x", Ref: "http://example.com", FirstVisit: true},
Hit{Path: "/x", Ref: "http://example.com"},
Hit{Path: "/x", Ref: "http://example.org"},
Hit{Path: "/y", Ref: "http://example.org", FirstVisit: true},
Hit{Path: "/x", Ref: "http://example.org"})
rng := ztime.NewRange(ztime.Now().Add(-1 * time.Hour)).To(ztime.Now().Add(1 * time.Hour))
{
var have HitStats
err := have.ListTopRefs(ctx, rng, nil, 10, 0)
if err != nil {
t.Fatal(err)
}
want := `{
"more": false,
"stats": [{
"name": "example.com",
"count": 1,
"ref_scheme": "h"
}, {
"name": "example.org",
"count": 1,
"ref_scheme": "h"
}]
}`
if d := ztest.Diff(zjson.MustMarshalString(have), want, ztest.DiffJSON); d != "" {
t.Error(d)
}
}
{
var have HitStats
err := have.ListTopRefs(ctx, rng, []int64{2}, 10, 0)
if err != nil {
t.Fatal(err)
}
want := `{
"more": false,
"stats": [{
"name": "example.org",
"count": 1,
"ref_scheme": "h"
}]
}`
if d := ztest.Diff(zjson.MustMarshalString(have), want, ztest.DiffJSON); d != "" {
t.Error(d)
}
}
}