2014-09-25 21:06:06 +00:00
|
|
|
var loopback = require('loopback');
|
|
|
|
var defineModelTestsWithDataSource = require('./util/model-tests');
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
describe('RemoteConnector', function() {
|
|
|
|
var remoteApp;
|
|
|
|
var remote;
|
|
|
|
|
|
|
|
defineModelTestsWithDataSource({
|
|
|
|
beforeEach: function(done) {
|
|
|
|
var test = this;
|
|
|
|
remoteApp = loopback();
|
|
|
|
remoteApp.use(loopback.rest());
|
|
|
|
remoteApp.listen(0, function() {
|
|
|
|
test.dataSource = loopback.createDataSource({
|
2015-07-21 01:58:17 +00:00
|
|
|
host: '127.0.0.1',
|
2014-09-25 21:06:06 +00:00
|
|
|
port: remoteApp.get('port'),
|
|
|
|
connector: loopback.Remote
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onDefine: function(Model) {
|
|
|
|
var RemoteModel = Model.extend(Model.modelName);
|
|
|
|
RemoteModel.attachTo(loopback.createDataSource({
|
|
|
|
connector: loopback.Memory
|
|
|
|
}));
|
|
|
|
remoteApp.model(RemoteModel);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function(done) {
|
|
|
|
var test = this;
|
|
|
|
remoteApp = this.remoteApp = loopback();
|
|
|
|
remoteApp.use(loopback.rest());
|
2014-09-25 21:19:39 +00:00
|
|
|
var ServerModel = this.ServerModel =
|
|
|
|
loopback.PersistedModel.extend('TestModel');
|
2014-09-25 21:06:06 +00:00
|
|
|
|
|
|
|
remoteApp.model(ServerModel);
|
|
|
|
|
|
|
|
remoteApp.listen(0, function() {
|
|
|
|
test.remote = loopback.createDataSource({
|
2015-07-21 01:58:17 +00:00
|
|
|
host: '127.0.0.1',
|
2014-09-25 21:06:06 +00:00
|
|
|
port: remoteApp.get('port'),
|
|
|
|
connector: loopback.Remote
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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;
|
|
|
|
var RemoteModel = loopback.PersistedModel.extend('TestModel');
|
|
|
|
RemoteModel.attachTo(this.remote);
|
|
|
|
|
|
|
|
var ServerModel = this.ServerModel;
|
|
|
|
|
|
|
|
ServerModel.create = function(data, cb) {
|
|
|
|
calledServerCreate = true;
|
|
|
|
data.id = 1;
|
|
|
|
cb(null, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerModel.setupRemoting();
|
|
|
|
|
|
|
|
var m = new RemoteModel({foo: 'bar'});
|
|
|
|
m.save(function(err, inst) {
|
|
|
|
assert(inst instanceof RemoteModel);
|
|
|
|
assert(calledServerCreate);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2015-03-27 00:14:35 +00:00
|
|
|
|
|
|
|
it('should support aliases', function(done) {
|
|
|
|
var RemoteModel = loopback.PersistedModel.extend('TestModel');
|
|
|
|
RemoteModel.attachTo(this.remote);
|
|
|
|
|
|
|
|
var ServerModel = this.ServerModel;
|
|
|
|
|
|
|
|
ServerModel.upsert = function(id, cb) {
|
|
|
|
done();
|
|
|
|
};
|
|
|
|
|
|
|
|
RemoteModel.updateOrCreate({}, function(err, inst) {
|
|
|
|
if (err) return done(err);
|
|
|
|
});
|
|
|
|
});
|
2014-09-25 21:06:06 +00:00
|
|
|
});
|