-
Notifications
You must be signed in to change notification settings - Fork 20
/
options_test.go
88 lines (74 loc) · 1.87 KB
/
options_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
package wire
import (
"context"
"testing"
"github.com/lib/pq/oid"
"github.com/neilotoole/slogt"
"github.com/stretchr/testify/assert"
)
func TestParseParameters(t *testing.T) {
type test struct {
query string
parameters []oid.Oid
}
tests := map[string]test{
"positional": {
query: "SELECT * FROM users WHERE id = $1 AND age > $2",
parameters: []oid.Oid{0, 0},
},
"unpositional": {
query: "SELECT * FROM users WHERE id = ? AND age > ?",
parameters: []oid.Oid{0, 0},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
parameters := ParseParameters(test.query)
assert.Equal(t, test.parameters, parameters)
})
}
}
func TestNilSessionHandler(t *testing.T) {
srv, err := NewServer(nil, Logger(slogt.New(t)))
assert.NoError(t, err)
assert.NotNil(t, srv)
bg := context.Background()
ctx, err := srv.Session(bg)
assert.NoError(t, err)
assert.Equal(t, bg, ctx)
}
func TestSessionHandler(t *testing.T) {
t.Parallel()
type test []OptionFn
type key string
mock := key("key")
value := "Super Secret Session ID"
tests := map[string]test{
"single": {
SessionMiddleware(func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, mock, value), nil
}),
},
"nested": {
SessionMiddleware(func(ctx context.Context) (context.Context, error) {
return ctx, nil
}),
SessionMiddleware(func(ctx context.Context) (context.Context, error) {
return context.WithValue(ctx, mock, value), nil
}),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
test = append(test, Logger(slogt.New(t)))
srv, err := NewServer(nil, test...)
assert.NoError(t, err)
assert.NotNil(t, srv)
ctx, err := srv.Session(context.Background())
assert.NoError(t, err)
assert.NotNil(t, ctx)
result := ctx.Value(mock)
assert.Equal(t, value, result)
})
}
}