Skip to content

Commit

Permalink
Merge pull request #37 from flyvictor/fix-in-filtering
Browse files Browse the repository at this point in the history
Fix in filtering
  • Loading branch information
jesuscript committed Jul 2, 2014
2 parents 0abbde4 + c3fceeb commit 02838bf
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/querytree.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ function parseQuery(query, requestedResource){
}
}else if (!!schemaBranch.ref && _.isObject(q)){
//ref by object
freeze[key] = createSubrequest(q, schemaBranch.ref);
if (q.in || q.$in){
freeze[key] = {
$in: _.isArray(q.in || q.$in) ? q.in || q.$in : [q.in || q.$in]
};
}else{
freeze[key] = createSubrequest(q, schemaBranch.ref);
}
}else{
//Business PK in schema or an operator in query
//Do nothing and skip this query to fetchIds without any change
Expand Down
76 changes: 76 additions & 0 deletions test/fortune/fields_and_filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,82 @@ module.exports = function(options){
});
});
});
it('should support $in query against one-to-one refs', function(done){
new Promise(function(resolve){
request(baseUrl).patch("/people/[email protected]")
.set("content-type", "application/json")
.send(JSON.stringify([
{
path: '/people/0/soulmate',
op: 'replace',
value: '[email protected]'
}
]))
.end(function(err){
should.not.exist(err);
resolve();
});
}).then(function(){
request(baseUrl).get("/people?filter[soulmate][$in][email protected]&filter[soulmate][$in][email protected]")
.expect(200)
.end(function(err, res){
should.not.exist(err);
var body = JSON.parse(res.text);
(body.people.length).should.equal(2);
done();
});
});
});
it('should support $in query against many-to-many refs', function(done){
new Promise(function(resolve){
request(baseUrl).patch("/people/[email protected]")
.set("content-type", "application/json")
.send(JSON.stringify([
{
path: '/people/0/lovers',
op: 'replace',
value: ['[email protected]']
}
]))
.end(function(err){
should.not.exist(err);
resolve();
});
}).then(function(){
request(baseUrl).get("/people?filter[lovers][$in][email protected]&filter[lovers][$in][email protected]")
.expect(200)
.end(function(err, res){
should.not.exist(err);
var body = JSON.parse(res.text);
(body.people.length).should.equal(2);
done();
});
});
});
it('should support $in query against external refs values', function(done){
new Promise(function(resolve){
request(baseUrl).patch("/cars/" + ids.cars[0])
.set("content-type", "application/json")
.send(JSON.stringify([{
path: "/cars/0/MOT",
op: "replace",
value: "Pimp-my-ride"
}]))
.end(function(err){
should.not.exist(err);
resolve();
});
}).then(function(){
request(baseUrl).get("/cars?filter[MOT][$in]=Pimp-my-ride")
.expect(200)
.end(function(err, res){
should.not.exist(err);
var body = JSON.parse(res.text);
(body.cars.length).should.equal(1);
done();
});
});
});
describe('filtering by related objects fields', function(){
beforeEach(function(done){
neighbourhood(adapter, ids).then(function(){
Expand Down

0 comments on commit 02838bf

Please sign in to comment.