-
Notifications
You must be signed in to change notification settings - Fork 27
/
raw_sql_test.go
75 lines (64 loc) · 2.16 KB
/
raw_sql_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
package godb
import (
"database/sql"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestRawSQLDo(t *testing.T) {
Convey("Given a test database", t, func() {
db := fixturesSetup(t)
defer db.Close()
Convey("Do execute the raw query and fills a given instance", func() {
singleDummy := Dummy{}
err := db.RawSQL("select * from dummies where an_integer = ?", 12).Do(&singleDummy)
So(err, ShouldBeNil)
So(singleDummy.ID, ShouldBeGreaterThan, 0)
So(singleDummy.AText, ShouldEqual, "Second")
So(singleDummy.AnotherText, ShouldEqual, "Second")
So(singleDummy.AnInteger, ShouldEqual, 12)
})
Convey("Do returns sql.sql.ErrNoRows if a single instance if requested but not found", func() {
dummy := Dummy{}
err := db.RawSQL("select * from dummies where an_integer = 123").Do(&dummy)
So(err, ShouldEqual, sql.ErrNoRows)
})
Convey("Do execute the query and fills a slice", func() {
dummiesSlice := make([]Dummy, 0)
err := db.RawSQL("select * from dummies").Do(&dummiesSlice)
So(err, ShouldBeNil)
So(len(dummiesSlice), ShouldEqual, 3)
So(dummiesSlice[0].ID, ShouldBeGreaterThan, 0)
So(dummiesSlice[0].AText, ShouldEqual, "First")
So(dummiesSlice[0].AnotherText, ShouldEqual, "Premier")
So(dummiesSlice[0].AnInteger, ShouldEqual, 11)
So(dummiesSlice[1].AnInteger, ShouldEqual, 12)
So(dummiesSlice[2].AnInteger, ShouldEqual, 13)
})
})
}
func TestRawSQLDoWithIterator(t *testing.T) {
Convey("Given a test database", t, func() {
db := fixturesSetup(t)
defer db.Close()
Convey("DoWithIterator executes the query and returns an iterator", func() {
iter, err := db.RawSQL("select * from dummies order by an_integer").DoWithIterator()
So(err, ShouldBeNil)
defer iter.Close()
count := 0
for iter.Next() {
count++
singleDummy := Dummy{}
err := iter.Scan(&singleDummy)
So(err, ShouldBeNil)
if count == 1 {
So(singleDummy.ID, ShouldBeGreaterThan, 0)
So(singleDummy.AText, ShouldEqual, "First")
So(singleDummy.AnotherText, ShouldEqual, "Premier")
So(singleDummy.AnInteger, ShouldEqual, 11)
}
}
So(count, ShouldEqual, 3)
So(iter.Err(), ShouldBeNil)
})
})
}