-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable_insert8_test.go
More file actions
73 lines (62 loc) · 2.04 KB
/
table_insert8_test.go
File metadata and controls
73 lines (62 loc) · 2.04 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
package ldb
import (
"fmt"
"regexp"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
)
func TestInsert8_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, age) VALUES (?, ?) AS new ON DUPLICATE KEY UPDATE name = new.name, age = new.age, id = LAST_INSERT_ID(id);")).
WithArgs("tom", 22).
WillReturnResult(sqlmock.NewResult(10, 1))
var u = User{
Id: 0,
Name: "tom",
Age: 22,
}
num, err := Insert(engine, &u, E().ShowSql().WhenDuplicateKey().DoUpdate())
as.Nil(err)
as.Equal(int64(1), num, "num error")
as.Equal(int64(10), u.Id, "id error")
}
func TestInsert8_mysql2(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 (?) AS new ON DUPLICATE KEY UPDATE name = new.name, id = LAST_INSERT_ID(id);")).
WithArgs("tom").
WillReturnResult(sqlmock.NewResult(10, 1))
var u = User{
Id: 0,
Name: "tom",
}
num, err := Insert(engine, &u, E().ShowSql().WhenDuplicateKey().DoUpdate())
as.Nil(err)
as.Equal(int64(1), num, "num error")
as.Equal(int64(10), u.Id, "id error")
}
func TestInsert8_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) ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name RETURNING id;")).
WithArgs("tom").
WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow(10),
)
var u = User{
Id: 0,
Name: "tom",
}
num, err := Insert(engine, &u, E().ShowSql().WhenDuplicateKey().DoUpdate())
as.Nil(err)
as.Equal(int64(1), num, "num error")
as.Equal(int64(10), u.Id, "id error")
}