Skip to content

Commit

Permalink
Add boolean conversion for find from string, with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deitch committed Feb 25, 2015
1 parent 6010509 commit c69d29d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 11 deletions.
27 changes: 17 additions & 10 deletions lib/model.js
Expand Up @@ -470,18 +470,25 @@ module.exports = {
_.each(search,function (val,key) {
if (key && val && fields && fields[key] && fields[key].type) {
// it needs to be a supported type
if (fields[key].type === "integer") {
// convert string
switch(fields[key].type) {
case "integer":
// convert string
if (typeof(val) === "string") {
converted[key] = parseInt(val,10);
} else if (typeof(val) === "object") {
converted[key] = _.clone(val);
_.each(val,function (vval,vkey) {
if (typeof(vval) === "string") {
converted[key][vkey] = parseInt(vval,10);
}
});
}
break;
case "boolean":
if (typeof(val) === "string") {
converted[key] = parseInt(val,10);
} else if (typeof(val) === "object") {
converted[key] = _.clone(val);
_.each(val,function (vval,vkey) {
if (typeof(vval) === "string") {
converted[key][vkey] = parseInt(vval,10);
}
});
converted[key] = (val === "true") ? true : false;
}
break;
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "booster",
"description": "Booster is the ***fastest*** way to get a full-fledged REST service up and running in [nodejs](http://nodejs.org)!",
"version": "1.6.2",
"version": "1.6.3",
"url": "http://boosterjs.com",
"author": "Avi Deitcher <avi@deitcher.net>",
"engines": ["node >= 0.8"],
Expand Down
28 changes: 28 additions & 0 deletions test/models.js
Expand Up @@ -55,6 +55,7 @@ describe('models',function () {
booster.model('modelfilter'); // just a model to test model filtering
booster.model('modelpost'); // just a model to test model post processing
booster.model('modeldoublepost'); // just a model to test create post processor that calls another create
booster.model('boolean'); // fields with boolean, to test search conversion
r = request(app);
done();
});
Expand Down Expand Up @@ -860,6 +861,33 @@ describe('models',function () {
r.get('/integers').query({index:{gt:cutoff}}).expect(200,gt).end(done);
});
});
describe('search by boolean', function(){
var data = db.data("boolean"), f = _.where(data,{bool:false}), t = _.where(data,{bool:true});
it('should give correct results for false match',function (done) {
booster.models.boolean.find({bool:false},function (err,res) {
res.should.eql(f);
done();
});
});
it('should give correct results for true match',function (done) {
booster.models.boolean.find({bool:true},function (err,res) {
res.should.eql(t);
done();
});
});
it('should give correct results for converting false string match',function (done) {
booster.models.boolean.find({bool:"false"},function (err,res) {
res.should.eql(f);
done();
});
});
it('should give correct results for converting true string match',function (done) {
booster.models.boolean.find({bool:"true"},function (err,res) {
res.should.eql(t);
done();
});
});
});
describe('with default filter', function(){
it('should successfully GET filtered records',function (done) {
r.get('/defaultfilter').expect(200,db.data("defaultfilter",{filter:"yes"})).end(done);
Expand Down
6 changes: 6 additions & 0 deletions test/resources/db.js
Expand Up @@ -117,6 +117,12 @@ var _ = require('lodash'), sjs = require('searchjs'), DATA = {
{id:"3",name:"three",index:18},
{id:"4",name:"four",index:20},
],
boolean: [
{id:"1",name:"one",bool:false},
{id:"2",name:"two",bool:false},
{id:"3",name:"three",bool:true},
{id:"4",name:"four",bool:true}
],
cascadeone: [
{id:"1",singlevalue:"singleone",arrayvalue:"arrayone",anyvalue:"anyone",multiplechildren:"lots of children",nochildren:"none",errorchildren:"oops"}
],
Expand Down
8 changes: 8 additions & 0 deletions test/resources/models/boolean.js
@@ -0,0 +1,8 @@
/*jslint node:true */
module.exports = {
fields: {
id: {required:true,createoptional:true,mutable:false},
name: {required:true},
bool: {required:true,type:"boolean"}
}
};

0 comments on commit c69d29d

Please sign in to comment.