This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
driver_test.go
89 lines (75 loc) · 1.99 KB
/
driver_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
package grpcsql_test
import (
"database/sql/driver"
"testing"
"google.golang.org/grpc"
"github.com/CanonicalLtd/go-grpc-sql"
"github.com/mpvl/subtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Open a new gRPC connection.
func TestDriver_Open(t *testing.T) {
driver, cleanup := newDriver()
defer cleanup()
conn, err := driver.Open(":memory:")
require.NoError(t, err)
defer conn.Close()
}
// Create a transaction and commit it.
func TestDriver_TxCommit(t *testing.T) {
driver, cleanup := newDriver()
defer cleanup()
conn, err := driver.Open(":memory:")
require.NoError(t, err)
defer conn.Close()
tx, err := conn.Begin()
require.NoError(t, err)
assert.NoError(t, tx.Commit())
}
// Open a new gRPC connection.
func TestDriver_BadConn(t *testing.T) {
drv, cleanup := newDriver()
conn, err := drv.Open(":memory:")
assert.NoError(t, err)
defer conn.Close()
// Shutdown the server to interrupt the gRPC connection.
cleanup()
stmt, err := conn.Prepare("SELECT * FROM sqlite_master")
assert.Nil(t, stmt)
assert.Equal(t, driver.ErrBadConn, err)
}
// Possible failure modes of Driver.Open().
func TestDriver_OpenError(t *testing.T) {
cases := []struct {
title string
dialer grpcsql.Dialer
err string
}{
{
"gRPC connection failed",
func() (*grpc.ClientConn, error) {
return grpc.Dial("1.2.3.4", grpc.WithInsecure())
},
"gRPC conn method failed",
},
}
for _, c := range cases {
subtest.Run(t, c.title, func(t *testing.T) {
driver := grpcsql.NewDriver(c.dialer)
_, err := driver.Open(":memory:")
require.NotNil(t, err)
assert.Contains(t, err.Error(), c.err)
})
}
}
// Return a new Driver instance configured to connect to a test gRPC server.
func newDriver() (*grpcsql.Driver, func()) {
server, address := newGatewayServer()
dialer := func() (*grpc.ClientConn, error) {
return grpc.Dial(address, grpc.WithInsecure())
}
driver := grpcsql.NewDriver(dialer)
cleanup := func() { server.Stop() }
return driver, cleanup
}