-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx.go
More file actions
79 lines (63 loc) · 1.36 KB
/
tx.go
File metadata and controls
79 lines (63 loc) · 1.36 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
package sqlmock
import (
"errors"
"fmt"
)
// Tx implements driver.Tx behavior on top of queued mock expectations.
type Tx struct {
conn *Conn
}
// Commit validates and consumes the next commit expectation.
//
// Commit fails if there is no active transaction or the next queued
// expectation is not a commit.
func (t *Tx) Commit() error {
c := t.conn
m := c.mock
m.mu.Lock()
defer m.mu.Unlock()
if !c.txStarted {
return errors.New("commit without active transaction")
}
op, err := m.next()
if err != nil {
return err
}
e, ok := op.(*ExpectedCommit)
if !ok {
return fmt.Errorf("expected %T, got COMMIT", op)
}
if err := e.match(runtimeOp{kind: "commit"}); err != nil {
return err
}
m.consume()
c.txStarted = false
return nil
}
// Rollback validates and consumes the next rollback expectation.
//
// Rollback fails if there is no active transaction or the next queued
// expectation is not a rollback.
func (t *Tx) Rollback() error {
c := t.conn
m := c.mock
m.mu.Lock()
defer m.mu.Unlock()
if !c.txStarted {
return errors.New("rollback without active transaction")
}
op, err := m.next()
if err != nil {
return err
}
e, ok := op.(*ExpectedRollback)
if !ok {
return fmt.Errorf("expected %T, got ROLLBACK", op)
}
if err := e.match(runtimeOp{kind: "rollback"}); err != nil {
return err
}
m.consume()
c.txStarted = false
return nil
}