forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk_test.go
76 lines (61 loc) · 1.98 KB
/
bulk_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
package command_test
import (
. "github.com/ForceCLI/force/command"
"io/ioutil"
"os"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Bulk", func() {
Describe("SplitCSV", func() {
var (
tempDir string
)
BeforeEach(func() {
tempDir, _ = ioutil.TempDir("", "bulk-test")
})
AfterEach(func() {
os.RemoveAll(tempDir)
})
It("should handle mulit-line field values", func() {
csvFilePath := tempDir + "/bulk.csv"
csvContents := `Id,Description
001000000000000000,single-line value
001000000000000001,single-line value
001000000000000002,"multi-line
value"`
ioutil.WriteFile(csvFilePath, []byte(csvContents), 0644)
batches, err := SplitCSV(csvFilePath, 2)
Expect(err).To(BeNil())
Expect(len(batches)).To(Equal(2))
Expect(batches[0]).To(HavePrefix("Id,Description"))
Expect(batches[1]).To(HavePrefix("Id,Description"))
Expect(batches[0]).To(HaveSuffix("single-line value\n"))
Expect(batches[1]).To(HaveSuffix("multi-line\nvalue\"\n"))
})
It("should handle single-row files", func() {
csvFilePath := tempDir + "/bulk.csv"
csvContents := `Id,Description
001000000000000000,single value`
ioutil.WriteFile(csvFilePath, []byte(csvContents), 0644)
batches, err := SplitCSV(csvFilePath, 2)
Expect(err).To(BeNil())
Expect(len(batches)).To(Equal(1))
Expect(batches[0]).To(HavePrefix("Id,Description"))
Expect(batches[0]).To(HaveSuffix("single value\n"))
})
It("should return an error for an invalid file", func() {
csvFilePath := tempDir + "/bulk.csv"
csvContents := `Column 1
001000000000000000,single value`
ioutil.WriteFile(csvFilePath, []byte(csvContents), 0644)
_, err := SplitCSV(csvFilePath, 2)
Expect(err).To(MatchError(MatchRegexp("wrong number of fields")))
})
It("should return an error for a missing file", func() {
csvFilePath := tempDir + "/no-such-file.csv"
_, err := SplitCSV(csvFilePath, 2)
Expect(err).To(MatchError(MatchRegexp("no such file or directory")))
})
})
})