Fix formatting

This commit is contained in:
Krishna Raman 2014-09-25 14:19:39 -07:00
parent 724fd7d99c
commit 2df5b726b1
3 changed files with 228 additions and 206 deletions

View File

@ -17,7 +17,9 @@ module.exports = RemoteConnector;
*/ */
function RemoteConnector(settings) { function RemoteConnector(settings) {
assert(typeof settings === 'object', 'cannot initiaze RemoteConnector without a settings object'); assert(typeof settings ===
'object',
'cannot initiaze RemoteConnector without a settings object');
this.client = settings.client; this.client = settings.client;
this.adapter = settings.adapter || 'rest'; this.adapter = settings.adapter || 'rest';
this.protocol = settings.protocol || 'http' this.protocol = settings.protocol || 'http'
@ -29,14 +31,15 @@ function RemoteConnector(settings) {
// TODO(ritch) make sure this name works with Model.getSourceId() // TODO(ritch) make sure this name works with Model.getSourceId()
this.name = 'remote-connector'; this.name = 'remote-connector';
if(settings.url) { if (settings.url) {
this.url = settings.url; this.url = settings.url;
} else { } else {
this.url = this.protocol + '://' + this.host + ':' + this.port + this.root; this.url = this.protocol + '://' + this.host + ':' + this.port + this.root;
} }
// handle mixins in the define() method // handle mixins in the define() method
var DAO = this.DataAccessObject = function() {}; var DAO = this.DataAccessObject = function() {
};
} }
RemoteConnector.prototype.connect = function() { RemoteConnector.prototype.connect = function() {
@ -44,7 +47,8 @@ RemoteConnector.prototype.connect = function() {
} }
RemoteConnector.initialize = function(dataSource, callback) { RemoteConnector.initialize = function(dataSource, callback) {
var connector = dataSource.connector = new RemoteConnector(dataSource.settings); var connector = dataSource.connector =
new RemoteConnector(dataSource.settings);
connector.connect(); connector.connect();
callback(); callback();
} }
@ -54,18 +58,19 @@ RemoteConnector.prototype.define = function(definition) {
var remotes = this.remotes; var remotes = this.remotes;
var SharedClass; var SharedClass;
assert(Model.sharedClass, 'cannot attach ' + Model.modelName assert(Model.sharedClass,
+ ' to a remote connector without a Model.sharedClass'); 'cannot attach ' +
Model.modelName +
' to a remote connector without a Model.sharedClass');
remotes.addClass(Model.sharedClass); remotes.addClass(Model.sharedClass);
Model Model.sharedClass.methods().forEach(function(remoteMethod) {
.sharedClass
.methods()
.forEach(function(remoteMethod) {
// TODO(ritch) more elegant way of ignoring a nested shared class // TODO(ritch) more elegant way of ignoring a nested shared class
if(remoteMethod.name !== 'Change' if (remoteMethod.name !==
&& remoteMethod.name !== 'Checkpoint') { 'Change' &&
remoteMethod.name !==
'Checkpoint') {
createProxyMethod(Model, remotes, remoteMethod); createProxyMethod(Model, remotes, remoteMethod);
} }
}); });
@ -74,12 +79,12 @@ RemoteConnector.prototype.define = function(definition) {
function createProxyMethod(Model, remotes, remoteMethod) { function createProxyMethod(Model, remotes, remoteMethod) {
var scope = remoteMethod.isStatic ? Model : Model.prototype; var scope = remoteMethod.isStatic ? Model : Model.prototype;
var original = scope[remoteMethod.name]; var original = scope[remoteMethod.name];
scope[remoteMethod.name] = function remoteMethodProxy() { scope[remoteMethod.name] = function remoteMethodProxy() {
var args = Array.prototype.slice.call(arguments); var args = Array.prototype.slice.call(arguments);
var lastArgIsFunc = typeof args[args.length - 1] === 'function'; var lastArgIsFunc = typeof args[args.length - 1] === 'function';
var callback; var callback;
if(lastArgIsFunc) { if (lastArgIsFunc) {
callback = args.pop(); callback = args.pop();
} }
@ -87,4 +92,5 @@ function createProxyMethod(Model, remotes, remoteMethod) {
} }
} }
function noop() {} function noop() {
}

View File

@ -33,7 +33,8 @@ describe('RemoteConnector', function() {
var test = this; var test = this;
remoteApp = this.remoteApp = loopback(); remoteApp = this.remoteApp = loopback();
remoteApp.use(loopback.rest()); remoteApp.use(loopback.rest());
var ServerModel = this.ServerModel = loopback.PersistedModel.extend('TestModel'); var ServerModel = this.ServerModel =
loopback.PersistedModel.extend('TestModel');
remoteApp.model(ServerModel); remoteApp.model(ServerModel);
@ -47,7 +48,7 @@ describe('RemoteConnector', function() {
}); });
}); });
it('should support the save method', function (done) { it('should support the save method', function(done) {
var calledServerCreate = false; var calledServerCreate = false;
var RemoteModel = loopback.PersistedModel.extend('TestModel'); var RemoteModel = loopback.PersistedModel.extend('TestModel');
RemoteModel.attachTo(this.remote); RemoteModel.attachTo(this.remote);

View File

@ -10,240 +10,255 @@ var TaskEmitter = require('strong-task-emitter');
module.exports = function defineModelTestsWithDataSource(options) { module.exports = function defineModelTestsWithDataSource(options) {
describe('Model Tests', function() { describe('Model Tests', function() {
var User, dataSource; var User, dataSource;
if(options.beforeEach) { if (options.beforeEach) {
beforeEach(options.beforeEach); beforeEach(options.beforeEach);
}
beforeEach(function() {
var test = this;
// setup a model / datasource
dataSource = this.dataSource || loopback.createDataSource(options.dataSource);
var extend = PersistedModel.extend;
// create model hook
PersistedModel.extend = function() {
var extendedModel = extend.apply(PersistedModel, arguments);
if(options.onDefine) {
options.onDefine.call(test, extendedModel);
}
return extendedModel;
} }
User = PersistedModel.extend('user', { beforeEach(function() {
'first': String, var test = this;
'last': String,
'age': Number, // setup a model / datasource
'password': String, dataSource =
'gender': String, this.dataSource || loopback.createDataSource(options.dataSource);
'domain': String,
'email': String var extend = PersistedModel.extend;
}, {
trackChanges: true // create model hook
PersistedModel.extend = function() {
var extendedModel = extend.apply(PersistedModel, arguments);
if (options.onDefine) {
options.onDefine.call(test, extendedModel);
}
return extendedModel;
}
User = PersistedModel.extend('user', {
'first': String,
'last': String,
'age': Number,
'password': String,
'gender': String,
'domain': String,
'email': String
}, {
trackChanges: true
});
// enable destroy all for testing
User.destroyAll.shared = true;
User.attachTo(dataSource);
}); });
// enable destroy all for testing describe('Model.validatesPresenceOf(properties...)', function() {
User.destroyAll.shared = true; it("Require a model to include a property to be considered valid",
User.attachTo(dataSource); function() {
}); User.validatesPresenceOf('first', 'last', 'age');
var joe = new User({first: 'joe'});
describe('Model.validatesPresenceOf(properties...)', function() { assert(joe.isValid() === false, 'model should not validate');
it("Require a model to include a property to be considered valid", function() { assert(joe.errors.last, 'should have a missing last error');
User.validatesPresenceOf('first', 'last', 'age'); assert(joe.errors.age, 'should have a missing age error');
var joe = new User({first: 'joe'}); });
assert(joe.isValid() === false, 'model should not validate');
assert(joe.errors.last, 'should have a missing last error');
assert(joe.errors.age, 'should have a missing age error');
}); });
});
describe('Model.validatesLengthOf(property, options)', function() { describe('Model.validatesLengthOf(property, options)', function() {
it("Require a property length to be within a specified range", function() { it("Require a property length to be within a specified range",
User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}}); function() {
var joe = new User({password: '1234'}); User.validatesLengthOf('password',
assert(joe.isValid() === false, 'model should not be valid'); {min: 5, message: {min: 'Password is too short'}});
assert(joe.errors.password, 'should have password error'); var joe = new User({password: '1234'});
assert(joe.isValid() === false, 'model should not be valid');
assert(joe.errors.password, 'should have password error');
});
}); });
});
describe('Model.validatesInclusionOf(property, options)', function() { describe('Model.validatesInclusionOf(property, options)', function() {
it("Require a value for `property` to be in the specified array", function() { it("Require a value for `property` to be in the specified array",
User.validatesInclusionOf('gender', {in: ['male', 'female']}); function() {
var foo = new User({gender: 'bar'}); User.validatesInclusionOf('gender', {in: ['male', 'female']});
assert(foo.isValid() === false, 'model should not be valid'); var foo = new User({gender: 'bar'});
assert(foo.errors.gender, 'should have gender error'); assert(foo.isValid() === false, 'model should not be valid');
assert(foo.errors.gender, 'should have gender error');
});
}); });
});
describe('Model.validatesExclusionOf(property, options)', function() { describe('Model.validatesExclusionOf(property, options)', function() {
it("Require a value for `property` to not exist in the specified array", function() { it("Require a value for `property` to not exist in the specified array",
User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']}); function() {
var foo = new User({domain: 'www'}); User.validatesExclusionOf('domain',
var bar = new User({domain: 'billing'}); {in: ['www', 'billing', 'admin']});
var bat = new User({domain: 'admin'}); var foo = new User({domain: 'www'});
assert(foo.isValid() === false); var bar = new User({domain: 'billing'});
assert(bar.isValid() === false); var bat = new User({domain: 'admin'});
assert(bat.isValid() === false); assert(foo.isValid() === false);
assert(foo.errors.domain, 'model should have a domain error'); assert(bar.isValid() === false);
assert(bat.errors.domain, 'model should have a domain error'); assert(bat.isValid() === false);
assert(bat.errors.domain, 'model should have a domain error'); assert(foo.errors.domain, 'model should have a domain error');
assert(bat.errors.domain, 'model should have a domain error');
assert(bat.errors.domain, 'model should have a domain error');
});
}); });
});
describe('Model.validatesNumericalityOf(property, options)', function() { describe('Model.validatesNumericalityOf(property, options)', function() {
it("Require a value for `property` to be a specific type of `Number`", function() { it("Require a value for `property` to be a specific type of `Number`",
User.validatesNumericalityOf('age', {int: true}); function() {
var joe = new User({age: 10.2}); User.validatesNumericalityOf('age', {int: true});
assert(joe.isValid() === false); var joe = new User({age: 10.2});
var bob = new User({age: 0}); assert(joe.isValid() === false);
assert(bob.isValid() === true); var bob = new User({age: 0});
assert(joe.errors.age, 'model should have an age error'); assert(bob.isValid() === true);
assert(joe.errors.age, 'model should have an age error');
});
}); });
});
describe('myModel.isValid()', function() { describe('myModel.isValid()', function() {
it("Validate the model instance", function() { it("Validate the model instance", function() {
User.validatesNumericalityOf('age', {int: true}); User.validatesNumericalityOf('age', {int: true});
var user = new User({first: 'joe', age: 'flarg'}) var user = new User({first: 'joe', age: 'flarg'})
var valid = user.isValid(); var valid = user.isValid();
assert(valid === false);
assert(user.errors.age, 'model should have age error');
});
it('Asynchronously validate the model', function(done) {
User.validatesNumericalityOf('age', {int: true});
var user = new User({first: 'joe', age: 'flarg'});
user.isValid(function (valid) {
assert(valid === false); assert(valid === false);
assert(user.errors.age, 'model should have age error'); assert(user.errors.age, 'model should have age error');
done();
}); });
});
});
describe('Model.create([data], [callback])', function() { it('Asynchronously validate the model', function(done) {
it("Create an instance of Model with given data and save to the attached data source", function(done) { User.validatesNumericalityOf('age', {int: true});
User.create({first: 'Joe', last: 'Bob'}, function(err, user) { var user = new User({first: 'joe', age: 'flarg'});
assert(user instanceof User); user.isValid(function(valid) {
done(); assert(valid === false);
}); assert(user.errors.age, 'model should have age error');
});
});
describe('model.save([options], [callback])', function() {
it("Save an instance of a Model to the attached data source", function(done) {
var joe = new User({first: 'Joe', last: 'Bob'});
joe.save(function(err, user) {
assert(user.id);
assert(!err);
assert(!user.errors);
done();
});
});
});
describe('model.updateAttributes(data, [callback])', function() {
it("Save specified attributes to the attached data source", function(done) {
User.create({first: 'joe', age: 100}, function (err, user) {
assert(!err);
assert.equal(user.first, 'joe');
user.updateAttributes({
first: 'updatedFirst',
last: 'updatedLast'
}, function (err, updatedUser) {
assert(!err);
assert.equal(updatedUser.first, 'updatedFirst');
assert.equal(updatedUser.last, 'updatedLast');
assert.equal(updatedUser.age, 100);
done(); done();
}); });
}); });
}); });
});
describe('Model.upsert(data, callback)', function() { describe('Model.create([data], [callback])', function() {
it("Update when record with id=data.id found, insert otherwise", function(done) { it("Create an instance of Model with given data and save to the attached data source",
User.upsert({first: 'joe', id: 7}, function (err, user) { function(done) {
assert(!err); User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
assert.equal(user.first, 'joe'); assert(user instanceof User);
done();
User.upsert({first: 'bob', id: 7}, function (err, updatedUser) { });
assert(!err);
assert.equal(updatedUser.first, 'bob');
done();
}); });
});
}); });
});
describe('model.destroy([callback])', function() { describe('model.save([options], [callback])', function() {
it("Remove a model from the attached data source", function(done) { it("Save an instance of a Model to the attached data source",
User.create({first: 'joe', last: 'bob'}, function (err, user) { function(done) {
User.findById(user.id, function (err, foundUser) { var joe = new User({first: 'Joe', last: 'Bob'});
assert.equal(user.id, foundUser.id); joe.save(function(err, user) {
foundUser.destroy(function () { assert(user.id);
User.findById(user.id, function (err, notFound) { assert(!err);
assert.equal(notFound, null); assert(!user.errors);
done();
});
});
});
describe('model.updateAttributes(data, [callback])', function() {
it("Save specified attributes to the attached data source",
function(done) {
User.create({first: 'joe', age: 100}, function(err, user) {
assert(!err);
assert.equal(user.first, 'joe');
user.updateAttributes({
first: 'updatedFirst',
last: 'updatedLast'
}, function(err, updatedUser) {
assert(!err);
assert.equal(updatedUser.first, 'updatedFirst');
assert.equal(updatedUser.last, 'updatedLast');
assert.equal(updatedUser.age, 100);
done(); done();
}); });
}); });
}); });
});
describe('Model.upsert(data, callback)', function() {
it("Update when record with id=data.id found, insert otherwise",
function(done) {
User.upsert({first: 'joe', id: 7}, function(err, user) {
assert(!err);
assert.equal(user.first, 'joe');
User.upsert({first: 'bob', id: 7}, function(err, updatedUser) {
assert(!err);
assert.equal(updatedUser.first, 'bob');
done();
});
});
});
});
describe('model.destroy([callback])', function() {
it("Remove a model from the attached data source", function(done) {
User.create({first: 'joe', last: 'bob'}, function(err, user) {
User.findById(user.id, function(err, foundUser) {
assert.equal(user.id, foundUser.id);
foundUser.destroy(function() {
User.findById(user.id, function(err, notFound) {
assert.equal(notFound, null);
done();
});
});
});
});
}); });
}); });
});
describe('Model.deleteById(id, [callback])', function () { describe('Model.deleteById(id, [callback])', function() {
it("Delete a model instance from the attached data source", function (done) { it("Delete a model instance from the attached data source",
User.create({first: 'joe', last: 'bob'}, function (err, user) { function(done) {
User.deleteById(user.id, function (err) { User.create({first: 'joe', last: 'bob'}, function(err, user) {
User.findById(user.id, function (err, notFound) { User.deleteById(user.id, function(err) {
assert.equal(notFound, null); User.findById(user.id, function(err, notFound) {
done(); assert.equal(notFound, null);
}); done();
}); });
});
}); });
});
});
describe('Model.findById(id, callback)', function() {
it("Find an instance by id", function(done) {
User.create({first: 'michael', last: 'jordan', id: 23}, function () {
User.findById(23, function (err, user) {
assert.equal(user.id, 23);
assert.equal(user.first, 'michael');
assert.equal(user.last, 'jordan');
done();
}); });
});
}); });
});
describe('Model.count([query], callback)', function() { describe('Model.findById(id, callback)', function() {
it("Query count of Model instances in data source", function(done) { it("Find an instance by id", function(done) {
(new TaskEmitter()) User.create({first: 'michael', last: 'jordan', id: 23}, function() {
.task(User, 'create', {first: 'jill', age: 100}) User.findById(23, function(err, user) {
.task(User, 'create', {first: 'bob', age: 200}) assert.equal(user.id, 23);
.task(User, 'create', {first: 'jan'}) assert.equal(user.first, 'michael');
.task(User, 'create', {first: 'sam'}) assert.equal(user.last, 'jordan');
.task(User, 'create', {first: 'suzy'})
.on('done', function () {
User.count({age: {gt: 99}}, function (err, count) {
assert.equal(count, 2);
done(); done();
}); });
}); });
});
}); });
});
}); describe('Model.count([query], callback)', function() {
it("Query count of Model instances in data source", function(done) {
(new TaskEmitter()).task(User,
'create',
{first: 'jill', age: 100}).task(User,
'create',
{first: 'bob', age: 200}).task(User,
'create',
{first: 'jan'}).task(User, 'create', {first: 'sam'}).task(User,
'create',
{first: 'suzy'}).on('done', function() {
User.count({age: {gt: 99}}, function(err, count) {
assert.equal(count, 2);
done();
});
});
});
});
});
} }