Add more tests to address the PR comments

This commit is contained in:
Raymond Feng 2013-12-04 10:00:33 -08:00
parent ed6d8839ba
commit abe6d2bb22
2 changed files with 75 additions and 12 deletions

View File

@ -10,7 +10,6 @@ var util = require('util');
var jutil = require('./jutil'); var jutil = require('./jutil');
var validations = require('./validations.js'); var validations = require('./validations.js');
var ValidationError = validations.ValidationError; var ValidationError = validations.ValidationError;
var List = require('./list.js');
require('./relations.js'); require('./relations.js');
var Inclusion = require('./include.js'); var Inclusion = require('./include.js');
var Relation = require('./relations.js'); var Relation = require('./relations.js');
@ -376,17 +375,31 @@ DataAccessObject._coerce = function (where) {
continue; continue;
} }
if (Array.isArray(DataType) || DataType === Array) { if (Array.isArray(DataType) || DataType === Array) {
DataType = List; DataType = DataType[0];
} else if (DataType.name === 'Date') { }
if (DataType === Date) {
var OrigDate = Date; var OrigDate = Date;
DataType = function Date(arg) { DataType = function Date(arg) {
return new OrigDate(arg); return new OrigDate(arg);
}; };
} else if (DataType === Boolean) {
DataType = function(val) {
if(val === 'true') {
return true;
} else if(val === 'false') {
return false;
} else {
return Boolean(val);
}
};
}
if (!DataType) {
continue;
} }
var val = where[p]; var val = where[p];
// Check there is an operator // Check there is an operator
var operator = null; var operator = null;
if ('object' === typeof val) { if ('object' === typeof val && Object.keys(val).length === 1) {
for (var op in operators) { for (var op in operators) {
if (op in val) { if (op in val) {
val = val[op]; val = val[op];

View File

@ -573,15 +573,27 @@ describe('Load models with relations', function () {
}); });
describe('DataAccessObject', function () { describe('DataAccessObject', function () {
it('should be able to coerce where clause based on the types', function () { var ds, model, where;
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'});
before(function () {
ds = new DataSource('memory');
model = ds.createModel('M1', {
id: {type: String, id: true},
age: Number,
vip: Boolean,
date: Date,
scores: [Number]
});
});
it('should be able to coerce where clause for string types', function () {
where = model._coerce({id: 1});
assert.deepEqual(where, {id: '1'});
where = model._coerce({id: '1'});
assert.deepEqual(where, {id: '1'});
});
it('should be able to coerce where clause for number types', function () {
where = model._coerce({age: '10'}); where = model._coerce({age: '10'});
assert.deepEqual(where, {age: 10}); assert.deepEqual(where, {age: 10});
@ -597,6 +609,44 @@ describe('DataAccessObject', function () {
where = model._coerce({age: {between: ['10', '20']}}); where = model._coerce({age: {between: ['10', '20']}});
assert.deepEqual(where, {age: {between: [10, 20]}}); assert.deepEqual(where, {age: {between: [10, 20]}});
}); });
it('should be able to coerce where clause for array types', function () {
where = model._coerce({scores: ['10', '20']});
assert.deepEqual(where, {scores: [10, 20]});
});
it('should be able to coerce where clause for date types', function () {
var d = new Date();
where = model._coerce({date: d});
assert.deepEqual(where, {date: d});
where = model._coerce({date: d.toISOString()});
assert.deepEqual(where, {date: d});
});
it('should be able to coerce where clause for boolean types', function () {
where = model._coerce({vip: 'true'});
assert.deepEqual(where, {vip: true});
where = model._coerce({vip: true});
assert.deepEqual(where, {vip: true});
where = model._coerce({vip: 'false'});
assert.deepEqual(where, {vip: false});
where = model._coerce({vip: false});
assert.deepEqual(where, {vip: false});
where = model._coerce({vip: '1'});
assert.deepEqual(where, {vip: true});
where = model._coerce({vip: 0});
assert.deepEqual(where, {vip: false});
where = model._coerce({vip: ''});
assert.deepEqual(where, {vip: false});
});
}); });
describe('Load models from json', function () { describe('Load models from json', function () {