deno.land / std@0.224.0 / csv / csv_stringify_stream_test.ts

csv_stringify_stream_test.ts
View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { CsvStringifyStream } from "./csv_stringify_stream.ts";import { StringifyError } from "./stringify.ts";import { assertEquals, assertRejects } from "../assert/mod.ts";
Deno.test({ name: "CsvStringifyStream handles various inputs", permissions: "none", fn: async (t) => { await t.step("with arrays", async () => { const readable = ReadableStream.from([ ["id", "name"], [1, "foo"], [2, "bar"], ]).pipeThrough(new CsvStringifyStream()); const output = await Array.fromAsync(readable); assertEquals(output, [ "id,name\r\n", "1,foo\r\n", "2,bar\r\n", ]); });
await t.step("with arrays, columns", async () => { const readable = ReadableStream.from([ [1, "foo"], [2, "bar"], // @ts-expect-error `columns` option is not allowed ]).pipeThrough(new CsvStringifyStream({ columns: ["id", "name"] })); await assertRejects( async () => await Array.fromAsync(readable), StringifyError, ); });
await t.step("with `separator`", async () => { const readable = ReadableStream.from([ [1, "one"], [2, "two"], [3, "three"], ]).pipeThrough(new CsvStringifyStream({ separator: "\t" })); const output = await Array.fromAsync(readable); assertEquals(output, [ "1\tone\r\n", "2\ttwo\r\n", "3\tthree\r\n", ]); });
await t.step("with invalid `separator`", async () => { const readable = ReadableStream.from([ ["one", "two", "three"], ]).pipeThrough(new CsvStringifyStream({ separator: "\r\n" })); await assertRejects( async () => await Array.fromAsync(readable), StringifyError, ); });
await t.step("with objects", async () => { const readable = ReadableStream.from([ { id: 1, name: "foo" }, { id: 2, name: "bar" }, { id: 3, name: "baz" }, ]).pipeThrough(new CsvStringifyStream({ columns: ["id", "name"] })); const output = await Array.fromAsync(readable); assertEquals(output, [ "id,name\r\n", "1,foo\r\n", "2,bar\r\n", "3,baz\r\n", ]); });
await t.step("with objects, no columns", async () => { const readable = ReadableStream.from([ { id: 1, name: "foo" }, { id: 2, name: "bar" }, { id: 3, name: "baz" }, // @ts-expect-error `columns` option is required ]).pipeThrough(new CsvStringifyStream()); await assertRejects( async () => await Array.fromAsync(readable), StringifyError, ); }); },});
std

Version Info

Tagged at
6 months ago