forked from tailscale/golink
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvex_test.go
144 lines (125 loc) · 2.99 KB
/
convex_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
// Copyright 2022 Tailscale Inc & Contributors
// SPDX-License-Identifier: BSD-3-Clause
package golink
import (
"reflect"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/joho/godotenv"
)
func clear(c *ConvexDB) {
c.mutation(&UdfExecution{Path: "clear", Args: map[string]interface{}{}, Format: "json"})
}
func getDbUrl() string {
envLocal, err := godotenv.Read(".env.local")
if err != nil {
return "https://feeble-gull-946.convex.cloud"
}
url := envLocal["VITE_CONVEX_URL"]
if len(url) == 0 {
url = "https://feeble-gull-946.convex.cloud"
}
return url
}
// Test saving and loading links for SQLiteDB
func Test_Convex_SaveLoadDeleteLinks(t *testing.T) {
url := getDbUrl()
db := NewConvexDB(url, "test")
clear(db)
defer clear(db)
links := []*Link{
{Short: "short", Long: "long", Created: time.Now(), LastEdit: time.Now()},
{Short: "Foo.Bar", Long: "long", Created: time.Now(), LastEdit: time.Now()},
}
approximateTimeEq := cmp.Options{cmp.FilterPath(func(path cmp.Path) bool { return path.Last().Type() == reflect.TypeOf(time.Time{}) }, cmpopts.EquateApproxTime(time.Second))}
for _, link := range links {
if err := db.Save(link); err != nil {
t.Error(err)
}
got, err := db.Load(link.Short)
if err != nil {
t.Error(err)
}
if !cmp.Equal(got, link, approximateTimeEq) {
t.Errorf("db save and load got %v, want %v", *got, *link)
}
}
got, err := db.LoadAll()
if err != nil {
t.Error(err)
}
sortLinks := cmpopts.SortSlices(func(a, b *Link) bool {
return a.Short < b.Short
})
if !cmp.Equal(got, links, sortLinks, approximateTimeEq) {
t.Errorf("db.LoadAll got %v, want %v", got, links)
}
for _, link := range links {
if err := db.Delete(link.Short); err != nil {
t.Error(err)
}
}
got, err = db.LoadAll()
if err != nil {
t.Error(err)
}
want := []*Link(nil)
if !cmp.Equal(got, want) {
t.Errorf("db.LoadAll got %v, want %v", got, want)
}
}
// Test saving and loading stats for SQLiteDB
func Test_Convex_SaveLoadDeleteStats(t *testing.T) {
url := getDbUrl()
db := NewConvexDB(url, "test")
clear(db)
// defer clear(db)
// preload some links
links := []*Link{
{Short: "a"},
{Short: "B-c"},
}
for _, link := range links {
if err := db.Save(link); err != nil {
t.Error(err)
}
}
// Stats to store do not need to be their canonical short name,
// but returned stats always should be.
stats := []ClickStats{
{"a": 1},
{"b-c": 1},
{"a": 1, "bc": 2},
}
want := ClickStats{
"a": 2,
"B-c": 3,
}
for _, s := range stats {
if err := db.SaveStats(s); err != nil {
t.Error(err)
}
}
got, err := db.LoadStats()
if err != nil {
t.Error(err)
}
if !cmp.Equal(got, want) {
t.Errorf("db.LoadStats got %v, want %v", got, want)
}
for k := range want {
if err := db.DeleteStats(k); err != nil {
t.Error(err)
}
}
got, err = db.LoadStats()
if err != nil {
t.Error(err)
}
want = ClickStats{}
if !cmp.Equal(got, want) {
t.Errorf("db.LoadStats got %v, want %v", got, want)
}
}