-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_advance_test.go
252 lines (220 loc) · 6.83 KB
/
reader_advance_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
// Copyright (C) 2023 by Posit Software, PBC
package rsf
import (
"bufio"
"io"
"os"
"testing"
"github.com/stretchr/testify/suite"
)
type ReaderMigrationSuite struct {
suite.Suite
}
func TestReaderMigrationSuite(t *testing.T) {
suite.Run(t, &ReaderMigrationSuite{})
}
func (s *ReaderMigrationSuite) TestAdvanceFields() {
buf := bufio.NewReader(getData(s.Suite))
r := NewReader()
// Read the index
_, err := r.ReadIndex(buf)
s.Assert().Nil(err)
s.Assert().Equal(117, r.Pos())
// Record should be 132 bytes in length
recordSz, err := r.ReadSizeField(buf)
s.Assert().Nil(err)
s.Assert().Equal(132, recordSz)
// Position increased by 4 (size field is 4 bytes)
s.Assert().Equal(121, r.Pos())
// Company
err = r.AdvanceTo(buf, "company")
s.Assert().Nil(err)
company, err := r.ReadStringField(buf)
s.Assert().Nil(err)
s.Assert().Equal("posit", company)
// Position increased by 9. Size field is 4 bytes + data is 5 bytes.
s.Assert().Equal(130, r.Pos())
// Skip the ready field and advance to "list"
// Array should be 100 bytes in size
err = r.AdvanceTo(buf, "list")
s.Assert().Nil(err)
arrayPos := r.Pos()
arraySz, err := r.ReadSizeField(buf)
s.Assert().Nil(err)
s.Assert().Equal(100, arraySz)
// Position increased by 4
s.Assert().Equal(135, r.Pos())
// Get expect array end position
arrayEndPos := arrayPos + arraySz
s.Assert().Equal(231, arrayEndPos)
// Array should be 3 elements in length
arrayLen, err := r.ReadSizeField(buf)
s.Assert().Nil(err)
s.Assert().Equal(3, arrayLen)
// Position increased by 4
s.Assert().Equal(139, r.Pos())
// Array index. Read all three index entries
// Entry 1
date, err := r.ReadFixedStringField(10, buf)
s.Assert().Nil(err)
s.Assert().Equal("2020-10-01", date)
elSz, err := r.ReadSizeField(buf)
s.Assert().Nil(err)
s.Assert().Equal(14, elSz)
//
// Entry 2
date, err = r.ReadFixedStringField(10, buf)
s.Assert().Nil(err)
s.Assert().Equal("2021-03-21", date)
elSz, err = r.ReadSizeField(buf)
s.Assert().Nil(err)
s.Assert().Equal(14, elSz)
//
// Entry 3
date, err = r.ReadFixedStringField(10, buf)
s.Assert().Nil(err)
s.Assert().Equal("2022-12-15", date)
elSz, err = r.ReadSizeField(buf)
s.Assert().Nil(err)
s.Assert().Equal(22, elSz)
// Position increased by 3(10+4) since each index entry uses
// a 10-byte fixed-length string and a 4-byte size field.
// 3*14=42
// 136+42=178
s.Assert().Equal(181, r.Pos())
// Get the first array element's "Name" field
err = r.AdvanceTo(buf, "list", "name")
s.Assert().Nil(err)
name, err := r.ReadStringField(buf)
s.Assert().Nil(err)
s.Assert().Equal("From 2020", name)
// Position increased by 4+9. String size uses 4 bytes and
// string value uses 9 bytes.
// 178+13=183
s.Assert().Equal(194, r.Pos())
// Skip the "Verified" field and advance to second array element's "Name" field
err = r.AdvanceToNextElement(buf)
s.Assert().Nil(err)
err = r.AdvanceTo(buf, "list", "name")
s.Assert().Nil(err)
name, err = r.ReadStringField(buf)
s.Assert().Nil(err)
s.Assert().Equal("From 2021", name)
// Position increased by 4+9+1. String size uses 4 bytes and
// string value uses 9 bytes. Also, the skipped field "verified"
// uses 1 byte
// 191+13+1=205
s.Assert().Equal(208, r.Pos())
// Read the second array element's "Verified" field
err = r.AdvanceTo(buf, "list", "verified")
s.Assert().Nil(err)
verified, err := r.ReadBoolField(buf)
s.Assert().Nil(err)
s.Assert().True(verified)
s.Assert().Equal(209, r.Pos())
// Skip the last array element's "Name" field and advance to "Verified".
// This tests skipping an array sub-element.
err = r.AdvanceToNextElement(buf)
s.Assert().Nil(err)
s.Assert().Equal(209, r.Pos())
err = r.AdvanceTo(buf, "list", "verified")
s.Assert().Nil(err)
// Last name field (skipped) read "this is from 2022"
// 17 bytes + 4 bytes (size) = 21
// 206 + 21 = 227
s.Assert().Equal(230, r.Pos())
verified, err = r.ReadBoolField(buf)
s.Assert().Nil(err)
s.Assert().True(verified)
// 219 + 1 = 220 (arrayEndPos)
s.Assert().Equal(arrayEndPos, r.Pos())
// Skip age field and advance to "rating"
err = r.AdvanceTo(buf, "rating")
s.Assert().Nil(err)
rating, err := r.ReadFloatField(buf)
s.Assert().Nil(err)
s.Assert().Equal(92.689, rating)
// Verify at EOF.
_, err = r.ReadSizeField(buf)
s.Assert().ErrorIs(err, io.EOF)
}
func (s *ReaderMigrationSuite) TestAdvanceArray() {
buf := bufio.NewReader(getData(s.Suite))
r := NewReader()
// Read the index
_, err := r.ReadIndex(buf)
s.Assert().Nil(err)
s.Assert().Equal(117, r.Pos())
// Record should be 132 bytes in length
recordSz, err := r.ReadSizeField(buf)
s.Assert().Nil(err)
s.Assert().Equal(132, recordSz)
// Position increased by 4 (size field is 4 bytes)
s.Assert().Equal(121, r.Pos())
// Company
err = r.AdvanceTo(buf, "company")
s.Assert().Nil(err)
company, err := r.ReadStringField(buf)
s.Assert().Nil(err)
s.Assert().Equal("posit", company)
// Position increased by 9. Size field is 4 bytes + data is 5 bytes.
s.Assert().Equal(130, r.Pos())
// Skip the "ready" field and the "list" array and advance
// to "age". This tests skipping both a regular field and an entire array.
err = r.AdvanceTo(buf, "age")
s.Assert().Nil(err)
age, err := r.ReadIntField(buf)
s.Assert().Nil(err)
s.Assert().Equal(int64(55), age)
s.Assert().Equal(241, r.Pos())
// Read the "rating" field.
err = r.AdvanceTo(buf, "rating")
s.Assert().Nil(err)
rating, err := r.ReadFloatField(buf)
s.Assert().Nil(err)
s.Assert().Equal(92.689, rating)
// Verify at EOF.
_, err = r.ReadSizeField(buf)
s.Assert().ErrorIs(err, io.EOF)
// Dump buffer to temp file to test `Seek`
tmp, err := os.CreateTemp("", "")
s.Assert().Nil(err)
defer os.Remove(tmp.Name())
buf = bufio.NewReader(getData(s.Suite))
_, err = io.Copy(tmp, buf)
// Seek back to the last array element.
err = r.Seek(209, tmp)
s.Assert().Nil(err)
// Position set to 209
s.Assert().Equal(209, r.Pos())
// Read last array element's "Name" field again from the temp file.
name, err := r.ReadStringField(tmp)
s.Assert().Nil(err)
s.Assert().Equal("this is from 2022", name)
s.Assert().Equal(230, r.Pos())
}
func (s *ReaderMigrationSuite) TestAdvanceErrors() {
buf := bufio.NewReader(getData(s.Suite))
r := NewReader()
// Read the index
_, err := r.ReadIndex(buf)
s.Assert().Nil(err)
s.Assert().Equal(117, r.Pos())
// Record should be 132 bytes in length
recordSz, err := r.ReadSizeField(buf)
s.Assert().Nil(err)
s.Assert().Equal(132, recordSz)
// Position increased by 4 (size field is 4 bytes)
s.Assert().Equal(121, r.Pos())
// Company
err = r.AdvanceTo(buf, "company")
s.Assert().Nil(err)
company, err := r.ReadStringField(buf)
s.Assert().Nil(err)
s.Assert().Equal("posit", company)
// Position increased by 9. Size field is 4 bytes + data is 5 bytes.
s.Assert().Equal(130, r.Pos())
// Attempt to advance to field that doesn't exist
err = r.AdvanceTo(buf, "nothere")
s.Assert().ErrorIs(err, ErrNoSuchField)
}