loopback-connector-remote/test/remote-models.test.js

162 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

2016-05-06 19:02:21 +00:00
// Copyright IBM Corp. 2016. All Rights Reserved.
// Node module: loopback-connector-remote
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2016-09-01 08:12:12 +00:00
'use strict';
2016-01-07 06:45:29 +00:00
var assert = require('assert');
var helper = require('./helper');
var TaskEmitter = require('strong-task-emitter');
describe('Remote model tests', function() {
var ctx = this;
2016-02-05 12:29:43 +00:00
beforeEach(function(done) {
ctx.serverApp = helper.createRestAppAndListen();
2016-01-07 06:45:29 +00:00
ctx.ServerModel = helper.createModel({
parent: 'TestModel',
app: ctx.serverApp,
datasource: helper.createMemoryDataSource(),
properties: helper.userProperties
});
2016-02-05 12:29:43 +00:00
ctx.serverApp.locals.handler.on('listening', function() { done(); });
});
2016-01-07 06:45:29 +00:00
2016-02-05 12:29:43 +00:00
beforeEach(function setupRemoteClient(done) {
ctx.remoteApp = helper.createRestAppAndListen();
2016-01-07 06:45:29 +00:00
ctx.RemoteModel = helper.createModel({
parent: 'TestModel',
app: ctx.remoteApp,
datasource: helper.createRemoteDataSource(ctx.serverApp),
properties: helper.userProperties
});
2016-02-05 12:29:43 +00:00
ctx.remoteApp.locals.handler.on('listening', function() { done(); });
2016-01-07 06:45:29 +00:00
});
afterEach(function() {
ctx.serverApp.locals.handler.close();
ctx.remoteApp.locals.handler.close();
ctx.ServerModel = null;
ctx.RemoteModel = null;
});
describe('Model.create([data], [callback])', 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) {
2016-09-01 08:12:12 +00:00
if (err) return done(err);
2016-01-07 06:45:29 +00:00
assert(user instanceof ctx.RemoteModel);
done();
});
});
});
describe('model.save([options], [callback])', function() {
it('should save an instance of a Model to the attached data source',
function(done) {
var joe = new ctx.RemoteModel({first: 'Joe', last: 'Bob'});
joe.save(function(err, user) {
2016-09-01 08:12:12 +00:00
if (err) return done(err);
2016-01-07 06:45:29 +00:00
assert(user.id);
assert(!user.errors);
done();
});
});
});
describe('model.updateAttributes(data, [callback])', function() {
it('should save specified attributes to the attached data source',
function(done) {
ctx.ServerModel.create({first: 'joe', age: 100}, function(err, user) {
2016-09-01 08:12:12 +00:00
if (err) return done(err);
2016-01-07 06:45:29 +00:00
assert.equal(user.first, 'joe');
user.updateAttributes({
first: 'updatedFirst',
last: 'updatedLast'
}, function(err, updatedUser) {
2016-09-01 08:12:12 +00:00
if (err) return done(err);
2016-01-07 06:45:29 +00:00
assert.equal(updatedUser.first, 'updatedFirst');
assert.equal(updatedUser.last, 'updatedLast');
assert.equal(updatedUser.age, 100);
done();
});
});
});
});
describe('Model.upsert(data, callback)', 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) {
2016-09-01 08:12:12 +00:00
if (err) return done(err);
2016-01-07 06:45:29 +00:00
assert.equal(user.first, 'joe');
ctx.RemoteModel.upsert({first: 'bob', id: 7}, function(err,
updatedUser) {
2016-09-01 08:12:12 +00:00
if (err) return done(err);
2016-01-07 06:45:29 +00:00
assert.equal(updatedUser.first, 'bob');
done();
});
});
});
});
describe('Model.deleteById(id, [callback])', function() {
it('should delete a model instance from the attached data source',
function(done) {
ctx.ServerModel.create({first: 'joe', last: 'bob'}, function(err, user) {
2016-09-01 08:12:12 +00:00
if (err) return done(err);
2016-01-07 06:45:29 +00:00
ctx.RemoteModel.deleteById(user.id, function(err) {
2016-09-01 08:12:12 +00:00
if (err) return done(err);
2016-01-07 06:45:29 +00:00
ctx.RemoteModel.findById(user.id, function(err, notFound) {
assert.equal(notFound, null);
2016-09-01 08:12:12 +00:00
assert(err && err.statusCode === 404,
'should have failed with HTTP 404');
2016-01-07 06:45:29 +00:00
done();
});
});
});
});
});
describe('Model.findById(id, callback)', function() {
it('should find an instance by id from the attached data source',
function(done) {
ctx.ServerModel.create({first: 'michael', last: 'jordan', id: 23},
2016-09-01 08:12:12 +00:00
function(err) {
if (err) return done(err);
2016-01-07 06:45:29 +00:00
ctx.RemoteModel.findById(23, function(err, user) {
2016-09-01 08:12:12 +00:00
if (err) return done(err);
2016-01-07 06:45:29 +00:00
assert.equal(user.id, 23);
assert.equal(user.first, 'michael');
assert.equal(user.last, 'jordan');
done();
});
});
});
});
describe('Model.count([query], callback)', function() {
it('should return the count of Model instances from both data source',
function(done) {
var taskEmitter = new TaskEmitter();
taskEmitter
.task(ctx.ServerModel, 'create', {first: 'jill', age: 100})
.task(ctx.RemoteModel, 'create', {first: 'bob', age: 200})
.task(ctx.RemoteModel, 'create', {first: 'jan'})
.task(ctx.ServerModel, 'create', {first: 'sam'})
.task(ctx.ServerModel, 'create', {first: 'suzy'})
2016-09-01 08:12:12 +00:00
.on('done', function(err) {
if (err) return done(err);
2016-01-07 06:45:29 +00:00
ctx.RemoteModel.count({age: {gt: 99}}, function(err, count) {
2016-09-01 08:12:12 +00:00
if (err) return done(err);
2016-01-07 06:45:29 +00:00
assert.equal(count, 2);
done();
});
});
});
});
});