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

125 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-05-06 19:02:21 +00:00
// Copyright IBM Corp. 2014,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';
2014-09-25 21:06:06 +00:00
var assert = require('assert');
2016-01-07 06:45:29 +00:00
var helper = require('./helper');
2014-09-25 21:06:06 +00:00
describe('RemoteConnector', function() {
2016-01-07 06:45:29 +00:00
var ctx = this;
2016-02-05 12:29:43 +00:00
before(function setupServer(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()
});
2016-02-05 12:29:43 +00:00
ctx.serverApp.locals.handler.on('listening', function() { done(); });
});
before(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)
});
2016-02-05 12:29:43 +00:00
ctx.remoteApp.locals.handler.on('listening', function() { done(); });
2014-09-25 21:06:06 +00:00
});
2016-01-07 06:45:29 +00:00
after(function() {
ctx.serverApp.locals.handler.close();
ctx.remoteApp.locals.handler.close();
ctx.ServerModel = null;
ctx.RemoteModel = null;
2014-09-25 21:06:06 +00:00
});
2014-09-25 21:19:39 +00:00
it('should support the save method', function(done) {
2014-09-25 21:06:06 +00:00
var calledServerCreate = false;
2016-01-07 06:45:29 +00:00
ctx.ServerModel.create = function(data, cb, callback) {
2014-09-25 21:06:06 +00:00
calledServerCreate = true;
data.id = 1;
2016-01-07 06:45:29 +00:00
if (callback) callback(null, data);
else cb(null, data);
};
2014-09-25 21:06:06 +00:00
2016-01-07 06:45:29 +00:00
var m = new ctx.RemoteModel({foo: 'bar'});
m.save(function(err, instance) {
if (err) return done(err);
assert(instance);
assert(instance instanceof ctx.RemoteModel);
2014-09-25 21:06:06 +00:00
assert(calledServerCreate);
done();
});
});
2015-03-27 00:14:35 +00:00
it('should support aliases', function(done) {
2016-01-07 06:45:29 +00:00
var calledServerUpsert = false;
ctx.ServerModel.upsert = function(id, cb) {
calledServerUpsert = true;
cb();
2015-03-27 00:14:35 +00:00
};
2016-01-07 06:45:29 +00:00
ctx.RemoteModel.updateOrCreate({}, function(err, instance) {
2015-03-27 00:14:35 +00:00
if (err) return done(err);
2016-01-07 06:45:29 +00:00
assert(instance);
assert(instance instanceof ctx.RemoteModel);
2016-09-01 08:12:12 +00:00
assert(calledServerUpsert, 'server upsert should have been called');
2016-01-07 06:45:29 +00:00
done();
2015-03-27 00:14:35 +00:00
});
});
2014-09-25 21:06:06 +00:00
});
describe('Custom Path', function() {
2016-01-07 06:45:29 +00:00
var ctx = this;
2016-02-05 12:29:43 +00:00
before(function setupServer(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(),
options: {
http: {path: '/custom'}
}
});
2016-02-05 12:29:43 +00:00
ctx.serverApp.locals.handler.on('listening', function() { done(); });
});
2016-02-05 12:29:43 +00:00
before(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),
options: {
dataSource: 'remote',
http: {path: '/custom'}
}
});
2016-02-05 12:29:43 +00:00
ctx.remoteApp.locals.handler.on('listening', function() { done(); });
2016-01-07 06:45:29 +00:00
});
2016-01-07 06:45:29 +00:00
after(function(done)
{
ctx.serverApp.locals.handler.close();
ctx.remoteApp.locals.handler.close();
ctx.ServerModel = null;
ctx.RemoteModel = null;
done();
});
it('should support http.path configuration', function(done) {
2016-01-07 06:45:29 +00:00
ctx.RemoteModel.create({}, function(err, instance) {
if (err) return done(err);
assert(instance);
done();
});
});
2016-01-07 06:45:29 +00:00
});