Skip to content
Permalink
Browse files

new sort and limit for jobs search (#665)

  • Loading branch information...
edwin-jones committed Jul 4, 2019
1 parent 1a042ce commit 8c3470cc9a2fadac05d6682f5dfa7830ab84c0d5
Showing with 44 additions and 2 deletions.
  1. +8 −2 lib/agenda/jobs.js
  2. +36 −0 test/agenda.js
@@ -6,10 +6,16 @@ const {createJob} = require('../utils');
* @name Agenda#jobs
* @function
* @param {Object} query object for MongoDB
* @param {Object} sort object for MongoDB
* @param {Number} limit number of documents to return from MongoDB
* @returns {Promise} resolves when fails or passes
*/
module.exports = async function(query) {
const result = await this._collection.find(query).toArray();
module.exports = async function(query = {}, sort = {}, limit = 0) {
const result = await this._collection
.find(query)
.sort(sort)
.limit(limit)
.toArray();

return result.map(job => createJob(this, job));
};
@@ -503,4 +503,40 @@ describe('Agenda', () => {
expect(jobs3).to.have.length(1);
});
});

describe('search', () => {
beforeEach(async() => {
await jobs.create('jobA', 1).save();
await jobs.create('jobA', 2).save();
await jobs.create('jobA', 3).save();
});

afterEach(done => {
jobs._collection.remove({name: 'jobA'}, err => {
if (err) {
return done(err);
}
done();
});
});

it('should limit jobs', async() => {
const results = await jobs.jobs({name: 'jobA'}, {}, 2);
expect(results).to.have.length(2);
});

it('should sort jobs', async() => {
const results = await jobs.jobs({name: 'jobA'}, {data: -1});

expect(results).to.have.length(3);

const job1 = results[0];
const job2 = results[1];
const job3 = results[2];

expect(job1.attrs.data).to.be(3);
expect(job2.attrs.data).to.be(2);
expect(job3.attrs.data).to.be(1);
});
});
});

0 comments on commit 8c3470c

Please sign in to comment.
You can’t perform that action at this time.