Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Sawchuk committed Aug 1, 2017
1 parent 9ed0d72 commit ada4d67
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 12 deletions.
26 changes: 16 additions & 10 deletions packages/datastore/src/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ function entityToEntityProto(entityObject) {
}, {})
};

if (excludeFromIndexes && excludeFromIndexes.length > 0) {
excludeFromIndexes.forEach(function(excludePath) {
excludePathFromEntity(entityProto, excludePath);
});
}

return entityProto;

function excludePathFromEntity(entity, path) {
var arrayIndex = path.indexOf('[]');
var entityIndex = path.indexOf('.');
Expand All @@ -404,8 +412,10 @@ function entityToEntityProto(entityObject) {
var hasEntityPath = entityIndex > -1;

if (!hasArrayPath && !hasEntityPath) {
// This is the property to exclude!
entity.properties[path].excludeFromIndexes = true;
if (entity.properties[path]) {
// This is the property to exclude!
entity.properties[path].excludeFromIndexes = true;
}
return;
}

Expand All @@ -424,6 +434,10 @@ function entityToEntityProto(entityObject) {
var firstPathPart = splitPath.shift();
var remainderPath = splitPath.join(delimiter).replace(/^(\.|[])/, '');

if (!entity.properties[firstPathPart]) {
return;
}

if (firstPathPartIsArray) {
var array = entity.properties[firstPathPart].arrayValue;
array.values.forEach(function(arrayValue) {
Expand All @@ -434,14 +448,6 @@ function entityToEntityProto(entityObject) {
excludePathFromEntity(parentEntity, remainderPath);
}
}

if (excludeFromIndexes && excludeFromIndexes.length > 0) {
excludeFromIndexes.forEach(function(excludePath) {
excludePathFromEntity(entityProto, excludePath);
});
}

return entityProto;
}

entity.entityToEntityProto = entityToEntityProto;
Expand Down
19 changes: 19 additions & 0 deletions packages/datastore/system-test/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ describe('Datastore', function() {
metadata: {
longString: longString,
otherProperty: 'value',
obj: {
longStringArray: [
{
longString: longString,
nestedLongStringArray: [
{
longString: longString,
nestedProperty: true
},
{
longString: longString
}
]
}
]
},
longStringArray: [
{
longString: longString,
Expand All @@ -113,6 +129,9 @@ describe('Datastore', function() {
data: data,
excludeFromIndexes: [
'longString',
'metadata.obj.longString',
'metadata.obj.longStringArray[].longString',
'metadata.obj.longStringArray[].nestedLongStringArray[].longString',
'metadata.longString',
'metadata.longStringArray[].longString',
'metadata.longStringArray[].nestedLongStringArray[].longString'
Expand Down
155 changes: 153 additions & 2 deletions packages/datastore/test/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,12 +501,14 @@ describe('entity', function() {
var value = 'Stephen';

var entityObject = {
name: value
data: {
name: value
}
};

var expectedEntityProto = {
key: null,
properties: entityObject
properties: entityObject.data
};

entity.encodeValue = function(value_) {
Expand All @@ -519,6 +521,155 @@ describe('entity', function() {
expectedEntityProto
);
});

it('should respect excludeFromIndexes', function() {
var value = 'Stephen';

var entityObject = {
excludeFromIndexes: [
'name',
'entity.name',
'array[].name',
'array[].entity.name',
'array[].entity.array[].name',
'array[].array[].entity.name'
],

data: {
name: value,

entity: {
name: value
},

array: [
{
name: value
},
{
entity: {
name: value,
array: [
{
name: value
}
]
}
},
{
array: [
{
entity: {
name: value
}
}
]
}
]
}
};

var expectedEntityProto = {
key: null,
properties: {
name: {
stringValue: value,
excludeFromIndexes: true
},
entity: {
entityValue: {
properties: {
name: {
stringValue: value,
excludeFromIndexes: true
}
}
}
},
array: {
arrayValue: {
values: [
{
entityValue: {
properties: {
name: {
stringValue: value,
excludeFromIndexes: true
}
}
}
},
{
entityValue: {
properties: {
entity: {
entityValue: {
properties: {
name: {
stringValue: value,
excludeFromIndexes: true
},
array: {
arrayValue: {
values: [
{
entityValue: {
properties: {
name: {
stringValue: value,
excludeFromIndexes: true
}
}
}
}
]
}
}
}
}
}
}
}
},
{
entityValue: {
properties: {
array: {
arrayValue: {
values: [
{
entityValue: {
properties: {
entity: {
entityValue: {
properties: {
name: {
stringValue: value,
excludeFromIndexes: true
}
}
}
}
}
}
}
]
}
}
}
}
}
]
}
}
}
};

assert.deepEqual(
entity.entityToEntityProto(entityObject),
expectedEntityProto
);
});
});

describe('formatArray', function() {
Expand Down

0 comments on commit ada4d67

Please sign in to comment.