-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_insert7_test.go
More file actions
54 lines (46 loc) · 1.24 KB
/
table_insert7_test.go
File metadata and controls
54 lines (46 loc) · 1.24 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
package ldb
import (
"fmt"
"regexp"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
)
func TestInsert7_mysql(t *testing.T) {
as := assert.New(t)
db, mock, err := sqlmock.New()
as.Nil(err, fmt.Sprintf("failed to open sqlmock database: %s", err))
engine := MustConnectMock(db, &MysqlConf{})
mock.ExpectExec(regexp.QuoteMeta("INSERT INTO t_user (name) VALUES (?);")).
WithArgs("tom").
WillReturnError(nil).
WillReturnResult(sqlmock.NewResult(10, 1))
var u = User{
Id: 0,
Name: "tom",
}
num, err := Insert(engine, &u, E().ShowSql())
as.Nil(err)
as.Equal(int64(1), num, "num error")
as.Equal(int64(10), u.Id, "id error")
}
func TestInsert7_pg(t *testing.T) {
as := assert.New(t)
db, mock, err := sqlmock.New()
as.Nil(err, fmt.Sprintf("failed to open sqlmock database: %s", err))
engine := MustConnectMock(db, &PgConf{})
mock.ExpectQuery(regexp.QuoteMeta("INSERT INTO t_user (name) VALUES ($1) RETURNING id;")).
WithArgs("tom").
WillReturnError(nil).
WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow(10),
)
var u = User{
Id: 0,
Name: "tom",
}
num, err := Insert(engine, &u, E().ShowSql())
as.Nil(err)
as.Equal(int64(1), num, "num error")
as.Equal(int64(10), u.Id, "id error")
}