-
Notifications
You must be signed in to change notification settings - Fork 3
/
db_test.go
88 lines (65 loc) · 1.61 KB
/
db_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 yago_test
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestTx(t *testing.T) {
db, model, cleanup := initModel(t)
defer cleanup()
p := PersonStruct{FirstName: "Malcom"}
{
// Make an insert in a transaction and rollback it
tx, err := db.Begin()
assert.Nil(t, err)
assert.Nil(t, tx.Insert(&p))
exists, err := tx.Query(model.PersonStruct).Exists()
assert.Nil(t, err)
assert.True(t, exists)
tx.Rollback()
}
{
// Check that the insert was rolled back
exists, err := db.Query(model.PersonStruct).Exists()
assert.Nil(t, err)
assert.False(t, exists)
}
{
// Redo an insert in a transaction and commit it
tx, err := db.Begin()
assert.Nil(t, err)
assert.Nil(t, tx.Insert(&p))
exists, err := tx.Query(model.PersonStruct).Exists()
assert.Nil(t, err)
assert.True(t, exists)
tx.Commit()
}
{
// Check that the insert was committed
exists, err := db.Query(model.PersonStruct).Exists()
assert.Nil(t, err)
assert.True(t, exists)
}
}
func TestInsertWithReturning(t *testing.T) {
db, _, cleanup := initModelWithDriver(t, "postgres")
defer cleanup()
p := SimpleStruct{Name: "Test"}
assert.EqualValues(t, 0, p.ID)
assert.Nil(t, db.Insert(&p))
assert.EqualValues(t, 1, p.ID)
}
func TestMarshaledField(t *testing.T) {
db, model, cleanup := initModel(t)
defer cleanup()
p := PersonStruct{FirstName: "Martin", Gender: Male}
db.Insert(&p)
np := PersonStruct{}
err := db.Query(
model.PersonStruct,
).Filter(
model.PersonStruct.Gender.Eq(Male),
).One(&np)
assert.Nil(t, err)
assert.Equal(t, Male, np.Gender)
assert.Equal(t, "Martin", np.FirstName)
}