generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_test.go
73 lines (62 loc) · 1.88 KB
/
database_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
package sqltest_test
import (
"context"
"time"
. "github.com/dogmatiq/sqltest"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
var _ = Describe("type Database", func() {
Describe("func NewDatabase()", func() {
DescribeTable(
"it returns DSN that can be used to open a database",
func(d Driver, p Product) {
if !d.IsAvailable() {
Skip("driver is not available")
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
dsn, err := NewDatabase(ctx, d, p)
Expect(err).ShouldNot(HaveOccurred())
defer dsn.Close()
db, err := dsn.Open()
Expect(err).ShouldNot(HaveOccurred())
err = db.PingContext(ctx)
Expect(err).ShouldNot(HaveOccurred())
err = dsn.Close()
Expect(err).ShouldNot(HaveOccurred())
// Database should be closed now, ping should fail.
err = db.PingContext(ctx)
Expect(err).Should(HaveOccurred())
},
Entry("MySQL using the 'mysql' driver", MySQLDriver, MySQL),
Entry("MariaDB using the 'mysql' driver", MySQLDriver, MariaDB),
Entry("PostgreSQL using the 'pgx' driver", PGXDriver, PostgreSQL),
Entry("PostgreSQL using the 'postgres' driver", PostgresDriver, PostgreSQL),
Entry("SQLite using the 'sqlite3' driver", SQLite3Driver, SQLite),
)
})
Describe("func Open()", func() {
DescribeTable(
"returns an error",
func(db *Database) {
_, err := db.Open()
Expect(err).To(MatchError("attempted to open a database pool for a closed database"))
},
Entry("nil pointer", (*Database)(nil)),
Entry("zero value", &Database{}),
)
})
Describe("func Close()", func() {
DescribeTable(
"does not return an error",
func(db *Database) {
err := db.Close()
Expect(err).ShouldNot(HaveOccurred())
},
Entry("nil pointer", (*Database)(nil)),
Entry("zero value", &Database{}),
)
})
})