-
Notifications
You must be signed in to change notification settings - Fork 3
/
csv_writer_test.go
169 lines (138 loc) · 2.99 KB
/
csv_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
package ccsv
import (
"bufio"
"os"
"strconv"
"sync"
"testing"
)
func getCsvWriter() *CsvWriter {
csvWriter, err := NewCsvWriter("test.csv")
if err != nil {
panic("could not open test.csv for writing")
}
return csvWriter
}
func rowCount() int {
file, _ := os.Open("test.csv")
defer file.Close()
fileScanner := bufio.NewScanner(file)
rowCount := 0
for fileScanner.Scan() {
rowCount++
}
return rowCount
}
func deleteCsvFile() {
os.Remove("test.csv")
}
var (
row = []string{"A", "row", "in", "a", "CSV", "file"}
rows = [][]string{
{"First", "of", "three", "rows"},
{"Second", "of", "three", "rows"},
{"Third", "of", "three", "rows"},
}
)
func TestNewCsvWriter(t *testing.T) {
csvWriter, err := NewCsvWriter("test.csv")
if err != nil {
t.Error("should open a CSV file for writing")
}
err = csvWriter.Close()
if err != nil {
t.Error("should have closed CSV file")
}
deleteCsvFile()
}
func TestCsvWriter_Write(t *testing.T) {
csvWriter := getCsvWriter()
t.Run("multiple single row writes", func(t *testing.T) {
for i := 0; i < 10; i++ {
t.Run("single row write "+strconv.Itoa(i), func(t *testing.T) {
err := csvWriter.Write(row)
if err != nil {
t.Error("should have written a row to the CSV file")
}
})
}
})
csvWriter.Flush()
csvWriter.Close()
if ok := rowCount() != 10; ok {
t.Error("expected to see 10 rows in CSV file")
}
deleteCsvFile()
}
func TestCsvWriter_WriteAll(t *testing.T) {
csvWriter := getCsvWriter()
defer csvWriter.Close()
t.Run("multiple multi row writes", func(t *testing.T) {
for i := 0; i < 10; i++ {
t.Run("multi row write "+strconv.Itoa(i), func(t *testing.T) {
err := csvWriter.WriteAll(rows)
if err != nil {
t.Error("should have written a row to the CSV file")
}
})
}
})
csvWriter.Flush()
csvWriter.Close()
if ok := rowCount() != 30; ok {
t.Error("expected to see 30 rows in CSV file")
}
deleteCsvFile()
}
func TestCsvWriter_Flush(t *testing.T) {
csvWriter := getCsvWriter()
csvWriter.Write(row)
err := csvWriter.Flush()
if err != nil {
t.Error("should have flushed output buffers for CSV file")
}
csvWriter.Close()
if ok := rowCount() != 1; ok {
t.Error("expected to see one row in CSV file")
}
deleteCsvFile()
}
func TestCsvWriter_Close(t *testing.T) {
csvWriter := getCsvWriter()
err := csvWriter.Close()
if err != nil {
t.Error("expected to close underlying file io stream")
}
deleteCsvFile()
}
func BenchmarkCsvWriter_Write(b *testing.B) {
csvWriter := getCsvWriter()
b.Run("nil-slice", func(b *testing.B) {
wg := sync.WaitGroup{}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
wg.Add(1)
go func() {
defer wg.Done()
csvWriter.Write(nil)
}()
}
})
wg.Wait()
})
b.Run("row", func(b *testing.B) {
wg := sync.WaitGroup{}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
wg.Add(1)
go func() {
defer wg.Done()
csvWriter.Write(row)
}()
}
})
wg.Wait()
})
csvWriter.Close()
deleteCsvFile()
}