Skip to content

Commit

Permalink
Added the option to pass a function to execute as a filter value
Browse files Browse the repository at this point in the history
  • Loading branch information
sebelga committed Oct 18, 2016
1 parent 7e9f2f1 commit f20deff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ entity_old.js
config.json
.coveralls.yml
todo.txt
.vscode/
8 changes: 7 additions & 1 deletion lib/helpers/queryhelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ function buildFromOptions(query, options, ds) {

if (options.filters[0].length > 1) {
options.filters.forEach((filter) => {
query.filter.apply(query, filter);
// We check if the value is a function
// if it is, we execute it.
let value = filter[filter.length - 1];
value = is.fn(value) ? value() : value;
let f = filter.slice(0, -1).concat([value]);

query.filter.apply(query, f);
});
}
}
Expand Down
36 changes: 22 additions & 14 deletions test/helpers/queryhelpers-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
var chai = require('chai');
var expect = chai.expect;
var ds = require('@google-cloud/datastore')();
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
const ds = require('@google-cloud/datastore')();

var queryHelpers = require('../../lib/helper').QueryHelpers;
const queryHelpers = require('../../lib/helper').QueryHelpers;

describe('Query Helpers', () => {
"use strict";
let query;

describe('should build a Query from options', () => {
beforeEach(() => {
query = ds.createQuery();
});

it ('and throw error if no query passed', () => {
let fn = () => {queryHelpers.buildFromOptions();};

Expand All @@ -21,7 +27,6 @@ describe('Query Helpers', () => {
});

it ('and not modify query if no options passed', () => {
let query = ds.createQuery();
let originalQuery = {};
Object.keys(query).forEach((k) => {
originalQuery[k] = query[k];
Expand All @@ -36,7 +41,6 @@ describe('Query Helpers', () => {
});

it ('and update query', () => {
let query = ds.createQuery();
let options = {
limit : 10,
order : {property:'name', descending:true},
Expand All @@ -54,7 +58,6 @@ describe('Query Helpers', () => {
});

it ('and allow order on serveral properties', () => {
let query = ds.createQuery();
let options = {
order : [{property:'name', descending:true}, {property:'age'}]
};
Expand All @@ -65,7 +68,6 @@ describe('Query Helpers', () => {
});

it ('and allow select to be an Array', () => {
let query = ds.createQuery();
let options = {
select : ['name', 'lastname', 'email']
};
Expand All @@ -76,7 +78,6 @@ describe('Query Helpers', () => {
});

it('and update hasAncestor in query', () => {
let query = ds.createQuery();
let options = {
ancestors: ['Parent', 1234]
};
Expand All @@ -89,7 +90,6 @@ describe('Query Helpers', () => {
});

it ('and throw Error if no Datastore instance passed when passing ancestors', () => {
let query = ds.createQuery();
let options = {
ancestors: ['Parent', 123]
};
Expand All @@ -102,7 +102,6 @@ describe('Query Helpers', () => {
});

it ('and define one filter', () => {
let query = ds.createQuery();
let options = {
filters: ['name', '=', 'John']
};
Expand All @@ -116,7 +115,6 @@ describe('Query Helpers', () => {
});

it ('and define several filters', () => {
let query = ds.createQuery();
let options = {
filters: [['name', '=', 'John'], ['lastname', 'Snow'], ['age', '<', 30]]
};
Expand All @@ -130,8 +128,19 @@ describe('Query Helpers', () => {
expect(query.filters[2].op).equal('<');
});

it('and execute a function in a filter value, without modifying the filters Array', () => {
let spy = sinon.spy();
let options = {
filters: [['modifiedOn', '<', spy]]
};

query = queryHelpers.buildFromOptions(query, options, ds);

expect(spy.calledOnce).be.true;
expect(options.filters[0][2]).to.equal(spy);
});

it ('and throw error if wrong format for filters', () => {
let query = ds.createQuery();
let options = {
filters: 'name'
};
Expand All @@ -143,7 +152,6 @@ describe('Query Helpers', () => {
});

it('and add start cursor', () => {
let query = ds.createQuery();
let options = {
start: 'abcdef'
};
Expand Down
2 changes: 1 addition & 1 deletion test/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ describe('Model', function() {
queryHelpers.buildFromOptions.restore();
});

it('should override setting with options)', () => {
it('should override setting with options', () => {
let querySettings = {
limit:10
};
Expand Down

0 comments on commit f20deff

Please sign in to comment.