-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmock.go
More file actions
67 lines (55 loc) · 1.21 KB
/
mock.go
File metadata and controls
67 lines (55 loc) · 1.21 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
package sqlmock
import (
"errors"
"sync"
)
type (
// Option configures Mock behavior.
Option func(*Mock)
// Mock stores queued SQL and transaction expectations for a mock session.
Mock struct {
mu sync.Mutex
ops []expectedOp
strictTx bool
}
expectedOp interface {
match(op runtimeOp) error
}
runtimeOp struct {
kind string
sql string
args []any
}
)
// NewMock creates an empty mock expectation registry.
//
// By default, strict transaction mode is disabled.
func NewMock(opts ...Option) *Mock {
m := &Mock{
strictTx: false,
}
for _, opt := range opts {
opt(m)
}
return m
}
// WithStrictTx enables transaction enforcement for query and exec operations.
//
// When enabled, QueryContext and ExecContext return an error unless a
// transaction has been started with Begin/BeginTx.
func WithStrictTx() Option {
return func(m *Mock) {
m.strictTx = true
}
}
// next returns the next expected operation without removing it.
func (m *Mock) next() (expectedOp, error) {
if len(m.ops) == 0 {
return nil, errors.New("no expectation remaining")
}
return m.ops[0], nil
}
// consume removes the current expected operation from the queue.
func (m *Mock) consume() {
m.ops = m.ops[1:]
}