-
Notifications
You must be signed in to change notification settings - Fork 574
/
stream_test.ts
114 lines (109 loc) · 3.25 KB
/
stream_test.ts
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { CSVStream } from "./stream.ts";
import { ERR_QUOTE, ParseError } from "./_io.ts";
import { readableStreamFromIterable } from "../../streams/conversion.ts";
import {
assert,
assertEquals,
assertRejects,
assertStringIncludes,
} from "../../testing/asserts.ts";
import { fromFileUrl, join } from "../../path/mod.ts";
const testdataDir = join(fromFileUrl(import.meta.url), "../../testdata");
Deno.test({
name: "[encoding/csv/stream] CSVStream",
permissions: {
read: [testdataDir],
},
fn: async () => {
const file = await Deno.open(join(testdataDir, "simple.csv"));
const readable = file.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(new CSVStream());
const records = [] as Array<Array<string>>;
for await (const record of readable) {
records.push(record);
}
assertEquals(records, [
["id", "name"],
["1", "foobar"],
["2", "barbaz"],
]);
},
});
Deno.test({
name: "[encoding/csv/stream] CSVStream with `comment` option",
permissions: { read: [testdataDir] },
fn: async () => {
const file = await Deno.open(join(testdataDir, "complex.csv"));
const readable = file.readable
.pipeThrough(new TextDecoderStream())
.pipeThrough(
new CSVStream({
comment: "#",
}),
);
const records = [] as Array<Array<string>>;
for await (const record of readable) {
records.push(record);
}
assertEquals(records, [
["id", "name", "email"],
["1", "deno", "deno@example.com"],
["2", "node", "node@example.com"],
["3", "", "test@example.com"],
]);
},
});
Deno.test({
name: "[encoding/csv/stream] CSVStream with `separator` option",
fn: async () => {
const encoder = new TextEncoder();
const readable = readableStreamFromIterable([
encoder.encode("id\tname\n"),
encoder.encode("1\tfoo\n"),
encoder.encode("2\tbar\n"),
encoder.encode("3\tbaz\n"),
])
.pipeThrough(new TextDecoderStream())
.pipeThrough(
new CSVStream({
separator: "\t",
}),
);
const records = [] as Array<Array<string>>;
for await (const record of readable) {
records.push(record);
}
assertEquals(records, [
["id", "name"],
["1", "foo"],
["2", "bar"],
["3", "baz"],
]);
},
});
Deno.test({
name: "[encoding/csv/stream] CSVStream with invalid csv",
fn: async () => {
const encoder = new TextEncoder();
const readable = readableStreamFromIterable([
encoder.encode("id,name\n"),
encoder.encode("\n"),
encoder.encode("1,foo\n"),
encoder.encode('2,"baz\n'),
]).pipeThrough(new TextDecoderStream()).pipeThrough(
new CSVStream(),
);
const reader = readable.getReader();
assertEquals(await reader.read(), { done: false, value: ["id", "name"] });
assertEquals(await reader.read(), { done: false, value: ["1", "foo"] });
await assertRejects(() => reader.read(), (error: unknown) => {
assert(error instanceof ParseError);
assertEquals(error.startLine, 4);
assertEquals(error.line, 5);
assertEquals(error.column, 0);
assertStringIncludes(error.message, ERR_QUOTE);
});
},
});