Skip to content

Commit 49138d8

Browse files
committed
refactor: createReadPartitions signatures
1 parent e8f5ca1 commit 49138d8

File tree

4 files changed

+86
-7
lines changed

4 files changed

+86
-7
lines changed

src/batch-transaction.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ import {PreciseDate} from '@google-cloud/precise-date';
1818
import {promisifyAll} from '@google-cloud/promisify';
1919
import * as extend from 'extend';
2020
import * as is from 'is';
21-
import {Snapshot} from './transaction';
21+
import {ReadRequest, Snapshot} from './transaction';
2222
import {google} from '../protos/protos';
2323
import {Session, Database} from '.';
2424
import {
2525
CLOUD_RESOURCE_HEADER,
26+
ResourceCallback,
2627
addLeaderAwareRoutingHeader,
2728
} from '../src/common';
2829
import {startTrace, setSpanError, traceConfig} from './instrument';
@@ -34,6 +35,16 @@ export interface TransactionIdentifier {
3435
timestamp?: google.protobuf.ITimestamp;
3536
}
3637

38+
export type CreateReadPartitionsResponse = [
39+
google.spanner.v1.IPartitionReadRequest,
40+
google.spanner.v1.IPartitionResponse,
41+
];
42+
43+
export type CreateReadPartitionsCallback = ResourceCallback<
44+
google.spanner.v1.IPartitionReadRequest,
45+
google.spanner.v1.IPartitionResponse
46+
>;
47+
3748
/**
3849
* Use a BatchTransaction object to create partitions and read/query against
3950
* your Cloud Spanner database.
@@ -266,7 +277,17 @@ class BatchTransaction extends Snapshot {
266277
* @param {CreateReadPartitionsCallback} [callback] Callback function.
267278
* @returns {Promise<CreateReadPartitionsResponse>}
268279
*/
269-
createReadPartitions(options, callback) {
280+
createReadPartitions(
281+
options: ReadRequest,
282+
): Promise<CreateReadPartitionsResponse>;
283+
createReadPartitions(
284+
options: ReadRequest,
285+
callback: CreateReadPartitionsCallback,
286+
): void;
287+
createReadPartitions(
288+
options: ReadRequest,
289+
cb?: CreateReadPartitionsCallback,
290+
): void | Promise<CreateReadPartitionsResponse> {
270291
const traceConfig: traceConfig = {
271292
opts: this._observabilityOptions,
272293
dbName: this.getDBName(),
@@ -276,7 +297,7 @@ class BatchTransaction extends Snapshot {
276297
'BatchTransaction.createReadPartitions',
277298
traceConfig,
278299
span => {
279-
const reqOpts = Object.assign({}, options, {
300+
const reqOpts: ReadRequest = Object.assign({}, options, {
280301
keySet: Snapshot.encodeKeySet(options),
281302
});
282303

@@ -303,7 +324,7 @@ class BatchTransaction extends Snapshot {
303324
}
304325

305326
span.end();
306-
callback(err, partitions, resp);
327+
cb!(err, partitions, resp);
307328
},
308329
);
309330
},

test/batch-transaction.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ describe('BatchTransaction', () => {
341341
directedReadOptions: fakeDirectedReadOptionsForRequest,
342342
};
343343

344-
it('should make the correct request', () => {
344+
it('should make the correct request using callback', () => {
345345
const fakeKeySet = {};
346346
const expectedQuery = {
347347
table: QUERY.table,
@@ -368,6 +368,34 @@ describe('BatchTransaction', () => {
368368
Object.assign({[LEADER_AWARE_ROUTING_HEADER]: 'true'}),
369369
);
370370
});
371+
372+
it('should make the correct request using await', async () => {
373+
const fakeKeySet = {};
374+
const expectedQuery = {
375+
table: QUERY.table,
376+
keySet: fakeKeySet,
377+
dataBoostEnabled: true,
378+
directedReadOptions: fakeDirectedReadOptionsForRequest,
379+
};
380+
381+
const stub = sandbox.stub(batchTransaction, 'createPartitions_');
382+
383+
(sandbox.stub(FakeTransaction, 'encodeKeySet') as sinon.SinonStub)
384+
.withArgs(QUERY)
385+
.returns(fakeKeySet);
386+
387+
await batchTransaction.createReadPartitions(QUERY);
388+
389+
const {client, method, reqOpts, gaxOpts, headers} = stub.lastCall.args[0];
390+
assert.strictEqual(client, 'SpannerClient');
391+
assert.strictEqual(method, 'partitionRead');
392+
assert.deepStrictEqual(reqOpts, expectedQuery);
393+
assert.strictEqual(gaxOpts, GAX_OPTS);
394+
assert.deepStrictEqual(
395+
headers,
396+
Object.assign({[LEADER_AWARE_ROUTING_HEADER]: 'true'}),
397+
);
398+
});
371399
});
372400

373401
describe('execute', () => {

test/mockserver/mockspanner.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export class MockSpanner {
267267

268268
this.read = this.read.bind(this);
269269
this.streamingRead = this.streamingRead.bind(this);
270+
this.partitionRead = this.partitionRead.bind(this);
270271
}
271272

272273
/**
@@ -982,11 +983,21 @@ export class MockSpanner {
982983
}
983984

984985
partitionRead(
985-
call: grpc.ServerUnaryCall<protobuf.PartitionReadRequest, {}>,
986+
call: grpc.ServerUnaryCall<
987+
protobuf.PartitionReadRequest,
988+
protobuf.PartitionResponse
989+
>,
986990
callback: protobuf.Spanner.PartitionReadCallback,
987991
) {
988992
this.pushRequest(call.request!, call.metadata);
989-
callback(createUnimplementedError('PartitionQuery is not yet implemented'));
993+
this.simulateExecutionTime(this.partitionRead.name)
994+
.then(() => {
995+
const response = protobuf.PartitionResponse.create({
996+
partitions: [{partitionToken: Buffer.from('mock-token')}],
997+
});
998+
callback(null, response);
999+
})
1000+
.catch(err => callback(err));
9901001
}
9911002

9921003
private _updateTransaction(

test/spanner.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3651,6 +3651,25 @@ describe('Spanner with mock server', () => {
36513651
});
36523652
});
36533653

3654+
describe('createReadPartitions', () => {
3655+
it('should create set of read partitions', async () => {
3656+
const database = newTestDatabase({min: 0, incStep: 1});
3657+
const query = {
3658+
table: 'abc',
3659+
keys: ['a', 'b'],
3660+
ranges: [{}, {}],
3661+
gaxOptions: {},
3662+
dataBoostEnabled: true,
3663+
};
3664+
const [transaction] = await database.createBatchTransaction();
3665+
const [readPartitions] = await transaction.createReadPartitions(query);
3666+
assert.strictEqual(Object.keys(readPartitions).length, 1);
3667+
assert.strictEqual(readPartitions[0].table, 'abc');
3668+
transaction.close();
3669+
await database.close();
3670+
});
3671+
});
3672+
36543673
describe('pdml', () => {
36553674
it('should retry on aborted error', async () => {
36563675
const database = newTestDatabase();

0 commit comments

Comments
 (0)