From a9fed8118437012a52dc5a29fa47fa5d83dc93af Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Sat, 15 Feb 2025 23:38:41 -0800 Subject: [PATCH] Refactor to simplify functions --- test/normalization.test.js | 18 +++++------ test/utils.js | 63 ++------------------------------------ 2 files changed, 11 insertions(+), 70 deletions(-) diff --git a/test/normalization.test.js b/test/normalization.test.js index 81342ce..8398651 100644 --- a/test/normalization.test.js +++ b/test/normalization.test.js @@ -1,7 +1,7 @@ import { suite, test } from 'node:test'; import { deepStrictEqual } from 'node:assert/strict'; -import { csvStreamEqual, csvStreamEqualWritable, createCSVMockStream, assertConsole } from './utils.js'; +import { csvStreamEqualWritable, createCSVMockStream, assertConsole } from './utils.js'; import { CSVReader } from '../src/core.js'; import { toCamelCase, expandScientificNotation, CSVNormalizer, CSVDenormalizer } from '../src/normalization.js'; @@ -278,14 +278,14 @@ suite('CSVNormalizer and CSVDenormalizer end-to-end', { concurrency: true }, () { name: 'bigintCol', displayName: 'BigInt Column', type: 'bigint' }, { name: 'dateCol', displayName: 'Date Column', type: 'date' } ] - const stream = new CSVReader('./test/data/normalization.csv') + await new CSVReader('./test/data/normalization.csv') .pipeThrough(new CSVNormalizer(headers)) - .pipeThrough(new CSVDenormalizer()); - await csvStreamEqual(stream, [ - ['String Column', 'Number Column', 'BigInt Column', 'Date Column'], - ['abc 👨‍👩‍👧‍👦', 123456789.123456, '1000000000000000000', '2024-01-01T00:00:00.000Z'], - [', " 🏴‍☠️', 1000, '-1234567890000000000000000', '2024-06-01T00:00:00.000Z'], - ['N/A', 123100, '1000000000000000000', '2024-12-31T08:00:00.000Z'] - ]); + .pipeThrough(new CSVDenormalizer()) + .pipeTo(csvStreamEqualWritable([ + ['String Column', 'Number Column', 'BigInt Column', 'Date Column'], + ['abc 👨‍👩‍👧‍👦', 123456789.123456, '1000000000000000000', '2024-01-01T00:00:00.000Z'], + [', " 🏴‍☠️', 1000, '-1234567890000000000000000', '2024-06-01T00:00:00.000Z'], + ['N/A', 123100, '1000000000000000000', '2024-12-31T08:00:00.000Z'] + ])); }); }); \ No newline at end of file diff --git a/test/utils.js b/test/utils.js index ea1a7c4..7fdb41f 100644 --- a/test/utils.js +++ b/test/utils.js @@ -5,50 +5,8 @@ import { normalize } from 'node:path'; /** @import { TestContext } from 'node:test'; */ /** - * Log an entire stream along with the field count for each row and the total row count. - * - * @param {ReadableStream} stream A stream of CSV data. - * @param {string} [id] Optional label to include in the log. - */ -export async function csvStreamLog(stream, id) { - let out = '============================================================\n'; - out += `\x1b[45m${id ?? ''}\x1b[0m`; - out += '\n------------------------------------------------------------\n'; - let totalRows = 0; - for await (const value of stream) { - const row = JSON.parse(value); - const rowNoNewlines = row.map(f => f.replaceAll('\n', '\\n').replaceAll('\r', '\\r')); - out += `${row.length} || ${rowNoNewlines.join(' | ')}\n`; - totalRows++; - } - out += '------------------------------------------------------------\n'; - out += `Total row count: ${totalRows}\n`; - out += '============================================================'; - console.log(out); -} - -/** - * Compare each field in a CSV stream with a given array, assuming each chunk of the stream is a - * serialized `Array` representing a row of a CSV. - * - * @param {ReadableStream} stream A stream of CSV data. - * @param {Array>} csv A 2-D array representing the CSV file, where each inner array - * is a row of the CSV. - */ -export async function csvStreamEqual(stream, csv) { - let i = 0; - for await (const value of stream) { - const row = JSON.parse(value); - for (let j = 0; j < csv[i].length; j++) { - deepStrictEqual(row[j], csv[i][j]); - } - i++; - } -} - -/** - * A `WritableStream` version of `csvStreamEqual`. Compares each field in a CSV stream with a given - * array, assuming each chunk of the stream is a serialized `Array` representing a row of a CSV. + * A `WritableStream` sink that compares each field in a CSV stream with a given array, assuming each chunk of the + * stream is a serialized `Array` representing a row of a CSV. * * @param {Array>} csv A 2-D array representing the CSV file, where each inner array * is a row of the CSV. @@ -67,23 +25,6 @@ export function csvStreamEqualWritable(csv) { }); } -/** - * Negative version of `csvStreamEqual`. Tests that the given stream is NOT equal to the given CSV. - * - * @param {ReadableStream} stream A stream of CSV data. - * @param {Array>} csv A 2-D array representing the CSV file, where each inner array - */ -export async function csvStreamNotEqual(stream, csv) { - let i = 0; - for await (const value of stream) { - const row = JSON.parse(value); - for (let j = 0; j < csv[i].length; j++) { - notStrictEqual(row[j], csv[i][j]); - } - i++; - } -} - /** * Negative version of `csvStreamEqualWritable`. Tests that the piped stream is NOT equal to the given CSV. * -- 2.47.3