2013-04-06 10:57:12 +00:00
|
|
|
// This test written in mocha+should.js
|
|
|
|
var should = require('./init.js');
|
|
|
|
|
|
|
|
var db, Person;
|
2013-11-25 16:20:05 +00:00
|
|
|
var ValidationError = require('..').ValidationError;
|
2013-03-26 19:33:11 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('manipulation', function () {
|
|
|
|
|
|
|
|
before(function (done) {
|
|
|
|
db = getSchema();
|
|
|
|
|
|
|
|
Person = db.define('Person', {
|
|
|
|
name: String,
|
|
|
|
gender: String,
|
|
|
|
married: Boolean,
|
|
|
|
age: {type: Number, index: true},
|
|
|
|
dob: Date,
|
|
|
|
createdAt: {type: Number, default: Date.now}
|
2014-09-05 14:35:01 +00:00
|
|
|
}, { forceId: true });
|
2013-03-26 19:33:11 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
db.automigrate(done);
|
2013-03-26 19:33:11 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-26 19:33:11 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('create', function () {
|
2013-03-26 19:33:11 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
before(function (done) {
|
|
|
|
Person.destroyAll(done);
|
|
|
|
});
|
2013-03-30 17:06:09 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should create instance', function (done) {
|
|
|
|
Person.create({name: 'Anatoliy'}, function (err, p) {
|
|
|
|
p.name.should.equal('Anatoliy');
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(p);
|
|
|
|
Person.findById(p.id, function (err, person) {
|
|
|
|
person.id.should.equal(p.id);
|
|
|
|
person.name.should.equal('Anatoliy');
|
|
|
|
done();
|
2013-03-26 19:33:11 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
2014-09-05 15:09:23 +00:00
|
|
|
|
|
|
|
it('should instantiate an object', function (done) {
|
|
|
|
var p = new Person({name: 'Anatoliy'});
|
|
|
|
p.name.should.equal('Anatoliy');
|
|
|
|
p.isNewRecord().should.be.true;
|
|
|
|
p.save(function(err, inst) {
|
|
|
|
should.not.exist(err);
|
|
|
|
inst.isNewRecord().should.be.false;
|
|
|
|
inst.should.equal(p);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-03-26 19:33:11 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should return instance of object', function (done) {
|
|
|
|
var person = Person.create(function (err, p) {
|
|
|
|
p.id.should.eql(person.id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
should.exist(person);
|
|
|
|
person.should.be.an.instanceOf(Person);
|
|
|
|
should.not.exist(person.id);
|
|
|
|
});
|
2014-09-05 14:35:01 +00:00
|
|
|
|
2014-09-05 15:09:23 +00:00
|
|
|
it('should not allow user-defined value for the id of object - create', function (done) {
|
|
|
|
Person.create({id: 123456}, function (err, p) {
|
2014-09-05 14:35:01 +00:00
|
|
|
err.should.be.instanceof(ValidationError);
|
|
|
|
err.statusCode.should.equal(422);
|
2015-01-12 13:52:54 +00:00
|
|
|
err.details.messages.id.should.eql(['can\'t be set']);
|
2014-09-05 14:35:01 +00:00
|
|
|
p.should.be.instanceof(Person);
|
|
|
|
p.id.should.equal(123456);
|
2014-09-05 15:09:23 +00:00
|
|
|
p.isNewRecord().should.be.true;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not allow user-defined value for the id of object - save', function (done) {
|
|
|
|
var p = new Person({id: 123456});
|
|
|
|
p.isNewRecord().should.be.true;
|
|
|
|
p.save(function(err, inst) {
|
|
|
|
err.should.be.instanceof(ValidationError);
|
|
|
|
err.statusCode.should.equal(422);
|
2015-01-12 13:52:54 +00:00
|
|
|
err.details.messages.id.should.eql(['can\'t be set']);
|
2014-09-05 15:09:23 +00:00
|
|
|
inst.id.should.equal(123456);
|
|
|
|
inst.isNewRecord().should.be.true;
|
2014-09-05 14:35:01 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-03-26 19:33:11 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should work when called without callback', function (done) {
|
|
|
|
Person.afterCreate = function (next) {
|
|
|
|
this.should.be.an.instanceOf(Person);
|
|
|
|
this.name.should.equal('Nickolay');
|
|
|
|
should.exist(this.id);
|
|
|
|
Person.afterCreate = null;
|
|
|
|
next();
|
|
|
|
setTimeout(done, 10);
|
|
|
|
};
|
|
|
|
Person.create({name: 'Nickolay'});
|
|
|
|
});
|
2013-03-31 09:40:37 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should create instance with blank data', function (done) {
|
|
|
|
Person.create(function (err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(p);
|
|
|
|
should.not.exists(p.name);
|
|
|
|
Person.findById(p.id, function (err, person) {
|
|
|
|
person.id.should.equal(p.id);
|
|
|
|
should.not.exists(person.name);
|
|
|
|
done();
|
2013-03-31 09:40:37 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-26 19:33:11 +00:00
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should work when called with no data and callback', function (done) {
|
|
|
|
Person.afterCreate = function (next) {
|
|
|
|
this.should.be.an.instanceOf(Person);
|
|
|
|
should.not.exist(this.name);
|
|
|
|
should.exist(this.id);
|
|
|
|
Person.afterCreate = null;
|
|
|
|
next();
|
|
|
|
setTimeout(done, 30);
|
|
|
|
};
|
|
|
|
Person.create();
|
|
|
|
});
|
2013-03-28 11:27:27 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should create batch of objects', function (done) {
|
|
|
|
var batch = [
|
|
|
|
{name: 'Shaltay'},
|
|
|
|
{name: 'Boltay'},
|
|
|
|
{}
|
|
|
|
];
|
|
|
|
Person.create(batch,function (e, ps) {
|
|
|
|
should.not.exist(e);
|
|
|
|
should.exist(ps);
|
|
|
|
ps.should.be.instanceOf(Array);
|
|
|
|
ps.should.have.lengthOf(batch.length);
|
|
|
|
|
|
|
|
Person.validatesPresenceOf('name');
|
|
|
|
Person.create(batch,function (errors, persons) {
|
2014-08-26 15:51:01 +00:00
|
|
|
delete Person.validations;
|
2014-01-24 17:09:53 +00:00
|
|
|
should.exist(errors);
|
|
|
|
errors.should.have.lengthOf(batch.length);
|
|
|
|
should.not.exist(errors[0]);
|
|
|
|
should.not.exist(errors[1]);
|
|
|
|
should.exist(errors[2]);
|
|
|
|
|
|
|
|
should.exist(persons);
|
|
|
|
persons.should.have.lengthOf(batch.length);
|
|
|
|
persons[0].errors.should.be.false;
|
|
|
|
done();
|
|
|
|
}).should.be.instanceOf(Array);
|
|
|
|
}).should.have.lengthOf(3);
|
|
|
|
});
|
|
|
|
});
|
2013-03-28 11:27:27 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('save', function () {
|
|
|
|
|
|
|
|
it('should save new object', function (done) {
|
|
|
|
var p = new Person;
|
|
|
|
p.save(function (err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(p.id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-03-28 11:27:27 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should save existing object', function (done) {
|
|
|
|
Person.findOne(function (err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
p.name = 'Hans';
|
|
|
|
p.save(function (err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Person.findOne(function (err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
p.name.should.equal('Hans');
|
|
|
|
done();
|
|
|
|
});
|
2013-03-28 11:27:27 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
2013-03-28 11:27:27 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should save invalid object (skipping validation)', function (done) {
|
|
|
|
Person.findOne(function (err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
p.isValid = function (done) {
|
|
|
|
process.nextTick(done);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
p.name = 'Nana';
|
|
|
|
p.save(function (err) {
|
|
|
|
should.exist(err);
|
|
|
|
p.save({validate: false}, function (err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
done();
|
|
|
|
});
|
2013-03-28 11:27:27 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
2013-03-30 17:07:16 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should save throw error on validation', function () {
|
|
|
|
Person.findOne(function (err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
p.isValid = function (cb) {
|
|
|
|
cb(false);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
(function () {
|
|
|
|
p.save({
|
|
|
|
'throws': true
|
|
|
|
});
|
|
|
|
}).should.throw(ValidationError);
|
|
|
|
});
|
2013-03-30 17:07:16 +00:00
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-30 17:07:16 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
describe('updateAttributes', function () {
|
|
|
|
var person;
|
2013-03-30 17:07:16 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
before(function (done) {
|
|
|
|
Person.destroyAll(function () {
|
|
|
|
person = Person.create(done);
|
|
|
|
});
|
2013-03-26 19:33:11 +00:00
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should update one attribute', function (done) {
|
|
|
|
person.updateAttribute('name', 'Paul Graham', function (err, p) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Person.all(function (e, ps) {
|
|
|
|
should.not.exist(err);
|
|
|
|
ps.should.have.lengthOf(1);
|
|
|
|
ps.pop().name.should.equal('Paul Graham');
|
|
|
|
done();
|
2013-03-27 00:51:00 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('destroy', function () {
|
|
|
|
|
|
|
|
it('should destroy record', function (done) {
|
|
|
|
Person.create(function (err, p) {
|
|
|
|
p.destroy(function (err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Person.exists(p.id, function (err, ex) {
|
|
|
|
ex.should.not.be.ok;
|
|
|
|
done();
|
|
|
|
});
|
2013-03-27 00:51:00 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-26 19:33:11 +00:00
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should destroy all records', function (done) {
|
|
|
|
Person.destroyAll(function (err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
Person.all(function (err, posts) {
|
|
|
|
posts.should.have.lengthOf(0);
|
|
|
|
Person.count(function (err, count) {
|
|
|
|
count.should.eql(0);
|
|
|
|
done();
|
|
|
|
});
|
2013-03-26 19:33:11 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
});
|
2013-03-26 19:33:11 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
// TODO: implement destroy with filtered set
|
|
|
|
it('should destroy filtered set of records');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('initialize', function () {
|
|
|
|
it('should initialize object properly', function () {
|
|
|
|
var hw = 'Hello word',
|
|
|
|
now = Date.now(),
|
|
|
|
person = new Person({name: hw});
|
|
|
|
|
|
|
|
person.name.should.equal(hw);
|
|
|
|
person.name = 'Goodbye, Lenin';
|
|
|
|
(person.createdAt >= now).should.be.true;
|
|
|
|
person.isNewRecord().should.be.true;
|
2013-03-26 19:33:11 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
// it('should work when constructor called as function', function() {
|
|
|
|
// var p = Person({name: 'John Resig'});
|
|
|
|
// p.should.be.an.instanceOf(Person);
|
|
|
|
// p.name.should.equal('John Resig');
|
|
|
|
// });
|
|
|
|
});
|
2013-03-26 19:33:11 +00:00
|
|
|
});
|