forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_builder_test.go
74 lines (61 loc) · 1.45 KB
/
insert_builder_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
package db
import (
"testing"
"github.com/stellar/go/support/db/dbtest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInsertBuilder_Exec(t *testing.T) {
db := dbtest.Postgres(t).Load(testSchema)
defer db.Close()
sess := &Session{DB: db.Open()}
defer sess.DB.Close()
tbl := sess.GetTable("people")
_, err := tbl.Insert(person{
Name: "bubba",
HungerLevel: "120",
}).Exec()
if assert.NoError(t, err) {
var found []person
err := sess.SelectRaw(
&found,
"SELECT * FROM people WHERE name = ?",
"bubba",
)
require.NoError(t, err)
if assert.Len(t, found, 1) {
assert.Equal(t, "bubba", found[0].Name)
assert.Equal(t, "120", found[0].HungerLevel)
}
}
// no rows
_, err = tbl.Insert().Exec()
if assert.Error(t, err) {
assert.IsType(t, &NoRowsError{}, err)
assert.EqualError(t, err, "no rows provided to insert")
}
// multi rows
r, err := tbl.Insert(person{
Name: "bubba2",
HungerLevel: "120",
}, person{
Name: "bubba3",
HungerLevel: "120",
}).Exec()
if assert.NoError(t, err) {
count, err := r.RowsAffected()
require.NoError(t, err)
assert.Equal(t, int64(2), count)
}
// invalid columns in struct
_, err = tbl.Insert(struct {
Name string `db:"name"`
HungerLevel string `db:"hunger_level"`
NotAColumn int `db:"not_a_column"`
}{
Name: "bubba2",
HungerLevel: "120",
NotAColumn: 3,
}).Exec()
assert.Error(t, err)
}