Merge pull request #1172 from strongloop/tests_cleanup

Tests cleanup
This commit is contained in:
Amirali Jafarian 2016-11-30 14:50:59 -05:00 committed by GitHub
commit 3ba7a5d1e8
2 changed files with 77 additions and 58 deletions

View File

@ -83,9 +83,10 @@ describe('manipulation', function() {
it('should create instance', function(done) {
Person.create({name: 'Anatoliy'}, function(err, p) {
p.name.should.equal('Anatoliy');
should.not.exist(err);
if (err) return done(err);
should.exist(p);
Person.findById(p.id, function(err, person) {
if (err) return done(err);
person.id.should.eql(p.id);
person.name.should.equal('Anatoliy');
done();
@ -113,7 +114,7 @@ describe('manipulation', function() {
p.name.should.equal('Anatoliy');
p.isNewRecord().should.be.true;
p.save(function(err, inst) {
should.not.exist(err);
if (err) return done(err);
inst.isNewRecord().should.be.false;
inst.should.equal(p);
done();
@ -136,6 +137,7 @@ describe('manipulation', function() {
it('should not return instance of object', function(done) {
var person = Person.create(function(err, p) {
if (err) return done(err);
should.exist(p.id);
if (person) person.should.not.be.an.instanceOf(Person);
done();
@ -209,10 +211,11 @@ describe('manipulation', function() {
it('should create instance with blank data', function(done) {
Person.create(function(err, p) {
should.not.exist(err);
if (err) return done(err);
should.exist(p);
should.not.exists(p.name);
Person.findById(p.id, function(err, person) {
if (err) return done(err);
person.id.should.eql(p.id);
should.not.exists(person.name);
done();
@ -350,7 +353,7 @@ describe('manipulation', function() {
it('should save new object', function(done) {
var p = new Person;
p.save(function(err) {
should.not.exist(err);
if (err) return done(err);
should.exist(p.id);
done();
});
@ -368,13 +371,13 @@ describe('manipulation', function() {
it('should save existing object', function(done) {
Person.findOne(function(err, p) {
should.not.exist(err);
if (err) return done(err);
p.name = 'Hans';
p.save(function(err) {
should.not.exist(err);
if (err) return done(err);
p.name.should.equal('Hans');
Person.findOne(function(err, p) {
should.not.exist(err);
if (err) return done(err);
p.name.should.equal('Hans');
done();
});
@ -400,7 +403,7 @@ describe('manipulation', function() {
it('should save invalid object (skipping validation)', function(done) {
Person.findOne(function(err, p) {
should.not.exist(err);
if (err) return done(err);
p.isValid = function(done) {
process.nextTick(done);
return false;
@ -409,7 +412,7 @@ describe('manipulation', function() {
p.save(function(err) {
should.exist(err);
p.save({validate: false}, function(err) {
should.not.exist(err);
if (err) return done(err);
done();
});
});
@ -441,7 +444,7 @@ describe('manipulation', function() {
it('should save throw error on validation', function() {
Person.findOne(function(err, p) {
should.not.exist(err);
if (err) return done(err);
p.isValid = function(cb) {
cb(false);
return false;
@ -548,6 +551,7 @@ describe('manipulation', function() {
function(done) {
Person.definition.settings.strict = true;
Person.findById(person.id, function(err, p) {
if (err) return done(err);
p.updateAttributes({name: 'John', unknownVar: undefined},
function(err, p) {
// if uknownVar was defined, it would return validationError
@ -566,6 +570,7 @@ describe('manipulation', function() {
function(done) {
Person.definition.settings.strict = false;
Person.findById(person.id, function(err, p) {
if (err) return done(err);
p.updateAttributes({name: 'John', foo: 'bar'},
function(err, p) {
if (err) return done(err);
@ -584,6 +589,7 @@ describe('manipulation', function() {
// changes to '{}' and breaks other tests
Person.definition.settings.strict = true;
Person.findById(person.id, function(err, p) {
if (err) return done(err);
p.updateAttributes({name: 'John', foo: 'bar'},
function(err, p) {
should.exist(err);
@ -604,6 +610,7 @@ describe('manipulation', function() {
it('should fallback to strict:true when using strict: throw', function(done) {
Person.definition.settings.strict = 'throw';
Person.findById(person.id, function(err, p) {
if (err) return done(err);
p.updateAttributes({foo: 'bar'},
function(err, p) {
should.exist(err);
@ -623,6 +630,7 @@ describe('manipulation', function() {
it('should fallback to strict:true when using strict:validate', function(done) {
Person.definition.settings.strict = 'validate';
Person.findById(person.id, function(err, p) {
if (err) return done(err);
p.updateAttributes({foo: 'bar'},
function(err, p) {
should.exist(err);
@ -1356,9 +1364,11 @@ describe('manipulation', function() {
it('should destroy record', function(done) {
Person.create(function(err, p) {
if (err) return done(err);
p.destroy(function(err) {
should.not.exist(err);
if (err) return done(err);
Person.exists(p.id, function(err, ex) {
if (err) return done(err);
ex.should.not.be.ok;
done();
});
@ -1383,10 +1393,12 @@ describe('manipulation', function() {
it('should destroy all records', function(done) {
Person.destroyAll(function(err) {
should.not.exist(err);
if (err) return done(err);
Person.all(function(err, posts) {
if (err) return done(err);
posts.should.have.lengthOf(0);
Person.count(function(err, count) {
if (err) return done(err);
count.should.eql(0);
done();
});
@ -1525,6 +1537,7 @@ describe('manipulation', function() {
it('should allow delete(id) - success', function(done) {
Person.findOne(function(e, p) {
if (e) return done(e);
p.delete(function(err, info) {
if (err) return done(err);
info.should.have.property('count', 1);
@ -1536,6 +1549,7 @@ describe('manipulation', function() {
it('should allow delete(id) - fail', function(done) {
Person.settings.strictDelete = false;
Person.findOne(function(e, p) {
if (e) return done(e);
p.delete(function(err, info) {
if (err) return done(err);
info.should.have.property('count', 1);
@ -1550,7 +1564,8 @@ describe('manipulation', function() {
it('should allow delete(id) - fail with error', function(done) {
Person.settings.strictDelete = true;
Person.findOne(function(e, u) {
Person.findOne(function(err, u) {
if (err) return done(err);
u.delete(function(err, info) {
if (err) return done(err);
info.should.have.property('count', 1);
@ -1592,10 +1607,10 @@ describe('manipulation', function() {
function(done) {
var now = Date.now();
var myCustomModel = CustomModel.create(function(err, m) {
CustomModel.create(function(err, model) {
should.not.exists(err);
m.createdAt.should.be.instanceOf(Date);
(m.createdAt >= now).should.be.true;
model.createdAt.should.be.instanceOf(Date);
(model.createdAt >= now).should.be.true;
});
done();
@ -1614,10 +1629,10 @@ describe('manipulation', function() {
it('should report \'$now\' as default value for string property',
function(done) {
var myCustomModel = CustomModel.create(function(err, m) {
should.not.exists(err);
m.now.should.be.instanceOf(String);
m.now.should.equal('$now');
CustomModel.create(function(err, model) {
if (err) return done(err);
model.now.should.be.instanceOf(String);
model.now.should.equal('$now');
});
done();
@ -1637,10 +1652,10 @@ describe('manipulation', function() {
it('should generate current time when "defaultFn" is "now"',
function(done) {
var now = Date.now();
var inst = CustomModel.create(function(err, m) {
should.not.exists(err);
m.now.should.be.instanceOf(Date);
m.now.should.be.within(now, now + 200);
CustomModel.create(function(err, model) {
if (err) return done(err);
model.now.should.be.instanceOf(Date);
model.now.should.be.within(now, now + 200);
done();
});
});
@ -1657,9 +1672,9 @@ describe('manipulation', function() {
});
it('should generate a new id when "defaultFn" is "guid"', function(done) {
var inst = CustomModel.create(function(err, m) {
should.not.exists(err);
m.guid.should.match(UUID_REGEXP);
CustomModel.create(function(err, model) {
if (err) return done(err);
model.guid.should.match(UUID_REGEXP);
done();
});
});
@ -1676,9 +1691,9 @@ describe('manipulation', function() {
});
it('should generate a new id when "defaultfn" is "uuid"', function(done) {
var inst = CustomModel.create(function(err, m) {
should.not.exists(err);
m.guid.should.match(UUID_REGEXP);
CustomModel.create(function(err, model) {
if (err) return done(err);
model.guid.should.match(UUID_REGEXP);
done();
});
});
@ -1695,9 +1710,9 @@ describe('manipulation', function() {
});
it('should generate a new id when "defaultfn" is "uuidv4"', function(done) {
var inst = CustomModel.create(function(err, m) {
CustomModel.create(function(err, model) {
should.not.exists(err);
m.guid.should.match(UUID_REGEXP);
model.guid.should.match(UUID_REGEXP);
done();
});
});

View File

@ -48,7 +48,7 @@ describe('scope', function() {
Station.scope('active', {where: {isActive: true}});
Station.scopes.should.have.property('active');
Station.active.create(function(err, station) {
should.not.exist(err);
if (err) return done(err);
should.exist(station);
should.exist(station.isActive);
station.isActive.should.be.true;
@ -60,7 +60,7 @@ describe('scope', function() {
Station.scope('active', {where: {isActive: true}});
Station.scope('subway', {where: {isUndeground: true}});
Station.active.subway.create(function(err, station) {
should.not.exist(err);
if (err) return done(err);
should.exist(station);
station.isActive.should.be.true;
station.isUndeground.should.be.true;
@ -75,6 +75,7 @@ describe('scope', function() {
Station.active.ground.create(function() {
Station.inactive.ground.create(function() {
Station.ground.inactive(function(err, ss) {
if (err) return done(err);
ss.should.have.lengthOf(1);
done();
});
@ -135,6 +136,7 @@ describe('scope - order', function() {
it('should define scope with default order', function(done) {
Station.reverse(function(err, stations) {
if (err) return done(err);
stations[0].name.should.equal('c');
stations[0].order.should.equal(3);
stations[1].name.should.equal('b');
@ -147,6 +149,7 @@ describe('scope - order', function() {
it('should override default scope order', function(done) {
Station.reverse({order: 'order ASC'}, function(err, stations) {
if (err) return done(err);
stations[0].name.should.equal('a');
stations[0].order.should.equal(1);
stations[1].name.should.equal('b');
@ -187,6 +190,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
beforeEach(function(done) {
Station.create({name: 'a', order: 1}, function(err, inst) {
if (err) return done(err);
stationA = inst;
done();
});
@ -202,7 +206,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
it('should find all - verify', function(done) {
Station.ordered(function(err, stations) {
should.not.exist(err);
if (err) return done(err);
stations.should.have.length(4);
stations[0].name.should.equal('a');
stations[1].name.should.equal('b');
@ -214,7 +218,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
it('should find one', function(done) {
Station.active.findOne(function(err, station) {
should.not.exist(err);
if (err) return done(err);
station.name.should.equal('a');
done();
});
@ -222,7 +226,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
it('should find one - with filter', function(done) {
Station.active.findOne({where: {name: 'c'}}, function(err, station) {
should.not.exist(err);
if (err) return done(err);
station.name.should.equal('c');
done();
});
@ -230,7 +234,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
it('should find by id - match', function(done) {
Station.active.findById(stationA.id, function(err, station) {
should.not.exist(err);
if (err) return done(err);
station.name.should.equal('a');
done();
});
@ -238,7 +242,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
it('should find by id - no match', function(done) {
Station.inactive.findById(stationA.id, function(err, station) {
should.not.exist(err);
if (err) return done(err);
should.not.exist(station);
done();
});
@ -246,7 +250,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
it('should count all in scope - active', function(done) {
Station.active.count(function(err, count) {
should.not.exist(err);
if (err) return done(err);
count.should.equal(2);
done();
});
@ -254,7 +258,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
it('should count all in scope - inactive', function(done) {
Station.inactive.count(function(err, count) {
should.not.exist(err);
if (err) return done(err);
count.should.equal(2);
done();
});
@ -262,7 +266,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
it('should count filtered - active', function(done) {
Station.active.count({order: {gt: 1}}, function(err, count) {
should.not.exist(err);
if (err) return done(err);
count.should.equal(1);
done();
});
@ -270,7 +274,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
it('should count filtered - inactive', function(done) {
Station.inactive.count({order: 2}, function(err, count) {
should.not.exist(err);
if (err) return done(err);
count.should.equal(1);
done();
});
@ -278,14 +282,14 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
it('should allow updateAll', function(done) {
Station.inactive.updateAll({flagged: true}, function(err, result) {
should.not.exist(err);
if (err) return done(err);
result.count.should.equal(2);
verify();
});
var verify = function() {
Station.flagged.count(function(err, count) {
should.not.exist(err);
if (err) return done(err);
count.should.equal(2);
done();
});
@ -294,14 +298,14 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
it('should allow filtered updateAll', function(done) {
Station.ordered.updateAll({active: true}, {flagged: true}, function(err, result) {
should.not.exist(err);
if (err) return done(err);
result.count.should.equal(2);
verify();
});
var verify = function() {
Station.flagged.count(function(err, count) {
should.not.exist(err);
if (err) return done(err);
count.should.equal(2);
done();
});
@ -310,16 +314,16 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
it('should allow filtered destroyAll', function(done) {
Station.ordered.destroyAll({active: false}, function(err) {
should.not.exist(err);
if (err) return done(err);
verify();
});
var verify = function() {
Station.ordered.count(function(err, count) {
should.not.exist(err);
if (err) return done(err);
count.should.equal(2);
Station.inactive.count(function(err, count) {
should.not.exist(err);
if (err) return done(err);
count.should.equal(0);
done();
});
@ -330,7 +334,7 @@ describe('scope - filtered count, updateAll and destroyAll', function() {
});
describe('scope - dynamic target class', function() {
var Collection, Media, Image, Video;
var Collection, Image, Video;
before(function() {
db = getSchema();
@ -375,10 +379,10 @@ describe('scope - dynamic target class', function() {
it('should deduce modelTo at runtime - Image', function(done) {
Collection.findOne({where: {modelName: 'Image'}}, function(err, coll) {
should.not.exist(err);
if (err) return done(err);
coll.name.should.equal('Images');
coll.items(function(err, items) {
should.not.exist(err);
if (err) return done(err);
items.length.should.equal(1);
items[0].name.should.equal('Image A');
items[0].should.be.instanceof(Image);
@ -389,10 +393,10 @@ describe('scope - dynamic target class', function() {
it('should deduce modelTo at runtime - Video', function(done) {
Collection.findOne({where: {modelName: 'Video'}}, function(err, coll) {
should.not.exist(err);
if (err) return done(err);
coll.name.should.equal('Videos');
coll.items(function(err, items) {
should.not.exist(err);
if (err) return done(err);
items.length.should.equal(1);
items[0].name.should.equal('Video A');
items[0].should.be.instanceof(Video);
@ -403,7 +407,7 @@ describe('scope - dynamic target class', function() {
it('should throw if modelTo is invalid', function(done) {
Collection.findOne({where: {name: 'Things'}}, function(err, coll) {
should.not.exist(err);
if (err) return done(err);
coll.modelName.should.equal('Unknown');
(function() {
coll.items(function(err, items) {});
@ -435,10 +439,10 @@ describe('scope - dynamic function', function() {
it('should deduce item by runtime creator', function(done) {
Item.dynamicQuery.findOne(function(err, firstQuery) {
should.not.exist(err);
if (err) return done(err);
firstQuery.title.should.equal(1);
Item.dynamicQuery.findOne(function(err, secondQuery) {
should.not.exist(err);
if (err) return done(err);
secondQuery.title.should.equal(2);
done();
});