-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_writer_test.go
121 lines (87 loc) · 3.43 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
package peanut_test
import (
"io/ioutil"
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/jimsmart/peanut"
)
var _ = Describe("CSVWriter", func() {
newFn := func(suffix string) peanut.Writer {
w := peanut.NewCSVWriter("./test/output-", suffix)
return w
}
expectedOutput1 := "foo_string,foo_int\n" +
"test 1,1\n" +
"test 2,2\n" +
"test 3,3\n"
expectedOutput2 := "bar_int,bar_string\n" +
"1,test 1\n" +
"2,test 2\n" +
"3,test 3\n"
expectedOutput3 := "baz_string,baz_bool,baz_float32,baz_float64,baz_int,baz_int8,baz_int16,baz_int32,baz_int64,baz_uint,baz_uint8,baz_uint16,baz_uint32,baz_uint64\n" +
"test 1,true,1.234,9.876,-12345,-8,-16,-32,-64,12345,8,16,32,64\n"
AfterEach(func() {
os.Remove("./test/output-Foo-sequential.csv")
os.Remove("./test/output-Bar-sequential.csv")
os.Remove("./test/output-Baz-sequential.csv")
os.Remove("./test/output-Qux-sequential.csv")
os.Remove("./test/output-Foo-interleave.csv")
os.Remove("./test/output-Bar-interleave.csv")
os.Remove("./test/output-Baz-interleave.csv")
os.Remove("./test/output-Qux-interleave.csv")
})
It("should write the correct data when sequential structs are written", func() {
w := newFn("-sequential")
testWritesAndCloseSequential(w)
output1, err := ioutil.ReadFile("./test/output-Foo-sequential.csv")
Expect(err).To(BeNil())
Expect(string(output1)).To(Equal(expectedOutput1))
output2, err := ioutil.ReadFile("./test/output-Bar-sequential.csv")
Expect(err).To(BeNil())
Expect(string(output2)).To(Equal(expectedOutput2))
output3, err := ioutil.ReadFile("./test/output-Baz-sequential.csv")
Expect(err).To(BeNil())
Expect(string(output3)).To(Equal(expectedOutput3))
Expect("./test/output-Qux-sequential.csv").ToNot(BeAnExistingFile())
})
It("should write the correct data when interleaved structs are written", func() {
w := newFn("-interleave")
testWritesAndCloseSequential(w)
output1, err := ioutil.ReadFile("./test/output-Foo-interleave.csv")
Expect(err).To(BeNil())
Expect(string(output1)).To(Equal(expectedOutput1))
output2, err := ioutil.ReadFile("./test/output-Bar-interleave.csv")
Expect(err).To(BeNil())
Expect(string(output2)).To(Equal(expectedOutput2))
output3, err := ioutil.ReadFile("./test/output-Baz-interleave.csv")
Expect(err).To(BeNil())
Expect(string(output3)).To(Equal(expectedOutput3))
Expect("./test/output-Qux-interleave.csv").ToNot(BeAnExistingFile())
})
It("should not write anything when structs are written and cancel is called", func() {
w := newFn("-cancel")
testWritesAndCancel(w)
Expect("./test/output-Foo-cancel.csv").ToNot(BeAnExistingFile())
Expect("./test/output-Bar-cancel.csv").ToNot(BeAnExistingFile())
})
It("should return an error when Write is called after Close", func() {
w := newFn("-close-write")
testWriteAfterClose(w)
Expect("./test/output-Foo-close-write.csv").ToNot(BeAnExistingFile())
})
It("should return an error when the path is bad", func() {
w := peanut.NewCSVWriter("./no-such-location/output-bogus-", "")
err := w.Write(testOutputFoo[0])
Expect(err).To(BeNil())
err = w.Close()
Expect(err).ToNot(BeNil())
})
Context("when given a struct with an unsupported field type", func() {
It("should return an error with an informative message", func() {
w := peanut.NewCSVWriter("./no-such-location/output-bogus-", "")
testWriteBadType(w)
// TODO(js) Do we need further checks, e.g. file not exists ...?
})
})
})