forked from luno/shift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shift_test.go
269 lines (210 loc) · 6.9 KB
/
shift_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
package shift_test
import (
"context"
"database/sql"
"testing"
"time"
"github.com/luno/jettison/errors"
"github.com/luno/jettison/j"
"github.com/luno/jettison/jtest"
"github.com/luno/reflex"
"github.com/luno/reflex/rsql"
"github.com/luno/shift"
"github.com/stretchr/testify/require"
)
//go:generate go run github.com/luno/shift/shiftgen -inserter=insert -updaters=update,complete -table=users -out=gen_1_test.go
type insert struct {
Name string
DateOfBirth time.Time `shift:"dob"` // Override column name.
}
type update struct {
ID int64
Name string
Amount Currency
}
type complete struct {
ID int64
}
type TestStatus int
func (s TestStatus) ShiftStatus() int {
return int(s)
}
func (s TestStatus) ReflexType() int {
return int(s)
}
const (
StatusInit TestStatus = 1
StatusUpdate TestStatus = 2
StatusComplete TestStatus = 3
)
const usersTable = "users"
var events = rsql.NewEventsTableInt("events")
var fsm = shift.NewFSM(events).
Insert(StatusInit, insert{}, StatusUpdate).
Update(StatusUpdate, update{}, StatusComplete).
Update(StatusComplete, complete{}).
Build()
func TestAboveFSM(t *testing.T) {
dbc := setup(t)
jtest.RequireNil(t, shift.TestFSM(t, dbc, fsm))
}
func TestBasic(t *testing.T) {
dbc := setup(t)
t0 := time.Now().Truncate(time.Second)
amount := Currency{Valid: true, Amount: 99}
ctx := context.Background()
// Init model
id, err := fsm.Insert(ctx, dbc, insert{Name: "insertMe", DateOfBirth: t0})
jtest.RequireNil(t, err)
require.Equal(t, int64(1), id)
assertUser(t, dbc, events.ToStream(dbc), usersTable, id, "insertMe", t0, Currency{}, 1)
// Update model
err = fsm.Update(ctx, dbc, StatusInit, StatusUpdate, update{ID: id, Name: "updateMe", Amount: amount})
jtest.RequireNil(t, err)
assertUser(t, dbc, events.ToStream(dbc), usersTable, id, "updateMe", t0, amount, 1, 2)
// Complete model
err = fsm.Update(ctx, dbc, StatusUpdate, StatusComplete, complete{ID: id})
jtest.RequireNil(t, err)
assertUser(t, dbc, events.ToStream(dbc), usersTable, id, "updateMe", t0, amount, 1, 2, 3)
}
func assertUser(t *testing.T, dbc *sql.DB, stream reflex.StreamFunc, table string,
id any, exName string, exDOB time.Time, exAmount Currency, exEvents ...TestStatus) {
var name sql.NullString
var amount Currency
var dob time.Time
err := dbc.QueryRow("select name, dob, amount "+
"from "+table+" where id=?", id).Scan(&name, &dob, &amount)
jtest.RequireNil(t, err)
require.Equal(t, exName, name.String)
require.Equal(t, exDOB.UTC(), dob.UTC())
require.Equal(t, exAmount, amount)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sc, err := stream(ctx, "")
jtest.RequireNil(t, err)
for _, exE := range exEvents {
e, err := sc.Recv()
jtest.RequireNil(t, err)
require.Equal(t, int(exE), e.Type.ReflexType())
}
}
//go:generate go run github.com/luno/shift/shiftgen -inserter=insertStr -updaters=updateStr,completeStr -table=usersStr -out=gen_string_test.go
type insertStr struct {
ID string
Name string
DateOfBirth time.Time `shift:"dob"` // Override column name.
}
type updateStr struct {
ID string
Name string
Amount Currency
}
type completeStr struct {
ID string
}
const usersStrTable = "usersStr"
var eventsStr = rsql.NewEventsTable("eventsStr")
var fsmStr = shift.NewGenFSM[string](eventsStr).
Insert(StatusInit, insertStr{}, StatusUpdate).
Update(StatusUpdate, updateStr{}, StatusComplete).
Update(StatusComplete, completeStr{}).
Build()
func TestBasic_StringFSM(t *testing.T) {
dbc := setup(t)
t0 := time.Now().Truncate(time.Second)
amount := Currency{Valid: true, Amount: 99}
ctx := context.Background()
// Init model
id, err := fsmStr.Insert(ctx, dbc, insertStr{ID: "abcdef123456", Name: "insertMe", DateOfBirth: t0})
jtest.RequireNil(t, err)
require.Equal(t, "abcdef123456", id)
assertUser(t, dbc, eventsStr.ToStream(dbc), usersStrTable, id, "insertMe", t0, Currency{}, 1)
// Update model
err = fsmStr.Update(ctx, dbc, StatusInit, StatusUpdate, updateStr{ID: id, Name: "updateMe", Amount: amount})
jtest.RequireNil(t, err)
assertUser(t, dbc, eventsStr.ToStream(dbc), usersStrTable, id, "updateMe", t0, amount, 1, 2)
// Complete model
err = fsmStr.Update(ctx, dbc, StatusUpdate, StatusComplete, completeStr{ID: id})
jtest.RequireNil(t, err)
assertUser(t, dbc, eventsStr.ToStream(dbc), usersStrTable, id, "updateMe", t0, amount, 1, 2, 3)
}
func (ii i) Validate(ctx context.Context, tx *sql.Tx, id int64, status shift.Status) error {
if id > 1 {
return errInsertInvalid
}
return nil
}
func (uu u) Validate(ctx context.Context, tx *sql.Tx, from shift.Status, to shift.Status) error {
if from.ShiftStatus() == to.ShiftStatus() {
return errUpdateInvalid
}
return nil
}
var (
errInsertInvalid = errors.New("only single row permitted", j.C("ERR_d9ec7823de79aa28"))
errUpdateInvalid = errors.New("only single row permitted", j.C("ERR_e67f85dcb425e083"))
)
func TestWithValidation(t *testing.T) {
dbc := setup(t)
defer dbc.Close()
fsm := shift.NewFSM(events, shift.WithValidation()).
Insert(s(1), i{}, s(2)).
Update(s(2), u{}, s(2)). // Allow 2 -> 2 update, validation will fail.
Build()
ctx := context.Background()
// First insert is ok
id, err := fsm.Insert(ctx, dbc, i{I3: time.Now()})
jtest.RequireNil(t, err)
require.Equal(t, int64(1), id)
// Second insert fails.
_, err = fsm.Insert(ctx, dbc, i{I3: time.Now()})
jtest.Require(t, errInsertInvalid, err)
// Update from 1 -> 2 is ok
err = fsm.Update(ctx, dbc, s(1), s(2), u{ID: id})
jtest.RequireNil(t, err)
// Update from 2 -> 2 fails
err = fsm.Update(ctx, dbc, s(2), s(2), u{ID: id, U1: true})
jtest.Require(t, errUpdateInvalid, err)
}
//go:generate go run github.com/luno/shift/shiftgen -inserter=i_t -updaters=u_t -table=tests -out=gen_3_test.go
type i_t struct {
I1 int64
I2 string
I3 time.Time
CreatedAt time.Time
UpdatedAt time.Time
}
type u_t struct {
ID int64
U1 bool
U2 Currency
U3 sql.NullTime
U4 sql.NullString
U5 []byte
UpdatedAt time.Time
}
func TestWithTimestamps(t *testing.T) {
dbc := setup(t)
defer dbc.Close()
fsm := shift.NewFSM(events).
Insert(s(1), i_t{}, s(2)).
Update(s(2), u_t{}, s(2)). // Allow 2 -> 2 update, validation will fail.
Build()
ctx := context.Background()
t0 := time.Now()
id, err := fsm.Insert(ctx, dbc, i_t{I3: time.Now(), UpdatedAt: t0})
require.Error(t, err, "created_at is required")
require.Zero(t, 0)
id, err = fsm.Insert(ctx, dbc, i_t{I3: time.Now(), CreatedAt: t0})
require.Error(t, err, "updated_at is required")
require.Zero(t, 0)
// First insert is ok
id, err = fsm.Insert(ctx, dbc, i_t{I3: time.Now(), CreatedAt: t0, UpdatedAt: t0})
jtest.RequireNil(t, err)
require.Equal(t, int64(1), id)
err = fsm.Update(ctx, dbc, s(1), s(2), u_t{ID: id})
require.Error(t, err, "updated_at is required")
// Update from 1 -> 2 is ok
err = fsm.Update(ctx, dbc, s(1), s(2), u_t{ID: id, UpdatedAt: t0})
jtest.RequireNil(t, err)
}