-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patharc_test.go
100 lines (83 loc) · 2.42 KB
/
arc_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
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)
}
func TestArcIsValidTransition(t *testing.T) {
ctx := context.Background()
dbc := setup(t)
t0 := time.Now().Truncate(time.Second)
// Init model
id1, err := afsm.Insert(ctx, dbc, StatusInit, insert{Name: "insert", DateOfBirth: t0})
jtest.RequireNil(t, err)
require.Equal(t, int64(1), id1)
tests := []struct {
name string
from shift.Status
to shift.Status
exp bool
}{
{
name: "Valid",
from: StatusInit,
to: StatusUpdate,
exp: true,
},
{
name: "Invalid State Transition",
from: StatusComplete,
to: StatusUpdate,
exp: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := afsm.IsValidTransition(tt.from, tt.to)
require.Equal(t, tt.exp, b)
})
}
}