-
Notifications
You must be signed in to change notification settings - Fork 2
/
gateway_test.go
313 lines (257 loc) · 8.88 KB
/
gateway_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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package orm_test
import (
"context"
"fmt"
"github.com/bxcodec/faker/v3"
"github.com/phogolabs/orm"
"github.com/phogolabs/orm/dialect/sql"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Open", func() {
Context("when cannot open the database", func() {
It("returns an error", func() {
gateway, err := orm.Open("sqlite4", "/tmp/orm.db")
Expect(gateway).To(BeNil())
Expect(err).To(MatchError(`sql: unknown driver "sqlite4" (forgotten import?)`))
})
})
Context("when the dns is not wrong", func() {
It("returns an error", func() {
gateway, err := orm.Open("mysql", "localhost")
Expect(gateway).To(BeNil())
Expect(err).To(MatchError(`invalid DSN: missing the slash separating the database name`))
})
})
})
var _ = Describe("Connect", func() {
It("opens the URL successfully", func() {
gateway, err := orm.Connect("sqlite3://orm.db")
Expect(err).To(BeNil())
Expect(gateway).NotTo(BeNil())
Expect(gateway.Close()).To(Succeed())
})
Context("when cannot open the database", func() {
It("returns an error", func() {
gateway, err := orm.Connect("unknown://")
Expect(err).To(MatchError(`sql: unknown driver "unknown" (forgotten import?)`))
Expect(gateway).To(BeNil())
})
})
Context("when the dns is not wrong", func() {
It("returns an error", func() {
gateway, err := orm.Connect("sqlite3://localhost:5430/db")
Expect(gateway).To(BeNil())
Expect(err).To(MatchError("unable to open database file: no such file or directory"))
})
})
})
var _ = Describe("Gateway", func() {
var (
ctx context.Context
gateway *orm.Gateway
)
BeforeEach(func() {
var err error
gateway, err = orm.Open("sqlite3", "file:test.db?cache=shared&mode=memory")
Expect(err).To(BeNil())
ctx = context.TODO()
query :=
sql.CreateTable("users").IfNotExists().
Columns(
sql.Column("id").Type("int"),
sql.Column("first_name").Type("varchar(255)").Attr("NOT NULL"),
sql.Column("last_name").Type("varchar(255)").Attr("NOT NULL"),
sql.Column("email").Type("varchar(255)").Attr("NULL"),
sql.Column("created_at").Type("timestamp").Attr("NULL"),
).
PrimaryKey("id")
_, err = gateway.Exec(ctx, query)
Expect(err).To(Succeed())
for i := 0; i < 10; i++ {
query :=
sql.Insert("users").
Columns("id", "first_name", "last_name", "email").
Values(i, faker.FirstName(), faker.LastName(), faker.Email()).
Returning("id")
_, err := gateway.Exec(ctx, query)
Expect(err).To(Succeed())
}
})
AfterEach(func() {
_, err := gateway.Exec(ctx, sql.Raw("DELETE FROM users"))
Expect(err).To(Succeed())
Expect(gateway.Close()).To(Succeed())
})
Describe("Begin", func() {
It("starts new transaction", func() {
tx, err := gateway.Begin(ctx)
Expect(err).NotTo(HaveOccurred())
Expect(tx).NotTo(BeNil())
})
Context("when the gateway is closed", func() {
It("returns an error", func() {
gateway, err := orm.Open("sqlite3", "file:test.db?cache=shared&mode=memory")
Expect(err).To(BeNil())
Expect(gateway.Close()).To(Succeed())
tx, err := gateway.Begin(ctx)
Expect(err).To(MatchError("sql: database is closed"))
Expect(tx).To(BeNil())
})
})
})
Describe("RunInTx", func() {
It("starts new transaction", func() {
err := gateway.RunInTx(ctx, func(tx *orm.GatewayTx) error {
entities := []*User{}
Expect(tx.All(ctx, sql.Raw("SELECT * FROM users"), &entities)).To(Succeed())
entity := &User{}
Expect(tx.Only(ctx, sql.Raw("SELECT * FROM users WHERE id = 0"), entity)).To(Succeed())
Expect(tx.First(ctx, sql.Raw("SELECT * FROM users WHERE id = 0"), entity)).To(Succeed())
_, err := tx.Exec(ctx, sql.Raw("UPDATE users SET created_at = strftime()"))
Expect(err).To(Succeed())
rows, err := tx.Query(ctx, sql.Raw("UPDATE users SET created_at = strftime()"))
Expect(err).To(Succeed())
Expect(rows.Close()).To(Succeed())
return nil
})
Expect(err).NotTo(HaveOccurred())
})
Context("when the gateway is closed", func() {
It("returns an error", func() {
gateway, err := orm.Open("sqlite3", "file:test.db?cache=shared&mode=memory")
Expect(err).To(BeNil())
Expect(gateway.Close()).To(Succeed())
err = gateway.RunInTx(ctx, func(tx *orm.GatewayTx) error {
return nil
})
Expect(err).To(MatchError("sql: database is closed"))
})
})
Context("when the transaction fails", func() {
It("returns an error", func() {
err := gateway.RunInTx(ctx, func(tx *orm.GatewayTx) error {
Expect(tx).NotTo(BeNil())
return fmt.Errorf("oh no")
})
Expect(err).To(MatchError("oh no"))
})
})
})
Describe("Dialect", func() {
It("returns the dialect", func() {
Expect(gateway.Dialect()).To(Equal("sqlite3"))
})
})
Describe("All", func() {
It("returns all entities", func() {
entities := []*User{}
Expect(gateway.All(ctx, sql.Raw("SELECT * FROM users"), &entities)).To(Succeed())
Expect(entities).To(HaveLen(10))
Expect(entities[0].ID).To(Equal(0))
Expect(entities[0].Email).NotTo(BeNil())
Expect(entities[1].ID).To(Equal(1))
Expect(entities[1].Email).NotTo(BeNil())
})
Context("when the database operation fail", func() {
It("returns an error", func() {
entities := []*User{}
Expect(gateway.All(ctx, sql.Raw("SELECT * FROM unknown.users"), &entities)).To(MatchError("no such table: unknown.users"))
Expect(entities).To(HaveLen(0))
})
})
Context("when the routine is unknown", func() {
It("returns an error", func() {
entities := []*User{}
err := gateway.All(ctx, sql.Routine("my-unknown-routine"), entities)
Expect(err).To(MatchError("query 'my-unknown-routine' not found"))
})
})
})
Describe("Only", func() {
It("returns the first entity", func() {
entity := &User{}
Expect(gateway.Only(ctx, sql.Raw("SELECT * FROM users WHERE id = 0"), entity)).To(Succeed())
Expect(entity.ID).To(Equal(0))
Expect(entity.Email).NotTo(BeNil())
})
Context("when the provided type is not compatible", func() {
It("returns an error", func() {
entity := "root"
Expect(gateway.Only(ctx, sql.Raw("SELECT * FROM users WHERE id = 0"), &entity)).To(MatchError("sql/scan: columns do not match (5 > 1)"))
})
})
Context("when there are more than one entities", func() {
It("returns an error", func() {
entity := &User{}
Expect(gateway.Only(ctx, sql.Raw("SELECT * FROM users"), entity)).To(MatchError("orm: user not singular"))
})
})
Context("when there are not entities", func() {
It("returns an error", func() {
entity := &User{}
Expect(gateway.Only(ctx, sql.Raw("SELECT * FROM users WHERE id > 1000"), entity)).To(MatchError("orm: user not found"))
})
})
Context("when the database operation fail", func() {
It("returns an error", func() {
entity := &User{}
Expect(gateway.Only(ctx, sql.Raw("SELECT * FROM unknown.users"), entity)).To(MatchError("no such table: unknown.users"))
})
})
Context("when the routine is unknown", func() {
It("returns an error", func() {
entity := &User{}
err := gateway.Only(ctx, sql.Routine("my-unknown-routine"), entity)
Expect(err).To(MatchError("query 'my-unknown-routine' not found"))
})
})
})
Describe("First", func() {
It("returns the first entity", func() {
entity := &User{}
Expect(gateway.First(ctx, sql.Raw("SELECT * FROM users"), entity)).To(Succeed())
Expect(entity.ID).To(Equal(0))
Expect(entity.Email).NotTo(BeNil())
})
Context("when the provided type is not compatible", func() {
It("returns an error", func() {
entity := "root"
Expect(gateway.First(ctx, sql.Raw("SELECT * FROM users WHERE id = 0"), &entity)).To(MatchError("sql/scan: columns do not match (5 > 1)"))
})
})
Context("when there are not entities", func() {
It("returns an error", func() {
entity := &User{}
Expect(gateway.First(ctx, sql.Raw("SELECT * FROM users WHERE id > 1000"), entity)).To(MatchError("orm: user not found"))
})
})
Context("when the database operation fail", func() {
It("returns an error", func() {
entity := &User{}
Expect(gateway.First(ctx, sql.Raw("SELECT * FROM unknown.users"), entity)).To(MatchError("no such table: unknown.users"))
})
})
Context("when the routine is unknown", func() {
It("returns an error", func() {
entity := &User{}
err := gateway.First(ctx, sql.Routine("my-unknown-routine"), entity)
Expect(err).To(MatchError("query 'my-unknown-routine' not found"))
})
})
})
Describe("Exec", func() {
Context("when the query has wrong syntax", func() {
It("returns an error", func() {
_, err := gateway.Exec(ctx, sql.Raw("SELECT * FROM unknown.users"))
Expect(err).To(MatchError("no such table: unknown.users"))
})
})
Context("when the routine is unknown", func() {
It("returns an error", func() {
_, err := gateway.Exec(ctx, sql.Routine("my-unknown-routine"))
Expect(err).To(MatchError("query 'my-unknown-routine' not found"))
})
})
})
})