-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbutil_test.go
103 lines (95 loc) · 3.76 KB
/
dbutil_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
package dbutil
import (
"testing"
"github.com/recursionpharma/ghost-postgres"
. "github.com/smartystreets/goconvey/convey"
)
func TestConnect(t *testing.T) {
Convey("Given a dbURL", t, func() {
Convey("If we can't get a driver", func() {
db, err := Connect("foo")
Convey("No driver should be returned", func() { So(db, ShouldBeNil) })
Convey("An error should be returned", func() { So(err, ShouldNotBeNil) })
Convey("The error message should contain...", func() { So(err.Error(), ShouldContainSubstring, "is missing '://'") })
})
Convey("If we can't open a connection to the DB", func() {
db, err := Connect("foo://bar")
Convey("No driver should be returned", func() { So(db, ShouldBeNil) })
Convey("An error should be returned", func() { So(err, ShouldNotBeNil) })
Convey("The error message should contain...", func() { So(err.Error(), ShouldContainSubstring, "unknown driver") })
})
Convey("If we can't connectto the DB", func() {
db, err := Connect("postgres://bar")
Convey("No driver should be returned", func() { So(db, ShouldBeNil) })
Convey("An error should be returned", func() { So(err, ShouldNotBeNil) })
Convey("The error message should contain...", func() { So(err.Error(), ShouldContainSubstring, "no such host") })
})
Convey("If we can connect to the DB", func() {
gp := ghost_postgres.New()
defer gp.Terminate()
if err := gp.Prepare(); err != nil {
t.Fatal(err)
}
db, err := Connect(gp.URL())
Convey("A driver should be returned", func() { So(db, ShouldNotBeNil) })
Convey("No error should be returned", func() { So(err, ShouldBeNil) })
})
})
}
func TestGetDriver(t *testing.T) {
Convey("Given a dbURL", t, func() {
Convey("If it doesn't contain '://'", func() {
driver, err := GetDriver("foo")
Convey("No driver should be returned", func() { So(driver, ShouldBeEmpty) })
Convey("An error should be returned", func() { So(err, ShouldNotBeNil) })
Convey("The error message should contain...", func() { So(err.Error(), ShouldContainSubstring, "is missing '://'") })
})
Convey("If the driver is empty", func() {
driver, err := GetDriver("://")
Convey("No driver should be returned", func() { So(driver, ShouldBeEmpty) })
Convey("An error should be returned", func() { So(err, ShouldNotBeNil) })
Convey("The error message should contain...", func() { So(err.Error(), ShouldContainSubstring, "is missing a driver") })
})
Convey("If the driver is valid", func() {
driver, err := GetDriver("foo://")
Convey("The driver should be returned", func() { So(driver, ShouldEqual, "foo") })
Convey("No error should be returned", func() { So(err, ShouldBeNil) })
})
})
}
func TestExists(t *testing.T) {
gp := ghost_postgres.New()
defer gp.Terminate()
if err := gp.Prepare(); err != nil {
t.Fatal(err)
}
db, err := Connect(gp.URL())
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`
CREATE TABLE foo (
bar VARCHAR(10)
);
`); err != nil {
t.Fatal(err)
}
Convey("If the query returns rows", t, func() {
if _, err := db.Exec("INSERT INTO foo ( bar ) VALUES ( 'baz' );"); err != nil {
t.Fatal(err)
}
b, err := Exists(db, "SELECT 1 FROM foo WHERE bar = 'baz'")
Convey("No error should be returned", func() { So(err, ShouldBeNil) })
Convey("It returns true", func() { So(b, ShouldBeTrue) })
})
Convey("If the query doesn't return rows", t, func() {
b, err := Exists(db, "SELECT 1 FROM foo WHERE bar = 'quux'")
Convey("No error should be returned", func() { So(err, ShouldBeNil) })
Convey("It returns false", func() { So(b, ShouldBeFalse) })
})
Convey("If the query errors", t, func() {
b, err := Exists(db, "kaboom!")
Convey("An error should be returned", func() { So(err, ShouldNotBeNil) })
Convey("Result should be false", func() { So(b, ShouldBeFalse) })
})
}