forked from arp242/goatcounter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite_test.go
91 lines (79 loc) · 2.03 KB
/
site_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
// Copyright © 2019 Martin Tournoij <[email protected]>
// This file is part of GoatCounter and published under the terms of the EUPL
// v1.2, which can be found in the LICENSE file or at http://eupl12.zgo.at
package goatcounter_test
import (
"context"
"fmt"
"reflect"
"testing"
. "zgo.at/goatcounter"
"zgo.at/goatcounter/gctest"
"zgo.at/zvalidate"
)
func TestSiteInsert(t *testing.T) {
ctx, clean := gctest.DB(t)
defer clean()
s := Site{Code: "the_code", Name: "the-code.com", Plan: PlanPersonal}
err := s.Insert(ctx)
if err != nil {
t.Fatal(err)
}
if s.ID == 0 {
t.Fatal("ID is 0")
}
}
func TestSiteValidate(t *testing.T) {
tests := []struct {
in Site
prefn func(context.Context)
want map[string][]string
}{
{
Site{Name: "Hello", Code: "hello-_0", State: StateActive, Plan: PlanPersonal},
nil,
nil,
},
{
Site{Name: "Hello", Code: "h€llo", State: StateActive, Plan: PlanPersonal},
nil,
map[string][]string{"code": {"'€' not allowed; characters are limited to '_', '-', a to z, and numbers"}},
},
{
Site{Name: "Hello", Code: "-hello", State: StateActive, Plan: PlanPersonal},
nil,
map[string][]string{"code": {"cannot start with underscore or dash (_, -)"}},
},
{
Site{Name: "Hello", Code: "hello", State: StateActive, Plan: PlanPersonal},
func(ctx context.Context) {
s := Site{Name: "Hello", Code: "hello", State: StateActive, Plan: PlanPersonal}
err := s.Insert(ctx)
if err != nil {
panic(err)
}
},
map[string][]string{"code": {"already exists"}},
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
ctx, clean := gctest.DB(t)
defer clean()
if tt.prefn != nil {
tt.prefn(ctx)
}
err := tt.in.Validate(ctx)
if err == nil && tt.want == nil {
return
}
verr, ok := err.(*zvalidate.Validator)
if !ok {
t.Fatalf("unexpected error type %T: %#[1]v", err)
}
if !reflect.DeepEqual(verr.Errors, tt.want) {
t.Errorf("wrong error\nout: %s\nwant: %s", verr.Errors, tt.want)
}
})
}
}