2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-06-28 06:07:36 +00:00
|
|
|
import { CsvStringifyStream } from "./stringify_stream.ts";
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals, assertRejects } from "@std/assert";
|
2023-03-27 02:48:12 +00:00
|
|
|
|
|
|
|
Deno.test({
|
2023-12-28 04:40:00 +00:00
|
|
|
name: "CsvStringifyStream handles various inputs",
|
2023-03-27 02:48:12 +00:00
|
|
|
permissions: "none",
|
|
|
|
fn: async (t) => {
|
|
|
|
await t.step("with arrays", async () => {
|
2023-07-17 06:08:20 +00:00
|
|
|
const readable = ReadableStream.from([
|
2023-03-27 02:48:12 +00:00
|
|
|
["id", "name"],
|
|
|
|
[1, "foo"],
|
|
|
|
[2, "bar"],
|
2023-11-10 19:00:28 +00:00
|
|
|
]).pipeThrough(new CsvStringifyStream());
|
|
|
|
const output = await Array.fromAsync(readable);
|
2023-03-27 02:48:12 +00:00
|
|
|
assertEquals(output, [
|
|
|
|
"id,name\r\n",
|
|
|
|
"1,foo\r\n",
|
|
|
|
"2,bar\r\n",
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
await t.step("with arrays, columns", async () => {
|
2023-07-17 06:08:20 +00:00
|
|
|
const readable = ReadableStream.from([
|
2023-03-27 02:48:12 +00:00
|
|
|
[1, "foo"],
|
|
|
|
[2, "bar"],
|
2023-11-10 19:00:28 +00:00
|
|
|
// @ts-expect-error `columns` option is not allowed
|
|
|
|
]).pipeThrough(new CsvStringifyStream({ columns: ["id", "name"] }));
|
|
|
|
await assertRejects(
|
|
|
|
async () => await Array.fromAsync(readable),
|
2024-07-10 10:32:59 +00:00
|
|
|
TypeError,
|
2023-11-10 19:00:28 +00:00
|
|
|
);
|
2023-03-27 02:48:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await t.step("with `separator`", async () => {
|
2023-07-17 06:08:20 +00:00
|
|
|
const readable = ReadableStream.from([
|
2023-03-27 02:48:12 +00:00
|
|
|
[1, "one"],
|
|
|
|
[2, "two"],
|
|
|
|
[3, "three"],
|
2023-11-10 19:00:28 +00:00
|
|
|
]).pipeThrough(new CsvStringifyStream({ separator: "\t" }));
|
|
|
|
const output = await Array.fromAsync(readable);
|
2023-03-27 02:48:12 +00:00
|
|
|
assertEquals(output, [
|
|
|
|
"1\tone\r\n",
|
|
|
|
"2\ttwo\r\n",
|
|
|
|
"3\tthree\r\n",
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
await t.step("with invalid `separator`", async () => {
|
2023-07-17 06:08:20 +00:00
|
|
|
const readable = ReadableStream.from([
|
2023-03-27 02:48:12 +00:00
|
|
|
["one", "two", "three"],
|
2023-11-10 19:00:28 +00:00
|
|
|
]).pipeThrough(new CsvStringifyStream({ separator: "\r\n" }));
|
|
|
|
await assertRejects(
|
|
|
|
async () => await Array.fromAsync(readable),
|
2024-07-10 10:32:59 +00:00
|
|
|
TypeError,
|
2023-11-10 19:00:28 +00:00
|
|
|
);
|
2023-03-27 02:48:12 +00:00
|
|
|
});
|
|
|
|
|
2024-06-27 03:47:27 +00:00
|
|
|
await t.step("with invalid `columns`", async () => {
|
|
|
|
const readable = ReadableStream.from([
|
|
|
|
["one", "two", "three"],
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
]).pipeThrough(new CsvStringifyStream({ columns: { length: 1 } as any }));
|
|
|
|
await assertRejects(
|
|
|
|
async () => await Array.fromAsync(readable),
|
2024-07-10 10:32:59 +00:00
|
|
|
TypeError,
|
2024-06-27 03:47:27 +00:00
|
|
|
"No property accessor function was provided for object",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-03-27 02:48:12 +00:00
|
|
|
await t.step("with objects", async () => {
|
2023-07-17 06:08:20 +00:00
|
|
|
const readable = ReadableStream.from([
|
2023-03-27 02:48:12 +00:00
|
|
|
{ id: 1, name: "foo" },
|
|
|
|
{ id: 2, name: "bar" },
|
|
|
|
{ id: 3, name: "baz" },
|
2023-11-10 19:00:28 +00:00
|
|
|
]).pipeThrough(new CsvStringifyStream({ columns: ["id", "name"] }));
|
|
|
|
const output = await Array.fromAsync(readable);
|
2023-03-27 02:48:12 +00:00
|
|
|
assertEquals(output, [
|
2023-03-30 06:36:17 +00:00
|
|
|
"id,name\r\n",
|
2023-03-27 02:48:12 +00:00
|
|
|
"1,foo\r\n",
|
|
|
|
"2,bar\r\n",
|
|
|
|
"3,baz\r\n",
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
await t.step("with objects, no columns", async () => {
|
2023-07-17 06:08:20 +00:00
|
|
|
const readable = ReadableStream.from([
|
2023-03-27 02:48:12 +00:00
|
|
|
{ id: 1, name: "foo" },
|
|
|
|
{ id: 2, name: "bar" },
|
|
|
|
{ id: 3, name: "baz" },
|
2023-11-10 19:00:28 +00:00
|
|
|
// @ts-expect-error `columns` option is required
|
|
|
|
]).pipeThrough(new CsvStringifyStream());
|
|
|
|
await assertRejects(
|
|
|
|
async () => await Array.fromAsync(readable),
|
2024-07-10 10:32:59 +00:00
|
|
|
TypeError,
|
2023-11-10 19:00:28 +00:00
|
|
|
);
|
2023-03-27 02:48:12 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|