test: use strict, handle errors

This commit is contained in:
Miroslav Bajtoš 2016-09-01 10:12:12 +02:00
parent f35dd4bfc7
commit 3ed4d31eec
4 changed files with 44 additions and 15 deletions

View File

@ -3,6 +3,8 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var loopback = require('loopback'); var loopback = require('loopback');
var remoteConnector = require('..'); var remoteConnector = require('..');

View File

@ -3,6 +3,8 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var assert = require('assert'); var assert = require('assert');
var helper = require('./helper'); var helper = require('./helper');
var TaskEmitter = require('strong-task-emitter'); 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', it('should create an instance and save to the attached data source',
function(done) { function(done) {
User.create({first: 'Joe', last: 'Bob'}, function(err, user) { User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
if (err) return done(err);
assert(user instanceof User); assert(user instanceof User);
done(); done();
}); });
@ -114,8 +117,8 @@ describe('Model tests', function() {
function(done) { function(done) {
var joe = new User({first: 'Joe', last: 'Bob'}); var joe = new User({first: 'Joe', last: 'Bob'});
joe.save(function(err, user) { joe.save(function(err, user) {
if (err) return done(err);
assert(user.id); assert(user.id);
assert(!err);
assert(!user.errors); assert(!user.errors);
done(); done();
}); });
@ -126,14 +129,14 @@ describe('Model tests', function() {
it('should save specified attributes to the attached data source', it('should save specified attributes to the attached data source',
function(done) { function(done) {
User.create({first: 'joe', age: 100}, function(err, user) { User.create({first: 'joe', age: 100}, function(err, user) {
assert(!err); if (err) return done(err);
assert.equal(user.first, 'joe'); assert.equal(user.first, 'joe');
user.updateAttributes({ user.updateAttributes({
first: 'updatedFirst', first: 'updatedFirst',
last: 'updatedLast' last: 'updatedLast'
}, function(err, updatedUser) { }, function(err, updatedUser) {
assert(!err); if (err) return done(err);
assert.equal(updatedUser.first, 'updatedFirst'); assert.equal(updatedUser.first, 'updatedFirst');
assert.equal(updatedUser.last, 'updatedLast'); assert.equal(updatedUser.last, 'updatedLast');
assert.equal(updatedUser.age, 100); 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', it('should update when a record with id=data.id is found, insert otherwise',
function(done) { function(done) {
User.upsert({first: 'joe', id: 7}, function(err, user) { User.upsert({first: 'joe', id: 7}, function(err, user) {
assert(!err); if (err) return done(err);
assert.equal(user.first, 'joe'); assert.equal(user.first, 'joe');
User.upsert({first: 'bob', id: 7}, function(err, updatedUser) { User.upsert({first: 'bob', id: 7}, function(err, updatedUser) {
assert(!err); if (err) return done(err);
assert.equal(updatedUser.first, 'bob'); assert.equal(updatedUser.first, 'bob');
done(); done();
}); });
@ -162,10 +165,14 @@ describe('Model tests', function() {
describe('model.destroy([callback])', function() { describe('model.destroy([callback])', function() {
it('should remove a model from the attached data source', function(done) { it('should remove a model from the attached data source', function(done) {
User.create({first: 'joe', last: 'bob'}, function(err, user) { User.create({first: 'joe', last: 'bob'}, function(err, user) {
if (err) return done(err);
User.findById(user.id, function(err, foundUser) { User.findById(user.id, function(err, foundUser) {
if (err) return done(err);
assert.equal(user.id, foundUser.id); 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) { User.findById(user.id, function(err, notFound) {
if (err) return done(err);
assert.equal(notFound, null); assert.equal(notFound, null);
done(); done();
}); });
@ -179,8 +186,11 @@ describe('Model tests', function() {
it('should delete a model instance from the attached data source', it('should delete a model instance from the attached data source',
function(done) { function(done) {
User.create({first: 'joe', last: 'bob'}, function(err, user) { User.create({first: 'joe', last: 'bob'}, function(err, user) {
if (err) return done(err);
User.deleteById(user.id, function(err) { User.deleteById(user.id, function(err) {
if (err) return done(err);
User.findById(user.id, function(err, notFound) { User.findById(user.id, function(err, notFound) {
if (err) return done(err);
assert.equal(notFound, null); assert.equal(notFound, null);
done(); done();
}); });
@ -191,8 +201,11 @@ describe('Model tests', function() {
describe('Model.findById(id, callback)', function() { describe('Model.findById(id, callback)', function() {
it('should find an instance by id', function(done) { 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) { 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.id, 23);
assert.equal(user.first, 'michael'); assert.equal(user.first, 'michael');
assert.equal(user.last, 'jordan'); assert.equal(user.last, 'jordan');
@ -214,6 +227,7 @@ describe('Model tests', function() {
.task(User, 'create', {first: 'suzy'}) .task(User, 'create', {first: 'suzy'})
.on('done', function() { .on('done', function() {
User.count({age: {gt: 99}}, function(err, count) { User.count({age: {gt: 99}}, function(err, count) {
if (err) return done(err);
assert.equal(count, 2); assert.equal(count, 2);
done(); done();
}); });

View File

@ -3,6 +3,8 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var assert = require('assert'); var assert = require('assert');
var helper = require('./helper'); var helper = require('./helper');
@ -67,7 +69,7 @@ describe('RemoteConnector', function() {
if (err) return done(err); if (err) return done(err);
assert(instance); assert(instance);
assert(instance instanceof ctx.RemoteModel); assert(instance instanceof ctx.RemoteModel);
assert(calledServerUpsert); assert(calledServerUpsert, 'server upsert should have been called');
done(); done();
}); });
}); });

View File

@ -3,6 +3,8 @@
// This file is licensed under the MIT License. // This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT // License text available at https://opensource.org/licenses/MIT
'use strict';
var assert = require('assert'); var assert = require('assert');
var helper = require('./helper'); var helper = require('./helper');
var TaskEmitter = require('strong-task-emitter'); 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', it('should create an instance and save to the attached data source',
function(done) { function(done) {
ctx.RemoteModel.create({first: 'Joe', last: 'Bob'}, function(err, user) { ctx.RemoteModel.create({first: 'Joe', last: 'Bob'}, function(err, user) {
if (err) return done(err);
assert(user instanceof ctx.RemoteModel); assert(user instanceof ctx.RemoteModel);
done(); done();
}); });
@ -54,8 +57,8 @@ describe('Remote model tests', function() {
function(done) { function(done) {
var joe = new ctx.RemoteModel({first: 'Joe', last: 'Bob'}); var joe = new ctx.RemoteModel({first: 'Joe', last: 'Bob'});
joe.save(function(err, user) { joe.save(function(err, user) {
if (err) return done(err);
assert(user.id); assert(user.id);
assert(!err);
assert(!user.errors); assert(!user.errors);
done(); done();
}); });
@ -66,14 +69,14 @@ describe('Remote model tests', function() {
it('should save specified attributes to the attached data source', it('should save specified attributes to the attached data source',
function(done) { function(done) {
ctx.ServerModel.create({first: 'joe', age: 100}, function(err, user) { ctx.ServerModel.create({first: 'joe', age: 100}, function(err, user) {
assert(!err); if (err) return done(err);
assert.equal(user.first, 'joe'); assert.equal(user.first, 'joe');
user.updateAttributes({ user.updateAttributes({
first: 'updatedFirst', first: 'updatedFirst',
last: 'updatedLast' last: 'updatedLast'
}, function(err, updatedUser) { }, function(err, updatedUser) {
assert(!err); if (err) return done(err);
assert.equal(updatedUser.first, 'updatedFirst'); assert.equal(updatedUser.first, 'updatedFirst');
assert.equal(updatedUser.last, 'updatedLast'); assert.equal(updatedUser.last, 'updatedLast');
assert.equal(updatedUser.age, 100); 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', it('should update when a record with id=data.id is found, insert otherwise',
function(done) { function(done) {
ctx.RemoteModel.upsert({first: 'joe', id: 7}, function(err, user) { ctx.RemoteModel.upsert({first: 'joe', id: 7}, function(err, user) {
assert(!err); if (err) return done(err);
assert.equal(user.first, 'joe'); assert.equal(user.first, 'joe');
ctx.RemoteModel.upsert({first: 'bob', id: 7}, function(err, ctx.RemoteModel.upsert({first: 'bob', id: 7}, function(err,
updatedUser) { updatedUser) {
assert(!err); if (err) return done(err);
assert.equal(updatedUser.first, 'bob'); assert.equal(updatedUser.first, 'bob');
done(); done();
}); });
@ -104,9 +107,13 @@ describe('Remote model tests', function() {
it('should delete a model instance from the attached data source', it('should delete a model instance from the attached data source',
function(done) { function(done) {
ctx.ServerModel.create({first: 'joe', last: 'bob'}, function(err, user) { ctx.ServerModel.create({first: 'joe', last: 'bob'}, function(err, user) {
if (err) return done(err);
ctx.RemoteModel.deleteById(user.id, function(err) { ctx.RemoteModel.deleteById(user.id, function(err) {
if (err) return done(err);
ctx.RemoteModel.findById(user.id, function(err, notFound) { ctx.RemoteModel.findById(user.id, function(err, notFound) {
assert.equal(notFound, null); assert.equal(notFound, null);
assert(err && err.statusCode === 404,
'should have failed with HTTP 404');
done(); done();
}); });
}); });
@ -118,8 +125,10 @@ describe('Remote model tests', function() {
it('should find an instance by id from the attached data source', it('should find an instance by id from the attached data source',
function(done) { function(done) {
ctx.ServerModel.create({first: 'michael', last: 'jordan', id: 23}, ctx.ServerModel.create({first: 'michael', last: 'jordan', id: 23},
function() { function(err) {
if (err) return done(err);
ctx.RemoteModel.findById(23, function(err, user) { ctx.RemoteModel.findById(23, function(err, user) {
if (err) return done(err);
assert.equal(user.id, 23); assert.equal(user.id, 23);
assert.equal(user.first, 'michael'); assert.equal(user.first, 'michael');
assert.equal(user.last, 'jordan'); assert.equal(user.last, 'jordan');
@ -139,8 +148,10 @@ describe('Remote model tests', function() {
.task(ctx.RemoteModel, 'create', {first: 'jan'}) .task(ctx.RemoteModel, 'create', {first: 'jan'})
.task(ctx.ServerModel, 'create', {first: 'sam'}) .task(ctx.ServerModel, 'create', {first: 'sam'})
.task(ctx.ServerModel, 'create', {first: 'suzy'}) .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) { ctx.RemoteModel.count({age: {gt: 99}}, function(err, count) {
if (err) return done(err);
assert.equal(count, 2); assert.equal(count, 2);
done(); done();
}); });