-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_table_insert_test.go
88 lines (81 loc) · 2.13 KB
/
db_table_insert_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
package lorm
import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/lontten/lorm/types"
"github.com/stretchr/testify/assert"
"testing"
)
type UserP struct {
Id *int
Name *string
}
func TestCreate(t *testing.T) {
as := assert.New(t)
db, mock, err := sqlmock.New()
as.Nil(err, "new sqlmock error")
engine := MustConnectMock(db, &PgConf{})
mock.ExpectQuery("user_p *").
WithArgs(2, "up").
WillReturnError(nil).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(88))
user := UserP{
Id: types.NewInt(2),
Name: types.NewString("up"),
}
num, err := Insert(engine, &user, E().ShowSql())
as.Nil(err)
as.Equal(int64(1), num)
as.Equal(88, *user.Id)
as.Nil(mock.ExpectationsWereMet(), "we make sure that all expectations were met")
}
//func _TestCreateOrUpdate(t *testing.T) {
// as := assert.New(t)
// ldb, mock, err := sqlmock.New()
// as.Nil(err, "new sqlmock error")
// engine := MustConnectMock(ldb, &PgConf{})
//
// //-----------------create--------------------------
// mock.ExpectQuery("INSERT *").
// WithArgs(2, "add", "add").
// WillReturnError(nil).
// WillReturnRows(sqlmock.NewRows([]string{"id"}).
// AddRow(1),
// )
//
// user := UserP{
// Name: types.NewString("add"),
// Id: types.NewInt(2),
// }
// num, err := engine.InsertOrUpdate(&user).ByPrimaryKey()
// as.Nil(err)
// as.Equal(int64(1), num)
// as.Equal(1, *user.Id)
//
// as.Nil(mock.ExpectationsWereMet(), "we make sure that all expectations were met")
//}
//
//func _TestCreateOrUpdate2(t *testing.T) {
// as := assert.New(t)
// ldb, mock, err := sqlmock.New()
// as.Nil(err, "new sqlmock error")
// engine := MustConnectMock(ldb, &PgConf{})
//
// //-----------------update--------------------------
// mock.ExpectQuery("INSERT*").
// WithArgs(1, "upd", "upd").
// WillReturnError(nil).
// WillReturnRows(sqlmock.NewRows([]string{"id"}).
// AddRow(2),
// )
//
// user := UserP{
// Name: types.NewString("upd"),
// Id: types.NewInt(2),
// }
// num, err := engine.InsertOrUpdate(&user).ByPrimaryKey()
// as.Nil(err)
// as.Equal(int64(2), num)
// as.Equal(2, *user.Id)
//
// as.Nil(mock.ExpectationsWereMet(), "we make sure that all expectations were met")
//}