-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils_test.go
More file actions
47 lines (38 loc) · 948 Bytes
/
Copy pathutils_test.go
File metadata and controls
47 lines (38 loc) · 948 Bytes
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
package main
import (
"bytes"
"encoding/csv"
"fmt"
"sync"
"testing"
"github.com/jszwec/csvutil"
)
type sampleCSV struct {
ID int `csv:"id"`
Name string `csv:"name"`
}
// Ensures createCSVEncodeCh drains all objects and writes CSV rows.
func TestCreateCSVEncodeCh(t *testing.T) {
buf := &bytes.Buffer{}
csvWriter := csv.NewWriter(buf)
enc := csvutil.NewEncoder(csvWriter)
var wg sync.WaitGroup
ch := createCSVEncodeCh(&wg, enc, csvWriter, 2)
total := 5
for i := 0; i < total; i++ {
ch <- sampleCSV{ID: i, Name: fmt.Sprintf("n%d", i)}
}
close(ch)
wg.Wait()
csvWriter.Flush()
rows, err := csv.NewReader(bytes.NewReader(buf.Bytes())).ReadAll()
if err != nil {
t.Fatalf("failed to read csv: %v", err)
}
if len(rows) != total+1 { // header + rows
t.Fatalf("expected %d rows, got %d", total+1, len(rows))
}
if rows[1][0] != "0" || rows[1][1] != "n0" {
t.Fatalf("unexpected first data row: %v", rows[1])
}
}