Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arp242 committed Oct 30, 2022
1 parent b1b086f commit 4fcd99a
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 52 deletions.
16 changes: 9 additions & 7 deletions cron/hit_stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func TestHitStats(t *testing.T) {
}...)

check := func(wantT, want0, want1 string) {
t.Helper()

var stats goatcounter.HitLists
display, displayUnique, more, err := stats.List(ctx,
ztime.NewRange(now.Add(-1*time.Hour)).To(now.Add(1*time.Hour)),
Expand All @@ -40,29 +42,29 @@ func TestHitStats(t *testing.T) {

gotT := fmt.Sprintf("%d %d %t", display, displayUnique, more)
if wantT != gotT {
t.Fatalf("wrong totals\ngot: %s\nwant: %s", gotT, wantT)
t.Fatalf("wrong totals\nhave: %s\nwant: %s", gotT, wantT)
}
if len(stats) != 2 {
t.Fatalf("len(stats) is not 2: %d", len(stats))
}

got0 := string(zjson.MustMarshal(stats[0]))
if got0 != want0 {
t.Errorf("first wrong\ngot: %s\nwant: %s", got0, want0)
t.Errorf("first wrong\nhave: %s\nwant: %s", got0, want0)
}

got1 := string(zjson.MustMarshal(stats[1]))
if got1 != want1 {
t.Errorf("second wrong\ngot: %s\nwant: %s", got1, want1)
t.Errorf("second wrong\nhave: %s\nwant: %s", got1, want1)
}
}

check("3 1 false",
`{"count":2,"count_unique":1,"path_id":1,"path":"/asd","event":false,"title":"aSd","max":2,`+
`{"count":2,"count_unique":1,"path_id":1,"path":"/asd","event":false,"title":"aSd","max":1,`+
`"stats":[{"day":"2019-08-31",`+
`"hourly":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0],`+
`"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0],"daily":2,"daily_unique":1}]}`,
`{"count":1,"count_unique":0,"path_id":2,"path":"/zxc","event":false,"title":"","max":1,`+
`{"count":1,"count_unique":0,"path_id":2,"path":"/zxc","event":false,"title":"","max":0,`+
`"stats":[{"day":"2019-08-31",`+
`"hourly":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0],`+
`"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"daily":1,"daily_unique":0}]}`,
Expand All @@ -74,11 +76,11 @@ func TestHitStats(t *testing.T) {
}...)

check("5 2 false",
`{"count":4,"count_unique":2,"path_id":1,"path":"/asd","event":false,"title":"aSd","max":2,`+
`{"count":4,"count_unique":2,"path_id":1,"path":"/asd","event":false,"title":"aSd","max":1,`+
`"stats":[{"day":"2019-08-31",`+
`"hourly":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,2,0,0,0,0,0,0,0],`+
`"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0],"daily":4,"daily_unique":2}]}`,
`{"count":1,"count_unique":0,"path_id":2,"path":"/zxc","event":false,"title":"","max":1,`+
`{"count":1,"count_unique":0,"path_id":2,"path":"/zxc","event":false,"title":"","max":0,`+
`"stats":[{"day":"2019-08-31",`+
`"hourly":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0],`+
`"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"daily":1,"daily_unique":0}]}`,
Expand Down
1 change: 1 addition & 0 deletions handlers/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestBackendPagesMore(t *testing.T) {
delete(body, "html")
haveJSON := string(zjson.MustMarshalIndent(body, "", "\t"))
wantJSON := `{
"max": 10,
"more": false,
"total_unique_display": 0
}`
Expand Down
183 changes: 139 additions & 44 deletions hit_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package goatcounter_test
import (
"fmt"
"os"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -234,63 +235,157 @@ func TestHitListTotals(t *testing.T) {
t.Run("hourly", func(t *testing.T) {
rng := ztime.NewRange(ztime.Now()).To(ztime.Now())

want := []string{
`10 {"count":12,"count_unique":2,"path_id":0,"path":"TOTAL ","event":false,"title":"","max":0,"stats":[` +
`{"day":"2020-06-18","hourly":[0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0],"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0],"daily":0,"daily_unique":0}]}`,

`10 {"count":11,"count_unique":1,"path_id":0,"path":"TOTAL ","event":false,"title":"","max":0,"stats":[` +
`{"day":"2020-06-18","hourly":[0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,0,0,0,0,0,0,0,0],"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],"daily":0,"daily_unique":0}]}`,

`10 {"count":1,"count_unique":1,"path_id":0,"path":"TOTAL ","event":false,"title":"","max":0,"stats":[` +
`{"day":"2020-06-18","hourly":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],"daily":0,"daily_unique":0}]}`,

`10 {"count":12,"count_unique":2,"path_id":0,"path":"TOTAL ","event":false,"title":"","max":0,"stats":[` +
`{"day":"2020-06-18","hourly":[0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0],"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0],"daily":0,"daily_unique":0}]}`,
want := [][]string{
{`10`, `{
"count":12,
"count_unique":2,
"path_id":0,
"path":"TOTAL ",
"event":false,
"title":"",
"max":0,
"stats":[{
"day":"2020-06-18","hourly":[0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0],"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0],"daily":0,"daily_unique":0}
]}`},

{`10`, `{
"count":11,
"count_unique":1,
"path_id":0,
"path":"TOTAL ",
"event":false,
"title":"",
"max":0,
"stats":[{
"day":"2020-06-18","hourly":[0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,0,0,0,0,0,0,0,0],"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],"daily":0,"daily_unique":0
}]}`},

{`10`, `{
"count":1,
"count_unique":1,
"path_id":0,
"path":"TOTAL ",
"event":false,
"title":"",
"max":0,
"stats":[{
"day":"2020-06-18","hourly":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],"daily":0,"daily_unique":0
}]}`},

{`10`, `{
"count":12,
"count_unique":2,
"path_id":0,
"path":"TOTAL ",
"event":false,
"title":"",
"max":0,
"stats":[{
"day":"2020-06-18","hourly":[0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0],"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0],"daily":0,"daily_unique":0}
]}`},
}
for i, filter := range [][]int64{nil, []int64{1}, []int64{2}, []int64{1, 2}} {
var hs HitList
count, err := hs.Totals(ctx, rng, filter, false, false)
if err != nil {
t.Fatal(err)
}
t.Run("", func(t *testing.T) {
var hs HitList
count, err := hs.Totals(ctx, rng, filter, false, false)
if err != nil {
t.Fatal(err)
}

have := fmt.Sprintf("%d %s", count, zjson.MustMarshal(hs))
w := want[i]
if have != w {
t.Errorf("\nwant: %s\nhave: %v\nfilter: %v", w, have, filter)
}
if strconv.Itoa(count) != want[i][0] {
t.Errorf("count wrong\nhave: %d\nwant: %s", count, want[i][0])
}
if d := ztest.Diff(zjson.MustMarshalString(hs), want[i][1], ztest.DiffJSON, ztest.DiffVerbose); d != "" {
t.Error(d)
}
})
}
})

t.Run("daily", func(t *testing.T) {
rng := ztime.NewRange(ztime.Now()).To(ztime.Now())

want := []string{
`12 {"count":12,"count_unique":2,"path_id":0,"path":"TOTAL ","event":false,"title":"","max":0,"stats":[` +
`{"day":"2020-06-18","hourly":[0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0],"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0],"daily":12,"daily_unique":2}]}`,

`11 {"count":11,"count_unique":1,"path_id":0,"path":"TOTAL ","event":false,"title":"","max":0,"stats":[` +
`{"day":"2020-06-18","hourly":[0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,0,0,0,0,0,0,0,0],"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],"daily":11,"daily_unique":1}]}`,

`10 {"count":1,"count_unique":1,"path_id":0,"path":"TOTAL ","event":false,"title":"","max":0,"stats":[` +
`{"day":"2020-06-18","hourly":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],"daily":1,"daily_unique":1}]}`,

`12 {"count":12,"count_unique":2,"path_id":0,"path":"TOTAL ","event":false,"title":"","max":0,"stats":[` +
`{"day":"2020-06-18","hourly":[0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0],"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0],"daily":12,"daily_unique":2}]}`,
want := [][]string{
{`10`, `{
"count":12,
"count_unique":2,
"path_id":0,
"path":"TOTAL ",
"event":false,
"title":"",
"max":0,
"stats":[{
"day":"2020-06-18",
"hourly": [0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0],
"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0],
"daily":12,
"daily_unique":2}
]}`},

{`10`, `{
"count":11,
"count_unique":1,
"path_id":0,
"path":"TOTAL ",
"event":false,
"title":"",
"max":0,
"stats":[{
"day":"2020-06-18",
"hourly":[0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,0,0,0,0,0,0,0,0],
"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
"daily":11,
"daily_unique":1
}]}`},

{`10`, `{
"count":1,
"count_unique":1,
"path_id":0,
"path":"TOTAL ",
"event":false,
"title":"",
"max":0,
"stats":[{
"day":"2020-06-18",
"hourly":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0],
"daily":1,
"daily_unique":1
}]}`},

{`10`, `{
"count":12,
"count_unique":2,
"path_id":0,
"path":"TOTAL ",
"event":false,
"title":"",
"max":0,
"stats":[{
"day":"2020-06-18",
"hourly":[0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0],
"hourly_unique":[0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0],
"daily":12,
"daily_unique":2
}]}`},
}

for i, filter := range [][]int64{nil, []int64{1}, []int64{2}, []int64{1, 2}} {
var hs HitList
count, err := hs.Totals(ctx, rng, filter, true, false)
if err != nil {
t.Fatal(err)
}
t.Run("", func(t *testing.T) {
var hs HitList
count, err := hs.Totals(ctx, rng, filter, true, false)
if err != nil {
t.Fatal(err)
}

have := fmt.Sprintf("%d %s", count, zjson.MustMarshal(hs))
w := want[i]
if have != w {
t.Errorf("\nwant: %s\nhave: %v\nfilter: %v", w, have, filter)
}
if strconv.Itoa(count) != want[i][0] {
t.Errorf("count wrong\nhave: %d\nwant: %s", count, want[i][0])
}
if d := ztest.Diff(zjson.MustMarshalString(hs), want[i][1], ztest.DiffJSON, ztest.DiffVerbose); d != "" {
t.Error(d)
}
})
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestMetrics(t *testing.T) {

want := `
test 30ms 10ms 20ms
test.x 15ms 15ms 15ms
test·x 15ms 15ms 15ms
`[1:]

if want != have {
Expand Down

0 comments on commit 4fcd99a

Please sign in to comment.