-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
loader_test.go
158 lines (129 loc) · 3.17 KB
/
loader_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
156
157
158
package pg_test
import (
"errors"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
"github.com/go-pg/pg/v10/types"
. "gopkg.in/check.v1"
)
type LoaderTest struct {
db *pg.DB
}
var _ = Suite(&LoaderTest{})
func (t *LoaderTest) SetUpTest(c *C) {
t.db = pg.Connect(pgOptions())
}
func (t *LoaderTest) TearDownTest(c *C) {
c.Assert(t.db.Close(), IsNil)
}
type numLoader struct {
Num int
}
type embeddedLoader struct {
*numLoader
Num2 int
}
type multipleLoader struct {
One struct {
Num int
}
Num int
}
func (t *LoaderTest) TestQuery(c *C) {
var dst numLoader
_, err := t.db.Query(&dst, "SELECT 1 AS num")
c.Assert(err, IsNil)
c.Assert(dst.Num, Equals, 1)
}
func (t *LoaderTest) TestQueryNull(c *C) {
var dst numLoader
_, err := t.db.Query(&dst, "SELECT NULL AS num")
c.Assert(err, IsNil)
c.Assert(dst.Num, Equals, 0)
}
func (t *LoaderTest) TestQueryEmbeddedStruct(c *C) {
src := &embeddedLoader{
numLoader: &numLoader{
Num: 1,
},
Num2: 2,
}
dst := &embeddedLoader{
numLoader: &numLoader{},
}
_, err := t.db.QueryOne(dst, "SELECT ?num AS num, ?num2 as num2", src)
c.Assert(err, IsNil)
c.Assert(dst, DeepEquals, src)
}
func (t *LoaderTest) TestQueryNestedStructs(c *C) {
src := &multipleLoader{}
src.One.Num = 1
src.Num = 2
dst := &multipleLoader{}
_, err := t.db.QueryOne(dst, `SELECT ?one__num AS one__num, ?num as num`, src)
c.Assert(err, IsNil)
c.Assert(dst, DeepEquals, src)
}
func (t *LoaderTest) TestQueryStmt(c *C) {
stmt, err := t.db.Prepare("SELECT 1 AS num")
c.Assert(err, IsNil)
defer stmt.Close()
dst := &numLoader{}
_, err = stmt.Query(dst)
c.Assert(err, IsNil)
c.Assert(dst.Num, Equals, 1)
}
func (t *LoaderTest) TestQueryInts(c *C) {
var ids pg.Ints
_, err := t.db.Query(&ids, "SELECT s.num AS num FROM generate_series(0, 10) AS s(num)")
c.Assert(err, IsNil)
c.Assert(ids, DeepEquals, pg.Ints{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
}
func (t *LoaderTest) TestQueryInts2(c *C) {
var ints pg.Ints
_, err := t.db.Query(&ints, "SELECT * FROM generate_series(1, 1000000)")
c.Assert(err, IsNil)
c.Assert(ints, HasLen, 1000000)
}
func (t *LoaderTest) TestQueryStrings(c *C) {
var strings pg.Strings
_, err := t.db.Query(&strings, "SELECT 'hello'")
c.Assert(err, IsNil)
c.Assert(strings, DeepEquals, pg.Strings{"hello"})
}
type errLoader struct {
err error
}
var _ orm.HooklessModel = (*errLoader)(nil)
func newErrLoader(err error) *errLoader {
return &errLoader{
err: err,
}
}
func (m *errLoader) Init() error {
return nil
}
func (m *errLoader) NextColumnScanner() orm.ColumnScanner {
return m
}
func (m *errLoader) AddColumnScanner(_ orm.ColumnScanner) error {
return nil
}
func (m *errLoader) ScanColumn(types.ColumnInfo, types.Reader, int) error {
return m.err
}
func (t *LoaderTest) TestLoaderError(c *C) {
tx, err := t.db.Begin()
c.Assert(err, IsNil)
defer tx.Rollback()
loader := newErrLoader(errors.New("my error"))
_, err = tx.QueryOne(loader, "SELECT 1, 2")
c.Assert(err, Not(IsNil))
c.Assert(err.Error(), Equals, "my error")
// Verify that client is still functional.
var n1, n2 int
_, err = tx.QueryOne(pg.Scan(&n1, &n2), "SELECT 1, 2")
c.Assert(err, IsNil)
c.Assert(n1, Equals, 1)
c.Assert(n2, Equals, 2)
}