From d331bbebaa668c3d14ef46f5ef8acc88a665d77c Mon Sep 17 00:00:00 2001 From: Peter van Gulik Date: Thu, 28 Dec 2023 17:42:38 +0100 Subject: [PATCH] chore: add unit tests for bulkIngestJob and update test for bulkJob --- .../src/__tests__/bulkIngestJob.test.ts | 116 ++++++++++++++++++ .../salesforce/src/__tests__/bulkJob.test.ts | 6 + 2 files changed, 122 insertions(+) create mode 100644 packages/salesforce/src/__tests__/bulkIngestJob.test.ts diff --git a/packages/salesforce/src/__tests__/bulkIngestJob.test.ts b/packages/salesforce/src/__tests__/bulkIngestJob.test.ts new file mode 100644 index 00000000..d2e84da1 --- /dev/null +++ b/packages/salesforce/src/__tests__/bulkIngestJob.test.ts @@ -0,0 +1,116 @@ +import 'jest'; + +import { BulkIngestJob, IngestJobInfo } from '../bulk'; +import { RestClient } from '../restClient'; + +function getRestClientMock(merge?: Partial) { + return { + extraHeaders: {}, + connection: undefined!, + endpoint: '', + contentType: '', + extraHeader: jest.fn(), + post: jest.fn(() => Promise.resolve({})), + get: jest.fn(() => Promise.resolve({})), + patch: jest.fn(() => Promise.resolve({})), + put: jest.fn(() => Promise.resolve({})), + delete: jest.fn(() => Promise.resolve(200)), + getRequestHeaders: jest.fn(), + formatUrl: jest.fn(), + formatBody: jest.fn(), + ...merge + } as unknown as RestClient; +} + +describe('bulkIngestJob', () => { + describe('#uploadData', () => { + it('should call close when keepOpen is false', async () => { + // Arrange + const bulkJobInfo = { + id: '123', + state: 'Open', + lineEnding: 'LF', + } as unknown as IngestJobInfo; + const clientMock = getRestClientMock(); + const data = [{ Id: 1 }]; + + // Act + const bulkJob = new BulkIngestJob(clientMock, bulkJobInfo); + bulkJob.close = jest.fn(); + await bulkJob.uploadData(data, { keepOpen: false }); + + // Assert + expect(bulkJob.close).toHaveBeenCalled(); + }); + it('should call not call close when keepOpen is true', async () => { + // Arrange + const bulkJobInfo = { + id: '123', + state: 'Open', + lineEnding: 'LF', + } as unknown as IngestJobInfo; + const clientMock = getRestClientMock(); + const data = [{ Id: 1 }]; + + // Act + const bulkJob = new BulkIngestJob(clientMock, bulkJobInfo); + bulkJob.close = jest.fn(); + await bulkJob.uploadData(data, { keepOpen: true }); + + // Assert + expect(bulkJob.close).toHaveBeenCalledTimes(0); + expect(clientMock.put).toHaveBeenCalledTimes(0); + expect(clientMock.patch).toHaveBeenCalledTimes(0); + }); + }); + describe('#close', () => { + it('should call put on rest client with encoded data', async () => { + // Arrange + const bulkJobInfo = { + id: '123', + state: 'Open', + lineEnding: 'LF', + } as unknown as IngestJobInfo; + const expectedOptions = { contentType: 'text/csv; charset=utf-8' }; + const clientMock = getRestClientMock(); + const data = [{ Id: 1 }]; + + // Act + const bulkJob = new BulkIngestJob(clientMock, bulkJobInfo); + await bulkJob.uploadData(data, { keepOpen: true }); + await bulkJob.close(); + + // Assert + expect(clientMock.put).toHaveBeenCalledWith('Id\n1\n','123/batches', expectedOptions); + expect(clientMock.patch).toHaveBeenCalledWith({ state: 'UploadComplete' }, '123'); + }); + it('should call put and patch multiple times when job size exceeds chunkDataSize', async () => { + // Arrange + const bulkJobInfo = { + id: '123', + state: 'Open', + lineEnding: 'LF', + } as unknown as IngestJobInfo; + const expectedOptions = { contentType: 'text/csv; charset=utf-8' }; + const clientMock = getRestClientMock({ + post: jest.fn(() => Promise.resolve({ id: '456' })) + }); + const data = [{ Id: 1 },{ Id: 2 }]; + + // Act + const bulkJob = new BulkIngestJob(clientMock, bulkJobInfo); + bulkJob.chunkDataSize = 2; + await bulkJob.uploadData(data, { keepOpen: true }); + await bulkJob.close(); + + // Assert + expect(clientMock.put).toHaveBeenCalledTimes(2); + expect(clientMock.put).toHaveBeenNthCalledWith(1, 'Id\n1\n','123/batches', expectedOptions); + expect(clientMock.put).toHaveBeenNthCalledWith(2, 'Id\n2\n','456/batches', expectedOptions); + + expect(clientMock.patch).toHaveBeenCalledTimes(2); + expect(clientMock.patch).toHaveBeenNthCalledWith(1, { state: 'UploadComplete' }, '123'); + expect(clientMock.patch).toHaveBeenNthCalledWith(2, { state: 'UploadComplete' }, '456'); + }); + }); +}); \ No newline at end of file diff --git a/packages/salesforce/src/__tests__/bulkJob.test.ts b/packages/salesforce/src/__tests__/bulkJob.test.ts index 102987c2..bd4266ec 100644 --- a/packages/salesforce/src/__tests__/bulkJob.test.ts +++ b/packages/salesforce/src/__tests__/bulkJob.test.ts @@ -34,6 +34,12 @@ describe('bulkJob', () => { expect(bulkJob.isAborted).toEqual(false); expect(bulkJob.isFailed).toEqual(false); expect(bulkJob.isComplete).toEqual(false); + expect(bulkJob['lineEndingCharacters']).toEqual('\r\n'); + }); + it('should default line-ending to LF when not set', () => { + const bulkJobInfo = {} as unknown as BulkJobInfo; + const bulkJob = new BulkJob(undefined as unknown as RestClient, bulkJobInfo); + expect(bulkJob['lineEndingCharacters']).toEqual('\n'); }); }); }); \ No newline at end of file