|
1 | 1 | package sqlmock
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "database/sql/driver" |
5 |
| - "fmt" |
| 4 | + "database/sql/driver" |
| 5 | + "fmt" |
6 | 6 | )
|
7 | 7 |
|
8 | 8 | type conn struct {
|
9 |
| - expectations []expectation |
10 |
| - active expectation |
| 9 | + expectations []expectation |
| 10 | + active expectation |
11 | 11 | }
|
12 | 12 |
|
13 | 13 | // Close a mock database driver connection. It should
|
14 | 14 | // be always called to ensure that all expectations
|
15 | 15 | // were met successfully. Returns error if there is any
|
16 | 16 | func (c *conn) Close() (err error) {
|
17 |
| - for _, e := range mock.conn.expectations { |
18 |
| - if !e.fulfilled() { |
19 |
| - err = fmt.Errorf("there is a remaining expectation %T which was not matched yet", e) |
20 |
| - break |
21 |
| - } |
22 |
| - } |
23 |
| - mock.conn.expectations = []expectation{} |
24 |
| - mock.conn.active = nil |
25 |
| - return err |
| 17 | + for _, e := range mock.conn.expectations { |
| 18 | + if !e.fulfilled() { |
| 19 | + err = fmt.Errorf("there is a remaining expectation %T which was not matched yet", e) |
| 20 | + break |
| 21 | + } |
| 22 | + } |
| 23 | + mock.conn.expectations = []expectation{} |
| 24 | + mock.conn.active = nil |
| 25 | + return err |
26 | 26 | }
|
27 | 27 |
|
28 | 28 | func (c *conn) Begin() (driver.Tx, error) {
|
29 |
| - e := c.next() |
30 |
| - if e == nil { |
31 |
| - return nil, fmt.Errorf("all expectations were already fulfilled, call to begin transaction was not expected") |
32 |
| - } |
33 |
| - |
34 |
| - etb, ok := e.(*expectedBegin) |
35 |
| - if !ok { |
36 |
| - return nil, fmt.Errorf("call to begin transaction, was not expected, next expectation is %T as %+v", e, e) |
37 |
| - } |
38 |
| - etb.triggered = true |
39 |
| - return &transaction{c}, etb.err |
| 29 | + e := c.next() |
| 30 | + if e == nil { |
| 31 | + return nil, fmt.Errorf("all expectations were already fulfilled, call to begin transaction was not expected") |
| 32 | + } |
| 33 | + |
| 34 | + etb, ok := e.(*expectedBegin) |
| 35 | + if !ok { |
| 36 | + return nil, fmt.Errorf("call to begin transaction, was not expected, next expectation is %T as %+v", e, e) |
| 37 | + } |
| 38 | + etb.triggered = true |
| 39 | + return &transaction{c}, etb.err |
40 | 40 | }
|
41 | 41 |
|
42 | 42 | // get next unfulfilled expectation
|
43 | 43 | func (c *conn) next() (e expectation) {
|
44 |
| - for _, e = range c.expectations { |
45 |
| - if !e.fulfilled() { |
46 |
| - return |
47 |
| - } |
48 |
| - } |
49 |
| - return nil // all expectations were fulfilled |
| 44 | + for _, e = range c.expectations { |
| 45 | + if !e.fulfilled() { |
| 46 | + return |
| 47 | + } |
| 48 | + } |
| 49 | + return nil // all expectations were fulfilled |
50 | 50 | }
|
51 | 51 |
|
52 | 52 | func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
|
53 |
| - e := c.next() |
54 |
| - query = stripQuery(query) |
55 |
| - if e == nil { |
56 |
| - return nil, fmt.Errorf("all expectations were already fulfilled, call to exec '%s' query with args %+v was not expected", query, args) |
57 |
| - } |
58 |
| - |
59 |
| - eq, ok := e.(*expectedExec) |
60 |
| - if !ok { |
61 |
| - return nil, fmt.Errorf("call to exec query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e) |
62 |
| - } |
63 |
| - |
64 |
| - eq.triggered = true |
65 |
| - if eq.err != nil { |
66 |
| - return nil, eq.err // mocked to return error |
67 |
| - } |
68 |
| - |
69 |
| - if eq.result == nil { |
70 |
| - return nil, fmt.Errorf("exec query '%s' with args %+v, must return a database/sql/driver.result, but it was not set for expectation %T as %+v", query, args, eq, eq) |
71 |
| - } |
72 |
| - |
73 |
| - if !eq.queryMatches(query) { |
74 |
| - return nil, fmt.Errorf("exec query '%s', does not match regex '%s'", query, eq.sqlRegex.String()) |
75 |
| - } |
76 |
| - |
77 |
| - if !eq.argsMatches(args) { |
78 |
| - return nil, fmt.Errorf("exec query '%s', args %+v does not match expected %+v", query, args, eq.args) |
79 |
| - } |
80 |
| - |
81 |
| - return eq.result, nil |
| 53 | + e := c.next() |
| 54 | + query = stripQuery(query) |
| 55 | + if e == nil { |
| 56 | + return nil, fmt.Errorf("all expectations were already fulfilled, call to exec '%s' query with args %+v was not expected", query, args) |
| 57 | + } |
| 58 | + |
| 59 | + eq, ok := e.(*expectedExec) |
| 60 | + if !ok { |
| 61 | + return nil, fmt.Errorf("call to exec query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e) |
| 62 | + } |
| 63 | + |
| 64 | + eq.triggered = true |
| 65 | + if eq.err != nil { |
| 66 | + return nil, eq.err // mocked to return error |
| 67 | + } |
| 68 | + |
| 69 | + if eq.result == nil { |
| 70 | + return nil, fmt.Errorf("exec query '%s' with args %+v, must return a database/sql/driver.result, but it was not set for expectation %T as %+v", query, args, eq, eq) |
| 71 | + } |
| 72 | + |
| 73 | + if !eq.queryMatches(query) { |
| 74 | + return nil, fmt.Errorf("exec query '%s', does not match regex '%s'", query, eq.sqlRegex.String()) |
| 75 | + } |
| 76 | + |
| 77 | + if !eq.argsMatches(args) { |
| 78 | + return nil, fmt.Errorf("exec query '%s', args %+v does not match expected %+v", query, args, eq.args) |
| 79 | + } |
| 80 | + |
| 81 | + return eq.result, nil |
82 | 82 | }
|
83 | 83 |
|
84 | 84 | func (c *conn) Prepare(query string) (driver.Stmt, error) {
|
85 |
| - return &statement{mock.conn, stripQuery(query)}, nil |
| 85 | + return &statement{mock.conn, stripQuery(query)}, nil |
86 | 86 | }
|
87 | 87 |
|
88 | 88 | func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
|
89 |
| - e := c.next() |
90 |
| - query = stripQuery(query) |
91 |
| - if e == nil { |
92 |
| - return nil, fmt.Errorf("all expectations were already fulfilled, call to query '%s' with args %+v was not expected", query, args) |
93 |
| - } |
94 |
| - |
95 |
| - eq, ok := e.(*expectedQuery) |
96 |
| - if !ok { |
97 |
| - return nil, fmt.Errorf("call to query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e) |
98 |
| - } |
99 |
| - |
100 |
| - eq.triggered = true |
101 |
| - if eq.err != nil { |
102 |
| - return nil, eq.err // mocked to return error |
103 |
| - } |
104 |
| - |
105 |
| - if eq.rows == nil { |
106 |
| - return nil, fmt.Errorf("query '%s' with args %+v, must return a database/sql/driver.rows, but it was not set for expectation %T as %+v", query, args, eq, eq) |
107 |
| - } |
108 |
| - |
109 |
| - if !eq.queryMatches(query) { |
110 |
| - return nil, fmt.Errorf("query '%s', does not match regex [%s]", query, eq.sqlRegex.String()) |
111 |
| - } |
112 |
| - |
113 |
| - if !eq.argsMatches(args) { |
114 |
| - return nil, fmt.Errorf("query '%s', args %+v does not match expected %+v", query, args, eq.args) |
115 |
| - } |
116 |
| - |
117 |
| - return eq.rows, nil |
| 89 | + e := c.next() |
| 90 | + query = stripQuery(query) |
| 91 | + if e == nil { |
| 92 | + return nil, fmt.Errorf("all expectations were already fulfilled, call to query '%s' with args %+v was not expected", query, args) |
| 93 | + } |
| 94 | + |
| 95 | + eq, ok := e.(*expectedQuery) |
| 96 | + if !ok { |
| 97 | + return nil, fmt.Errorf("call to query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e) |
| 98 | + } |
| 99 | + |
| 100 | + eq.triggered = true |
| 101 | + if eq.err != nil { |
| 102 | + return nil, eq.err // mocked to return error |
| 103 | + } |
| 104 | + |
| 105 | + if eq.rows == nil { |
| 106 | + return nil, fmt.Errorf("query '%s' with args %+v, must return a database/sql/driver.rows, but it was not set for expectation %T as %+v", query, args, eq, eq) |
| 107 | + } |
| 108 | + |
| 109 | + if !eq.queryMatches(query) { |
| 110 | + return nil, fmt.Errorf("query '%s', does not match regex [%s]", query, eq.sqlRegex.String()) |
| 111 | + } |
| 112 | + |
| 113 | + if !eq.argsMatches(args) { |
| 114 | + return nil, fmt.Errorf("query '%s', args %+v does not match expected %+v", query, args, eq.args) |
| 115 | + } |
| 116 | + |
| 117 | + return eq.rows, nil |
118 | 118 | }
|
0 commit comments