-
Notifications
You must be signed in to change notification settings - Fork 22
/
main_test.go
159 lines (153 loc) · 2.9 KB
/
main_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"errors"
"os"
"reflect"
"sort"
"testing"
"github.com/charmbracelet/charm/testserver"
)
func TestFindDbs(t *testing.T) {
defaultDbs := []string{
"spongebob",
"charm.sh.kv.user.default",
"charm.sh.skate.default",
"sk",
}
tests := []struct {
tname string
name string
dbs []string
err error
}{
{
tname: "unique, single char",
name: "p",
dbs: defaultDbs,
err: errDBNotFound{
suggestions: []string{
"@spongebob",
},
},
},
{
tname: "name > db",
name: "pcharm.sh.kv.user.defaultii",
dbs: defaultDbs,
err: errDBNotFound{
suggestions: nil,
},
},
{
tname: "empty",
name: "",
dbs: defaultDbs,
err: errDBNotFound{
suggestions: formatDbs(defaultDbs),
},
},
{
tname: "single match",
name: "@spon",
dbs: defaultDbs,
err: errDBNotFound{
suggestions: []string{
"@spongebob",
},
},
},
{
tname: "charm match",
name: "@char",
dbs: defaultDbs,
err: errDBNotFound{
suggestions: []string{
"@charm.sh.kv.user.default",
"@charm.sh.skate.default",
},
},
},
{
tname: "single match, no @",
name: "spon",
dbs: defaultDbs,
err: errDBNotFound{
suggestions: []string{
"@spongebob",
},
},
},
{
tname: "no match, no @",
name: "endo",
dbs: defaultDbs,
err: errDBNotFound{
suggestions: nil,
},
},
{
tname: "no match",
name: "@endo",
dbs: defaultDbs,
err: errDBNotFound{
suggestions: nil,
},
},
{
tname: "exact match",
name: "@spongebob",
dbs: defaultDbs,
err: nil,
},
}
for _, tc := range tests {
t.Run(tc.tname, func(t *testing.T) {
path := setup(t, tc.dbs)
defer teardown(path)
_, err := findDb(tc.name, tc.dbs)
if tc.err != nil {
if err == nil {
t.Fatalf("expected an error, got: %v", err)
}
// check we got the right type of error
var perr errDBNotFound
if !errors.As(err, &perr) {
t.Fatalf("something went wrong! got: %v", err)
}
// check suggestions match
gotSuggestions := err.(errDBNotFound).suggestions
wantSuggestions := tc.err.(errDBNotFound).suggestions
sort.Strings(gotSuggestions)
sort.Strings(wantSuggestions)
if !reflect.DeepEqual(gotSuggestions, wantSuggestions) {
t.Fatalf("got != want. got: %v, want: %v", err, tc.err)
}
}
if err != nil && tc.err == nil {
t.Fatalf("got an unexpected error: %v", err)
}
})
}
}
func setup(t *testing.T, dbs []string) string {
// set up a charm server for testing
client := testserver.SetupTestServer(t)
// add the skate dbs
for _, db := range dbs {
charmKV, err := openKV(db)
if err != nil {
t.Fatal(err)
}
if err := charmKV.Close(); err != nil {
t.Fatal(err)
}
}
path, err := client.DataPath()
if err != nil {
t.Fatal(err)
}
return path
}
func teardown(path string) error {
return os.RemoveAll(path)
}