-
Notifications
You must be signed in to change notification settings - Fork 46
/
tx_test.go
195 lines (177 loc) · 3.84 KB
/
tx_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
package weave
import (
"testing"
"github.com/iov-one/weave/errors"
"github.com/iov-one/weave/weavetest/assert"
)
// DemoMsg
type DemoMsg struct {
Num int
Text string
}
func (DemoMsg) Path() string { return "path" }
func (DemoMsg) Validate() error { return nil }
func (DemoMsg) Marshal() ([]byte, error) { return []byte("foo"), nil }
func (*DemoMsg) Unmarshal(bz []byte) error { return nil }
var _ Msg = (*DemoMsg)(nil)
type Container struct {
Data *DemoMsg
}
type BigContainer struct {
Data *DemoMsg
Random string
}
type BadContents struct {
Data *Container
}
func TestExtractMsgFromSum(t *testing.T) {
msg := &DemoMsg{
Num: 17,
Text: "hello world",
}
cases := map[string]struct {
input interface{}
wantErr *errors.Error
}{
"success": {
input: &Container{msg},
},
"nil input is not allowed": {
input: nil,
wantErr: errors.ErrInput,
},
"invalid input content, number": {
input: 7,
wantErr: errors.ErrInput,
},
"invalid input content, string": {
input: "seven",
wantErr: errors.ErrInput,
},
"empty container": {
input: &Container{},
wantErr: errors.ErrState,
},
"container must be a pointer": {
input: Container{msg},
wantErr: errors.ErrInput,
},
"wrong number of fields": {
input: &BigContainer{msg, "foo"},
wantErr: errors.ErrInput,
},
"haw?": {
input: &BadContents{&Container{}},
wantErr: errors.ErrType,
},
}
for testName, tc := range cases {
t.Run(testName, func(t *testing.T) {
res, err := ExtractMsgFromSum(tc.input)
if !tc.wantErr.Is(err) {
t.Fatalf("unexpected error: %#v", err)
}
if tc.wantErr == nil {
if res == nil {
t.Fatal("nil result")
}
} else {
assert.Nil(t, res)
}
})
}
}
func TestLoadMsg(t *testing.T) {
cases := map[string]struct {
Tx Tx
Dest interface{}
WantMsg Msg
WantErr *errors.Error
}{
"success, msgmock type message": {
Tx: &TxMock{
Msg: &MsgMock{ID: 4219},
},
Dest: &MsgMock{},
WantMsg: &MsgMock{ID: 4219},
},
"success, demomsg type message": {
Tx: &TxMock{
Msg: &DemoMsg{Num: 102, Text: "foobar"},
},
Dest: &DemoMsg{},
WantMsg: &DemoMsg{Num: 102, Text: "foobar"},
},
"transaction contains a nil message": {
Tx: &TxMock{Msg: nil},
WantErr: errors.ErrState,
},
"invalid destination message, not a pointer": {
Tx: &TxMock{
Msg: &DemoMsg{Num: 81421, Text: "foo"},
},
Dest: MsgMock{},
WantErr: errors.ErrType,
},
"invalid destination message, wrong message type": {
Tx: &TxMock{
Msg: &DemoMsg{Num: 94151, Text: "foo"},
},
Dest: &MsgMock{},
WantErr: errors.ErrType,
},
"invalid destination message, nil interface": {
Tx: &TxMock{
Msg: &MsgMock{ID: 45192},
},
Dest: Msg(nil),
WantErr: errors.ErrType,
},
"invalid destination message, unaddressable": {
Tx: &TxMock{
Msg: &MsgMock{ID: 91841231},
},
Dest: (*MsgMock)(nil),
WantErr: errors.ErrType,
},
"invalid destination message type, random value": {
Tx: &TxMock{
Msg: &MsgMock{ID: 2914},
},
Dest: "foobar",
WantErr: errors.ErrType,
},
"invalid message in transaction, failed validation": {
Tx: &TxMock{
Msg: &MsgMock{ID: 5, Err: errors.ErrExpired},
},
WantErr: errors.ErrExpired,
},
}
for testName, tc := range cases {
t.Run(testName, func(t *testing.T) {
if err := LoadMsg(tc.Tx, tc.Dest); !tc.WantErr.Is(err) {
t.Fatalf("want %q error, got %q", tc.WantErr, err)
}
if tc.WantErr == nil {
assert.Equal(t, tc.WantMsg, tc.Dest)
}
})
}
}
type TxMock struct {
Tx
Msg Msg
}
func (tx *TxMock) GetMsg() (Msg, error) {
return tx.Msg, nil
}
type MsgMock struct {
Msg
// ID is used only to compare instances if the content is the same.
ID int64
Err error
}
func (mock *MsgMock) Validate() error {
return mock.Err
}