-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlmock.go
More file actions
40 lines (32 loc) · 995 Bytes
/
sqlmock.go
File metadata and controls
40 lines (32 loc) · 995 Bytes
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
// Package sqlmock provides a lightweight SQL driver mock for testing code that uses database/sql.
package sqlmock
import (
"database/sql"
"fmt"
"sync/atomic"
)
const driverNamePrefix = "sqlmock"
var driverCounter atomic.Uint64
// New registers the mock SQL driver and returns a DB handle with its mock state.
//
// New uses a unique driver name per call, allowing repeated and parallel usage.
func New() (*sql.DB, *Mock, error) {
return NewWithOptions()
}
// NewWithOptions registers the mock SQL driver and returns a DB handle with its
// configured mock state.
//
// NewWithOptions uses a unique driver name per call, allowing repeated and
// parallel usage.
func NewWithOptions(opts ...Option) (*sql.DB, *Mock, error) {
mock := NewMock(opts...)
driverName := fmt.Sprintf("%s_%d", driverNamePrefix, driverCounter.Add(1))
sql.Register(driverName, &Driver{
mock: mock,
})
db, err := sql.Open(driverName, "")
if err != nil {
return nil, nil, err
}
return db, mock, nil
}