Rewriting tests

This commit is contained in:
Anatoliy Chakkaev 2013-03-26 23:33:11 +04:00
parent d5d07d8806
commit 49be07545a
4 changed files with 307 additions and 83 deletions

194
test/basic-querying.test.js Normal file
View File

@ -0,0 +1,194 @@
var db, User, should = require('should');
describe('basic-querying', function() {
before(function() {
db = getSchema();
User = db.define('User', {
name: String,
email: {type: String, index: true},
role: {type: String, index: true},
order: {type: Number, index: true}
});
});
describe('find', function() {
before(function(done) {
User.destroyAll(done);
});
it('should query by id: not found', function(done) {
User.find(1, function(err, u) {
should.not.exist(u);
should.not.exist(err);
done();
});
});
it('should query by id: found', function(done) {
User.create(function(err, u) {
should.exist(u.id);
User.find(u.id, function(err, u) {
should.exist(u);
should.not.exist(err);
u.should.be.an.instanceOf(User);
done();
});
});
});
});
describe('all', function() {
before(seed);
it('should query collection', function(done) {
User.all(function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.have.lengthOf(6);
done();
});
});
it('should query filtered collection', function(done) {
User.all({where: {role: 'lead'}}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.should.have.lengthOf(2);
done();
});
});
it('should query collection sorted by numeric field', function(done) {
User.all({order: 'order'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.forEach(function(u, i) {
u.order.should.eql(i + 1);
});
done();
});
});
it('should query collection desc sorted by numeric field', function(done) {
User.all({order: 'order DESC'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.forEach(function(u, i) {
u.order.should.eql(users.length - i);
});
done();
});
});
it('should query collection sorted by string field', function(done) {
User.all({order: 'name'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.shift().name.should.equal('George Harrison');
users.shift().name.should.equal('John Lennon');
users.pop().name.should.equal('Stuart Sutcliffe');
done();
});
});
it('should query collection desc sorted by string field', function(done) {
User.all({order: 'name DESC'}, function(err, users) {
should.exists(users);
should.not.exists(err);
users.pop().name.should.equal('George Harrison');
users.pop().name.should.equal('John Lennon');
users.shift().name.should.equal('Stuart Sutcliffe');
done();
});
});
});
describe('count', function() {
before(seed);
it('should query total count', function(done) {
User.count(function(err, n) {
should.not.exist(err);
should.exist(n);
n.should.equal(6);
done();
});
});
it('should query filtered count', function(done) {
User.count({role: 'lead'}, function(err, n) {
should.not.exist(err);
should.exist(n);
n.should.equal(2);
done();
});
});
});
describe('exists', function() {
before(seed);
it('should check whether record exist', function(done) {
User.exists(1, function(err, exists) {
should.not.exist(err);
should.exist(exists);
exists.should.be.true;
done();
});
});
it('should check whether record not exist', function(done) {
User.destroyAll(function() {
User.exists(42, function(err, exists) {
should.not.exist(err);
exists.should.be.false;
done();
});
});
});
});
});
function seed(done) {
var count = 0;
var beatles = [
{ id: 1,
name: 'John Lennon',
mail: 'john@b3atl3s.co.uk',
role: 'lead',
order: 2
}, {
name: 'Paul McCartney',
mail: 'paul@b3atl3s.co.uk',
role: 'lead',
order: 1
},
{name: 'George Harrison', order: 5},
{name: 'Ringo Starr', order: 6},
{name: 'Pete Best', order: 4},
{name: 'Stuart Sutcliffe', order: 3}
];
User.destroyAll(function() {
beatles.forEach(function(beatle) {
User.create(beatle, ok);
});
});
function ok() {
if (++count === beatles.length) {
done();
}
}
}

1
test/common.batch.js Normal file
View File

@ -0,0 +1 @@
require('./basic-querying.test.js');

View File

@ -209,42 +209,6 @@ function testOrm(schema) {
test.done(); test.done();
}); });
it('should be exported to JSON', function (test) {
var outString = '{"title":"hello, json","date":1,"published":false,"likes":[],"related":[],"id":1}'
if (schema.name === 'nano')
outString = '{"title":"hello, json","subject":null,"content":null,"date":1,"published":false,"likes":[],"related":[],"_rev":null,"id":1,"userId":null}'
test.equal(JSON.stringify(new Post({id: 1, title: 'hello, json', date: 1})),outString);
test.done();
});
it('should create object', function (test) {
Post.create(function (err, post) {
if (err) throw err;
test.ok(post.id, 'Id present');
test.ok(!post.title, 'Title is blank');
Post.exists(post.id, function (err, exists) {
if (err) throw err;
test.ok(exists);
test.done();
});
});
});
it('should create object without callback', function (test) {
var uniqueTitle = 'Unique title ' + Date.now();
Post.create({title: uniqueTitle});
setTimeout(delayedCallback, 100);
function delayedCallback() {
Post.all({where: {title: uniqueTitle}}, function (err, posts) {
test.equal(posts.length, 1);
test.done();
});
}
});
it('should save object', function (test) { it('should save object', function (test) {
var title = 'Initial title', title2 = 'Hello world', var title = 'Initial title', title2 = 'Hello world',
date = new Date; date = new Date;
@ -420,53 +384,6 @@ function testOrm(schema) {
}); });
}); });
it('should fetch count of records in collection', function (test) {
Post.count(function (err, count) {
console.log(countOfposts, count);
test.equal(countOfposts, count, 'unfiltered count');
Post.count({title: 'title'}, function (err, count) {
console.log(countOfpostsFiltered, count, 'filtered count');
test.equal(countOfpostsFiltered, count, 'filtered count');
test.done();
});
});
});
it('should find filtered set of records', function (test) {
var wait = 1;
// exact match with string
Post.all({where: {title: 'New title'}}, function (err, res) {
var pass = true;
res.forEach(function (r) {
if (r.title != 'New title') pass = false;
});
test.ok(res.length > 0, 'Exact match with string returns dataset');
test.ok(pass, 'Exact match with string');
done();
});
// matching null
// Post.all({where: {title: null}}, function (err, res) {
// var pass = true;
// res.forEach(function (r) {
// if (r.title != null) pass = false;
// });
// test.ok(res.length > 0, 'Matching null returns dataset');
// test.ok(pass, 'Matching null');
// done();
// });
function done() {
if (--wait === 0) {
test.done();
}
}
});
it('should find records filtered with multiple attributes', function (test) { it('should find records filtered with multiple attributes', function (test) {
var d = new Date; var d = new Date;
Post.create({title: 'title', content: 'content', published: true, date: d}, function (err, post) { Post.create({title: 'title', content: 'content', published: true, date: d}, function (err, post) {

112
test/manipulation.test.js Normal file
View File

@ -0,0 +1,112 @@
var db, Person, should = require('should');
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}
});
db.automigrate(done);
});
describe('create', function() {
before(function(done) {
Person.destroyAll(done);
});
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.find(p.id, function(err, person) {
person.id.should.equal(p.id);
person.name.should.equal('Anatoliy');
done();
});
});
});
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'});
});
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.find(p.id, function(err, person) {
person.id.should.equal(p.id);
should.not.exists(person.name);
done();
});
});
});
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, 10);
};
Person.create();
});
});
describe('save', function() {
it('should save new object');
it('should save existing object');
it('should save invalid object (skipping validation)');
it('should save throw error on validation');
});
describe('destroy', function() {
it('should destroy record');
it('should destroy all records');
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.propertyChanged('name').should.be.false;
person.name = 'Goodbye, Lenin';
person.name_was.should.equal(hw);
person.propertyChanged('name').should.be.true;
(person.createdAt >= now).should.be.true;
person.isNewRecord().should.be.true;
});
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');
});
});
});