forked from max-mapper/csv-write-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
172 lines (130 loc) · 3.98 KB
/
test.js
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
170
171
172
var test = require('tape')
var csv = require('./')
var concat = require('concat-stream')
test('encode from basic array', function(t) {
var writer = csv({ headers: ["hello", "foo"]})
writer.pipe(concat(function(data) {
t.equal('hello,foo\nworld,bar\n', data.toString())
t.end()
}))
writer.write(["world", "bar"])
writer.end()
})
test('encode from array w/ escaped quotes in header row', function(t) {
var writer = csv({ headers: ['why "hello" there', "foo"]})
writer.pipe(concat(function(data) {
t.equal('"why ""hello"" there",foo\nworld,bar\n', data.toString())
t.end()
}))
writer.write(["world", "bar"])
writer.end()
})
test('encode from array w/ escaped quotes in cells', function(t) {
var writer = csv({ headers: ["hello", "foo"]})
writer.pipe(concat(function(data) {
t.equal('hello,foo\nworld,"this is an ""escaped"" cell"\n', data.toString())
t.end()
}))
writer.write(["world", 'this is an "escaped" cell'])
writer.end()
})
test('encode from array w/ escaped newline in cells', function(t) {
var writer = csv({ headers: ["hello", "foo"]})
writer.pipe(concat(function(data) {
t.equal('hello,foo\nworld,"this is a\nmultiline cell"\n', data.toString())
t.end()
}))
writer.write(["world", 'this is a\nmultiline cell'])
writer.end()
})
test('encode from array w/ escaped comma in cells', function(t) {
var writer = csv({ headers: ["hello", "foo"]})
writer.pipe(concat(function(data) {
t.equal('hello,foo\nworld,"this is a cell with, commas, in it"\n', data.toString())
t.end()
}))
writer.write(["world", 'this is a cell with, commas, in it'])
writer.end()
})
test('encode from object w/ headers specified', function(t) {
var writer = csv({ headers: ["hello", "foo"]})
writer.pipe(concat(function(data) {
t.equal('hello,foo\nworld,bar\n', data.toString())
t.end()
}))
writer.write({hello: "world", foo: "bar", baz: "taco"})
writer.end()
})
test('encode from object w/ auto headers', function(t) {
var writer = csv()
writer.pipe(concat(function(data) {
t.equal('hello,foo,baz\nworld,bar,taco\n', data.toString())
t.end()
}))
writer.write({hello: "world", foo: "bar", baz: "taco"})
writer.end()
})
test('no headers specified for array', function(t) {
var writer = csv()
writer.pipe(concat(function(data) {
t.equal('foo,bar\n', data.toString())
t.end()
}))
writer.write(['foo', 'bar'])
writer.end()
})
test('no headers displayed', function(t) {
var writer = csv({sendHeaders: false})
writer.pipe(concat(function(data) {
t.equal('world,bar,taco\n', data.toString())
t.end()
}))
writer.write({hello: "world", foo: "bar", baz: "taco"})
writer.end()
})
test('serialize falsy values', function (t) {
// see https://github.com/maxogden/csv-write-stream/issues/8#issuecomment-41873534
var writer = csv({
headers: ['boolean','string','number','null','undefined'],
sendHeaders: false
})
writer.pipe(concat(function (data) {
t.equal('false,false,0,,\n', data.toString())
t.end()
}))
writer.write([false,'false',0,null,undefined])
writer.end()
})
test('handle objects and arrays', function (t) {
var writer = csv({sendHeaders: false})
writer.pipe(concat(function (data) {
t.equal(data, '1,"1,2,3",[object Object]\n')
t.end()
}))
writer.write({a: 1, b: [1,2,3], c: {d: 1}})
writer.end()
})
test('destroy with error', function (t) {
var writer = csv({sendHeaders: false})
t.plan(2)
writer.pipe(concat(function (data) {
t.equal(data, '1,2\n', 'date received')
}))
writer.on('error', function (err) {
writer.end()
t.equal(err.message, 'error')
})
writer.write({a: 1, b : 2})
writer.destroy(new Error('error'))
})
test('lots of cols', function (t) {
var writer = csv()
var obj = {}
writer.pipe(concat(function (data) {
t.equal(data, Object.keys(obj).join(',') + '\n' + Object.keys(obj).join(',') + '\n')
t.end()
}))
for (var i = 0; i < 5000; i++) obj[i] = '' + i
writer.write(obj)
writer.end()
})