forked from pashagolub/pgxmock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
58 lines (46 loc) · 1.46 KB
/
driver.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
package pgxmock
import (
"context"
"errors"
pgx "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
type pgxmockConn struct {
pgxmock
}
// NewConn creates PgxConnIface database connection and a mock to manage expectations.
// Accepts options, like QueryMatcherOption, to match SQL query strings in more sophisticated ways.
func NewConn(options ...func(*pgxmock) error) (PgxConnIface, error) {
smock := &pgxmockConn{}
smock.ordered = true
return smock, smock.open(options)
}
func (c *pgxmockConn) Config() *pgx.ConnConfig {
return &pgx.ConnConfig{}
}
type pgxmockPool struct {
pgxmock
}
// NewPool creates PgxPoolIface pool of database connections and a mock to manage expectations.
// Accepts options, like QueryMatcherOption, to match SQL query strings in more sophisticated ways.
func NewPool(options ...func(*pgxmock) error) (PgxPoolIface, error) {
smock := &pgxmockPool{}
smock.ordered = true
return smock, smock.open(options)
}
func (p *pgxmockPool) Close() {
p.pgxmock.Close(context.Background())
}
func (p *pgxmockPool) Acquire(context.Context) (*pgxpool.Conn, error) {
return nil, errors.New("pgpool.Acquire() method is not implemented")
}
func (p *pgxmockPool) Config() *pgxpool.Config {
return &pgxpool.Config{}
}
// AsConn is similar to Acquire but returns proper mocking interface
func (p *pgxmockPool) AsConn() PgxConnIface {
return &pgxmockConn{pgxmock: p.pgxmock}
}
func (p *pgxmockPool) Stat() *pgxpool.Stat {
return &pgxpool.Stat{}
}