Repository navigation
Expand file tree
/
Copy pathexample_sqlconn_test.go
More file actions
126 lines (100 loc) · 2.78 KB
/
Copy pathexample_sqlconn_test.go
File metadata and controls
126 lines (100 loc) · 2.78 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package sqlconn_test
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/tecnickcom/nurago/pkg/sqlconn"
)
func ExampleNew() {
// A real program registers a driver (for example go-sql-driver/mysql)
// and lets sqlconn call sql.Open. WithSQLOpenFunc substitutes a mock so
// the example needs no database.
mockDB, mock, err := sqlmock.New()
if err != nil {
fmt.Println(err)
return
}
// The connection check runs the validation query, and Shutdown closes
// the pool.
mock.ExpectQuery("SELECT 1").WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1))
mock.ExpectClose()
conn, err := sqlconn.New(
context.TODO(),
"mysql",
"user:pass@tcp(127.0.0.1:3306)/testdb",
sqlconn.WithConnMaxOpen(25),
sqlconn.WithConnMaxIdleCount(5),
sqlconn.WithConnMaxLifetime(5*time.Minute),
sqlconn.WithPingTimeout(2*time.Second),
sqlconn.WithSQLOpenFunc(func(_, _ string) (*sql.DB, error) {
return mockDB, nil
}),
)
if err != nil {
fmt.Println(err)
return
}
defer func() { _ = conn.Shutdown(context.TODO()) }()
// DB returns the pooled *sql.DB for normal query execution.
fmt.Println(conn.DB() != nil)
// Output:
// true
}
func ExampleConnect() {
mockDB, mock, err := sqlmock.New()
if err != nil {
fmt.Println(err)
return
}
mock.ExpectQuery("SELECT 1").WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1))
mock.ExpectClose()
// Connect takes a single DSN URL and derives the driver from its scheme,
// which suits configuration supplied as one environment variable.
conn, err := sqlconn.Connect(
context.TODO(),
"mysql://user:pass@tcp(127.0.0.1:3306)/testdb",
sqlconn.WithSQLOpenFunc(func(_, _ string) (*sql.DB, error) {
return mockDB, nil
}),
)
if err != nil {
fmt.Println(err)
return
}
defer func() { _ = conn.Shutdown(context.TODO()) }()
fmt.Println(conn.DB() != nil)
// Output:
// true
}
func ExampleSQLConn_HealthCheck() {
mockDB, mock, err := sqlmock.New()
if err != nil {
fmt.Println(err)
return
}
// One validation query on connect, a second one for the health probe,
// then the close performed by Shutdown.
mock.ExpectQuery("SELECT 1").WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1))
mock.ExpectQuery("SELECT 1").WillReturnRows(sqlmock.NewRows([]string{"1"}).AddRow(1))
mock.ExpectClose()
conn, err := sqlconn.New(
context.TODO(),
"mysql",
"user:pass@tcp(127.0.0.1:3306)/testdb",
sqlconn.WithSQLOpenFunc(func(_, _ string) (*sql.DB, error) {
return mockDB, nil
}),
)
if err != nil {
fmt.Println(err)
return
}
defer func() { _ = conn.Shutdown(context.TODO()) }()
// HealthCheck satisfies healthcheck.HealthChecker, so the database can
// be registered directly on a service health endpoint.
fmt.Println(conn.HealthCheck(context.TODO()))
// Output:
// <nil>
}