Skip to content

Commit

Permalink
Merge pull request #54 from strongloop/fix/test
Browse files Browse the repository at this point in the history
test: use strict, handle errors
  • Loading branch information
bajtos authored Sep 1, 2016
2 parents 5882ea4 + e19cddd commit 59090d8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
8 changes: 8 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('..');

Expand All @@ -18,6 +20,12 @@ function createRestAppAndListen() {
app.set('host', '127.0.0.1');
app.set('port', 0);

app.set('legacyExplorer', false);
app.set('remoting', {
errorHandler: { debug: true, log: false },
context: false,
});

app.use(loopback.rest());
app.locals.handler = app.listen();

Expand Down
28 changes: 21 additions & 7 deletions test/models.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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);
Expand All @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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');
Expand All @@ -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();
});
Expand Down
4 changes: 3 additions & 1 deletion test/remote-connector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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();
});
});
Expand Down
25 changes: 18 additions & 7 deletions test/remote-models.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();
});
Expand All @@ -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();
});
Expand All @@ -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);
Expand All @@ -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();
});
Expand All @@ -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();
});
});
Expand All @@ -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');
Expand All @@ -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();
});
Expand Down

0 comments on commit 59090d8

Please sign in to comment.