-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreader_test.go
More file actions
168 lines (136 loc) · 3.96 KB
/
reader_test.go
File metadata and controls
168 lines (136 loc) · 3.96 KB
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
package rowboat_test
import (
"reflect"
"slices"
"strings"
"testing"
"time"
"github.com/notnil/rowboat"
)
func TestRowBoat(t *testing.T) {
// CSV data as a string
csvData := `Name,Email,Age
Alice,alice@example.com,30
Bob,bob@example.com,25
Charlie,charlie@example.com,35`
// Create a reader from the CSV data string
reader := strings.NewReader(csvData)
// Create a new RowBoat instance
rb, err := rowboat.NewReader[Person](reader)
if err != nil {
t.Fatalf("Failed to create RowBoat: %v", err)
}
// Expected results
expected := []Person{
{Name: "Alice", Email: "alice@example.com", Age: 30},
{Name: "Charlie", Email: "charlie@example.com", Age: 35},
}
results := slices.Collect(rowboat.Filter(func(p Person) bool {
return p.Age > 25
}, rb.All()))
// Compare results
if !reflect.DeepEqual(results, expected) {
t.Errorf("Parsed results do not match expected.\nExpected: %+v\nGot: %+v", expected, results)
}
}
func TestExtraneousColumns(t *testing.T) {
// CSV data with extra columns that aren't in the struct
csvData := `Name,Email,Age,ExtraCol1,ExtraCol2
Alice,alice@example.com,30,unused1,unused2
Bob,bob@example.com,25,unused3,unused4`
reader := strings.NewReader(csvData)
// Create a new RowBoat instance
rb, err := rowboat.NewReader[Person](reader)
if err != nil {
t.Fatalf("Failed to create RowBoat: %v", err)
}
// Expected results - should ignore the extra columns
expected := []Person{
{Name: "Alice", Email: "alice@example.com", Age: 30},
{Name: "Bob", Email: "bob@example.com", Age: 25},
}
results := slices.Collect(rb.All())
// Compare results
if !reflect.DeepEqual(results, expected) {
t.Errorf("Parsed results do not match expected.\nExpected: %+v\nGot: %+v", expected, results)
}
}
func TestBlankRows(t *testing.T) {
// CSV data with blank rows
csvData := `Name,Email,Age
Alice,alice@example.com,30
Bob,bob@example.com,25
`
reader := strings.NewReader(csvData)
// Create a new RowBoat instance
rb, err := rowboat.NewReader[Person](reader)
if err != nil {
t.Fatalf("Failed to create RowBoat: %v", err)
}
// Expected results - should skip blank rows
expected := []Person{
{Name: "Alice", Email: "alice@example.com", Age: 30},
{Name: "Bob", Email: "bob@example.com", Age: 25},
}
results := slices.Collect(rb.All())
// Compare results
if !reflect.DeepEqual(results, expected) {
t.Errorf("Parsed results do not match expected.\nExpected: %+v\nGot: %+v", expected, results)
}
}
func TestComplexTypes(t *testing.T) {
// CSV data with various types
csvData := `name,created_at,active,score,count,rate,tags
John,2023-01-02T15:04:05Z,true,98.6,42,3.14,test;debug
Jane,2023-06-15T09:30:00Z,false,75.2,100,2.718,prod;live`
reader := strings.NewReader(csvData)
rb, err := rowboat.NewReader[ComplexRecord](reader)
if err != nil {
t.Fatalf("Failed to create RowBoat: %v", err)
}
// Parse expected time values
t1, _ := time.Parse(time.RFC3339, "2023-01-02T15:04:05Z")
t2, _ := time.Parse(time.RFC3339, "2023-06-15T09:30:00Z")
expected := []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",
},
}
results := slices.Collect(rb.All())
if !reflect.DeepEqual(results, expected) {
t.Errorf("Parsed results do not match expected.\nExpected: %+v\nGot: %+v", expected, results)
}
}
func TestCustomUnmarshaler(t *testing.T) {
csvData := `point
1;2
3;4`
reader := strings.NewReader(csvData)
rb, err := rowboat.NewReader[Custom](reader)
if err != nil {
t.Fatalf("Failed to create RowBoat: %v", err)
}
results := slices.Collect(rb.All())
expected := []Custom{
{Point: Point{X: 1, Y: 2}},
{Point: Point{X: 3, Y: 4}},
}
if !reflect.DeepEqual(results, expected) {
t.Errorf("Parsed results do not match expected.\nExpected: %+v\nGot: %+v", expected, results)
}
}