feat: add promise based signatures for createQueryPartitions by alkatrivedi · Pull Request #2284 · googleapis/nodejs-spanner · GitHub
Skip to content
This repository was archived by the owner on Mar 4, 2026. It is now read-only.
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
44 changes: 31 additions & 13 deletions src/batch-transaction.ts
38 changes: 34 additions & 4 deletions test/batch-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
CLOUD_RESOURCE_HEADER,
LEADER_AWARE_ROUTING_HEADER,
} from '../src/common';
import {ExecuteSqlRequest} from '../src/transaction';
import {CallOptions} from 'google-gax';

let promisified = false;
const fakePfy = extend({}, pfy, {
Expand Down Expand Up @@ -159,16 +161,16 @@ describe('BatchTransaction', () => {
},
};

const QUERY = {
const QUERY: ExecuteSqlRequest = {
sql: 'SELECT * FROM Singers',
gaxOptions: GAX_OPTS,
gaxOptions: GAX_OPTS as CallOptions,
params: {},
types: {},
dataBoostEnabled: true,
directedReadOptions: fakeDirectedReadOptionsForRequest,
};

it('should make the correct request', () => {
it('should make the correct request using callback', () => {
const fakeParams = {
params: {a: 'b'},
paramTypes: {a: 'string'},
Expand Down Expand Up @@ -196,7 +198,7 @@ describe('BatchTransaction', () => {
);
});

it('should accept query as string', () => {
it('should accept query as string in a callback based request to createQueryPartitions', () => {
const query = 'SELECT * FROM Singers';

const expectedQuery = Object.assign({}, {sql: query});
Expand All @@ -214,6 +216,34 @@ describe('BatchTransaction', () => {
assert.deepStrictEqual(reqOpts, expectedQuery);
assert.strictEqual(gaxOpts, undefined);
});

it('should make the correct request using await', async () => {
const fakeParams = {
params: {a: 'b'},
paramTypes: {a: 'string'},
dataBoostEnabled: true,
directedReadOptions: fakeDirectedReadOptionsForRequest,
};

const expectedQuery = Object.assign({sql: QUERY.sql}, fakeParams);
const stub = sandbox.stub(batchTransaction, 'createPartitions_');

(sandbox.stub(FakeTransaction, 'encodeParams') as sinon.SinonStub)
.withArgs(QUERY)
.returns(fakeParams);

await batchTransaction.createQueryPartitions(QUERY);

const {client, method, reqOpts, gaxOpts, headers} = stub.lastCall.args[0];
assert.strictEqual(client, 'SpannerClient');
assert.strictEqual(method, 'partitionQuery');
assert.deepStrictEqual(reqOpts, expectedQuery);
assert.strictEqual(gaxOpts, GAX_OPTS);
assert.deepStrictEqual(
headers,
Object.assign({[LEADER_AWARE_ROUTING_HEADER]: 'true'}),
);
});
});

describe('createPartitions_', () => {
Expand Down
15 changes: 13 additions & 2 deletions test/mockserver/mockspanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export class MockSpanner {

this.executeBatchDml = this.executeBatchDml.bind(this);
this.executeStreamingSql = this.executeStreamingSql.bind(this);
this.partitionQuery = this.partitionQuery.bind(this);

this.read = this.read.bind(this);
this.streamingRead = this.streamingRead.bind(this);
Expand Down Expand Up @@ -974,11 +975,21 @@ export class MockSpanner {
}

partitionQuery(
call: grpc.ServerUnaryCall<protobuf.PartitionQueryRequest, {}>,
call: grpc.ServerUnaryCall<
protobuf.PartitionQueryRequest,
protobuf.PartitionResponse
>,
callback: protobuf.Spanner.PartitionQueryCallback,
) {
this.pushRequest(call.request!, call.metadata);
callback(createUnimplementedError('PartitionQuery is not yet implemented'));
this.simulateExecutionTime(this.partitionQuery.name)
.then(() => {
const response = protobuf.PartitionResponse.create({
partitions: [{partitionToken: Buffer.from('mock-token')}],
});
callback(null, response);
})
.catch(err => callback(err));
}

partitionRead(
Expand Down
15 changes: 15 additions & 0 deletions test/spanner.ts