odata-resource
Version:
REST + OData + Mongoose/MongoDb
438 lines (408 loc) • 17.6 kB
JavaScript
var should = require('should'),
util = require('./util/util'),
models = util.models,
api = util.api;
describe('Filter',function(){
var authorOne = {
firstname: 'Joe',
middlename: 'The',
lastname: 'Author'
},
authorTwo = {
firstname: 'William',
lastname: 'Writer'
},
theBooks = [{
title: 'B: Exciting Book',
genre: 'Action',
pages: 10,
release: new Date(2018,0,1)
},{
title: 'A: Depressing Book',
genre: 'Drama',
pages: 200,
release: new Date(2017,0,1)
},{
title: 'C: Another Book',
genre: 'Action',
pages: 15,
release: new Date(2016,0,1)
},{
title: 'D: Dragons',
genre: 'Fantasy',
pages: 120,
release: new Date(2015,0,1)
}],
dateString = function(d) { // dates stored in local tz with offset so can't just use hardcoded strings
return JSON.stringify(d).replace(/"/g,'');
};
before(function(done){
util.before(function(){
models.Author.create([authorOne,authorTwo],function(err,authors){
if(err) {
throw err;
}
authorOne._id = authors[0]._id.toString();
authorTwo._id = authors[1]._id.toString();
theBooks[0]._author = authorOne._id;
theBooks[1]._author = authorOne._id;
theBooks[2]._author = authorTwo._id;
theBooks[3]._author = authorTwo._id;
models.Book.create(theBooks,function(err,books){
if(err) {
throw err;
}
books.forEach(function(b,i) {
theBooks[i]._id = b._id.toString();
});
done();
});
});
});
});
after(util.after);
it('eq',function(done){
api.get('/api/books?$filter=title eq \'B: Exciting Book\'')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(1);
util.testBook(res.body.list[0],theBooks[0],authorOne);
done();
});
});
it('ne',function(done){
api.get('/api/books?$filter=title ne \'B: Exciting Book\'&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(3);
util.testBook(res.body.list[0],theBooks[1],authorOne);
util.testBook(res.body.list[1],theBooks[2],authorTwo);
util.testBook(res.body.list[2],theBooks[3],authorTwo);
res.body.should.have.property('_links').and.be.instanceof(Object);
var links = res.body._links;
links.should.have.property('genres','/api/books/genres');
links.should.have.property('count','/api/books/count?%24filter=title%20ne%20\'B%3A%20Exciting%20Book\'');
done();
});
});
it('ne count',function(done){
api.get('/api/books/count?$filter=title ne \'B: Exciting Book\'')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Number).and.equal(3);
done();
});
});
it('lt',function(done){
api.get('/api/books?$filter=pages lt 120&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(2);
util.testBook(res.body.list[0],theBooks[0],authorOne);
util.testBook(res.body.list[1],theBooks[2],authorTwo);
done();
});
});
it('le',function(done){
api.get('/api/books?$filter=pages le 120&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(3);
util.testBook(res.body.list[0],theBooks[0],authorOne);
util.testBook(res.body.list[1],theBooks[2],authorTwo);
util.testBook(res.body.list[2],theBooks[3],authorTwo);
res.body.should.have.property('_links').and.be.instanceof(Object);
var links = res.body._links;
links.should.have.property('genres','/api/books/genres');
links.should.have.property('count','/api/books/count?%24filter=pages%20le%20120');
done();
});
});
it('gt',function(done){
api.get('/api/books?$filter=pages gt 120&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(1);
util.testBook(res.body.list[0],theBooks[1],authorOne);
done();
});
});
it('ge',function(done){
api.get('/api/books?$filter=pages ge 120&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(2);
util.testBook(res.body.list[0],theBooks[1],authorOne);
util.testBook(res.body.list[1],theBooks[3],authorTwo);
done();
});
});
it('startswith',function(done){
api.get('/api/books?$filter=startswith(title,\'A\')&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(1);
util.testBook(res.body.list[0],theBooks[1],authorOne);
done();
});
});
it('endswith',function(done){
api.get('/api/books?$filter=endswith(title,\'Book\')&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(3);
util.testBook(res.body.list[0],theBooks[1],authorOne);
util.testBook(res.body.list[1],theBooks[0],authorOne);
util.testBook(res.body.list[2],theBooks[2],authorTwo);
done();
});
});
it('contains',function(done){
api.get('/api/books?$filter=contains(title,\'ing\')&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(2);
util.testBook(res.body.list[0],theBooks[1],authorOne);
util.testBook(res.body.list[1],theBooks[0],authorOne);
done();
});
});
it('in',function(done){
api.get('/api/books?$filter=in(pages,10,15)&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(2);
util.testBook(res.body.list[0],theBooks[0],authorOne);
util.testBook(res.body.list[1],theBooks[2],authorTwo);
done();
});
});
it('notin',function(done){
api.get('/api/books?$filter=notin(pages,10,15)&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(2);
util.testBook(res.body.list[0],theBooks[1],authorOne);
util.testBook(res.body.list[1],theBooks[3],authorTwo);
done();
});
});
it('and',function(done){
api.get('/api/books?$filter=endswith(title,\'Book\') and genre eq \'Action\'&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(2);
util.testBook(res.body.list[0],theBooks[0],authorOne);
util.testBook(res.body.list[1],theBooks[2],authorTwo);
done();
});
});
it('or',function(done){
api.get('/api/books?$filter=genre eq \'Action\' or genre eq \'Fantasy\'&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(3);
util.testBook(res.body.list[0],theBooks[0],authorOne);
util.testBook(res.body.list[1],theBooks[2],authorTwo);
util.testBook(res.body.list[2],theBooks[3],authorTwo);
done();
});
});
it('or-and',function(done){
api.get('/api/books?$filter=(genre eq \'Action\' or genre eq \'Drama\') and (pages gt 100 or pages lt 15) &$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(2);
util.testBook(res.body.list[0],theBooks[1],authorOne);
util.testBook(res.body.list[1],theBooks[0],authorOne);
done();
});
});
it('date-eq',function(done){
var d = dateString(new Date(2018,0,1));
api.get(`/api/books?$filter=release eq ${d}`)
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(1);
util.testBook(res.body.list[0],theBooks[0],authorOne);
done();
});
});
it('date-inc',function(done){
var d1 = dateString(new Date(2015,0,1)),
d2 = dateString(new Date(2018,0,1));
api.get(`/api/books?$filter=release gt ${d1} and release lt ${d2}&orderby=title`)
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(2);
util.testBook(res.body.list[0],theBooks[1],authorOne);
util.testBook(res.body.list[1],theBooks[2],authorTwo);
done();
});
});
it('date-ge-ordered',function(done){
var d = dateString(new Date(2016,0,1));
api.get(`/api/books?$filter=release ge ${d}&$orderby=release asc`)
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(3);
util.testBook(res.body.list[0],theBooks[2],authorTwo);
util.testBook(res.body.list[1],theBooks[1],authorOne);
util.testBook(res.body.list[2],theBooks[0],authorOne);
done();
});
});
// basic $filter through a relationship
it('rel',function(done){
api.get('/api/authors/'+authorOne._id+'/books?$filter=endswith(title,\'Book\') and genre eq \'Action\'&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(1);
util.testBook(res.body.list[0],theBooks[0],authorOne);
done();
});
});
it('rel2',function(done){
api.get('/api/authors/'+authorOne._id+'/books?$filter=endswith(title,\'Book\')&$orderby=title')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(2);
util.testBook(res.body.list[0],theBooks[1],authorOne);
util.testBook(res.body.list[1],theBooks[0],authorOne);
done();
});
});
it('not null',function(done){
api.get('/api/authors?$filter=middlename ne null')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(1);
var author = res.body.list[0];
author.should.have.property('firstname').and.equal('Joe');
author.should.have.property('middlename').and.equal('The');
author.should.have.property('lastname').and.equal('Author');
done();
});
});
it('null',function(done){
api.get('/api/authors?$filter=middlename eq null')
.expect(200)
.expect('Content-Type', /json/)
.end(function(err,res) {
if(err) {
return done(err);
}
res.body.should.be.instanceof(Object);
res.body.should.have.property('list').and.be.instanceof(Array).with.lengthOf(1);
var author = res.body.list[0];
author.should.have.property('firstname').and.equal('William');
author.should.have.property('lastname').and.equal('Writer');
author.should.not.have.property('middlename');
done();
});
});
});