-
Notifications
You must be signed in to change notification settings - Fork 291
/
repeating_group_test.go
293 lines (250 loc) · 8.72 KB
/
repeating_group_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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright (c) quickfixengine.org All rights reserved.
//
// This file may be distributed under the terms of the quickfixengine.org
// license as defined by quickfixengine.org and appearing in the file
// LICENSE included in the packaging of this file.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE.
//
// See http://www.quickfixengine.org/LICENSE for licensing information.
//
// Contact [email protected] if any conditions of this licensing
// are not clear to you.
package quickfix
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRepeatingGroup_Add(t *testing.T) {
f := RepeatingGroup{template: GroupTemplate{GroupElement(1)}}
var tests = []struct {
expectedLen int
}{
{1},
{2},
}
for _, test := range tests {
g := f.Add()
if f.Len() != test.expectedLen {
t.Errorf("Expected %v groups, got %v", test.expectedLen, f.Len())
}
g.SetField(Tag(1), FIXString("hello"))
if !f.groups[test.expectedLen-1].Has(Tag(1)) {
t.Errorf("expected tag in group %v", test.expectedLen)
}
var v FIXString
require.Nil(t, f.groups[test.expectedLen-1].GetField(Tag(1), &v))
if string(v) != "hello" {
t.Errorf("expected hello in group %v", test.expectedLen)
}
}
}
func TestRepeatingGroup_Write(t *testing.T) {
f1 := RepeatingGroup{tag: 10, template: GroupTemplate{
GroupElement(1),
GroupElement(2),
}}
f1.Add().SetField(Tag(1), FIXString("hello"))
f2 := RepeatingGroup{tag: 11, template: GroupTemplate{
GroupElement(1),
GroupElement(2),
}}
f2.Add().SetField(Tag(1), FIXString("hello")).SetField(Tag(2), FIXString("world"))
f3 := RepeatingGroup{tag: 12, template: GroupTemplate{
GroupElement(1),
GroupElement(2),
}}
f3.Add().SetField(Tag(1), FIXString("hello"))
f3.Add().SetField(Tag(1), FIXString("world"))
f4 := RepeatingGroup{tag: 13, template: GroupTemplate{
GroupElement(1),
GroupElement(2),
}}
f4.Add().SetField(Tag(1), FIXString("hello")).SetField(Tag(2), FIXString("world"))
f4.Add().SetField(Tag(1), FIXString("goodbye"))
var tests = []struct {
f RepeatingGroup
expected []byte
}{
{f1, []byte("10=11=hello")},
{f2, []byte("11=11=hello2=world")},
{f3, []byte("12=21=hello1=world")},
{f4, []byte("13=21=hello2=world1=goodbye")},
}
for _, test := range tests {
tvbytes := []byte{}
for _, tv := range test.f.Write() {
tvbytes = append(tvbytes, tv.bytes...)
}
if !bytes.Equal(test.expected, tvbytes) {
t.Errorf("expected %s got %s", test.expected, tvbytes)
}
}
}
func TestRepeatingGroup_ReadError(t *testing.T) {
singleFieldTemplate := GroupTemplate{GroupElement(1)}
tests := []struct {
tv []TagValue
expectedGroupNum int
}{
{
[]TagValue{
{value: []byte("1")},
{tag: Tag(2), value: []byte("not in template")},
{tag: Tag(1), value: []byte("hello")},
}, 0},
{
[]TagValue{
{value: []byte("2")},
{tag: Tag(1), value: []byte("hello")},
{tag: Tag(2), value: []byte("not in template")},
{tag: Tag(1), value: []byte("hello")},
}, 1}}
for _, s := range tests {
f := RepeatingGroup{template: singleFieldTemplate}
_, err := f.Read(s.tv)
if err == nil || len(f.groups) != s.expectedGroupNum {
t.Errorf("Should have raised an error because expected group number is wrong: %v instead of %v", len(f.groups), s.expectedGroupNum)
}
}
}
func TestRepeatingGroup_Read(t *testing.T) {
singleFieldTemplate := GroupTemplate{GroupElement(1)}
multiFieldTemplate := GroupTemplate{GroupElement(1), GroupElement(2), GroupElement(3)}
tests := []struct {
groupTemplate GroupTemplate
tv []TagValue
expectedGroupTvs [][]TagValue
}{
{singleFieldTemplate, []TagValue{{value: []byte("0")}},
[][]TagValue{}},
{singleFieldTemplate, []TagValue{{value: []byte("1")}, {tag: Tag(1), value: []byte("hello")}},
[][]TagValue{{TagValue{tag: Tag(1), value: []byte("hello")}}}},
{singleFieldTemplate,
[]TagValue{{value: []byte("1")},
{tag: Tag(1), value: []byte("hello")},
{tag: Tag(2), value: []byte("not in group")}},
[][]TagValue{
{TagValue{tag: Tag(1), value: []byte("hello")}}}},
{singleFieldTemplate,
[]TagValue{{value: []byte("2")},
{tag: Tag(1), value: []byte("hello")},
{tag: Tag(1), value: []byte("world")}},
[][]TagValue{
{TagValue{tag: Tag(1), value: []byte("hello")}},
{TagValue{tag: Tag(1), value: []byte("world")}},
}},
{multiFieldTemplate,
[]TagValue{
{value: []byte("2")},
{tag: Tag(1), value: []byte("hello")},
{tag: Tag(1), value: []byte("goodbye")}, {tag: Tag(2), value: []byte("cruel")}, {tag: Tag(3), value: []byte("world")},
},
[][]TagValue{
{TagValue{tag: Tag(1), value: []byte("hello")}},
{TagValue{tag: Tag(1), value: []byte("goodbye")}, TagValue{tag: Tag(2), value: []byte("cruel")}, TagValue{tag: Tag(3), value: []byte("world")}},
}},
{multiFieldTemplate,
[]TagValue{
{value: []byte("3")},
{tag: Tag(1), value: []byte("hello")},
{tag: Tag(1), value: []byte("goodbye")}, {tag: Tag(2), value: []byte("cruel")}, {tag: Tag(3), value: []byte("world")},
{tag: Tag(1), value: []byte("another")},
},
[][]TagValue{
{TagValue{tag: Tag(1), value: []byte("hello")}},
{TagValue{tag: Tag(1), value: []byte("goodbye")}, TagValue{tag: Tag(2), value: []byte("cruel")}, TagValue{tag: Tag(3), value: []byte("world")}},
{TagValue{tag: Tag(1), value: []byte("another")}},
}},
}
for _, test := range tests {
f := RepeatingGroup{template: test.groupTemplate}
_, err := f.Read(test.tv)
if err != nil {
t.Error(err)
}
if len(f.groups) != len(test.expectedGroupTvs) {
t.Errorf("expected %v groups, got %v", len(test.expectedGroupTvs), len(f.groups))
}
for g, group := range f.groups {
for _, expected := range test.expectedGroupTvs[g] {
var actual FIXString
require.Nil(t, group.GetField(expected.tag, &actual))
require.NotNil(t, group.tags)
require.Equal(t, len(group.tags), len(group.tagLookup))
if !bytes.Equal(expected.value, []byte(actual)) {
t.Errorf("%v, %v: expected %s, got %s", g, expected.tag, expected.value, actual)
}
}
}
}
}
func TestRepeatingGroup_ReadRecursive(t *testing.T) {
singleFieldTemplate := GroupTemplate{GroupElement(4)}
parentTemplate := GroupTemplate{GroupElement(2), NewRepeatingGroup(Tag(3), singleFieldTemplate), GroupElement(5)}
f := NewRepeatingGroup(Tag(1), parentTemplate)
_, err := f.Read([]TagValue{
{value: []byte("2")},
{tag: Tag(2), value: []byte("hello")},
{tag: 3, value: []byte("1")}, {tag: 4, value: []byte("foo")},
{tag: Tag(2), value: []byte("world")},
{tag: 3, value: []byte("2")}, {tag: 4, value: []byte("foo")}, {tag: 4, value: []byte("bar")}, {tag: 5, value: []byte("fubar")},
})
require.Nil(t, err)
assert.Equal(t, 2, f.Len())
}
func TestRepeatingGroup_ReadComplete(t *testing.T) {
rawMsg := bytes.NewBufferString("8=FIXT.1.19=26835=W34=711849=TEST52=20151027-18:41:52.69856=TST22=9948=TSTX15262=7268=4269=4270=0.07499272=20151027273=18:41:52.698269=7270=0.07501272=20151027273=18:41:52.698269=8270=0.07494272=20151027273=18:41:52.698269=B271=60272=20151027273=18:41:52.69810=163")
msg := NewMessage()
err := ParseMessage(msg, rawMsg)
if err != nil {
t.Error("Unexpected error, ", err)
}
template := GroupTemplate{
GroupElement(269),
GroupElement(270),
GroupElement(271),
GroupElement(272),
GroupElement(273),
}
f := RepeatingGroup{tag: 268, template: template}
err = msg.Body.GetGroup(&f)
if err != nil {
t.Error("Unexpected error, ", err)
}
if len(f.groups) != 4 {
t.Errorf("expected %v groups, got %v", 4, len(f.groups))
}
expectedGroupTags := [][]Tag{
{Tag(269), Tag(270), Tag(272), Tag(273)},
{Tag(269), Tag(270), Tag(272), Tag(273)},
{Tag(269), Tag(270), Tag(272), Tag(273)},
{Tag(269), Tag(271), Tag(272), Tag(273)},
}
expectedGroupValues := [][]FIXString{
{FIXString("4"), FIXString("0.07499"), FIXString("20151027"), FIXString("18:41:52.698")},
{FIXString("7"), FIXString("0.07501"), FIXString("20151027"), FIXString("18:41:52.698")},
{FIXString("8"), FIXString("0.07494"), FIXString("20151027"), FIXString("18:41:52.698")},
{FIXString("B"), FIXString("60"), FIXString("20151027"), FIXString("18:41:52.698")},
}
for i, group := range f.groups {
for j, tag := range expectedGroupTags[i] {
if !group.Has(tag) {
t.Errorf("expected %v in group %v", expectedGroupTags[i][j], i)
continue
}
var actual FIXString
if err := group.GetField(tag, &actual); err != nil {
t.Errorf("error getting field %v from group %v", tag, i)
continue
}
if expectedGroupValues[i][j] != actual {
t.Errorf("Expected %v got %v", expectedGroupTags[i][j], actual)
}
}
}
}