-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
customer.go
269 lines (200 loc) · 6.22 KB
/
customer.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
package suite
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"github.com/cucumber/godog"
"github.com/swaggest/assertjson"
"go.nhat.io/clock"
"go.nhat.io/otelsql/tests/suite/customer"
)
// CustomerRepositoryConstructor is constructor to create a new repository using DatabaseContext.
type CustomerRepositoryConstructor func(db DatabaseExecer, c clock.Clock) customer.Repository
type customerTests struct {
databaseDriver string
databaseDSN string
db *sql.DB
runner DatabaseContext
construct CustomerRepositoryConstructor
repository customer.Repository
clock clock.Clock
usePreparer bool
lastResult interface{}
lastError error
}
func (t *customerTests) constructRepository() {
t.repository = t.construct(newDatabaseExecer(t.runner, t.usePreparer), t.clock)
}
func (t *customerTests) RegisterSteps(sc *godog.ScenarioContext) {
sc.Before(func(ctx context.Context, _ *godog.Scenario) (context.Context, error) {
db, err := openDB(t.databaseDriver, t.databaseDSN)
if err != nil {
return ctx, err
}
t.db = db
t.runner = db
t.usePreparer = false
t.constructRepository()
return ctx, err
})
sc.After(func(ctx context.Context, _ *godog.Scenario, err error) (context.Context, error) {
if t.db != nil {
_ = t.db.Close() // nolint: errcheck
}
return ctx, nil
})
sc.Step(`I do not use database preparer`, func() error {
t.usePreparer = false
t.constructRepository()
return nil
})
sc.Step(`I use database preparer`, func() error {
t.usePreparer = true
t.constructRepository()
return nil
})
sc.Step(`I create a new customer:`, t.createCustomer)
sc.Step(`I update a customer by id "([^"]+)":`, t.updateCustomerByID)
sc.Step(`I find a customer by id "([^"]+)"`, t.findCustomerByID)
sc.Step(`I delete a customer by id "([^"]+)"`, t.deleteCustomerByID)
sc.Step(`I find all customers`, t.findAllCustomers)
sc.Step(`I got a customer:`, t.haveCustomer)
sc.Step(`I got these customers:`, t.haveCustomers)
sc.Step(`I got an error$`, t.haveAnError)
sc.Step(`I got an error "([^"]+)"`, t.haveAnErrorWithMessage)
sc.Step(`I got an error:`, t.haveAnErrorWithMessageInDocString)
sc.Step(`I got no error`, t.haveNoError)
sc.Step(`I start a new transaction`, t.startTransaction)
sc.Step(`I commit the transaction`, t.commitTransaction)
sc.Step(`I rollback the transaction`, t.rollbackTransaction)
}
func (t *customerTests) findCustomerByID(id int) error {
t.lastResult, t.lastError = t.repository.Find(context.Background(), id)
return nil
}
func (t *customerTests) findAllCustomers() error {
t.lastResult, t.lastError = t.repository.FindAll(context.Background())
return nil
}
func (t *customerTests) createCustomer(data *godog.DocString) error {
c := customer.Customer{}
if err := json.Unmarshal([]byte(data.Content), &c); err != nil {
return fmt.Errorf("could not unmarshal customer data: %w", err)
}
t.lastError = t.repository.Create(context.Background(), c)
t.lastResult = nil
return nil
}
func (t *customerTests) updateCustomerByID(id int64, data *godog.DocString) error {
c := customer.Customer{}
if err := json.Unmarshal([]byte(data.Content), &c); err != nil {
return fmt.Errorf("could not unmarshal customer data: %w", err)
}
c.ID = id
t.lastError = t.repository.Update(context.Background(), c)
t.lastResult = nil
return nil
}
func (t *customerTests) deleteCustomerByID(id int) error {
t.lastError = t.repository.Delete(context.Background(), id)
t.lastResult = nil
return nil
}
func (t *customerTests) haveCustomer(expected *godog.DocString) error {
c, ok := t.lastResult.(*customer.Customer)
if !ok {
if t.lastError != nil {
return fmt.Errorf("could not get customer: %w", t.lastError)
}
return fmt.Errorf("got no customer")
}
actual, err := json.Marshal(c)
if err != nil {
return fmt.Errorf("could not marshal customer: %w", err)
}
return assertjson.FailNotEqual([]byte(expected.Content), actual)
}
func (t *customerTests) haveCustomers(expected *godog.DocString) error {
customers, ok := t.lastResult.([]customer.Customer)
if !ok {
if t.lastError != nil {
return fmt.Errorf("could not get customers: %w", t.lastError)
}
return fmt.Errorf("got no customer")
}
actual, err := json.Marshal(customers)
if err != nil {
return fmt.Errorf("could not marshal customers: %w", err)
}
return assertjson.FailNotEqual([]byte(expected.Content), actual)
}
func (t *customerTests) haveNoError() error {
if t.lastError != nil {
return fmt.Errorf("expect no error, got: %w", t.lastError)
}
return nil
}
func (t *customerTests) haveAnError() error {
if t.lastError == nil {
return fmt.Errorf("expect an error, got nothing")
}
return nil
}
func (t *customerTests) haveAnErrorWithMessageInDocString(err *godog.DocString) error {
return t.haveAnErrorWithMessage(err.Content)
}
func (t *customerTests) haveAnErrorWithMessage(err string) error {
if t.lastError == nil {
return fmt.Errorf("there is no error, expected: %s", err)
}
if err != t.lastError.Error() {
return fmt.Errorf("got error: %s, expected: %s", t.lastError.Error(), err) // nolint: errorlint
}
return nil
}
func (t *customerTests) startTransaction() error {
tx, err := t.db.BeginTx(context.Background(), &sql.TxOptions{})
if err != nil {
return fmt.Errorf("could not start a new transaction: %w", err)
}
t.runner = &txContext{Tx: tx}
t.constructRepository()
return nil
}
func (t *customerTests) commitTransaction() error {
tx, ok := t.runner.(*txContext)
if !ok {
return fmt.Errorf("no transaction to commit")
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("could not commit transaction: %w", err)
}
t.runner = t.db
t.constructRepository()
return nil
}
func (t *customerTests) rollbackTransaction() error {
tx, ok := t.runner.(*txContext)
if !ok {
return fmt.Errorf("no transaction to rollback")
}
if err := tx.Rollback(); err != nil {
return fmt.Errorf("could not rollback transaction: %w", err)
}
t.runner = t.db
t.constructRepository()
return nil
}
func newCustomerTests(
databaseDriver, databaseDSN string,
constructor CustomerRepositoryConstructor,
clock clock.Clock,
) *customerTests {
return &customerTests{
databaseDriver: databaseDriver,
databaseDSN: databaseDSN,
construct: constructor,
clock: clock,
}
}