-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.go
62 lines (45 loc) · 1.1 KB
/
create.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
package crud
import (
"regexp"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/sanity-io/litter"
mocks "github.com/simiancreative/simiango/mocks/data/sql"
)
func (m *Model) Create(params interface{}, destination interface{}) error {
ds := m.dialect.Insert(m.Table).Rows(params)
query, qParams, _ := ds.ToSQL()
result, err := m.cx.Exec(query, qParams...)
if err != nil {
return err
}
id, err := result.LastInsertId()
if err != nil {
return err
}
if err := m.One(destination, id); err != nil {
return err
}
return nil
}
func (m *Model) SetupCreateTest(
t *testing.T,
params interface{},
) func() error {
db, mock, clos := mocks.SetupAndMock("mysql", t)
m.Initialize("mysql", db)
ds := m.dialect.Insert(m.Table).Rows(params)
query, _, _ := ds.ToSQL()
mock.
ExpectExec(regexp.QuoteMeta(query)).
WillReturnResult(sqlmock.NewResult(456, 1))
oneDs := m.oneQuery(456)
oneQuery, _, _ := oneDs.ToSQL()
litter.Dump(oneQuery)
rows := sqlmock.NewRows([]string{"id"}).AddRow(456)
mock.
ExpectQuery(regexp.QuoteMeta(oneQuery)).
WillReturnRows(rows)
m.cx = db
return clos
}