Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: no maxquery warning when records.length is 0 #502

Merged
merged 4 commits into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,9 @@ export class Connection extends JSForceConnection {
.on('record', (rec) => records.push(rec))
.on('error', (err) => reject(err))
.on('end', () => {
const totalSize = getNumber(query, 'totalSize') || 0;
if (totalSize > records.length) {
const totalSize = getNumber(query, 'totalSize', 0);
// records.legnth can be 0 in count() query, but totalSize is bigger.
if (records.length && totalSize > records.length) {
void Lifecycle.getInstance().emitWarning(
`The query result is missing ${
totalSize - records.length
Expand Down
36 changes: 28 additions & 8 deletions test/unit/connectionTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const TEST_IP = '1.1.1.1';

describe('Connection', () => {
const testConnectionOptions = { loginUrl: 'connectionTest/loginUrl' };

let warningStub: sinon.SinonStub;
let requestMock: sinon.SinonStub;
let initializeStub: sinon.SinonStub;

Expand All @@ -34,7 +34,7 @@ describe('Connection', () => {

beforeEach(() => {
$$.SANDBOXES.CONNECTION.restore();
$$.SANDBOX.stub(process, 'emitWarning');
warningStub = $$.SANDBOX.stub(process, 'emitWarning');
$$.SANDBOX.stub(MyDomainResolver.prototype, 'resolve').resolves(TEST_IP);

initializeStub = $$.SANDBOX.stub(jsforce.Connection.prototype, 'initialize').returns();
Expand Down Expand Up @@ -270,7 +270,7 @@ describe('Connection', () => {

const records = [{ id: 7 }, { id: 8 }, { id: 10 }, { id: 11 }, { id: 12 }];
const queryResponse = { totalSize: 50000, done: true, records };
requestMock.returns(Promise.resolve(queryResponse));
requestMock.resolves(queryResponse);

const conn = await Connection.create({ authInfo: fromStub(testAuthInfo) });
const toolingQuerySpy = $$.SANDBOX.spy(conn.tooling, 'query');
Expand All @@ -282,21 +282,41 @@ describe('Connection', () => {
expect(toolingQuerySpy.firstCall.args[1]).to.have.property('maxFetch', 50000);
});

it('tooling.autoFetchQuery() should not throw a warning when records is empty', async () => {
const soql = 'TEST_SOQL';

const queryResponse = { totalSize: 5, done: true, records: [] };
requestMock.resolves(queryResponse);

const conn = await Connection.create({ authInfo: fromStub(testAuthInfo) });
const toolingQuerySpy = $$.SANDBOX.spy(conn.tooling, 'query');

$$.SANDBOX.stub(ConfigAggregator.prototype, 'getInfo').returns({ value: 3 } as ConfigInfo);
await conn.tooling.autoFetchQuery(soql);

expect(toolingQuerySpy.firstCall.args[0]).to.equal(soql);
expect(toolingQuerySpy.firstCall.args[1]).to.have.property('autoFetch', true);
expect(toolingQuerySpy.firstCall.args[1]).to.have.property('maxFetch', 3);
expect(warningStub.callCount).to.equal(0);
});

it('tooling.autoFetchQuery() should throw a warning when more than 10k records returned without the config', async () => {
const soql = 'TEST_SOQL';

const records = [{ id: 7 }, { id: 8 }, { id: 10 }, { id: 11 }, { id: 12 }];
const queryResponse = { totalSize: 5, done: true, records };
requestMock.returns(Promise.resolve(queryResponse));
requestMock.resolves(queryResponse);

const conn = await Connection.create({ authInfo: fromStub(testAuthInfo) });
const toolingQuerySpy = $$.SANDBOX.spy(conn.tooling, 'query');

$$.SANDBOX.stub(ConfigAggregator.prototype, 'getInfo').returns({ value: 3 } as ConfigInfo);
await conn.tooling.autoFetchQuery(soql);

expect(toolingQuerySpy.firstCall.args[0]).to.equal(soql);
expect(toolingQuerySpy.firstCall.args[1]).to.have.property('autoFetch', true);
expect(toolingQuerySpy.firstCall.args[1]).to.have.property('maxFetch', 3);
expect(warningStub.callCount).to.equal(1);
});

it('autoFetch() should reject the promise upon query error', async () => {
Expand Down Expand Up @@ -353,7 +373,7 @@ describe('Connection', () => {
});

it('singleRecordQuery throws on no-records', async () => {
requestMock.returns(Promise.resolve({ totalSize: 0, records: [] }));
requestMock.resolves({ totalSize: 0, records: [] });
const conn = await Connection.create({ authInfo: fromStub(testAuthInfoWithDomain) });

try {
Expand All @@ -365,7 +385,7 @@ describe('Connection', () => {
});

it('singleRecordQuery throws on multiple records', async () => {
requestMock.returns(Promise.resolve({ totalSize: 2, records: [{ id: 1 }, { id: 2 }] }));
requestMock.resolves({ totalSize: 2, records: [{ id: 1 }, { id: 2 }] });
const conn = await Connection.create({ authInfo: fromStub(testAuthInfoWithDomain) });

try {
Expand All @@ -377,7 +397,7 @@ describe('Connection', () => {
});

it('singleRecordQuery throws on multiple records with options', async () => {
requestMock.returns(Promise.resolve({ totalSize: 2, records: [{ id: 1 }, { id: 2 }] }));
requestMock.resolves({ totalSize: 2, records: [{ id: 1 }, { id: 2 }] });
const conn = await Connection.create({ authInfo: fromStub(testAuthInfoWithDomain) });

try {
Expand All @@ -394,7 +414,7 @@ describe('Connection', () => {
id: '123',
name: 'testName',
};
requestMock.returns(Promise.resolve({ totalSize: 1, records: [mockSingleRecord] }));
requestMock.resolves({ totalSize: 1, records: [mockSingleRecord] });
const soql = 'TEST_SOQL';

const conn = await Connection.create({ authInfo: fromStub(testAuthInfoWithDomain) });
Expand Down