Add a test case

This commit is contained in:
Raymond Feng 2013-12-03 21:27:46 -08:00
parent 2df0c4b21d
commit ed6d8839ba
2 changed files with 31 additions and 4 deletions

View File

@ -364,7 +364,7 @@ var operators = {
nlike: 'NOT LIKE'
};
DataAccessObject.coerce = function (where) {
DataAccessObject._coerce = function (where) {
if (!where) {
return where;
}
@ -453,7 +453,7 @@ DataAccessObject.find = function find(params, cb) {
params = removeUndefined(params);
if(params.where) {
params.where = this.coerce(params.where);
params.where = this._coerce(params.where);
}
if(near) {
if(supportsGeo) {
@ -577,7 +577,7 @@ DataAccessObject.destroyAll = function destroyAll(where, cb) {
} else {
// Support an optional where object
where = removeUndefined(where);
where = this.coerce(where);
where = this._coerce(where);
this.getDataSource().connector.destroyAll(this.modelName, where, function (err, data) {
cb && cb(err, data);
}.bind(this));
@ -623,7 +623,7 @@ DataAccessObject.count = function (where, cb) {
where = null;
}
where = removeUndefined(where);
where = this.coerce(where);
where = this._coerce(where);
this.getDataSource().connector.count(this.modelName, cb, where);
};

View File

@ -572,6 +572,33 @@ describe('Load models with relations', function () {
});
describe('DataAccessObject', function () {
it('should be able to coerce where clause based on the types', function () {
var ds = new DataSource('memory');
var model = ds.createModel('M1', {
id: {type: String, id: true},
age: Number
});
var where = model._coerce({id: 1});
assert.deepEqual(where, {id: '1'});
where = model._coerce({age: '10'});
assert.deepEqual(where, {age: 10});
where = model._coerce({age: 10});
assert.deepEqual(where, {age: 10});
where = model._coerce({age: {gt: 10}});
assert.deepEqual(where, {age: {gt: 10}});
where = model._coerce({age: {gt: '10'}});
assert.deepEqual(where, {age: {gt: 10}});
where = model._coerce({age: {between: ['10', '20']}});
assert.deepEqual(where, {age: {between: [10, 20]}});
});
});
describe('Load models from json', function () {
it('should be able to define models from json', function () {
var path = require('path'),