forked from jeroenrinzema/psql-wire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions_test.go
More file actions
103 lines (87 loc) · 2.23 KB
/
options_test.go
File metadata and controls
103 lines (87 loc) · 2.23 KB
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
package wire
import (
"context"
"testing"
"github.com/jackc/pgx/v5/pgtype"
"github.com/neilotoole/slogt"
"github.com/stretchr/testify/assert"
)
func TestParseParameters(t *testing.T) {
type test struct {
query string
parameters []uint32
}
tests := map[string]test{
"positional": {
query: "SELECT * FROM users WHERE id = $1 AND age > $2",
parameters: []uint32{0, 0},
},
"unpositional": {
query: "SELECT * FROM users WHERE id = ? AND age > ?",
parameters: []uint32{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)
})
}
}
func TestExtendTypes(t *testing.T) {
extensionCalled := false
srv, err := NewServer(nil,
Logger(slogt.New(t)),
ExtendTypes(func(typeMap *pgtype.Map) {
extensionCalled = true
}),
)
assert.NoError(t, err)
assert.NotNil(t, srv)
assert.NotNil(t, srv.typeExtension)
assert.False(t, extensionCalled, "Extension should not be called during server creation")
}