Skip to content

Commit

Permalink
Split model tests into their own files for different methods
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Dec 13, 2013
1 parent ac63605 commit debc799
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 70 deletions.
66 changes: 66 additions & 0 deletions test/findOne.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/// <reference path="../nodelib/node.js"/>
/// <reference path="../nodelib/mocha.js"/>
/// <reference path="../nodelib/should.js"/>
/// <reference path="../index.js"/>

var config = require('./config');
var Database = require('../index');
var Model = Database.Model;
var Instance = Database.Instance;
var should = require('should');
var Concoction = require('concoction');

describe('orm', function () {
"use strict";

describe('Model', function () {
var db = null;

before(function (done) {
db = new Database(config);
db.connect(done);
});

describe('database', function() {
var model = null;

before(function(done) {
model = new Model(db, 'model', {
name: /.+/
}, {
preprocessors: [new Concoction.Rename({ _id: 'name' })]
});

model.remove(function(err) {
if(err) return done(err);

model.create({
name: 'Demo1'
}, function(err, instance) {
if(err) return done(err);
return done();
});
});
});

describe('findOne', function() {
it('should be able to locate a single object', function(done) {
model.findOne({ name: 'Demo1' }, function(err, obj) {
if(err) return done(err);
should.exist(obj);
obj.should.have.ownProperty('name', 'Demo1');
done();
})
});

it('should not throw an error if it couldn\'t find an object', function(done) {
model.findOne({ name: 'NotFound' }, function(err, obj) {
if(err) return done(err);
should.not.exist(obj);
done();
})
});
});
});
});
});
83 changes: 83 additions & 0 deletions test/insertion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/// <reference path="../nodelib/node.js"/>
/// <reference path="../nodelib/mocha.js"/>
/// <reference path="../nodelib/should.js"/>
/// <reference path="../index.js"/>

var config = require('./config');
var Database = require('../index');
var Model = Database.Model;
var Instance = Database.Instance;
var should = require('should');
var Concoction = require('concoction');

describe('orm', function () {
"use strict";

describe('Model', function () {
var db = null;

before(function (done) {
db = new Database(config);
db.connect(done);
});

describe('database', function() {
var model = null;

before(function(done) {
model = new Model(db, 'model', {
name: /.+/
}, {
preprocessors: [new Concoction.Rename({ _id: 'name' })]
});

model.remove(done);
});

describe('insertion', function() {

it('should allow a single object to be inserted', function(done) {
model.create({
name: 'Demo1'
}, function(err, instance) {
if(err) return done(err);
instance.should.have.ownProperty('name', 'Demo1');
return done();
});
});

it('should have created the instance in the database', function(done) {
model.count({ name: 'Demo1' }, function(err, number) {
if(err) return done(err);
number.should.eql(1);
done();
});
});

it('should allow multiple objects to be inserted', function(done) {
model.create([{
name: 'Demo2'
}, {
name: 'Demo3'
}], function(err, instances) {
if(err) return done(err);
should(Array.isArray(instances));
instances[0].should.have.ownProperty('name', 'Demo2');
instances[1].should.have.ownProperty('name', 'Demo3');
return done();
});
});

it('should pass along DB errors', function(done) {
model.create({
name: 'Demo1'
}, function(err, inserted) {
should.exist(err);
should.not.exist(inserted);
done();
});
});
});
});
});
});
107 changes: 37 additions & 70 deletions test/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,83 +29,50 @@ describe('orm', function () {

});
});
});

describe('database', function() {
var model = null;

before(function(done) {
model = new Model(db, 'model', {
name: /.+/
}, {
preprocessors: [new Concoction.Rename({ _id: 'name' })]
});

model.remove(done);
it('should return an object with isModel set to true', function() {
var model = new Model(db, 'model', {}, {});
model.isModel.should.be.true;
});

describe('insertion', function() {

it('should allow a single object to be inserted', function(done) {
model.create({
name: 'Demo1'
}, function(err, instance) {
if(err) return done(err);
instance.should.have.ownProperty('name', 'Demo1');
return done();
});
});

it('should have created the instance in the database', function(done) {
model.count({ name: 'Demo1' }, function(err, number) {
if(err) return done(err);
number.should.eql(1);
done();
});
});

it('should allow multiple objects to be inserted', function(done) {
model.create([{
name: 'Demo2'
}, {
name: 'Demo3'
}], function(err, instances) {
if(err) return done(err);
should(Array.isArray(instances));
instances[0].should.have.ownProperty('name', 'Demo2');
instances[1].should.have.ownProperty('name', 'Demo3');
return done();
});
});

it('should pass along DB errors', function(done) {
model.create({
name: 'Demo1'
}, function(err, inserted) {
should.exist(err);
should.not.exist(inserted);
done();
});
});
it('should return a new instance even if called without the new keyword', function() {
var $ = {};
var model = Model.call($, db, 'model', {}, {});
should(model instanceof Model);
});

describe('findOne', function() {
it('should be able to locate a single object', function(done) {
model.findOne({ name: 'Demo1' }, function(err, obj) {
if(err) return done(err);
should.exist(obj);
obj.should.have.ownProperty('name', 'Demo1');
done();
})
});
it('should provide the full model API', function() {
var api = [
'preprocessors',
'options',
'schema',
'database',
'collection',
'extraValidators',
'wrap',
'toSource',
'fromSource',
'find',
'findOne',
'get',
'insert',
'create',
'update',
'count',
'remove',
'aggregate',
'ensureIndex',
'setupIndexes'
];

var model = new Model(db, 'model', {

}, {

it('should not throw an error if it couldn\'t find an object', function(done) {
model.findOne({ name: 'NotFound' }, function(err, obj) {
if(err) return done(err);
should.not.exist(obj);
done();
})
});

for(var i = 0; i < api.length; i++)
model.should.have.property(api[i]);
});
});
});
Expand Down

0 comments on commit debc799

Please sign in to comment.