generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprovider_test.go
149 lines (125 loc) · 3.45 KB
/
provider_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
//go:build cgo
// +build cgo
package sqlpersistence_test
import (
"context"
"database/sql"
"time"
"github.com/dogmatiq/sqltest"
"github.com/dogmatiq/sqltest/sqlstub"
"github.com/dogmatiq/verity/persistence"
"github.com/dogmatiq/verity/persistence/internal/providertest"
. "github.com/dogmatiq/verity/persistence/sqlpersistence"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"go.uber.org/multierr"
)
var _ = Describe("type Provider", func() {
var (
database *sqltest.Database
db *sql.DB
)
providertest.Declare(
func(ctx context.Context, in providertest.In) providertest.Out {
var err error
database, err = sqltest.NewDatabase(ctx, sqltest.SQLite3Driver, sqltest.SQLite)
Expect(err).ShouldNot(HaveOccurred())
db, err = database.Open()
Expect(err).ShouldNot(HaveOccurred())
err = CreateSchema(ctx, db)
Expect(err).ShouldNot(HaveOccurred())
return providertest.Out{
NewProvider: func() (persistence.Provider, func()) {
return &Provider{
DB: db,
}, nil
},
IsShared: true,
}
},
func() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
err := DropSchema(ctx, db)
Expect(err).ShouldNot(HaveOccurred())
err = database.Close()
Expect(err).ShouldNot(HaveOccurred())
},
)
})
var _ = Describe("type DSNProvider", func() {
var database *sqltest.Database
providertest.Declare(
func(ctx context.Context, in providertest.In) providertest.Out {
var err error
database, err = sqltest.NewDatabase(ctx, sqltest.SQLite3Driver, sqltest.SQLite)
Expect(err).ShouldNot(HaveOccurred())
db, err := database.Open()
Expect(err).ShouldNot(HaveOccurred())
defer db.Close()
err = CreateSchema(ctx, db)
Expect(err).ShouldNot(HaveOccurred())
return providertest.Out{
NewProvider: func() (persistence.Provider, func()) {
return &DSNProvider{
DriverName: database.DataSource.DriverName(),
DSN: database.DataSource.DSN(),
}, nil
},
IsShared: true,
}
},
func() {
err := database.Close()
Expect(err).ShouldNot(HaveOccurred())
},
)
Describe("func Open()", func() {
It("returns an error if the DB can not be opened", func() {
provider := &DSNProvider{
DriverName: "<nonsense-driver>",
DSN: "<nonsense-dsn>",
}
ds, err := provider.Open(context.Background(), "<app-key>")
if ds != nil {
ds.Close()
}
Expect(err).Should(HaveOccurred())
})
})
Context("const DefaultMaxIdleConns", func() {
It("is not zero", func() {
Expect(DefaultMaxIdleConns).To(BeNumerically(">", 0))
})
})
Context("const DefaultMaxOpenConns", func() {
It("is larger than DefaultMaxIdleConns", func() {
Expect(DefaultMaxOpenConns).To(BeNumerically(">", DefaultMaxIdleConns))
})
})
Context("const DefaultMaxConnLifetime", func() {
It("is not zero", func() {
Expect(DefaultMaxConnLifetime).To(BeNumerically(">", 0))
})
})
})
var _ = Describe("type provider", func() {
Describe("func open()", func() {
It("returns an error if a compatible driver can not be found", func() {
provider := &Provider{
DB: sql.OpenDB(&sqlstub.Connector{}),
}
ds, err := provider.Open(context.Background(), "<app-key>")
if ds != nil {
ds.Close()
}
expect := "could not find a driver that is compatible with *sqlstub.Driver"
for _, e := range multierr.Errors(err) {
if e.Error() == expect {
return
}
}
Expect(err).To(MatchError(expect))
})
})
})