-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_test.go
49 lines (43 loc) · 979 Bytes
/
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
package csv
import (
"bytes"
"io/ioutil"
"os"
"testing"
)
func TestWriteError(t *testing.T) {
const path = "/path/to/a/nonexistant/directory/to/cause/an/error"
r, err := Create(path)
if err == nil {
r.Close()
os.Remove(path)
}
if _, ok := err.(*os.PathError); !ok {
t.Fatalf("expected *os.PathError; got: %v", err)
}
}
func TestWrite(t *testing.T) {
const path = "writer_test.csv"
r, err := Create(path)
if err != nil {
t.Fatalf("unexpected open error: %v", err)
}
for _, row := range exampleData {
if err := r.Write(row); err != nil {
t.Errorf("unexpected write error: %v", err)
}
}
if err := r.Close(); err != nil {
t.Errorf("unexpected close error: %v", err)
}
f1, _ := os.Open(path)
f2, _ := os.Open("testdata/example.csv")
b1, _ := ioutil.ReadAll(f1)
b2, _ := ioutil.ReadAll(f2)
if bytes.Compare(b1, b2) != 0 {
t.Errorf("expected testdata/example.csv to be the same as %s", path)
}
f1.Close()
f2.Close()
os.Remove(path)
}