-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
298 lines (251 loc) · 6.75 KB
/
writer_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
294
295
296
297
298
package rowboat_test
import (
"bytes"
"iter"
"reflect"
"slices"
"strings"
"testing"
"time"
"github.com/notnil/rowboat"
)
func TestWriter(t *testing.T) {
// Create test data
people := []Person{
{Name: "Alice", Email: "alice@example.com", Age: 30},
{Name: "Bob", Email: "bob@example.com", Age: 25},
{Name: "Charlie", Email: "charlie@example.com", Age: 35},
}
// Create a buffer to write CSV data
var buf bytes.Buffer
// Create a new Writer instance
writer, err := rowboat.NewWriter[Person](&buf)
if err != nil {
t.Fatalf("Failed to create Writer: %v", err)
}
if err := writer.WriteHeader(); err != nil {
t.Fatalf("Failed to write header: %v", err)
}
// Write all records
for _, person := range people {
if err := writer.Write(person); err != nil {
t.Fatalf("Failed to write record: %v", err)
}
}
// Get the written CSV data
csvData := buf.String()
// Create a reader to verify the written data
reader := strings.NewReader(csvData)
rb, err := rowboat.NewReader[Person](reader)
if err != nil {
t.Fatalf("Failed to create Reader: %v", err)
}
// Read back the data and compare
results := slices.Collect(rb.All())
if !reflect.DeepEqual(results, people) {
t.Errorf("Written results do not match expected.\nExpected: %+v\nGot: %+v", people, results)
}
}
func TestComplexWriter(t *testing.T) {
// Create test data with complex types
t1 := time.Date(2023, 1, 2, 15, 4, 5, 0, time.UTC)
t2 := time.Date(2023, 6, 15, 9, 30, 0, 0, time.UTC)
records := []ComplexRecord{
{
Name: "John",
CreatedAt: t1,
Active: true,
Score: 98.6,
Count: 42,
Rate: 3.14,
Tags: "test;debug",
},
{
Name: "Jane",
CreatedAt: t2,
Active: false,
Score: 75.2,
Count: 100,
Rate: 2.718,
Tags: "prod;live",
},
}
var buf bytes.Buffer
writer, err := rowboat.NewWriter[ComplexRecord](&buf)
if err != nil {
t.Fatalf("Failed to create Writer: %v", err)
}
if err := writer.WriteHeader(); err != nil {
t.Fatalf("Failed to write header: %v", err)
}
for _, record := range records {
if err := writer.Write(record); err != nil {
t.Fatalf("Failed to write record: %v", err)
}
}
// Read back and verify
reader := strings.NewReader(buf.String())
rb, err := rowboat.NewReader[ComplexRecord](reader)
if err != nil {
t.Fatalf("Failed to create Reader: %v", err)
}
results := slices.Collect(rb.All())
if !reflect.DeepEqual(results, records) {
t.Errorf("Written results do not match expected.\nExpected: %+v\nGot: %+v", records, results)
}
}
func TestWriteAll(t *testing.T) {
t1 := time.Date(2023, 1, 2, 15, 4, 5, 0, time.UTC)
t2 := time.Date(2023, 6, 15, 9, 30, 0, 0, time.UTC)
records := []ComplexRecord{
{
Name: "John",
CreatedAt: t1,
Active: true,
Score: 98.6,
Count: 42,
Rate: 3.14,
Tags: "test;debug",
},
{
Name: "Jane",
CreatedAt: t2,
Active: false,
Score: 75.2,
Count: 100,
Rate: 2.718,
Tags: "prod;live",
},
}
var buf bytes.Buffer
writer, err := rowboat.NewWriter[ComplexRecord](&buf)
if err != nil {
t.Fatalf("Failed to create Writer: %v", err)
}
// Convert slice to iterator using iter.Seq
recordIter := iter.Seq[ComplexRecord](func(yield func(ComplexRecord) bool) {
for _, record := range records {
if !yield(record) {
return
}
}
})
if err := writer.WriteHeader(); err != nil {
t.Fatalf("Failed to write header: %v", err)
}
if err := writer.WriteAll(recordIter); err != nil {
t.Fatalf("Failed to write records: %v", err)
}
// Read back and verify
reader := strings.NewReader(buf.String())
rb, err := rowboat.NewReader[ComplexRecord](reader)
if err != nil {
t.Fatalf("Failed to create Reader: %v", err)
}
results := slices.Collect(rb.All())
if !reflect.DeepEqual(results, records) {
t.Errorf("Written results do not match expected.\nExpected: %+v\nGot: %+v", records, results)
}
}
func TestCustomMarshaler(t *testing.T) {
records := []Custom{
{
Point: Point{X: 1.23, Y: 4.56},
},
{
Point: Point{X: 7.89, Y: 0.12},
},
}
var buf bytes.Buffer
writer, err := rowboat.NewWriter[Custom](&buf)
if err != nil {
t.Fatalf("Failed to create Writer: %v", err)
}
// Convert slice to iterator using iter.Seq
recordIter := iter.Seq[Custom](func(yield func(Custom) bool) {
for _, record := range records {
if !yield(record) {
return
}
}
})
if err := writer.WriteHeader(); err != nil {
t.Fatalf("Failed to write header: %v", err)
}
if err := writer.WriteAll(recordIter); err != nil {
t.Fatalf("Failed to write records: %v", err)
}
expected := "point\n1.23;4.56\n7.89;0.12\n"
if buf.String() != expected {
t.Errorf("Written CSV does not match expected.\nExpected:\n%s\nGot:\n%s", expected, buf.String())
}
}
func TestWriterWithIndexing(t *testing.T) {
// Create test data
people := []Person{
{Name: "Alice", Email: "alice@example.com", Age: 30},
{Name: "Bob", Email: "bob@example.com", Age: 25},
{Name: "Charlie", Email: "charlie@example.com", Age: 35},
}
// Define Person with indexing
type IndexedPerson struct {
Name string `csv:"Name,index=1"`
Email string `csv:"Email,index=2"`
Age int `csv:"Age,index=0"`
}
// Convert people to IndexedPerson
indexedPeople := make([]IndexedPerson, len(people))
for i, p := range people {
indexedPeople[i] = IndexedPerson{
Name: p.Name,
Email: p.Email,
Age: p.Age,
}
}
// Create a buffer to write CSV data
var buf bytes.Buffer
// Create a new Writer instance
writer, err := rowboat.NewWriter[IndexedPerson](&buf)
if err != nil {
t.Fatalf("Failed to create Writer: %v", err)
}
if err := writer.WriteHeader(); err != nil {
t.Fatalf("Failed to write header: %v", err)
}
// Write all records
for _, person := range indexedPeople {
if err := writer.Write(person); err != nil {
t.Fatalf("Failed to write record: %v", err)
}
}
// Expected CSV output
expectedCSV := `Age,Name,Email
30,Alice,alice@example.com
25,Bob,bob@example.com
35,Charlie,charlie@example.com
`
csvStr := buf.String()
if csvStr != expectedCSV {
t.Errorf("Written CSV does not match expected.\nExpected:\n%s\nGot:\n%s", expectedCSV, csvStr)
}
// Verify by reading back
reader := strings.NewReader(csvStr)
rb, err := rowboat.NewReader[IndexedPerson](reader)
if err != nil {
t.Fatalf("Failed to create Reader: %v", err)
}
// Read back the data and compare
results := slices.Collect(rb.All())
// Convert back to original person for comparison
readPeople := make([]Person, len(results))
for i, ip := range results {
readPeople[i] = Person{
Name: ip.Name,
Email: ip.Email,
Age: ip.Age,
}
}
if !reflect.DeepEqual(readPeople, people) {
t.Errorf("Written and read results do not match expected.\nExpected: %+v\nGot: %+v", people, readPeople)
}
}