forked from geoffreybauduin/yaorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbprovider_test.go
155 lines (137 loc) · 4.07 KB
/
dbprovider_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package yaorm_test
import (
"context"
"fmt"
"os"
"reflect"
"testing"
"github.com/geoffreybauduin/yaorm"
"github.com/geoffreybauduin/yaorm/testdata"
"github.com/geoffreybauduin/yaorm/yaormfilter"
"github.com/juju/errors"
"github.com/stretchr/testify/assert"
)
func TestDbprovider_Tx(t *testing.T) {
killDb, err := testdata.SetupTestDatabase("test")
defer killDb()
assert.Nil(t, err)
dbp, err := yaorm.NewDBProvider(context.TODO(), "test")
assert.Nil(t, err)
err = dbp.Tx()
assert.Nil(t, err)
m := &testdata.Category{Name: "category"}
m.SetDBP(dbp)
assert.Equal(t, int64(0), m.ID)
err = yaorm.GenericInsert(m)
assert.Nil(t, err)
dbp2, err := yaorm.NewDBProvider(context.TODO(), "test")
assert.Nil(t, err)
// does not exist outside of tx
_, err = yaorm.GenericSelectOne(dbp2, testdata.NewCategoryFilter().ID(yaormfilter.Equals(m.ID)))
assert.NotNil(t, err)
assert.True(t, errors.IsNotFound(err))
// exists in tx
_, err = yaorm.GenericSelectOne(dbp, testdata.NewCategoryFilter().ID(yaormfilter.Equals(m.ID)))
assert.Nil(t, err)
err = dbp.Commit()
assert.Nil(t, err)
// now it exists outside of tx
_, err = yaorm.GenericSelectOne(dbp2, testdata.NewCategoryFilter().ID(yaormfilter.Equals(m.ID)))
assert.Nil(t, err)
}
func TestDbprovider_Rollback(t *testing.T) {
killDb, err := testdata.SetupTestDatabase("test")
defer killDb()
assert.Nil(t, err)
dbp, err := yaorm.NewDBProvider(context.TODO(), "test")
assert.Nil(t, err)
err = dbp.Tx()
assert.Nil(t, err)
m := &testdata.Category{Name: "category"}
m.SetDBP(dbp)
assert.Equal(t, int64(0), m.ID)
err = yaorm.GenericInsert(m)
assert.Nil(t, err)
dbp2, err := yaorm.NewDBProvider(context.TODO(), "test")
assert.Nil(t, err)
// does not exist outside of tx
_, err = yaorm.GenericSelectOne(dbp2, testdata.NewCategoryFilter().ID(yaormfilter.Equals(m.ID)))
assert.NotNil(t, err)
assert.True(t, errors.IsNotFound(err))
// exists in tx
_, err = yaorm.GenericSelectOne(dbp, testdata.NewCategoryFilter().ID(yaormfilter.Equals(m.ID)))
assert.Nil(t, err)
err = dbp.Rollback()
assert.Nil(t, err)
// still does not exist outside of tx
_, err = yaorm.GenericSelectOne(dbp2, testdata.NewCategoryFilter().ID(yaormfilter.Equals(m.ID)))
assert.NotNil(t, err)
assert.True(t, errors.IsNotFound(err))
// inside old tx it does not exist either
_, err = yaorm.GenericSelectOne(dbp, testdata.NewCategoryFilter().ID(yaormfilter.Equals(m.ID)))
assert.NotNil(t, err)
assert.True(t, errors.IsNotFound(err))
}
func TestDBProvider_RunInTransaction(t *testing.T) {
killDb, err := testdata.SetupTestDatabase("test")
defer killDb()
assert.NoError(t, err)
dbp, err := yaorm.NewDBProvider(context.TODO(), "test")
assert.NoError(t, err)
errTx := dbp.RunInTransaction(func() error {
return nil
})
assert.NoError(t, errTx)
errTx2 := dbp.RunInTransaction(func() error {
return fmt.Errorf("test error")
})
assert.Error(t, errTx2)
assert.Equal(t, "test error", errTx2.Error())
}
func TestDBProvider_Postgres_OnSessionCreated(t *testing.T) {
if os.Getenv("DB") != "postgres" {
return
}
exec := &pgSpecificExecutor{
queries: make([]logParams, 0),
}
killDb, err := testdata.SetupPostgres("test", yaorm.PostgresSpecific{IntervalStyle: "iso_8601"}, exec)
defer killDb()
assert.NoError(t, err)
dbp, err := yaorm.NewDBProvider(context.TODO(), "test")
assert.NoError(t, err)
assert.Len(t, exec.queries, 1)
assert.True(t, reflect.DeepEqual(exec.queries, []logParams{
{
query: "SET intervalstyle = 'iso_8601'",
args: []interface{}{},
},
}))
_, err = dbp.DB().Exec("SELECT 1")
assert.NoError(t, err)
assert.Len(t, exec.queries, 2)
assert.True(t, reflect.DeepEqual(exec.queries, []logParams{
{
query: "SET intervalstyle = 'iso_8601'",
args: []interface{}{},
},
{
query: "SELECT 1",
args: nil,
},
}))
}
type pgSpecificExecutor struct {
yaorm.DefaultExecutorHook
queries []logParams
}
type logParams struct {
query string
args []interface{}
}
func (exec *pgSpecificExecutor) BeforeExec(ctx context.Context, query string, args ...interface{}) {
exec.queries = append(exec.queries, logParams{
query: query,
args: args,
})
}