-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_shift_test.go
138 lines (118 loc) · 2.78 KB
/
test_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
package shift_test
import (
"context"
"database/sql"
"fmt"
"testing"
"time"
"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=i -updaters=u -table=tests -out=gen_2_test.go
type i struct {
I1 int64
I2 string
I3 time.Time
}
type u struct {
ID int64
U1 bool
U2 Currency
U3 sql.NullTime
U4 sql.NullString
U5 []byte
}
// TestTestFSM tests the TestFSM functionality which tests FSM instances
// by driving it through all state changes with fuzzed data.
func TestTestFSM(t *testing.T) {
cases := []struct {
name string
fsm *shift.FSM
err string
}{
{
name: "insert only",
fsm: shift.NewFSM(events).
Insert(s(1), i{}).
Build(),
},
{
name: "insert update",
fsm: shift.NewFSM(events).
Insert(s(1), i{}, s(2)).
Update(s(2), u{}).
Build(),
},
{
name: "update not reachable",
fsm: shift.NewFSM(events).
Insert(s(1), i{}).
Update(s(2), u{}).
Build(),
err: "status not reachable",
},
{
name: "cycle",
fsm: shift.NewFSM(events).
Insert(s(1), i{}, s(2)).
Update(s(2), u{}, s(1)).
Build(),
},
{
name: "loop",
fsm: shift.NewFSM(events).
Insert(s(1), i{}, s(2)).
Update(s(2), u{}, s(2)).
Build(),
},
}
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
dbc := setup(t)
err := shift.TestFSM(t, dbc, test.fsm)
if test.err == "" {
require.NoError(t, err)
} else {
require.EqualError(t, err, test.err)
}
})
}
}
func (ii i) GetMetadata(ctx context.Context, tx *sql.Tx, id int64, status shift.Status) ([]byte, error) {
return []byte(fmt.Sprint(id)), nil
}
func (uu u) GetMetadata(ctx context.Context, tx *sql.Tx, from shift.Status, to shift.Status) ([]byte, error) {
return []byte(fmt.Sprint(uu.ID)), nil
}
func TestWithMeta(t *testing.T) {
dbc := setup(t)
defer dbc.Close()
events = events.Clone(rsql.WithEventMetadataField("metadata"))
fsm := shift.NewFSM(events, shift.WithMetadata()).
Insert(s(1), i{}, s(2)).
Update(s(2), u{}).
Build()
err := shift.TestFSM(t, dbc, fsm)
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sc, err := events.ToStream(dbc)(context.Background(), "")
require.NoError(t, err)
var c int
err = dbc.QueryRowContext(ctx, "select count(*) from events").Scan(&c)
require.NoError(t, err)
require.Equal(t, 2, c)
e, err := sc.Recv()
require.NoError(t, err)
require.True(t, reflex.IsType(s(1), e.Type))
require.Equal(t, e.ForeignID, string(e.MetaData))
e, err = sc.Recv()
require.NoError(t, err)
require.True(t, reflex.IsType(s(2), e.Type))
require.Equal(t, e.ForeignID, string(e.MetaData))
}
func s(i int) shift.Status {
return TestStatus(i)
}