forked from luno/shift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharc_test.go
More file actions
64 lines (49 loc) · 1.69 KB
/
arc_test.go
File metadata and controls
64 lines (49 loc) · 1.69 KB
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
package shift_test
import (
"context"
"testing"
"time"
"github.com/luno/jettison/jtest"
"github.com/stretchr/testify/require"
"github.com/luno/shift"
)
//go:generate go run github.com/luno/shift/shiftgen -inserters=insert2 -updaters=move -table=users -out=gen_4_test.go
type insert2 struct {
Name string
DateOfBirth time.Time `shift:"dob"` // Override column name.
Amount Currency
}
type move struct {
ID int64
}
// afsm defines an ArcFSM with two ways to initialise an entry
// (with insert{} or insert2{}) as well as being able to move
// back Init from Update via move{}.
var afsm = shift.NewArcFSM(events).
Insert(StatusInit, insert{}).
Insert(StatusInit, insert2{}).
Update(StatusInit, StatusUpdate, move{}).
Update(StatusUpdate, StatusInit, move{}).
Build()
func TestArcFSM(t *testing.T) {
dbc := setup(t)
t0 := time.Now().Truncate(time.Second)
amount := Currency{Valid: true, Amount: 99}
ctx := context.Background()
// Init model
id1, err := afsm.Insert(ctx, dbc, StatusInit, insert{Name: "insert", DateOfBirth: t0})
jtest.RequireNil(t, err)
require.Equal(t, int64(1), id1)
// Move to Updated
err = afsm.Update(ctx, dbc, StatusInit, StatusUpdate, move{ID: id1})
jtest.RequireNil(t, err)
// Move back to Init
err = afsm.Update(ctx, dbc, StatusUpdate, StatusInit, move{ID: id1})
jtest.RequireNil(t, err)
assertUser(t, dbc, events.ToStream(dbc), usersTable, id1, "insert", t0, Currency{}, 1, 2, 1)
// Init another model
id2, err := afsm.Insert(ctx, dbc, StatusInit, insert2{Name: "insert2", DateOfBirth: t0, Amount: amount})
jtest.RequireNil(t, err)
require.Equal(t, int64(2), id2)
assertUser(t, dbc, events.ToStream(dbc), usersTable, id2, "insert2", t0, amount, 1)
}