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';
{ 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
/** @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<any>` representing a row of a CSV.
- *
- * @param {ReadableStream} stream A stream of CSV data.
- * @param {Array<Array<any>>} 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<any>` 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<any>` representing a row of a CSV.
*
* @param {Array<Array<any>>} csv A 2-D array representing the CSV file, where each inner array
* is a row of the 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<Array<any>>} 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.
*