Skip to content

Commit

Permalink
refactor: wip parsing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dtbuchholz committed Jul 19, 2024
1 parent 2639b0c commit 58661c7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,13 @@ export const logger = {

export const parseCsvFile = async function (file: string): Promise<string[][]> {
return await new Promise(function (resolve, reject) {
const parser = parse();
// custom parsing
const parserOptions = {
skipEmptyLines: true,
trim: true,
};

const parser = parse(parserOptions);
const rows: any[] = [];

parser.on("readable", function () {
Expand Down
31 changes: 29 additions & 2 deletions packages/cli/test/import-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const defaultArgs = [
TEST_PROJECT_ID,
];

describe("commands/import-data", function () {
describe.only("commands/import-data", function () {
this.timeout(30000 * TEST_TIMEOUT_FACTOR);

let table1: string;
Expand Down Expand Up @@ -74,7 +74,7 @@ describe("commands/import-data", function () {
restore();
});

test("can import with all values included", async function () {
test("can import a single row", async function () {
const csvStr = `id,val\n1,test_value`;
const csvFile = await temporaryWrite(csvStr, { extension: "csv" });

Expand All @@ -93,6 +93,33 @@ describe("commands/import-data", function () {
equal(successRes, `successfully inserted 1 row into ${defName1}`);
});

test.only("can import with quotes, escape chars, and multi line", async function () {
/* eslint-disable no-useless-escape */
const csvStr = `id,val
1,"I'll work"
1,And I'll work
4,This ends with a backslash \\
7,"Escapes \'single quotes\'"
8,This "quote" works
`;
console.log(csvStr);
const csvFile = await temporaryWrite(csvStr, { extension: "csv" });

const consoleLog = spy(logger, "error");
const stdin = mockStd.stdin();
setTimeout(() => {
stdin.send("y\n");
}, 1000);
await yargs(["import-data", defName1, csvFile, ...defaultArgs])
.command<GlobalOptions>(mod)
.parse();

const res = consoleLog.getCall(0).firstArg;
const successRes = res.match(/^(.*)$/m)[1];

equal(successRes, `successfully inserted 7 rows into ${defName1}`);
});

test("can import with empty row values", async function () {
const csvStr = `id,val\n1,\n,test_value\n`;
const csvFile = await temporaryWrite(csvStr, { extension: "csv" });
Expand Down

0 comments on commit 58661c7

Please sign in to comment.