Skip to content

Commit

Permalink
Fix PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaHydrae committed May 17, 2018
1 parent da967e0 commit 88cbcd5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
31 changes: 31 additions & 0 deletions server/api/utils/query-builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@ const { filter: filterFactory, queryBuilder: queryBuilderFactory } = require('./

describe('Query builder utilities', function() {
describe('filter', function() {
it('should modify a query using a value and filter functions', async function() {

const Model = Abstract.extend({});

const mockQuery = new Model(); // The base query.
const updatedQuery = mockQuery.clone().where('foo', 'bar'); // The updated query.

const valueFunc = stub().returns(42); // The value function that retrieves the value that will be used by the filter function.
const filterFunc = stub().returns(updatedQuery); // The filter function.
const filter = filterFactory(valueFunc, filterFunc);

const mockContext = {
get: stub().returns(mockQuery),
set: stub()
};

await expect(filter(mockContext)).to.eventually.equal(undefined);

expect(valueFunc).to.have.been.calledWithExactly(mockContext);
expect(valueFunc).to.have.callCount(1);
expect(mockContext.get).to.have.been.calledWithExactly('query');
expect(mockContext.get).to.have.been.calledImmediatelyAfter(valueFunc);
expect(mockContext.get).to.have.callCount(1);
expect(filterFunc).to.have.been.calledWithExactly(mockQuery, 42, mockContext);
expect(filterFunc).to.have.been.calledImmediatelyAfter(mockContext.get);
expect(filterFunc).to.have.callCount(1);
expect(mockContext.set).to.have.been.calledWithExactly('query', updatedQuery); // Make sure the query was updated.
expect(mockContext.set).to.have.been.calledImmediatelyAfter(filterFunc);
expect(mockContext.set).to.have.callCount(1);
});

it('should not call the filter function or modify the query if the value function returns undefined', async function() {

const valueFunc = stub(); // Returns undefined.
Expand Down
24 changes: 12 additions & 12 deletions server/models/abstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ const Abstract = db.bookshelf.Model.extend({
*
* @see {@link https://github.com/MediaComem/orm-query-builder}
*/
queryBuilder: function(options) {
return queryBuilder(_.extend(options, { baseQuery: this }));
},
queryBuilder: createQueryBuilder,

/**
* Returns a query builder initialized with this record as a base query and with pagination
Expand All @@ -41,9 +39,7 @@ const Abstract = db.bookshelf.Model.extend({
*
* @see {@link https://github.com/MediaComem/orm-query-builder}
*/
paginatedQueryBuilder: function(options = {}) {
return this.queryBuilder(_.omit(options, 'pagination')).use(pagination(options.pagination));
},
paginatedQueryBuilder: createPaginatedQueryBuilder,

/**
* Parses data from the specified source into this record's columns.
Expand Down Expand Up @@ -178,9 +174,7 @@ const Abstract = db.bookshelf.Model.extend({
*
* @see {@link https://github.com/MediaComem/orm-query-builder}
*/
queryBuilder: function(options) {
return queryBuilder(_.extend(options, { baseQuery: this }));
},
queryBuilder: createQueryBuilder,

/**
* Returns a query builder initialized with this model as a base query and with pagination
Expand All @@ -194,9 +188,15 @@ const Abstract = db.bookshelf.Model.extend({
*
* @see {@link https://github.com/MediaComem/orm-query-builder}
*/
paginatedQueryBuilder: function(options = {}) {
return this.queryBuilder(_.omit(options, 'pagination')).use(pagination(options.pagination));
}
paginatedQueryBuilder: createPaginatedQueryBuilder
});

function createQueryBuilder(options) {
return queryBuilder(_.extend(options, { baseQuery: this }));
}

function createPaginatedQueryBuilder(options = {}) {
return this.queryBuilder(_.omit(options, 'pagination')).use(pagination(options.pagination));
}

module.exports = Abstract;
2 changes: 1 addition & 1 deletion server/models/abstract.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('Abstract model', function() {

// An ORM Query Builder plugin that adds an "ORDER BY name" clause to the query.
const orderByName = {
use: builder => builder.before('end', context => context.set('query', context.get('query').query(qb => qb.orderBy('name'))))
use: builder => builder.before('end', context => context.set('query', context.get('query').orderBy('name')))
};

/**
Expand Down
25 changes: 25 additions & 0 deletions server/spec/fixtures/express.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
/**
* Test utilities to mock Express objects.
*
* @module server/spec/fixtures/express
*/
const chance = require('chance').Chance();
const { stub } = require('sinon');

const app = require('../../app');

/**
* Returns an object that looks like an Express request and that has various Sinon stubs ready for testing.
*
* @param {object} [data] - Optional request properties to override the defaults.
* @param {Application} [data.app] - The Express application, defaults to the project's application.
* @param {Function} [data.get] - A function that retrieves a header's value(s) by name (defaults to a stub).
* @param {string} [data.method] - The HTTP method (defaults to `GET`).
* @param {string} [data.path] - The path of the URL (defaults to a random path, e.g. `/foo`).
* @param {object} [data.query] - The query parameters (defaults to an empty object).
* @returns {object} A mocked Express request.
*/
exports.req = function(data = {}) {
return {
app: data.app || app,
Expand All @@ -14,6 +30,15 @@ exports.req = function(data = {}) {
};
};

/**
* Returns an object that looks like an Express response and that has various Sinon stubs ready for testing.
*
* @param {object} [data] - Optional request properties to override the defaults.
* @param {Application} [data.app] - The Express application, defaults to the project's application.
* @param {Function} [data.send] - A function that sends the response to the client with an optional body (defaults to a stub).
* @param {Function} [data.set] - A function that sets a header's value(s) (defaults to a stub).
* @returns {object} A mocked Express response.
*/
exports.res = function(data = {}) {
return {
app: data.app || app,
Expand Down

0 comments on commit 88cbcd5

Please sign in to comment.