From f8b5fa11ec78e28669938333817e02b00057876d Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Fri, 2 May 2014 21:19:14 -0700 Subject: [PATCH] All tests passing --- lib/models/acl.js | 2 +- lib/models/application.js | 2 +- lib/models/checkpoint.js | 6 ++-- lib/models/data-model.js | 3 +- lib/models/user.js | 6 ++-- test/app.test.js | 14 +++++--- test/data-source.test.js | 58 ++++++++++++++++++---------------- test/hidden-properties.test.js | 19 +++++++---- test/model.test.js | 42 +++++++++++++++++++++++- test/util/model-tests.js | 24 ++------------ 10 files changed, 106 insertions(+), 70 deletions(-) diff --git a/lib/models/acl.js b/lib/models/acl.js index d7f18b6f..27b53fcc 100644 --- a/lib/models/acl.js +++ b/lib/models/acl.js @@ -91,7 +91,7 @@ var ACLSchema = { * @inherits Model */ -var ACL = loopback.createModel('ACL', ACLSchema); +var ACL = loopback.DataModel.extend('ACL', ACLSchema); ACL.ALL = AccessContext.ALL; diff --git a/lib/models/application.js b/lib/models/application.js index f537559e..8d70f47e 100644 --- a/lib/models/application.js +++ b/lib/models/application.js @@ -113,7 +113,7 @@ function generateKey(hmacKey, algorithm, encoding) { * @inherits {Model} */ -var Application = loopback.createModel('Application', ApplicationSchema); +var Application = loopback.DataModel.extend('Application', ApplicationSchema); /*! * A hook to generate keys before creation diff --git a/lib/models/checkpoint.js b/lib/models/checkpoint.js index 22248bcb..71d4797e 100644 --- a/lib/models/checkpoint.js +++ b/lib/models/checkpoint.js @@ -2,7 +2,7 @@ * Module Dependencies. */ -var Model = require('../loopback').Model +var DataModel = require('../loopback').DataModel , loopback = require('../loopback') , assert = require('assert'); @@ -32,10 +32,10 @@ var options = { * @property sourceId {String} the source identifier * * @class - * @inherits {Model} + * @inherits {DataModel} */ -var Checkpoint = module.exports = Model.extend('Checkpoint', properties, options); +var Checkpoint = module.exports = DataModel.extend('Checkpoint', properties, options); /** * Get the current checkpoint id diff --git a/lib/models/data-model.js b/lib/models/data-model.js index d950f7c8..8b89694f 100644 --- a/lib/models/data-model.js +++ b/lib/models/data-model.js @@ -404,8 +404,9 @@ DataModel.setupRemoting = function() { setRemoting(DataModel.destroyAll, { description: 'Delete all matching records', accepts: {arg: 'where', type: 'object', description: 'filter.where object'}, - http: {verb: 'delete', path: '/'} + http: {verb: 'del', path: '/'} }); + DataModel.destroyAll.shared = false; setRemoting(DataModel.deleteById, { description: 'Delete a model instance by id from the data source', diff --git a/lib/models/user.js b/lib/models/user.js index 374fe7c4..605b4287 100644 --- a/lib/models/user.js +++ b/lib/models/user.js @@ -2,7 +2,7 @@ * Module Dependencies. */ -var Model = require('../loopback').Model +var DataModel = require('../loopback').DataModel , loopback = require('../loopback') , path = require('path') , SALT_WORK_FACTOR = 10 @@ -126,7 +126,7 @@ var options = { * @inherits {Model} */ -var User = module.exports = Model.extend('User', properties, options); +var User = module.exports = DataModel.extend('User', properties, options); /** * Login a user by with the given `credentials`. @@ -414,7 +414,7 @@ User.resetPassword = function(options, cb) { User.setup = function () { // We need to call the base class's setup method - Model.setup.call(this); + DataModel.setup.call(this); var UserModel = this; // max ttl diff --git a/test/app.test.js b/test/app.test.js index 47f31795..3aae923e 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -1,5 +1,7 @@ var path = require('path'); var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-app'); +var loopback = require('../'); +var DataModel = loopback.DataModel; describe('app', function() { @@ -11,21 +13,24 @@ describe('app', function() { }); it("Expose a `Model` to remote clients", function() { - var Color = db.createModel('color', {name: String}); + var Color = DataModel.extend('color', {name: String}); app.model(Color); + Color.attachTo(db); expect(app.models()).to.eql([Color]); }); it('uses singlar name as app.remoteObjects() key', function() { - var Color = db.createModel('color', {name: String}); + var Color = DataModel.extend('color', {name: String}); app.model(Color); + Color.attachTo(db); expect(app.remoteObjects()).to.eql({ color: Color }); }); it('uses singular name as shared class name', function() { - var Color = db.createModel('color', {name: String}); + var Color = DataModel.extend('color', {name: String}); app.model(Color); + Color.attachTo(db); expect(app.remotes().exports).to.eql({ color: Color }); }); @@ -33,8 +38,9 @@ describe('app', function() { app.use(loopback.rest()); request(app).get('/colors').expect(404, function(err, res) { if (err) return done(err); - var Color = db.createModel('color', {name: String}); + var Color = DataModel.extend('color', {name: String}); app.model(Color); + Color.attachTo(db); request(app).get('/colors').expect(200, done); }); }); diff --git a/test/data-source.test.js b/test/data-source.test.js index eb0e1537..46db9be1 100644 --- a/test/data-source.test.js +++ b/test/data-source.test.js @@ -36,38 +36,42 @@ describe('DataSource', function() { }); }); - describe('dataSource.operations()', function() { - it("List the enabled and disabled operations", function() { + describe('DataModel Methods', function() { + it("List the enabled and disabled methods", function() { + var TestModel = loopback.DataModel.extend('TestDataModel'); + TestModel.attachTo(loopback.memory()); + // assert the defaults // - true: the method should be remote enabled // - false: the method should not be remote enabled // - - existsAndShared('_forDB', false); - existsAndShared('create', true); - existsAndShared('updateOrCreate', true); - existsAndShared('upsert', true); - existsAndShared('findOrCreate', false); - existsAndShared('exists', true); - existsAndShared('find', true); - existsAndShared('findOne', true); - existsAndShared('destroyAll', false); - existsAndShared('count', true); - existsAndShared('include', false); - existsAndShared('relationNameFor', false); - existsAndShared('hasMany', false); - existsAndShared('belongsTo', false); - existsAndShared('hasAndBelongsToMany', false); - existsAndShared('save', false); - existsAndShared('isNewRecord', false); - existsAndShared('_adapter', false); - existsAndShared('destroyById', true); - existsAndShared('destroy', false); - existsAndShared('updateAttributes', true); - existsAndShared('reload', false); + existsAndShared(TestModel, '_forDB', false); + existsAndShared(TestModel, 'create', true); + existsAndShared(TestModel, 'updateOrCreate', true); + existsAndShared(TestModel, 'upsert', true); + existsAndShared(TestModel, 'findOrCreate', false); + existsAndShared(TestModel, 'exists', true); + existsAndShared(TestModel, 'find', true); + existsAndShared(TestModel, 'findOne', true); + existsAndShared(TestModel, 'destroyAll', false); + existsAndShared(TestModel, 'count', true); + existsAndShared(TestModel, 'include', false); + existsAndShared(TestModel, 'relationNameFor', false); + existsAndShared(TestModel, 'hasMany', false); + existsAndShared(TestModel, 'belongsTo', false); + existsAndShared(TestModel, 'hasAndBelongsToMany', false); + // existsAndShared(TestModel.prototype, 'updateAttributes', true); + existsAndShared(TestModel.prototype, 'save', false); + existsAndShared(TestModel.prototype, 'isNewRecord', false); + existsAndShared(TestModel.prototype, '_adapter', false); + existsAndShared(TestModel.prototype, 'destroy', false); + existsAndShared(TestModel.prototype, 'reload', false); - function existsAndShared(name, isRemoteEnabled) { - var op = memory.getOperation(name); - assert(op.remoteEnabled === isRemoteEnabled, name + ' ' + (isRemoteEnabled ? 'should' : 'should not') + ' be remote enabled'); + function existsAndShared(scope, name, isRemoteEnabled) { + var fn = scope[name]; + assert(fn, name + ' should be defined!'); + console.log(name, fn.shared, isRemoteEnabled); + assert(!!fn.shared === isRemoteEnabled, name + ' ' + (isRemoteEnabled ? 'should' : 'should not') + ' be remote enabled'); } }); }); diff --git a/test/hidden-properties.test.js b/test/hidden-properties.test.js index 356c20f7..d1d8740b 100644 --- a/test/hidden-properties.test.js +++ b/test/hidden-properties.test.js @@ -3,15 +3,20 @@ var loopback = require('../'); describe('hidden properties', function () { beforeEach(function (done) { var app = this.app = loopback(); - var Product = this.Product = app.model('product', { - options: {hidden: ['secret']}, - dataSource: loopback.memory() - }); - var Category = this.Category = this.app.model('category', { - dataSource: loopback.memory() - }); + var Product = this.Product = loopback.DataModel.extend('product', + {}, + {hidden: ['secret']} + ); + Product.attachTo(loopback.memory()); + + var Category = this.Category = loopback.DataModel.extend('category'); + Category.attachTo(loopback.memory()); Category.hasMany(Product); + + app.model(Product); + app.model(Category); app.use(loopback.rest()); + Category.create({ name: 'my category' }, function(err, category) { diff --git a/test/model.test.js b/test/model.test.js index dbf27c88..e5f7425b 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -14,6 +14,22 @@ describe('Model', function() { describe('Model.validatesUniquenessOf(property, options)', function() { it("Ensure the value for `property` is unique", function(done) { + var User = DataModel.extend('user', { + 'first': String, + 'last': String, + 'age': Number, + 'password': String, + 'gender': String, + 'domain': String, + 'email': String + }); + + var dataSource = loopback.createDataSource({ + connector: loopback.Memory + }); + + User.attachTo(dataSource); + User.validatesUniquenessOf('email', {message: 'email is not unique'}); var joe = new User({email: 'joe@joe.com'}); @@ -33,6 +49,9 @@ describe('Model', function() { describe('Model.attachTo(dataSource)', function() { it("Attach a model to a [DataSource](#data-source)", function() { var MyModel = loopback.createModel('my-model', {name: String}); + var dataSource = loopback.createDataSource({ + connector: loopback.Memory + }); assert(MyModel.find === undefined, 'should not have data access methods'); @@ -92,6 +111,27 @@ describe.onServer('Remote Methods', function(){ app.use(loopback.rest()); app.model(User); }); + + describe('Model.destroyAll(callback)', function() { + it("Delete all Model instances from data source", function(done) { + (new TaskEmitter()) + .task(User, 'create', {first: 'jill'}) + .task(User, 'create', {first: 'bob'}) + .task(User, 'create', {first: 'jan'}) + .task(User, 'create', {first: 'sam'}) + .task(User, 'create', {first: 'suzy'}) + .on('done', function () { + User.count(function (err, count) { + User.destroyAll(function () { + User.count(function (err, count) { + assert.equal(count, 0); + done(); + }); + }); + }); + }); + }); + }); describe('Example Remote Method', function () { it('Call the method using HTTP / REST', function(done) { @@ -326,7 +366,7 @@ describe.onServer('Remote Methods', function(){ describe('Model.extend()', function(){ it('Create a new model by extending an existing model', function() { - var User = loopback.Model.extend('test-user', { + var User = loopback.DataModel.extend('test-user', { email: String }); diff --git a/test/util/model-tests.js b/test/util/model-tests.js index 0b03a282..7927f2b3 100644 --- a/test/util/model-tests.js +++ b/test/util/model-tests.js @@ -47,6 +47,8 @@ describe('Model Tests', function() { trackChanges: true }); + // enable destroy all for testing + User.destroyAll.shared = true; User.attachTo(dataSource); }); @@ -209,28 +211,6 @@ describe('Model Tests', function() { }); }); - describe('Model.destroyAll(callback)', function() { - it("Delete all Model instances from data source", function(done) { - (new TaskEmitter()) - .task(User, 'create', {first: 'jill'}) - .task(User, 'create', {first: 'bob'}) - .task(User, 'create', {first: 'jan'}) - .task(User, 'create', {first: 'sam'}) - .task(User, 'create', {first: 'suzy'}) - .on('done', function () { - User.count(function (err, count) { - assert.equal(count, 5); - User.destroyAll(function () { - User.count(function (err, count) { - assert.equal(count, 0); - done(); - }); - }); - }); - }); - }); - }); - describe('Model.findById(id, callback)', function() { it("Find an instance by id", function(done) { User.create({first: 'michael', last: 'jordan', id: 23}, function () {