forked from go-rel/reltest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec_test.go
89 lines (73 loc) · 2.62 KB
/
exec_test.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package reltest
import (
"context"
"database/sql"
"testing"
"github.com/stretchr/testify/assert"
)
func TestExec(t *testing.T) {
var (
repo = New()
statement = "UPDATE users SET something = ? WHERE something2 = ?;"
args = []any{1, "2"}
)
repo.ExpectExec(statement, args).Result(1, 2)
lastInsertedId, rowsAffected, err := repo.Exec(context.TODO(), statement, args...)
assert.Nil(t, err)
assert.Equal(t, 1, lastInsertedId)
assert.Equal(t, 2, rowsAffected)
repo.AssertExpectations(t)
repo.ExpectExec(statement, args).Result(1, 2)
assert.NotPanics(t, func() {
lastInsertedId, rowsAffected := repo.MustExec(context.TODO(), statement, args...)
assert.Equal(t, 1, lastInsertedId)
assert.Equal(t, 2, rowsAffected)
})
repo.AssertExpectations(t)
}
func TestExec_error(t *testing.T) {
var (
repo = New()
statement = "UPDATE users SET something = ? WHERE something2 = ?;"
args = []any{1, "2"}
)
repo.ExpectExec(statement, args).ConnectionClosed()
lastInsertedId, rowsAffected, err := repo.Exec(context.TODO(), statement, args...)
assert.Equal(t, sql.ErrConnDone, err)
assert.Equal(t, 0, lastInsertedId)
assert.Equal(t, 0, rowsAffected)
repo.AssertExpectations(t)
repo.ExpectExec(statement, args).ConnectionClosed()
assert.Panics(t, func() {
repo.MustExec(context.TODO(), statement, args...)
})
repo.AssertExpectations(t)
}
func TestExec_assert(t *testing.T) {
var (
repo = New()
)
repo.ExpectExec("UPDATE users SET something = ? WHERE something2 = ?;", 1, 2)
assert.Panics(t, func() {
repo.Exec(context.TODO(), "UPDATE users SET something = ? WHERE something2 = ?;", 1, 2)
})
assert.False(t, repo.AssertExpectations(nt))
assert.Equal(t, "FAIL: Mock defined but not called:\nExec(ctx, \"UPDATE users SET something = ? WHERE something2 = ?;\", 1, 2)", nt.lastLog)
}
func TestExec_assert_transaction(t *testing.T) {
var (
repo = New()
)
repo.ExpectTransaction(func(repo *Repository) {
repo.ExpectExec("UPDATE users SET something = ? WHERE something2 = ?;", 1, 2)
})
assert.False(t, repo.AssertExpectations(nt))
assert.Equal(t, "FAIL: Mock defined but not called:\n<Transaction: 1> Exec(ctx, \"UPDATE users SET something = ? WHERE something2 = ?;\", 1, 2)", nt.lastLog)
}
func TestExec_String(t *testing.T) {
var (
mockExec = MockExec{assert: &Assert{}, argStatement: "UPDATE users SET something = ? WHERE something2 = ?;", argArgs: []any{1, 2}}
)
assert.Equal(t, "Exec(ctx, \"UPDATE users SET something = ? WHERE something2 = ?;\", 1, 2)", mockExec.String())
assert.Equal(t, "ExpectString(\"UPDATE users SET something = ? WHERE something2 = ?;\", 1, 2)", mockExec.ExpectString())
}