Skip to content

Commit

Permalink
test: add bulk update NUT
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiand391 committed Oct 22, 2024
1 parent a57409a commit 50d03b6
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/commands/data/update/bulk.nut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
import { expect } from 'chai';

describe('data update bulk NUTs', () => {
let session: TestSession;

before(async () => {
session = await TestSession.create({ devhubAuthStrategy: 'NONE' });
});

after(async () => {
await session?.clean();
});

it('should display provided name', () => {
const name = 'World';
const command = `data update bulk --name ${name}`;
const output = execCmd(command, { ensureExitCode: 0 }).shellOutput.stdout;
expect(output).to.contain(name);
});
});
70 changes: 70 additions & 0 deletions test/commands/data/update/resume.nut.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import path from 'node:path';
import { execCmd, TestSession } from '@salesforce/cli-plugins-testkit';
import { expect } from 'chai';
import { Org } from '@salesforce/core';
import { generateUpdatedCsv } from 'test/testUtil.js';
import { generateAccountsCsv } from '../import/resume.nut.js';
import { DataUpdateBulkResult } from '../../../../src/commands/data/update/bulk.js';
import { DataImportBulkResult } from '../../../../src/commands/data/import/bulk.js';

describe('data update resume NUTs', () => {
let session: TestSession;

before(async () => {
session = await TestSession.create({
scratchOrgs: [
{
config: 'config/project-scratch-def.json',
setDefault: true,
},
],
project: { sourceDir: path.join('test', 'test-files', 'data-project') },
devhubAuthStrategy: 'AUTO',
});
});

after(async () => {
await session?.clean();
});

it.only('should update account records', async () => {
const csvFile = await generateAccountsCsv(session.dir);

const result = execCmd<DataImportBulkResult>(
`data import bulk --file ${csvFile} --sobject Account --wait 10 --json`,
{ ensureExitCode: 0 }
).jsonOutput?.result as DataImportBulkResult;

// TODO: set org username above like here:
// https://github.com/salesforcecli/cli-plugins-testkit/blob/main/SAMPLES.md#testing-with-multiple-scratch-orgs
const username = [...session.orgs.keys()][0];

const conn = (
await Org.create({
aliasOrUsername: username,
})
).getConnection();

const importJob = conn.bulk2.job('ingest', {
id: result.jobId,
});

const successfulIds = (await importJob.getSuccessfulResults()).map((r) => r.sf__Id);

const updatedCsv = await generateUpdatedCsv(csvFile, successfulIds);

const dataUpdateResult = execCmd<DataUpdateBulkResult>(
`data update bulk --file ${updatedCsv}--sobject account --wait 10`
).jsonOutput?.result as DataUpdateBulkResult;

expect(dataUpdateResult.processedRecords).to.equal(10_000);
expect(dataUpdateResult.successfulRecords).to.equal(10_000);
expect(dataUpdateResult.failedRecords).to.equal(0);
});
});
38 changes: 38 additions & 0 deletions test/testUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import * as fs from 'node:fs';
import { writeFile } from 'node:fs/promises';
import { PassThrough, Writable } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { promisify } from 'node:util';
import { exec as execSync } from 'node:child_process';
import { Connection } from '@salesforce/core';
import { stringify as csvStringify } from 'csv-stringify/sync';

/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
Expand Down Expand Up @@ -213,3 +215,39 @@ export async function validateJson(filePath: string, totalqty: number): Promise<

expect(parseInt(lengthRes.stdout.trim(), 10)).equal(totalqty);
}

export async function generateUpdatedCsv(sourceCsv: string, ids: string[]) {
const csvReadStream = fs.createReadStream(sourceCsv);
const modifiedRows: Array<{ NAME: string; ID?: string }> = [];
let counter = 0;

await pipeline(
csvReadStream,
new csvParse({ columns: true, delimiter: ',' }),
new PassThrough({
objectMode: true,
transform(row: { NAME: string; ID?: string }, _encoding, callback) {
row.ID = ids[counter];
const modifiedRow = { ID: row['ID'], ...row };
modifiedRows.push(modifiedRow);
counter++;
callback(null, null);
},
}),
// dummy writable
new Writable({
write(_chunk, _encoding, callback) {
callback();
},
})
);

await writeFile(
'updated.csv',
csvStringify(modifiedRows, {
header: true,
})
);

return 'updated.csv';
}

0 comments on commit 50d03b6

Please sign in to comment.