From 3ed4d31eec81f529acdb9525255e8d7cfbcc62ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Thu, 1 Sep 2016 10:12:12 +0200 Subject: [PATCH] test: use strict, handle errors --- test/helper.js | 2 ++ test/models.test.js | 28 +++++++++++++++++++++------- test/remote-connector.test.js | 4 +++- test/remote-models.test.js | 25 ++++++++++++++++++------- 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/test/helper.js b/test/helper.js index b92df5d..da9a89c 100644 --- a/test/helper.js +++ b/test/helper.js @@ -3,6 +3,8 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; + var loopback = require('loopback'); var remoteConnector = require('..'); diff --git a/test/models.test.js b/test/models.test.js index 624cf0c..3201344 100644 --- a/test/models.test.js +++ b/test/models.test.js @@ -3,6 +3,8 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; + var assert = require('assert'); var helper = require('./helper'); var TaskEmitter = require('strong-task-emitter'); @@ -103,6 +105,7 @@ describe('Model tests', function() { it('should create an instance and save to the attached data source', function(done) { User.create({first: 'Joe', last: 'Bob'}, function(err, user) { + if (err) return done(err); assert(user instanceof User); done(); }); @@ -114,8 +117,8 @@ describe('Model tests', function() { function(done) { var joe = new User({first: 'Joe', last: 'Bob'}); joe.save(function(err, user) { + if (err) return done(err); assert(user.id); - assert(!err); assert(!user.errors); done(); }); @@ -126,14 +129,14 @@ describe('Model tests', function() { it('should save specified attributes to the attached data source', function(done) { User.create({first: 'joe', age: 100}, function(err, user) { - assert(!err); + if (err) return done(err); assert.equal(user.first, 'joe'); user.updateAttributes({ first: 'updatedFirst', last: 'updatedLast' }, function(err, updatedUser) { - assert(!err); + if (err) return done(err); assert.equal(updatedUser.first, 'updatedFirst'); assert.equal(updatedUser.last, 'updatedLast'); assert.equal(updatedUser.age, 100); @@ -147,11 +150,11 @@ describe('Model tests', function() { it('should update when a record with id=data.id is found, insert otherwise', function(done) { User.upsert({first: 'joe', id: 7}, function(err, user) { - assert(!err); + if (err) return done(err); assert.equal(user.first, 'joe'); User.upsert({first: 'bob', id: 7}, function(err, updatedUser) { - assert(!err); + if (err) return done(err); assert.equal(updatedUser.first, 'bob'); done(); }); @@ -162,10 +165,14 @@ describe('Model tests', function() { describe('model.destroy([callback])', function() { it('should remove a model from the attached data source', function(done) { User.create({first: 'joe', last: 'bob'}, function(err, user) { + if (err) return done(err); User.findById(user.id, function(err, foundUser) { + if (err) return done(err); assert.equal(user.id, foundUser.id); - foundUser.destroy(function() { + foundUser.destroy(function(err) { + if (err) return done(err); User.findById(user.id, function(err, notFound) { + if (err) return done(err); assert.equal(notFound, null); done(); }); @@ -179,8 +186,11 @@ describe('Model tests', function() { it('should delete a model instance from the attached data source', function(done) { User.create({first: 'joe', last: 'bob'}, function(err, user) { + if (err) return done(err); User.deleteById(user.id, function(err) { + if (err) return done(err); User.findById(user.id, function(err, notFound) { + if (err) return done(err); assert.equal(notFound, null); done(); }); @@ -191,8 +201,11 @@ describe('Model tests', function() { describe('Model.findById(id, callback)', function() { it('should find an instance by id', function(done) { - User.create({first: 'michael', last: 'jordan', id: 23}, function() { + User.create({first: 'michael', last: 'jordan', id: 23}, function(err) { + if (err) return done(err); User.findById(23, function(err, user) { + if (err) return done(err); + assert(user, 'user should have been found'); assert.equal(user.id, 23); assert.equal(user.first, 'michael'); assert.equal(user.last, 'jordan'); @@ -214,6 +227,7 @@ describe('Model tests', function() { .task(User, 'create', {first: 'suzy'}) .on('done', function() { User.count({age: {gt: 99}}, function(err, count) { + if (err) return done(err); assert.equal(count, 2); done(); }); diff --git a/test/remote-connector.test.js b/test/remote-connector.test.js index 0cd894d..7b928c9 100644 --- a/test/remote-connector.test.js +++ b/test/remote-connector.test.js @@ -3,6 +3,8 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; + var assert = require('assert'); var helper = require('./helper'); @@ -67,7 +69,7 @@ describe('RemoteConnector', function() { if (err) return done(err); assert(instance); assert(instance instanceof ctx.RemoteModel); - assert(calledServerUpsert); + assert(calledServerUpsert, 'server upsert should have been called'); done(); }); }); diff --git a/test/remote-models.test.js b/test/remote-models.test.js index c6044a3..fcabee2 100644 --- a/test/remote-models.test.js +++ b/test/remote-models.test.js @@ -3,6 +3,8 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT +'use strict'; + var assert = require('assert'); var helper = require('./helper'); var TaskEmitter = require('strong-task-emitter'); @@ -43,6 +45,7 @@ describe('Remote model tests', function() { it('should create an instance and save to the attached data source', function(done) { ctx.RemoteModel.create({first: 'Joe', last: 'Bob'}, function(err, user) { + if (err) return done(err); assert(user instanceof ctx.RemoteModel); done(); }); @@ -54,8 +57,8 @@ describe('Remote model tests', function() { function(done) { var joe = new ctx.RemoteModel({first: 'Joe', last: 'Bob'}); joe.save(function(err, user) { + if (err) return done(err); assert(user.id); - assert(!err); assert(!user.errors); done(); }); @@ -66,14 +69,14 @@ describe('Remote model tests', function() { it('should save specified attributes to the attached data source', function(done) { ctx.ServerModel.create({first: 'joe', age: 100}, function(err, user) { - assert(!err); + if (err) return done(err); assert.equal(user.first, 'joe'); user.updateAttributes({ first: 'updatedFirst', last: 'updatedLast' }, function(err, updatedUser) { - assert(!err); + if (err) return done(err); assert.equal(updatedUser.first, 'updatedFirst'); assert.equal(updatedUser.last, 'updatedLast'); assert.equal(updatedUser.age, 100); @@ -87,12 +90,12 @@ describe('Remote model tests', function() { it('should update when a record with id=data.id is found, insert otherwise', function(done) { ctx.RemoteModel.upsert({first: 'joe', id: 7}, function(err, user) { - assert(!err); + if (err) return done(err); assert.equal(user.first, 'joe'); ctx.RemoteModel.upsert({first: 'bob', id: 7}, function(err, updatedUser) { - assert(!err); + if (err) return done(err); assert.equal(updatedUser.first, 'bob'); done(); }); @@ -104,9 +107,13 @@ describe('Remote model tests', function() { it('should delete a model instance from the attached data source', function(done) { ctx.ServerModel.create({first: 'joe', last: 'bob'}, function(err, user) { + if (err) return done(err); ctx.RemoteModel.deleteById(user.id, function(err) { + if (err) return done(err); ctx.RemoteModel.findById(user.id, function(err, notFound) { assert.equal(notFound, null); + assert(err && err.statusCode === 404, + 'should have failed with HTTP 404'); done(); }); }); @@ -118,8 +125,10 @@ describe('Remote model tests', function() { it('should find an instance by id from the attached data source', function(done) { ctx.ServerModel.create({first: 'michael', last: 'jordan', id: 23}, - function() { + function(err) { + if (err) return done(err); ctx.RemoteModel.findById(23, function(err, user) { + if (err) return done(err); assert.equal(user.id, 23); assert.equal(user.first, 'michael'); assert.equal(user.last, 'jordan'); @@ -139,8 +148,10 @@ describe('Remote model tests', function() { .task(ctx.RemoteModel, 'create', {first: 'jan'}) .task(ctx.ServerModel, 'create', {first: 'sam'}) .task(ctx.ServerModel, 'create', {first: 'suzy'}) - .on('done', function() { + .on('done', function(err) { + if (err) return done(err); ctx.RemoteModel.count({age: {gt: 99}}, function(err, count) { + if (err) return done(err); assert.equal(count, 2); done(); });