2014-05-03 01:12:24 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
Before merging notes:
|
|
|
|
|
|
|
|
- must fix the ".skip" tests below before merging
|
|
|
|
- somehow need to handle callback values that are model typed
|
|
|
|
- findById isn't getting an id... perhaps a remoting bug?
|
|
|
|
|
|
|
|
eg.
|
|
|
|
|
|
|
|
User.create({name: 'joe'}, function(err, user) {
|
|
|
|
assert(user instanceof User); // ...!
|
|
|
|
});
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-01-28 22:32:13 +00:00
|
|
|
var async = require('async');
|
2014-05-03 01:12:24 +00:00
|
|
|
var describe = require('./describe');
|
|
|
|
var loopback = require('../../');
|
2014-01-28 22:32:13 +00:00
|
|
|
var ACL = loopback.ACL;
|
|
|
|
var Change = loopback.Change;
|
2014-05-03 01:12:24 +00:00
|
|
|
var DataModel = loopback.DataModel;
|
|
|
|
|
|
|
|
module.exports = function defineModelTestsWithDataSource(options) {
|
|
|
|
var User, dataSource;
|
|
|
|
|
|
|
|
if(options.beforeEach) {
|
|
|
|
beforeEach(options.beforeEach);
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
var test = this;
|
|
|
|
|
|
|
|
// setup a model / datasource
|
|
|
|
dataSource = this.dataSource || loopback.createDataSource(options.dataSource);
|
2013-12-07 01:04:47 +00:00
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
var extend = DataModel.extend;
|
2013-06-07 19:57:51 +00:00
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
// create model hook
|
|
|
|
DataModel.extend = function() {
|
|
|
|
var extendedModel = extend.apply(DataModel, arguments);
|
2013-06-12 22:44:38 +00:00
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
if(options.onDefine) {
|
|
|
|
options.onDefine.call(test, extendedModel)
|
|
|
|
}
|
|
|
|
|
|
|
|
return extendedModel;
|
|
|
|
}
|
|
|
|
|
|
|
|
User = DataModel.extend('user', {
|
2013-06-12 22:44:38 +00:00
|
|
|
'first': String,
|
|
|
|
'last': String,
|
|
|
|
'age': Number,
|
|
|
|
'password': String,
|
|
|
|
'gender': String,
|
|
|
|
'domain': String,
|
|
|
|
'email': String
|
2014-04-30 00:17:49 +00:00
|
|
|
}, {
|
|
|
|
trackChanges: true
|
2013-06-12 22:44:38 +00:00
|
|
|
});
|
2014-05-03 01:12:24 +00:00
|
|
|
|
|
|
|
User.attachTo(dataSource);
|
|
|
|
|
|
|
|
|
2013-06-24 19:26:46 +00:00
|
|
|
});
|
2013-06-12 22:44:38 +00:00
|
|
|
|
2013-06-07 19:57:51 +00:00
|
|
|
describe('Model.validatesPresenceOf(properties...)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Require a model to include a property to be considered valid", function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.validatesPresenceOf('first', 'last', 'age');
|
2013-06-12 22:44:38 +00:00
|
|
|
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');
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.validatesLengthOf(property, options)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Require a property length to be within a specified range", function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
|
2013-06-12 22:44:38 +00:00
|
|
|
var joe = new User({password: '1234'});
|
|
|
|
assert(joe.isValid() === false, 'model should not be valid');
|
|
|
|
assert(joe.errors.password, 'should have password error');
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.validatesInclusionOf(property, options)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Require a value for `property` to be in the specified array", function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.validatesInclusionOf('gender', {in: ['male', 'female']});
|
2013-06-12 22:44:38 +00:00
|
|
|
var foo = new User({gender: 'bar'});
|
|
|
|
assert(foo.isValid() === false, 'model should not be valid');
|
|
|
|
assert(foo.errors.gender, 'should have gender error');
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.validatesExclusionOf(property, options)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Require a value for `property` to not exist in the specified array", function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
|
2013-06-12 22:44:38 +00:00
|
|
|
var foo = new User({domain: 'www'});
|
|
|
|
var bar = new User({domain: 'billing'});
|
|
|
|
var bat = new User({domain: 'admin'});
|
|
|
|
assert(foo.isValid() === false);
|
|
|
|
assert(bar.isValid() === false);
|
|
|
|
assert(bat.isValid() === false);
|
|
|
|
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');
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.validatesNumericalityOf(property, options)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Require a value for `property` to be a specific type of `Number`", function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.validatesNumericalityOf('age', {int: true});
|
2013-06-12 22:44:38 +00:00
|
|
|
var joe = new User({age: 10.2});
|
|
|
|
assert(joe.isValid() === false);
|
|
|
|
var bob = new User({age: 0});
|
|
|
|
assert(bob.isValid() === true);
|
|
|
|
assert(joe.errors.age, 'model should have an age error');
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
describe.skip('Model.validatesUniquenessOf(property, options)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Ensure the value for `property` is unique", function(done) {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.validatesUniquenessOf('email', {message: 'email is not unique'});
|
2013-06-12 22:44:38 +00:00
|
|
|
|
|
|
|
var joe = new User({email: 'joe@joe.com'});
|
|
|
|
var joe2 = new User({email: 'joe@joe.com'});
|
2014-05-03 01:12:24 +00:00
|
|
|
|
2013-06-12 22:44:38 +00:00
|
|
|
joe.save(function () {
|
|
|
|
joe2.save(function (err) {
|
|
|
|
assert(err, 'should get a validation error');
|
|
|
|
assert(joe2.errors.email, 'model should have email error');
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('myModel.isValid()', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Validate the model instance", function() {
|
2013-06-12 22:44:38 +00:00
|
|
|
User.validatesNumericalityOf('age', {int: true});
|
|
|
|
var user = new User({first: 'joe', age: 'flarg'})
|
|
|
|
var valid = user.isValid();
|
|
|
|
assert(valid === false);
|
|
|
|
assert(user.errors.age, 'model should have age error');
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
2013-06-21 23:41:33 +00:00
|
|
|
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Asynchronously validate the model', function(done) {
|
2013-06-21 23:41:33 +00:00
|
|
|
User.validatesNumericalityOf('age', {int: true});
|
|
|
|
var user = new User({first: 'joe', age: 'flarg'})
|
|
|
|
user.isValid(function (valid) {
|
|
|
|
assert(valid === false);
|
|
|
|
assert(user.errors.age, 'model should have age error');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
describe.skip('Model.attachTo(dataSource)', function() {
|
2013-06-12 22:44:38 +00:00
|
|
|
it("Attach a model to a [DataSource](#data-source)", function() {
|
2013-07-16 17:49:25 +00:00
|
|
|
var MyModel = loopback.createModel('my-model', {name: String});
|
2013-06-12 22:44:38 +00:00
|
|
|
|
2013-06-24 19:26:46 +00:00
|
|
|
assert(MyModel.find === undefined, 'should not have data access methods');
|
2013-06-07 19:57:51 +00:00
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
MyModel.attachTo(dataSource);
|
2013-06-12 22:44:38 +00:00
|
|
|
|
2013-06-24 19:26:46 +00:00
|
|
|
assert(typeof MyModel.find === 'function', 'should have data access methods after attaching to a data source');
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
describe.skip('Model.create([data], [callback])', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Create an instance of Model with given data and save to the attached data source", function(done) {
|
2013-06-07 19:57:51 +00:00
|
|
|
User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
|
2013-06-12 22:44:38 +00:00
|
|
|
assert(user instanceof User);
|
|
|
|
done();
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('model.save([options], [callback])', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Save an instance of a Model to the attached data source", function(done) {
|
2013-06-07 19:57:51 +00:00
|
|
|
var joe = new User({first: 'Joe', last: 'Bob'});
|
|
|
|
joe.save(function(err, user) {
|
2013-06-12 22:44:38 +00:00
|
|
|
assert(user.id);
|
|
|
|
assert(!err);
|
|
|
|
assert(!user.errors);
|
|
|
|
done();
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
describe.skip('model.updateAttributes(data, [callback])', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Save specified attributes to the attached data source", function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-06-12 22:44:38 +00:00
|
|
|
describe('Model.upsert(data, callback)', function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
it("Update when record with id=data.id found, insert otherwise", function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('model.destroy([callback])', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Remove a model from the attached data source", function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
User.create({first: 'joe', last: 'bob'}, function (err, user) {
|
2014-05-03 01:12:24 +00:00
|
|
|
console.log(User.findById.accepts);
|
2013-06-24 19:26:46 +00:00
|
|
|
User.findById(user.id, function (err, foundUser) {
|
2013-06-12 22:44:38 +00:00
|
|
|
assert.equal(user.id, foundUser.id);
|
|
|
|
foundUser.destroy(function () {
|
2013-06-24 19:26:46 +00:00
|
|
|
User.findById(user.id, function (err, notFound) {
|
2013-06-12 22:44:38 +00:00
|
|
|
assert(!err);
|
|
|
|
assert.equal(notFound, null);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-12 00:01:51 +00:00
|
|
|
describe('Model.deleteById([callback])', function () {
|
|
|
|
it("Delete a model instance from the attached data source", function (done) {
|
|
|
|
User.create({first: 'joe', last: 'bob'}, function (err, user) {
|
|
|
|
User.deleteById(user.id, function (err) {
|
|
|
|
User.findById(user.id, function (err, notFound) {
|
|
|
|
assert(!err);
|
|
|
|
assert.equal(notFound, null);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-07-22 17:00:07 +00:00
|
|
|
|
2014-02-12 00:01:51 +00:00
|
|
|
describe('Model.destroyAll(callback)', function() {
|
2013-06-07 19:57:51 +00:00
|
|
|
it("Delete all Model instances from data source", function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
(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();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-06-24 19:26:46 +00:00
|
|
|
describe('Model.findById(id, callback)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Find an instance by id", function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
User.create({first: 'michael', last: 'jordan', id: 23}, function () {
|
2013-06-24 19:26:46 +00:00
|
|
|
User.findById(23, function (err, user) {
|
2013-06-12 22:44:38 +00:00
|
|
|
assert.equal(user.id, 23);
|
|
|
|
assert.equal(user.first, 'michael');
|
|
|
|
assert.equal(user.last, 'jordan');
|
|
|
|
done();
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.count([query], callback)', function() {
|
|
|
|
it("Query count of Model instances in data source", function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
(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();
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
2013-06-12 22:44:38 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-02-12 00:01:51 +00:00
|
|
|
describe.onServer('Remote Methods', function(){
|
|
|
|
|
2013-06-12 22:44:38 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
User.login = function (username, password, fn) {
|
|
|
|
if(username === 'foo' && password === 'bar') {
|
|
|
|
fn(null, 123);
|
|
|
|
} else {
|
|
|
|
throw new Error('bad username and password!');
|
|
|
|
}
|
2013-06-07 19:57:51 +00:00
|
|
|
}
|
2013-06-12 22:44:38 +00:00
|
|
|
|
2013-07-16 17:49:25 +00:00
|
|
|
loopback.remoteMethod(
|
2013-06-07 19:57:51 +00:00
|
|
|
User.login,
|
|
|
|
{
|
|
|
|
accepts: [
|
|
|
|
{arg: 'username', type: 'string', required: true},
|
|
|
|
{arg: 'password', type: 'string', required: true}
|
|
|
|
],
|
2013-07-25 00:21:15 +00:00
|
|
|
returns: {arg: 'sessionId', type: 'any', root: true},
|
2013-06-12 22:44:38 +00:00
|
|
|
http: {path: '/sign-in', verb: 'get'}
|
2013-06-07 19:57:51 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2013-07-16 17:49:25 +00:00
|
|
|
app.use(loopback.rest());
|
2013-06-12 22:44:38 +00:00
|
|
|
app.model(User);
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
2013-06-12 22:44:38 +00:00
|
|
|
|
2013-06-21 23:48:53 +00:00
|
|
|
describe('Example Remote Method', function () {
|
|
|
|
it('Call the method using HTTP / REST', function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
request(app)
|
|
|
|
.get('/users/sign-in?username=foo&password=bar')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res){
|
|
|
|
if(err) return done(err);
|
2013-07-25 00:21:15 +00:00
|
|
|
assert.equal(res.body, 123);
|
2013-06-12 22:44:38 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-11-21 15:17:19 +00:00
|
|
|
|
|
|
|
it('Converts null result of findById to 404 Not Found', function(done) {
|
|
|
|
request(app)
|
|
|
|
.get('/users/not-found')
|
|
|
|
.expect(404)
|
|
|
|
.end(done);
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
2013-06-12 22:44:38 +00:00
|
|
|
|
|
|
|
describe('Model.beforeRemote(name, fn)', function(){
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Run a function before a remote method is called by a client', function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
var hookCalled = false;
|
2013-06-07 19:57:51 +00:00
|
|
|
|
2013-07-03 05:37:31 +00:00
|
|
|
User.beforeRemote('create', function(ctx, user, next) {
|
2013-06-12 22:44:38 +00:00
|
|
|
hookCalled = true;
|
|
|
|
next();
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
|
2013-06-12 22:44:38 +00:00
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if(err) return done(err);
|
|
|
|
assert(hookCalled, 'hook wasnt called');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.afterRemote(name, fn)', function(){
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Run a function after a remote method is called by a client', function(done) {
|
2013-06-12 22:44:38 +00:00
|
|
|
var beforeCalled = false;
|
|
|
|
var afterCalled = false;
|
2013-06-07 19:57:51 +00:00
|
|
|
|
2013-07-03 05:37:31 +00:00
|
|
|
User.beforeRemote('create', function(ctx, user, next) {
|
2013-06-12 22:44:38 +00:00
|
|
|
assert(!afterCalled);
|
|
|
|
beforeCalled = true;
|
|
|
|
next();
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
2013-07-03 05:37:31 +00:00
|
|
|
User.afterRemote('create', function(ctx, user, next) {
|
2013-06-12 22:44:38 +00:00
|
|
|
assert(beforeCalled);
|
|
|
|
afterCalled = true;
|
|
|
|
next();
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
2013-06-12 22:44:38 +00:00
|
|
|
|
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if(err) return done(err);
|
|
|
|
assert(beforeCalled, 'before hook was not called');
|
|
|
|
assert(afterCalled, 'after hook was not called');
|
|
|
|
done();
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
2013-06-12 22:44:38 +00:00
|
|
|
|
|
|
|
describe('Remote Method invoking context', function () {
|
|
|
|
// describe('ctx.user', function() {
|
|
|
|
// it("The remote user model calling the method remotely", function(done) {
|
|
|
|
// done(new Error('test not implemented'));
|
|
|
|
// });
|
|
|
|
// });
|
2013-06-07 19:57:51 +00:00
|
|
|
|
2013-06-12 22:44:38 +00:00
|
|
|
describe('ctx.req', function() {
|
|
|
|
it("The express ServerRequest object", function(done) {
|
|
|
|
var hookCalled = false;
|
|
|
|
|
2013-07-03 05:37:31 +00:00
|
|
|
User.beforeRemote('create', function(ctx, user, next) {
|
2013-06-12 22:44:38 +00:00
|
|
|
hookCalled = true;
|
|
|
|
assert(ctx.req);
|
|
|
|
assert(ctx.req.url);
|
|
|
|
assert(ctx.req.method);
|
|
|
|
assert(ctx.res);
|
|
|
|
assert(ctx.res.write);
|
|
|
|
assert(ctx.res.end);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if(err) return done(err);
|
|
|
|
assert(hookCalled);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
|
2013-06-12 22:44:38 +00:00
|
|
|
describe('ctx.res', function() {
|
|
|
|
it("The express ServerResponse object", function(done) {
|
|
|
|
var hookCalled = false;
|
|
|
|
|
2013-07-03 05:37:31 +00:00
|
|
|
User.beforeRemote('create', function(ctx, user, next) {
|
2013-06-12 22:44:38 +00:00
|
|
|
hookCalled = true;
|
|
|
|
assert(ctx.req);
|
|
|
|
assert(ctx.req.url);
|
|
|
|
assert(ctx.req.method);
|
|
|
|
assert(ctx.res);
|
|
|
|
assert(ctx.res.write);
|
|
|
|
assert(ctx.res.end);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
// invoke save
|
|
|
|
request(app)
|
|
|
|
.post('/users')
|
|
|
|
.send({data: {first: 'foo', last: 'bar'}})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200)
|
|
|
|
.end(function(err, res) {
|
|
|
|
if(err) return done(err);
|
|
|
|
assert(hookCalled);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
2014-01-22 10:22:23 +00:00
|
|
|
|
|
|
|
describe('in compat mode', function() {
|
|
|
|
before(function() {
|
|
|
|
loopback.compat.usePluralNamesForRemoting = true;
|
|
|
|
});
|
|
|
|
after(function() {
|
|
|
|
loopback.compat.usePluralNamesForRemoting = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('correctly install before/after hooks', function(done) {
|
|
|
|
var hooksCalled = [];
|
|
|
|
|
|
|
|
User.beforeRemote('**', function(ctx, user, next) {
|
|
|
|
hooksCalled.push('beforeRemote');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
User.afterRemote('**', function(ctx, user, next) {
|
|
|
|
hooksCalled.push('afterRemote');
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
request(app).get('/users')
|
|
|
|
.expect(200, function(err, res) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(hooksCalled, 'hooks called')
|
|
|
|
.to.eql(['beforeRemote', 'afterRemote']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
|
2013-06-12 22:44:38 +00:00
|
|
|
describe('Model.hasMany(Model)', function() {
|
2013-07-16 20:46:33 +00:00
|
|
|
it("Define a one to many relationship", function(done) {
|
2014-05-03 01:12:24 +00:00
|
|
|
var Book = dataSource.createModel('book', {title: String, author: String});
|
|
|
|
var Chapter = dataSource.createModel('chapter', {title: String});
|
2013-06-07 19:57:51 +00:00
|
|
|
|
2013-06-12 22:44:38 +00:00
|
|
|
// by referencing model
|
|
|
|
Book.hasMany(Chapter);
|
2013-06-07 19:57:51 +00:00
|
|
|
|
2013-06-12 22:44:38 +00:00
|
|
|
Book.create({title: 'Into the Wild', author: 'Jon Krakauer'}, function(err, book) {
|
|
|
|
// using 'chapters' scope for build:
|
|
|
|
var c = book.chapters.build({title: 'Chapter 1'});
|
|
|
|
book.chapters.create({title: 'Chapter 2'}, function () {
|
|
|
|
c.save(function () {
|
|
|
|
Chapter.count({bookId: book.id}, function (err, count) {
|
|
|
|
assert.equal(count, 2);
|
|
|
|
book.chapters({where: {title: 'Chapter 1'}}, function(err, chapters) {
|
|
|
|
assert.equal(chapters.length, 1);
|
|
|
|
assert.equal(chapters[0].title, 'Chapter 1');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-06-06 00:11:21 +00:00
|
|
|
});
|
|
|
|
});
|
2013-06-28 01:26:44 +00:00
|
|
|
|
|
|
|
describe('Model.properties', function(){
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Normalized properties passed in originally by loopback.createModel()', function() {
|
2013-06-28 01:26:44 +00:00
|
|
|
var props = {
|
|
|
|
s: String,
|
|
|
|
n: {type: 'Number'},
|
|
|
|
o: {type: 'String', min: 10, max: 100},
|
|
|
|
d: Date,
|
2013-07-16 17:49:25 +00:00
|
|
|
g: loopback.GeoPoint
|
2013-06-28 01:26:44 +00:00
|
|
|
};
|
|
|
|
|
2013-07-16 17:49:25 +00:00
|
|
|
var MyModel = loopback.createModel('foo', props);
|
2013-06-28 01:26:44 +00:00
|
|
|
|
2013-10-04 22:51:48 +00:00
|
|
|
Object.keys(MyModel.definition.properties).forEach(function (key) {
|
|
|
|
var p = MyModel.definition.properties[key];
|
|
|
|
var o = MyModel.definition.properties[key];
|
2013-06-28 01:26:44 +00:00
|
|
|
assert(p);
|
|
|
|
assert(o);
|
|
|
|
assert(typeof p.type === 'function');
|
|
|
|
|
|
|
|
if(typeof o === 'function') {
|
|
|
|
// the normalized property
|
|
|
|
// should match the given property
|
|
|
|
assert(
|
|
|
|
p.type.name === o.name
|
|
|
|
||
|
|
|
|
p.type.name === o
|
|
|
|
)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-07-01 23:50:03 +00:00
|
|
|
|
|
|
|
describe('Model.extend()', function(){
|
2013-07-16 20:41:17 +00:00
|
|
|
it('Create a new model by extending an existing model', function() {
|
2013-07-16 17:49:25 +00:00
|
|
|
var User = loopback.Model.extend('test-user', {
|
2013-07-01 23:50:03 +00:00
|
|
|
email: String
|
|
|
|
});
|
|
|
|
|
|
|
|
User.foo = function () {
|
|
|
|
return 'bar';
|
|
|
|
}
|
|
|
|
|
|
|
|
User.prototype.bar = function () {
|
|
|
|
return 'foo';
|
|
|
|
}
|
|
|
|
|
|
|
|
var MyUser = User.extend('my-user', {
|
|
|
|
a: String,
|
|
|
|
b: String
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(MyUser.prototype.bar, User.prototype.bar);
|
|
|
|
assert.equal(MyUser.foo, User.foo);
|
|
|
|
|
|
|
|
var user = new MyUser({
|
|
|
|
email: 'foo@bar.com',
|
|
|
|
a: 'foo',
|
|
|
|
b: 'bar'
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(user.email, 'foo@bar.com');
|
|
|
|
assert.equal(user.a, 'foo');
|
|
|
|
assert.equal(user.b, 'bar');
|
|
|
|
});
|
|
|
|
});
|
2013-06-11 16:01:44 +00:00
|
|
|
|
2013-11-12 06:16:51 +00:00
|
|
|
describe('Model.extend() events', function() {
|
|
|
|
it('create isolated emitters for subclasses', function() {
|
2014-01-28 22:32:13 +00:00
|
|
|
var User1 = loopback.createModel('User1', {
|
|
|
|
'first': String,
|
|
|
|
'last': String
|
|
|
|
});
|
2013-11-12 06:16:51 +00:00
|
|
|
|
2014-01-28 22:32:13 +00:00
|
|
|
var User2 = loopback.createModel('User2', {
|
|
|
|
'name': String
|
|
|
|
});
|
2013-11-12 06:16:51 +00:00
|
|
|
|
2014-01-28 22:32:13 +00:00
|
|
|
var user1Triggered = false;
|
|
|
|
User1.once('x', function(event) {
|
|
|
|
user1Triggered = true;
|
|
|
|
});
|
2013-11-12 06:16:51 +00:00
|
|
|
|
|
|
|
|
2014-01-28 22:32:13 +00:00
|
|
|
var user2Triggered = false;
|
|
|
|
User2.once('x', function(event) {
|
|
|
|
user2Triggered = true;
|
|
|
|
});
|
2013-11-12 06:16:51 +00:00
|
|
|
|
2014-01-28 22:32:13 +00:00
|
|
|
assert(User1.once !== User2.once);
|
|
|
|
assert(User1.once !== loopback.Model.once);
|
2013-11-12 06:16:51 +00:00
|
|
|
|
2014-01-28 22:32:13 +00:00
|
|
|
User1.emit('x', User1);
|
2013-11-12 06:16:51 +00:00
|
|
|
|
2014-01-28 22:32:13 +00:00
|
|
|
assert(user1Triggered);
|
|
|
|
assert(!user2Triggered);
|
2013-11-12 06:16:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-12-07 01:04:47 +00:00
|
|
|
describe('Model.checkAccessTypeForMethod(remoteMethod)', function () {
|
|
|
|
shouldReturn('create', ACL.WRITE);
|
|
|
|
shouldReturn('updateOrCreate', ACL.WRITE);
|
|
|
|
shouldReturn('upsert', ACL.WRITE);
|
|
|
|
shouldReturn('exists', ACL.READ);
|
|
|
|
shouldReturn('findById', ACL.READ);
|
|
|
|
shouldReturn('find', ACL.READ);
|
|
|
|
shouldReturn('findOne', ACL.READ);
|
|
|
|
shouldReturn('destroyById', ACL.WRITE);
|
|
|
|
shouldReturn('deleteById', ACL.WRITE);
|
|
|
|
shouldReturn('removeById', ACL.WRITE);
|
|
|
|
shouldReturn('count', ACL.READ);
|
|
|
|
shouldReturn('unkown-model-method', ACL.EXECUTE);
|
|
|
|
|
|
|
|
function shouldReturn(methodName, expectedAccessType) {
|
|
|
|
describe(methodName, function () {
|
|
|
|
it('should return ' + expectedAccessType, function() {
|
|
|
|
var remoteMethod = {name: methodName};
|
|
|
|
assert.equal(
|
|
|
|
User._getAccessTypeForMethod(remoteMethod),
|
|
|
|
expectedAccessType
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-28 22:32:13 +00:00
|
|
|
describe('Model.getChangeModel()', function() {
|
|
|
|
it('Get the Change Model', function () {
|
|
|
|
var UserChange = User.getChangeModel();
|
|
|
|
var change = new UserChange();
|
|
|
|
assert(change instanceof Change);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.getSourceId(callback)', function() {
|
|
|
|
it('Get the Source Id', function (done) {
|
|
|
|
User.getSourceId(function(err, id) {
|
|
|
|
assert.equal('memory-user', id);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.checkpoint(callback)', function() {
|
|
|
|
it('Create a checkpoint', function (done) {
|
|
|
|
var Checkpoint = User.getChangeModel().getCheckpointModel();
|
|
|
|
var tasks = [
|
|
|
|
getCurrentCheckpoint,
|
|
|
|
checkpoint
|
|
|
|
];
|
|
|
|
var result;
|
|
|
|
var current;
|
|
|
|
|
|
|
|
async.parallel(tasks, function(err) {
|
|
|
|
if(err) return done(err);
|
|
|
|
|
|
|
|
assert.equal(result, current + 1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
function getCurrentCheckpoint(cb) {
|
|
|
|
Checkpoint.current(function(err, cp) {
|
|
|
|
current = cp;
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkpoint(cb) {
|
|
|
|
User.checkpoint(function(err, cp) {
|
|
|
|
result = cp.id;
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Replication / Change APIs', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
var test = this;
|
2014-05-03 01:12:24 +00:00
|
|
|
this.dataSource = loopback.createDataSource(options.dataSource);
|
2014-01-28 22:32:13 +00:00
|
|
|
var SourceModel = this.SourceModel = this.dataSource.createModel('SourceModel', {}, {
|
|
|
|
trackChanges: true
|
|
|
|
});
|
|
|
|
var TargetModel = this.TargetModel = this.dataSource.createModel('TargetModel', {}, {
|
|
|
|
trackChanges: true
|
|
|
|
});
|
|
|
|
|
|
|
|
var createOne = SourceModel.create.bind(SourceModel, {
|
|
|
|
name: 'baz'
|
|
|
|
});
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
createOne,
|
|
|
|
function(cb) {
|
|
|
|
SourceModel.currentCheckpoint(function(err, id) {
|
|
|
|
if(err) return cb(err);
|
|
|
|
test.startingCheckpoint = id;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
], process.nextTick.bind(process, done));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Model.changes(since, filter, callback)', function() {
|
|
|
|
it('Get changes since the given checkpoint', function (done) {
|
|
|
|
this.SourceModel.changes(this.startingCheckpoint, {}, function(err, changes) {
|
|
|
|
assert.equal(changes.length, 1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-05-03 01:12:24 +00:00
|
|
|
describe.skip('Model.replicate(since, targetModel, options, callback)', function() {
|
2014-01-28 22:32:13 +00:00
|
|
|
it('Replicate data using the target model', function (done) {
|
|
|
|
var test = this;
|
|
|
|
var options = {};
|
|
|
|
var sourceData;
|
|
|
|
var targetData;
|
|
|
|
|
|
|
|
this.SourceModel.replicate(this.startingCheckpoint, this.TargetModel,
|
|
|
|
options, function(err, conflicts) {
|
|
|
|
assert(conflicts.length === 0);
|
|
|
|
async.parallel([
|
|
|
|
function(cb) {
|
|
|
|
test.SourceModel.find(function(err, result) {
|
|
|
|
if(err) return cb(err);
|
|
|
|
sourceData = result;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function(cb) {
|
|
|
|
test.TargetModel.find(function(err, result) {
|
|
|
|
if(err) return cb(err);
|
|
|
|
targetData = result;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
], function(err) {
|
|
|
|
if(err) return done(err);
|
|
|
|
|
|
|
|
assert.deepEqual(sourceData, targetData);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-04-15 19:53:00 +00:00
|
|
|
|
2014-02-04 00:00:01 +00:00
|
|
|
describe('Model._getACLModel()', function() {
|
|
|
|
it('should return the subclass of ACL', function() {
|
2014-05-03 01:12:24 +00:00
|
|
|
var Model = require('../../').Model;
|
2014-02-04 00:00:01 +00:00
|
|
|
var acl = ACL.extend('acl');
|
|
|
|
Model._ACL(null); // Reset the ACL class for the base model
|
|
|
|
var model = Model._ACL();
|
|
|
|
assert.equal(model, acl);
|
|
|
|
});
|
|
|
|
});
|
2014-05-03 01:12:24 +00:00
|
|
|
}
|