Skip to content

Commit 91fbc80

Browse files
authored
fix: identify propertyName (#614)
* fix: identify propertyName * test: add system-test * fix: unit tests
1 parent 77f9d8e commit 91fbc80

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

handwritten/datastore/src/entity.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,7 @@ export namespace entity {
708708
// tslint:disable-next-line forin
709709
for (const property in properties) {
710710
const value = properties[property];
711+
value.propertyName = property;
711712
entityObject[property] = entity.decodeValueProto(value, wrapNumbers);
712713
}
713714

handwritten/datastore/system-test/datastore.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,31 @@ describe('Datastore', () => {
257257
await datastore.delete(postKey);
258258
});
259259

260+
it('should wrap specified properties via IntegerTypeCastOptions.properties', async () => {
261+
const postKey = datastore.key('Scores');
262+
const largeIntValueAsString = '9223372036854775807';
263+
const panthers = Datastore.int(largeIntValueAsString);
264+
const broncos = 922337203;
265+
let integerTypeCastFunctionCalled = 0;
266+
await datastore.save({key: postKey, data: {panthers, broncos}});
267+
const [entity] = await datastore.get(postKey, {
268+
wrapNumbers: {
269+
// tslint:disable-next-line no-any
270+
integerTypeCastFunction: (value: any) => {
271+
integerTypeCastFunctionCalled++;
272+
return value.toString();
273+
},
274+
properties: 'panthers',
275+
},
276+
});
277+
// verify that value of property 'panthers' was converted via 'integerTypeCastFunction'.
278+
assert.strictEqual(entity.panthers, largeIntValueAsString);
279+
assert.strictEqual(integerTypeCastFunctionCalled, 1);
280+
// verify that value of the property broncos was converted to int by default logic.
281+
assert.strictEqual(entity.broncos, broncos);
282+
await datastore.delete(postKey);
283+
});
284+
260285
it('should save/get/delete with a key name', async () => {
261286
const postKey = datastore.key(['Post', 'post1']);
262287
await datastore.save({key: postKey, data: post});

handwritten/datastore/test/entity.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,12 @@ describe('entity', () => {
997997
decodeValueProtoStub.restore();
998998
});
999999

1000+
it('should identify entity propertyName', () => {
1001+
entity.entityFromEntityProto(entityProto);
1002+
const valueProto = decodeValueProtoStub.getCall(0).args[0];
1003+
assert.strictEqual(valueProto.propertyName, 'number');
1004+
});
1005+
10001006
it('should pass `wrapNumbers` to decodeValueProto as undefined by default', () => {
10011007
entity.entityFromEntityProto(entityProto);
10021008
wrapNumbers = decodeValueProtoStub.getCall(0).args[1];

handwritten/datastore/test/request.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,14 @@ describe('Request', () => {
377377

378378
it('should emit an error from results decoding', done => {
379379
const largeInt = '922337203685477850';
380+
const propertyName = 'points';
380381
request.request_ = (config: RequestConfig, callback: Function) => {
381382
callback(null, {
382383
found: [
383384
{
384385
entity: {
385386
properties: {
386-
points: {
387+
[propertyName]: {
387388
integerValue: largeInt,
388389
valueType: 'integerValue',
389390
},
@@ -401,7 +402,7 @@ describe('Request', () => {
401402
.on('error', (err: Error) => {
402403
assert.deepStrictEqual(
403404
err,
404-
outOfBoundsError({integerValue: largeInt})
405+
outOfBoundsError({integerValue: largeInt, propertyName})
405406
);
406407
setImmediate(() => {
407408
assert.strictEqual(stream.destroyed, true);
@@ -990,6 +991,7 @@ describe('Request', () => {
990991

991992
it('should emit an error from results decoding', done => {
992993
const largeInt = '922337203685477850';
994+
const propertyName = 'points';
993995
sandbox.stub(entity, 'queryToQueryProto');
994996

995997
request.request_ = (config: RequestConfig, callback: Function) => {
@@ -999,7 +1001,7 @@ describe('Request', () => {
9991001
{
10001002
entity: {
10011003
properties: {
1002-
points: {
1004+
[propertyName]: {
10031005
integerValue: largeInt,
10041006
valueType: 'integerValue',
10051007
},
@@ -1017,7 +1019,7 @@ describe('Request', () => {
10171019
.on('error', (err: Error) => {
10181020
assert.deepStrictEqual(
10191021
err,
1020-
outOfBoundsError({integerValue: largeInt})
1022+
outOfBoundsError({integerValue: largeInt, propertyName})
10211023
);
10221024
setImmediate(() => {
10231025
assert.strictEqual(stream.destroyed, true);

0 commit comments

Comments
 (0)