-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurls_test.go
110 lines (97 loc) · 2.24 KB
/
urls_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
package cache
import (
"context"
"errors"
"net/url"
"testing"
"github.com/bartventer/gocache/pkg/driver"
"github.com/bartventer/gocache/pkg/keymod"
)
type mockURLOpener[K driver.String] struct{}
func (m *mockURLOpener[K]) OpenCacheURL(ctx context.Context, u *url.URL) (*GenericCache[K], error) {
if u.Scheme == "err" {
return nil, errors.New("forced error")
}
return nil, nil
}
func TestCache(t *testing.T) {
ctx := context.Background()
fake := &mockURLOpener[string]{}
RegisterCache("foo", fake)
RegisterCache("err", fake)
for _, tc := range []struct {
name string
url string
wantErr bool
}{
{
name: "empty URL",
wantErr: true,
},
{
name: "invalid URL",
url: ":foo",
wantErr: true,
},
{
name: "invalid URL no scheme",
url: "foo",
wantErr: true,
},
{
name: "unregistered scheme",
url: "bar://mycache",
wantErr: true,
},
{
name: "func returns error",
url: "err://mycache",
wantErr: true,
},
{
name: "no query options",
url: "foo://mycache",
},
{
name: "empty query options",
url: "foo://mycache?",
},
} {
t.Run(tc.name, func(t *testing.T) {
_, gotErr := OpenGenericCache[string](ctx, tc.url)
if (gotErr != nil) != tc.wantErr {
t.Fatalf("got err %v, want error %v", gotErr, tc.wantErr)
}
})
}
}
func TestRegisterCache(t *testing.T) {
fake := &mockURLOpener[string]{}
// Test registering a new scheme.
RegisterCache("new", fake)
// Register same scheme but with different type.
fake2 := &mockURLOpener[keymod.Key]{}
RegisterCache("new", fake2)
// Test registering an existing scheme and type. Should panic.
defer func() {
if r := recover(); r == nil {
t.Errorf("The code did not panic")
}
}()
RegisterCache("new", fake)
}
func TestOpenCache(t *testing.T) {
ctx := context.Background()
fake := &mockURLOpener[string]{}
// Test opening a cache with a valid scheme.
RegisterCache("baz", fake)
_, err := OpenGenericCache[string](ctx, "baz://mycache")
if err != nil {
t.Fatalf("Failed to open cache: %v", err)
}
// Test opening a cache with a valid scheme and invalid type.
_, err = OpenGenericCache[keymod.Key](ctx, "baz://mycache")
if err == nil {
t.Fatalf("Expected error, got nil")
}
}