Skip to content

Commit

Permalink
fix: fix apex test result query size
Browse files Browse the repository at this point in the history
@W-11622957@ fix apex test result query to set use actual result size
  • Loading branch information
peternhale committed Nov 30, 2023
1 parent 88ba0fe commit 031be9e
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/tests/asyncTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { Connection } from '@salesforce/core';
import { ConfigAggregator, Connection } from '@salesforce/core';
import { CancellationToken, Progress } from '../common';
import { nls } from '../i18n';
import { AsyncTestRun, StreamingClient } from '../streaming';
Expand Down Expand Up @@ -33,6 +33,7 @@ import * as util from 'util';
import { QUERY_RECORD_LIMIT } from './constants';
import { CodeCoverage } from './codeCoverage';
import { HttpRequest } from 'jsforce';
import { OrgConfigProperties } from '@salesforce/core/lib/org/orgConfigProperties';

export class AsyncTests {
public readonly connection: Connection;
Expand Down Expand Up @@ -292,17 +293,20 @@ export class AsyncTests {
public async getAsyncTestResults(
testQueueResult: ApexTestQueueItem
): Promise<ApexTestResult[]> {
const config = await ConfigAggregator.create();
let apexTestResultQuery = 'SELECT Id, QueueItemId, StackTrace, Message, ';
apexTestResultQuery +=
'RunTime, TestTimestamp, AsyncApexJobId, MethodName, Outcome, ApexLogId, ';
apexTestResultQuery +=
'ApexClass.Id, ApexClass.Name, ApexClass.NamespacePrefix ';
apexTestResultQuery += 'FROM ApexTestResult WHERE QueueItemId IN (%s)';

const apexTestResultQueryCount =
'Select count(id) from ApexTestResult where QueueItemId IN (%s)';
const apexResultIds = testQueueResult.records.map(record => record.Id);

// iterate thru ids, create query with id, & compare query length to char limit
const queries: string[] = [];
const countQueries: string[] = [];
for (let i = 0; i < apexResultIds.length; i += QUERY_RECORD_LIMIT) {
const recordSet: string[] = apexResultIds
.slice(i, i + QUERY_RECORD_LIMIT)
Expand All @@ -311,12 +315,26 @@ export class AsyncTests {
apexTestResultQuery,
recordSet.join(',')
);
const countQuery: string = util.format(
apexTestResultQueryCount,
recordSet.join(',')
);
countQueries.push(countQuery);
queries.push(query);
}

const queryPromises = queries.map(query => {
const countQueryPromises = countQueries.map(query => {
return this.connection.singleRecordQuery<{ expr0: number }>(query);
});

const countQueryResult = await Promise.all(countQueryPromises);

const queryPromises = queries.map((query, index) => {
return this.connection.tooling.query<ApexTestResultRecord>(query, {
autoFetch: true
autoFetch: true,
maxFetch:
countQueryResult[index]?.expr0 ??
config.getPropertyValue(OrgConfigProperties.ORG_MAX_QUERY_LIMIT)
});
});
const apexTestResults = await Promise.all(queryPromises);
Expand Down

0 comments on commit 031be9e

Please sign in to comment.