-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbinder_test.go
84 lines (67 loc) · 2.03 KB
/
binder_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
package orm
import (
"database/sql"
"testing"
"time"
"github.com/DATA-DOG/go-sqlmock"
_ "github.com/lib/pq"
"github.com/stretchr/testify/assert"
)
type User struct {
ID int64
Name string
Timestamps
}
func (u User) ConfigureEntity(e *EntityConfigurator) {
e.Table("users")
}
type Address struct {
ID int
Path string
}
func TestBind(t *testing.T) {
t.Run("single result", func(t *testing.T) {
db, mock, err := sqlmock.New()
assert.NoError(t, err)
mock.
ExpectQuery("SELECT .* FROM users").
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "created_at", "updated_at", "deleted_at"}).
AddRow(1, "amirreza", sql.NullTime{Time: time.Now(), Valid: true}, sql.NullTime{Time: time.Now(), Valid: true}, sql.NullTime{}))
rows, err := db.Query(`SELECT * FROM users`)
assert.NoError(t, err)
u := &User{}
md := schemaOfHeavyReflectionStuff(u)
err = newBinder(md).bind(rows, u)
assert.NoError(t, err)
assert.Equal(t, "amirreza", u.Name)
})
t.Run("multi result", func(t *testing.T) {
db, mock, err := sqlmock.New()
assert.NoError(t, err)
mock.
ExpectQuery("SELECT .* FROM users").
WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(1, "amirreza").AddRow(2, "milad"))
rows, err := db.Query(`SELECT * FROM users`)
assert.NoError(t, err)
md := schemaOfHeavyReflectionStuff(&User{})
var users []*User
err = newBinder(md).bind(rows, &users)
assert.NoError(t, err)
assert.Equal(t, "amirreza", users[0].Name)
assert.Equal(t, "milad", users[1].Name)
})
}
func TestBindMap(t *testing.T) {
db, mock, err := sqlmock.New()
assert.NoError(t, err)
mock.
ExpectQuery("SELECT .* FROM users").
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "created_at", "updated_at", "deleted_at"}).
AddRow(1, "amirreza", sql.NullTime{Time: time.Now(), Valid: true}, sql.NullTime{Time: time.Now(), Valid: true}, sql.NullTime{}))
rows, err := db.Query(`SELECT * FROM users`)
assert.NoError(t, err)
ms, err := bindToMap(rows)
assert.NoError(t, err)
assert.NotEmpty(t, ms)
assert.Len(t, ms, 1)
}