2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2013-12-20 22:58:29 +00:00
|
|
|
var loopback = require('../');
|
2015-12-31 23:41:18 +00:00
|
|
|
var lt = require('./helpers/loopback-testing-helper');
|
2013-12-20 22:58:29 +00:00
|
|
|
var path = require('path');
|
|
|
|
var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-integration-app');
|
2015-04-20 16:23:44 +00:00
|
|
|
var app = require(path.join(SIMPLE_APP, 'server/server.js'));
|
2013-12-20 22:58:29 +00:00
|
|
|
var assert = require('assert');
|
2016-11-22 14:30:04 +00:00
|
|
|
var expect = require('./helpers/expect');
|
2014-06-06 12:51:13 +00:00
|
|
|
var debug = require('debug')('loopback:test:relations.integration');
|
2014-07-03 18:18:34 +00:00
|
|
|
var async = require('async');
|
2013-12-20 22:58:29 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('relations - integration', function() {
|
2013-12-20 22:58:29 +00:00
|
|
|
lt.beforeEach.withApp(app);
|
|
|
|
|
|
|
|
lt.beforeEach.givenModel('store');
|
|
|
|
beforeEach(function(done) {
|
|
|
|
this.widgetName = 'foo';
|
|
|
|
this.store.widgets.create({
|
2016-04-01 09:14:26 +00:00
|
|
|
name: this.widgetName,
|
2013-12-20 22:58:29 +00:00
|
|
|
}, function() {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
afterEach(function(done) {
|
|
|
|
this.app.models.widget.destroyAll(done);
|
|
|
|
});
|
|
|
|
|
2015-05-28 23:15:02 +00:00
|
|
|
describe('polymorphicHasMany', function() {
|
2015-05-13 16:34:11 +00:00
|
|
|
before(function defineProductAndCategoryModels() {
|
2016-11-15 21:46:23 +00:00
|
|
|
var Team = app.registry.createModel('Team', {name: 'string'});
|
|
|
|
var Reader = app.registry.createModel('Reader', {name: 'string'});
|
2016-06-03 20:51:48 +00:00
|
|
|
var Picture = app.registry.createModel('Picture',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'string', imageableId: 'number', imageableType: 'string'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(Team, {dataSource: 'db'});
|
|
|
|
app.model(Reader, {dataSource: 'db'});
|
|
|
|
app.model(Picture, {dataSource: 'db'});
|
2015-05-13 16:34:11 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
Reader.hasMany(Picture, {polymorphic: { // alternative syntax
|
2015-05-13 16:34:11 +00:00
|
|
|
as: 'imageable', // if not set, default to: reference
|
|
|
|
foreignKey: 'imageableId', // defaults to 'as + Id'
|
2016-04-01 09:14:26 +00:00
|
|
|
discriminator: 'imageableType', // defaults to 'as + Type'
|
|
|
|
}});
|
2015-05-13 16:34:11 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
Picture.belongsTo('imageable', {polymorphic: {
|
2015-05-13 16:34:11 +00:00
|
|
|
foreignKey: 'imageableId',
|
2016-04-01 09:14:26 +00:00
|
|
|
discriminator: 'imageableType',
|
|
|
|
}});
|
2015-05-13 16:34:11 +00:00
|
|
|
|
2015-05-28 23:15:02 +00:00
|
|
|
Reader.belongsTo(Team);
|
2015-05-13 16:34:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
before(function createEvent(done) {
|
|
|
|
var test = this;
|
2016-11-15 21:46:23 +00:00
|
|
|
app.models.Team.create({name: 'Team 1'},
|
2015-05-28 23:15:02 +00:00
|
|
|
function(err, team) {
|
2015-05-14 20:09:51 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-05-28 23:15:02 +00:00
|
|
|
test.team = team;
|
2016-11-15 21:46:23 +00:00
|
|
|
app.models.Reader.create({name: 'Reader 1'},
|
2015-05-13 16:34:11 +00:00
|
|
|
function(err, reader) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-05-13 16:34:11 +00:00
|
|
|
test.reader = reader;
|
2016-11-15 21:46:23 +00:00
|
|
|
reader.pictures.create({name: 'Picture 1'});
|
|
|
|
reader.pictures.create({name: 'Picture 2'});
|
2015-05-28 23:15:02 +00:00
|
|
|
reader.team(test.team);
|
2015-05-13 16:34:11 +00:00
|
|
|
reader.save(done);
|
|
|
|
});
|
|
|
|
}
|
2015-05-14 20:09:51 +00:00
|
|
|
);
|
2015-05-13 16:34:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after(function(done) {
|
|
|
|
this.app.models.Reader.destroyAll(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes the related child model', function(done) {
|
|
|
|
var url = '/api/readers/' + this.reader.id;
|
|
|
|
this.get(url)
|
2016-11-15 21:46:23 +00:00
|
|
|
.query({'filter': {'include': 'pictures'}})
|
2015-05-13 16:34:11 +00:00
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-05-13 16:34:11 +00:00
|
|
|
expect(res.body.name).to.be.equal('Reader 1');
|
|
|
|
expect(res.body.pictures).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Picture 1', id: 1, imageableId: 1, imageableType: 'Reader'},
|
|
|
|
{name: 'Picture 2', id: 2, imageableId: 1, imageableType: 'Reader'},
|
2015-05-13 16:34:11 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-05-13 16:34:11 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes the related parent model', function(done) {
|
|
|
|
var url = '/api/pictures';
|
|
|
|
this.get(url)
|
2016-11-15 21:46:23 +00:00
|
|
|
.query({'filter': {'include': 'imageable'}})
|
2015-05-13 16:34:11 +00:00
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-05-13 16:34:11 +00:00
|
|
|
expect(res.body[0].name).to.be.equal('Picture 1');
|
|
|
|
expect(res.body[1].name).to.be.equal('Picture 2');
|
2016-11-15 21:46:23 +00:00
|
|
|
expect(res.body[0].imageable).to.be.eql({name: 'Reader 1', id: 1, teamId: 1});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-05-13 16:34:11 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes related models scoped to the related parent model', function(done) {
|
|
|
|
var url = '/api/pictures';
|
|
|
|
this.get(url)
|
2016-11-15 21:46:23 +00:00
|
|
|
.query({'filter': {'include': {
|
2016-04-01 09:14:26 +00:00
|
|
|
'relation': 'imageable',
|
2016-11-15 21:46:23 +00:00
|
|
|
'scope': {'include': 'team'},
|
2016-04-01 09:14:26 +00:00
|
|
|
}}})
|
2015-05-13 16:34:11 +00:00
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-05-13 16:34:11 +00:00
|
|
|
expect(res.body[0].name).to.be.equal('Picture 1');
|
|
|
|
expect(res.body[1].name).to.be.equal('Picture 2');
|
2015-05-28 23:15:02 +00:00
|
|
|
expect(res.body[0].imageable.name).to.be.eql('Reader 1');
|
2016-11-15 21:46:23 +00:00
|
|
|
expect(res.body[0].imageable.team).to.be.eql({name: 'Team 1', id: 1});
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-05-13 16:34:11 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-08 22:55:47 +00:00
|
|
|
describe('/store/superStores', function() {
|
|
|
|
it('should invoke scoped methods remotely', function(done) {
|
|
|
|
this.get('/api/stores/superStores')
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(res.body).to.be.an('array');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-08 22:55:47 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('/store/:id/widgets', function() {
|
2013-12-20 22:58:29 +00:00
|
|
|
beforeEach(function() {
|
|
|
|
this.url = '/api/stores/' + this.store.id + '/widgets';
|
|
|
|
});
|
|
|
|
lt.describe.whenCalledRemotely('GET', '/api/stores/:id/widgets', function() {
|
|
|
|
it('should succeed with statusCode 200', function() {
|
|
|
|
assert.equal(this.res.statusCode, 200);
|
|
|
|
});
|
|
|
|
describe('widgets (response.body)', function() {
|
|
|
|
beforeEach(function() {
|
2014-06-06 12:51:13 +00:00
|
|
|
debug('GET /api/stores/:id/widgets response: %s' +
|
|
|
|
'\nheaders: %j\nbody string: %s',
|
|
|
|
this.res.statusCode,
|
|
|
|
this.res.headers,
|
|
|
|
this.res.text);
|
2013-12-20 22:58:29 +00:00
|
|
|
this.widgets = this.res.body;
|
2014-06-06 12:51:13 +00:00
|
|
|
this.widget = this.res.body && this.res.body[0];
|
2014-11-21 01:46:21 +00:00
|
|
|
});
|
2013-12-20 22:58:29 +00:00
|
|
|
it('should be an array', function() {
|
|
|
|
assert(Array.isArray(this.widgets));
|
|
|
|
});
|
|
|
|
it('should include a single widget', function() {
|
|
|
|
assert(this.widgets.length === 1);
|
|
|
|
assert(this.widget);
|
|
|
|
});
|
|
|
|
it('should be a valid widget', function() {
|
|
|
|
assert(this.widget.id);
|
|
|
|
assert.equal(this.widget.storeId, this.store.id);
|
|
|
|
assert.equal(this.widget.name, this.widgetName);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('POST /api/store/:id/widgets', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.newWidgetName = 'baz';
|
|
|
|
this.newWidget = {
|
2016-04-01 09:14:26 +00:00
|
|
|
name: this.newWidgetName,
|
2013-12-20 22:58:29 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
beforeEach(function(done) {
|
|
|
|
this.http = this.post(this.url, this.newWidget);
|
|
|
|
this.http.send(this.newWidget);
|
|
|
|
this.http.end(function(err) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-12-20 22:58:29 +00:00
|
|
|
this.req = this.http.req;
|
|
|
|
this.res = this.http.res;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-12-20 22:58:29 +00:00
|
|
|
done();
|
|
|
|
}.bind(this));
|
|
|
|
});
|
|
|
|
it('should succeed with statusCode 200', function() {
|
|
|
|
assert.equal(this.res.statusCode, 200);
|
|
|
|
});
|
|
|
|
describe('widget (response.body)', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
this.widget = this.res.body;
|
2014-11-21 01:46:21 +00:00
|
|
|
});
|
2013-12-20 22:58:29 +00:00
|
|
|
it('should be an object', function() {
|
|
|
|
assert(typeof this.widget === 'object');
|
|
|
|
assert(!Array.isArray(this.widget));
|
|
|
|
});
|
|
|
|
it('should be a valid widget', function() {
|
|
|
|
assert(this.widget.id);
|
|
|
|
assert.equal(this.widget.storeId, this.store.id);
|
|
|
|
assert.equal(this.widget.name, this.newWidgetName);
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should have a single widget with storeId', function(done) {
|
2013-12-20 22:58:29 +00:00
|
|
|
this.app.models.widget.count({
|
2016-04-01 09:14:26 +00:00
|
|
|
storeId: this.store.id,
|
2013-12-20 22:58:29 +00:00
|
|
|
}, function(err, count) {
|
2014-11-21 02:35:36 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-12-20 22:58:29 +00:00
|
|
|
assert.equal(count, 2);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2013-12-20 22:58:29 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-09-02 12:40:11 +00:00
|
|
|
|
|
|
|
describe('PUT /api/store/:id/widgets/:fk', function() {
|
|
|
|
beforeEach(function(done) {
|
|
|
|
var self = this;
|
|
|
|
this.store.widgets.create({
|
|
|
|
name: this.widgetName,
|
|
|
|
}, function(err, widget) {
|
|
|
|
self.widget = widget;
|
|
|
|
self.url = '/api/stores/' + self.store.id + '/widgets/' + widget.id;
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('does not add default properties to request body', function(done) {
|
|
|
|
var self = this;
|
|
|
|
self.request.put(self.url)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({active: true})
|
2016-09-02 12:40:11 +00:00
|
|
|
.end(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
app.models.Widget.findById(self.widget.id, function(err, w) {
|
|
|
|
if (err) return done(err);
|
|
|
|
expect(w.name).to.equal(self.widgetName);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-12-20 22:58:29 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('/stores/:id/widgets/:fk - 200', function() {
|
|
|
|
beforeEach(function(done) {
|
2014-08-30 14:54:14 +00:00
|
|
|
var self = this;
|
|
|
|
this.store.widgets.create({
|
2016-04-01 09:14:26 +00:00
|
|
|
name: this.widgetName,
|
2014-08-30 14:54:14 +00:00
|
|
|
}, function(err, widget) {
|
|
|
|
self.widget = widget;
|
|
|
|
self.url = '/api/stores/' + self.store.id + '/widgets/' + widget.id;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-30 14:54:14 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
lt.describe.whenCalledRemotely('GET', '/stores/:id/widgets/:fk', function() {
|
|
|
|
it('should succeed with statusCode 200', function() {
|
2014-08-30 14:54:14 +00:00
|
|
|
assert.equal(this.res.statusCode, 200);
|
|
|
|
assert.equal(this.res.body.id, this.widget.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('/stores/:id/widgets/:fk - 404', function() {
|
|
|
|
beforeEach(function() {
|
2014-08-30 14:54:14 +00:00
|
|
|
this.url = '/api/stores/' + this.store.id + '/widgets/123456';
|
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
lt.describe.whenCalledRemotely('GET', '/stores/:id/widgets/:fk', function() {
|
|
|
|
it('should fail with statusCode 404', function() {
|
2014-08-30 14:54:14 +00:00
|
|
|
assert.equal(this.res.statusCode, 404);
|
2016-05-24 20:46:32 +00:00
|
|
|
assert.equal(this.res.body.error.statusCode, 404);
|
2014-08-30 14:54:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('/store/:id/widgets/count', function() {
|
2014-08-26 12:41:36 +00:00
|
|
|
beforeEach(function() {
|
|
|
|
this.url = '/api/stores/' + this.store.id + '/widgets/count';
|
|
|
|
});
|
|
|
|
lt.describe.whenCalledRemotely('GET', '/api/stores/:id/widgets/count', function() {
|
|
|
|
it('should succeed with statusCode 200', function() {
|
|
|
|
assert.equal(this.res.statusCode, 200);
|
|
|
|
});
|
|
|
|
it('should return the count', function() {
|
|
|
|
assert.equal(this.res.body.count, 1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('/store/:id/widgets/count - filtered (matches)', function() {
|
2014-08-26 12:41:36 +00:00
|
|
|
beforeEach(function() {
|
|
|
|
this.url = '/api/stores/' + this.store.id + '/widgets/count?where[name]=foo';
|
|
|
|
});
|
|
|
|
lt.describe.whenCalledRemotely('GET', '/api/stores/:id/widgets/count?where[name]=foo', function() {
|
|
|
|
it('should succeed with statusCode 200', function() {
|
|
|
|
assert.equal(this.res.statusCode, 200);
|
|
|
|
});
|
|
|
|
it('should return the count', function() {
|
|
|
|
assert.equal(this.res.body.count, 1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('/store/:id/widgets/count - filtered (no matches)', function() {
|
2014-08-26 12:41:36 +00:00
|
|
|
beforeEach(function() {
|
|
|
|
this.url = '/api/stores/' + this.store.id + '/widgets/count?where[name]=bar';
|
|
|
|
});
|
|
|
|
lt.describe.whenCalledRemotely('GET', '/api/stores/:id/widgets/count?where[name]=bar', function() {
|
|
|
|
it('should succeed with statusCode 200', function() {
|
|
|
|
assert.equal(this.res.statusCode, 200);
|
|
|
|
});
|
|
|
|
it('should return the count', function() {
|
|
|
|
assert.equal(this.res.body.count, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-12-20 22:58:29 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('/widgets/:id/store', function() {
|
|
|
|
beforeEach(function(done) {
|
2014-04-04 03:43:49 +00:00
|
|
|
var self = this;
|
|
|
|
this.store.widgets.create({
|
2016-04-01 09:14:26 +00:00
|
|
|
name: this.widgetName,
|
2014-04-04 03:43:49 +00:00
|
|
|
}, function(err, widget) {
|
|
|
|
self.widget = widget;
|
|
|
|
self.url = '/api/widgets/' + self.widget.id + '/store';
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-04-04 03:43:49 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
lt.describe.whenCalledRemotely('GET', '/api/widgets/:id/store', function() {
|
|
|
|
it('should succeed with statusCode 200', function() {
|
2014-04-04 03:43:49 +00:00
|
|
|
assert.equal(this.res.statusCode, 200);
|
2014-04-04 15:26:57 +00:00
|
|
|
assert.equal(this.res.body.id, this.store.id);
|
2014-04-04 03:43:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-03 01:09:05 +00:00
|
|
|
describe('hasMany through', function() {
|
2014-07-03 18:18:34 +00:00
|
|
|
function setup(connecting, cb) {
|
|
|
|
var root = {};
|
2014-07-03 01:09:05 +00:00
|
|
|
|
2014-07-03 18:18:34 +00:00
|
|
|
async.series([
|
|
|
|
// Clean up models
|
2014-11-21 02:35:36 +00:00
|
|
|
function(done) {
|
|
|
|
app.models.physician.destroyAll(function(err) {
|
|
|
|
app.models.patient.destroyAll(function(err) {
|
|
|
|
app.models.appointment.destroyAll(function(err) {
|
2014-07-03 18:18:34 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-07-03 01:09:05 +00:00
|
|
|
});
|
|
|
|
});
|
2014-07-03 18:18:34 +00:00
|
|
|
},
|
2014-07-03 01:09:05 +00:00
|
|
|
|
2014-07-03 18:18:34 +00:00
|
|
|
// Create a physician
|
2014-11-21 02:35:36 +00:00
|
|
|
function(done) {
|
2014-07-03 18:18:34 +00:00
|
|
|
app.models.physician.create({
|
2016-04-01 09:14:26 +00:00
|
|
|
name: 'ph1',
|
2014-11-21 02:35:36 +00:00
|
|
|
}, function(err, physician) {
|
2014-07-03 18:18:34 +00:00
|
|
|
root.physician = physician;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 18:18:34 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create a patient
|
2014-11-21 02:35:36 +00:00
|
|
|
connecting ? function(done) {
|
2014-07-03 18:18:34 +00:00
|
|
|
root.physician.patients.create({
|
2016-04-01 09:14:26 +00:00
|
|
|
name: 'pa1',
|
2014-11-21 02:35:36 +00:00
|
|
|
}, function(err, patient) {
|
2014-07-03 18:18:34 +00:00
|
|
|
root.patient = patient;
|
2015-02-20 14:31:15 +00:00
|
|
|
root.relUrl = '/api/physicians/' + root.physician.id +
|
|
|
|
'/patients/rel/' + root.patient.id;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 18:18:34 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
} : function(done) {
|
2014-07-03 18:18:34 +00:00
|
|
|
app.models.patient.create({
|
2016-04-01 09:14:26 +00:00
|
|
|
name: 'pa1',
|
2014-11-21 02:35:36 +00:00
|
|
|
}, function(err, patient) {
|
2014-07-03 18:18:34 +00:00
|
|
|
root.patient = patient;
|
2015-02-20 14:31:15 +00:00
|
|
|
root.relUrl = '/api/physicians/' + root.physician.id +
|
|
|
|
'/patients/rel/' + root.patient.id;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 18:18:34 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-11-21 02:35:36 +00:00
|
|
|
}], function(err, done) {
|
2014-07-03 18:18:34 +00:00
|
|
|
cb(err, root);
|
2014-07-03 01:09:05 +00:00
|
|
|
});
|
2014-07-03 18:18:34 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('PUT /physicians/:id/patients/rel/:fk', function() {
|
|
|
|
before(function(done) {
|
2014-07-03 01:09:05 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
setup(false, function(err, root) {
|
2014-07-03 18:18:34 +00:00
|
|
|
self.url = root.relUrl;
|
|
|
|
self.patient = root.patient;
|
|
|
|
self.physician = root.physician;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-01 00:53:40 +00:00
|
|
|
done(err);
|
2014-07-03 01:09:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
lt.describe.whenCalledRemotely('PUT', '/api/physicians/:id/patients/rel/:fk', function() {
|
|
|
|
it('should succeed with statusCode 200', function() {
|
2014-07-03 01:09:05 +00:00
|
|
|
assert.equal(this.res.statusCode, 200);
|
|
|
|
assert.equal(this.res.body.patientId, this.patient.id);
|
|
|
|
assert.equal(this.res.body.physicianId, this.physician.id);
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should create a record in appointment', function(done) {
|
2014-07-03 01:09:05 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
app.models.appointment.find(function(err, apps) {
|
2014-07-03 01:09:05 +00:00
|
|
|
assert.equal(apps.length, 1);
|
|
|
|
assert.equal(apps[0].patientId, self.patient.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 01:09:05 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should connect physician to patient', function(done) {
|
2014-07-03 01:09:05 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
self.physician.patients(function(err, patients) {
|
2014-07-03 01:09:05 +00:00
|
|
|
assert.equal(patients.length, 1);
|
|
|
|
assert.equal(patients[0].id, self.patient.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 01:09:05 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-03 18:18:34 +00:00
|
|
|
});
|
2014-07-03 01:09:05 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('PUT /physicians/:id/patients/rel/:fk with data', function() {
|
|
|
|
before(function(done) {
|
2014-09-01 01:28:13 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
setup(false, function(err, root) {
|
2014-09-01 01:28:13 +00:00
|
|
|
self.url = root.relUrl;
|
|
|
|
self.patient = root.patient;
|
|
|
|
self.physician = root.physician;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-01 00:53:40 +00:00
|
|
|
done(err);
|
2014-09-01 01:28:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
var NOW = Date.now();
|
2016-11-15 21:46:23 +00:00
|
|
|
var data = {date: new Date(NOW)};
|
2014-09-01 01:28:13 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
lt.describe.whenCalledRemotely('PUT', '/api/physicians/:id/patients/rel/:fk', data, function() {
|
|
|
|
it('should succeed with statusCode 200', function() {
|
2014-09-01 01:28:13 +00:00
|
|
|
assert.equal(this.res.statusCode, 200);
|
|
|
|
assert.equal(this.res.body.patientId, this.patient.id);
|
|
|
|
assert.equal(this.res.body.physicianId, this.physician.id);
|
|
|
|
assert.equal(new Date(this.res.body.date).getTime(), NOW);
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should create a record in appointment', function(done) {
|
2014-09-01 01:28:13 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
app.models.appointment.find(function(err, apps) {
|
2014-09-01 01:28:13 +00:00
|
|
|
assert.equal(apps.length, 1);
|
|
|
|
assert.equal(apps[0].patientId, self.patient.id);
|
|
|
|
assert.equal(apps[0].physicianId, self.physician.id);
|
|
|
|
assert.equal(apps[0].date.getTime(), NOW);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-09-01 01:28:13 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should connect physician to patient', function(done) {
|
2014-09-01 01:28:13 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
self.physician.patients(function(err, patients) {
|
2014-07-03 01:09:05 +00:00
|
|
|
assert.equal(patients.length, 1);
|
|
|
|
assert.equal(patients[0].id, self.patient.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 01:09:05 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-07-03 18:18:34 +00:00
|
|
|
});
|
2014-07-03 01:09:05 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('HEAD /physicians/:id/patients/rel/:fk', function() {
|
|
|
|
before(function(done) {
|
2014-07-28 16:37:50 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
setup(true, function(err, root) {
|
2014-07-28 16:37:50 +00:00
|
|
|
self.url = root.relUrl;
|
|
|
|
self.patient = root.patient;
|
|
|
|
self.physician = root.physician;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-01 00:53:40 +00:00
|
|
|
done(err);
|
2014-07-28 16:37:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
lt.describe.whenCalledRemotely('HEAD', '/api/physicians/:id/patients/rel/:fk', function() {
|
|
|
|
it('should succeed with statusCode 200', function() {
|
2014-07-28 16:37:50 +00:00
|
|
|
assert.equal(this.res.statusCode, 200);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('HEAD /physicians/:id/patients/rel/:fk that does not exist', function() {
|
|
|
|
before(function(done) {
|
2014-07-28 16:37:50 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
setup(true, function(err, root) {
|
2015-02-20 14:31:15 +00:00
|
|
|
self.url = '/api/physicians/' + root.physician.id +
|
|
|
|
'/patients/rel/' + '999';
|
2014-07-28 16:37:50 +00:00
|
|
|
self.patient = root.patient;
|
|
|
|
self.physician = root.physician;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-01 00:53:40 +00:00
|
|
|
done(err);
|
2014-07-28 16:37:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
lt.describe.whenCalledRemotely('HEAD', '/api/physicians/:id/patients/rel/:fk', function() {
|
|
|
|
it('should succeed with statusCode 404', function() {
|
2014-07-28 16:37:50 +00:00
|
|
|
assert.equal(this.res.statusCode, 404);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('DELETE /physicians/:id/patients/rel/:fk', function() {
|
|
|
|
before(function(done) {
|
2014-07-03 18:18:34 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
setup(true, function(err, root) {
|
2014-07-03 18:18:34 +00:00
|
|
|
self.url = root.relUrl;
|
|
|
|
self.patient = root.patient;
|
|
|
|
self.physician = root.physician;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-01 00:53:40 +00:00
|
|
|
done(err);
|
2014-07-03 18:18:34 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should create a record in appointment', function(done) {
|
2014-07-03 18:18:34 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
app.models.appointment.find(function(err, apps) {
|
2014-07-03 18:18:34 +00:00
|
|
|
assert.equal(apps.length, 1);
|
|
|
|
assert.equal(apps[0].patientId, self.patient.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 18:18:34 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should connect physician to patient', function(done) {
|
2014-07-03 18:18:34 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
self.physician.patients(function(err, patients) {
|
2014-07-03 18:18:34 +00:00
|
|
|
assert.equal(patients.length, 1);
|
|
|
|
assert.equal(patients[0].id, self.patient.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 18:18:34 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
lt.describe.whenCalledRemotely('DELETE', '/api/physicians/:id/patients/rel/:fk', function() {
|
|
|
|
it('should succeed with statusCode 204', function() {
|
2014-11-17 17:44:20 +00:00
|
|
|
assert.equal(this.res.statusCode, 204);
|
2014-07-03 01:09:05 +00:00
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should remove the record in appointment', function(done) {
|
2014-07-03 01:09:05 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
app.models.appointment.find(function(err, apps) {
|
2014-07-03 18:18:34 +00:00
|
|
|
assert.equal(apps.length, 0);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 01:09:05 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should remove the connection between physician and patient', function(done) {
|
2014-07-03 01:09:05 +00:00
|
|
|
var self = this;
|
2014-07-03 18:18:34 +00:00
|
|
|
// Need to refresh the cache
|
2014-11-21 02:35:36 +00:00
|
|
|
self.physician.patients(true, function(err, patients) {
|
2014-07-03 18:18:34 +00:00
|
|
|
assert.equal(patients.length, 0);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 01:09:05 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-07-03 18:18:34 +00:00
|
|
|
});
|
|
|
|
});
|
2014-07-03 01:09:05 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('GET /physicians/:id/patients/:fk', function() {
|
|
|
|
before(function(done) {
|
2014-07-03 18:18:34 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
setup(true, function(err, root) {
|
2015-02-20 14:31:15 +00:00
|
|
|
self.url = '/api/physicians/' + root.physician.id +
|
|
|
|
'/patients/' + root.patient.id;
|
2014-07-03 18:18:34 +00:00
|
|
|
self.patient = root.patient;
|
|
|
|
self.physician = root.physician;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-01 00:53:40 +00:00
|
|
|
done(err);
|
2014-07-03 18:18:34 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
lt.describe.whenCalledRemotely('GET', '/api/physicians/:id/patients/:fk', function() {
|
|
|
|
it('should succeed with statusCode 200', function() {
|
2014-07-03 18:18:34 +00:00
|
|
|
assert.equal(this.res.statusCode, 200);
|
|
|
|
assert.equal(this.res.body.id, this.physician.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
describe('DELETE /physicians/:id/patients/:fk', function() {
|
|
|
|
before(function(done) {
|
2014-07-03 18:18:34 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
setup(true, function(err, root) {
|
2015-02-20 14:31:15 +00:00
|
|
|
self.url = '/api/physicians/' + root.physician.id +
|
|
|
|
'/patients/' + root.patient.id;
|
2014-07-03 18:18:34 +00:00
|
|
|
self.patient = root.patient;
|
|
|
|
self.physician = root.physician;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-01-01 00:53:40 +00:00
|
|
|
done(err);
|
2014-07-03 18:18:34 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
lt.describe.whenCalledRemotely('DELETE', '/api/physicians/:id/patients/:fk', function() {
|
|
|
|
it('should succeed with statusCode 204', function() {
|
2014-11-17 17:44:20 +00:00
|
|
|
assert.equal(this.res.statusCode, 204);
|
2014-07-03 18:18:34 +00:00
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should remove the record in appointment', function(done) {
|
2014-07-03 01:09:05 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
app.models.appointment.find(function(err, apps) {
|
2014-07-03 18:18:34 +00:00
|
|
|
assert.equal(apps.length, 0);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 01:09:05 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should remove the connection between physician and patient', function(done) {
|
2014-07-03 01:09:05 +00:00
|
|
|
var self = this;
|
2014-07-03 18:18:34 +00:00
|
|
|
// Need to refresh the cache
|
2014-11-21 02:35:36 +00:00
|
|
|
self.physician.patients(true, function(err, patients) {
|
2014-07-03 18:18:34 +00:00
|
|
|
assert.equal(patients.length, 0);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 01:09:05 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
it('should remove the record in patient', function(done) {
|
2014-07-03 18:18:34 +00:00
|
|
|
var self = this;
|
2014-11-21 02:35:36 +00:00
|
|
|
app.models.patient.find(function(err, patients) {
|
2014-07-03 18:18:34 +00:00
|
|
|
assert.equal(patients.length, 0);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-03 18:18:34 +00:00
|
|
|
done();
|
2014-07-03 01:09:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-03-25 15:47:07 +00:00
|
|
|
describe('hasAndBelongsToMany', function() {
|
|
|
|
beforeEach(function defineProductAndCategoryModels() {
|
2016-08-31 13:25:08 +00:00
|
|
|
// Disable "Warning: overriding remoting type product"
|
|
|
|
this.app.remotes()._typeRegistry._options.warnWhenOverridingType = false;
|
|
|
|
|
2016-06-03 20:51:48 +00:00
|
|
|
var product = app.registry.createModel(
|
|
|
|
'product',
|
2016-11-15 21:46:23 +00:00
|
|
|
{id: 'string', name: 'string'}
|
2014-03-25 15:47:07 +00:00
|
|
|
);
|
2016-06-03 20:51:48 +00:00
|
|
|
var category = app.registry.createModel(
|
2014-03-25 15:47:07 +00:00
|
|
|
'category',
|
2016-11-15 21:46:23 +00:00
|
|
|
{id: 'string', name: 'string'}
|
2014-03-25 15:47:07 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(product, {dataSource: 'db'});
|
|
|
|
app.model(category, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
2014-03-25 15:47:07 +00:00
|
|
|
product.hasAndBelongsToMany(category);
|
|
|
|
category.hasAndBelongsToMany(product);
|
|
|
|
});
|
|
|
|
|
|
|
|
lt.beforeEach.givenModel('category');
|
|
|
|
|
|
|
|
beforeEach(function createProductsInCategory(done) {
|
|
|
|
var test = this;
|
|
|
|
this.category.products.create({
|
2016-04-01 09:14:26 +00:00
|
|
|
name: 'a-product',
|
2014-03-25 15:47:07 +00:00
|
|
|
}, function(err, product) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-03-25 15:47:07 +00:00
|
|
|
test.product = product;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-03-25 15:47:07 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function createAnotherCategoryAndProduct(done) {
|
2016-11-15 21:46:23 +00:00
|
|
|
app.models.category.create({name: 'another-category'},
|
2014-03-25 15:47:07 +00:00
|
|
|
function(err, cat) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
cat.products.create({name: 'another-product'}, done);
|
2014-03-25 15:47:07 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function(done) {
|
|
|
|
this.app.models.product.destroyAll(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it.skip('allows to find related objects via where filter', function(done) {
|
2017-01-06 11:12:35 +00:00
|
|
|
// TODO https://github.com/strongloop/loopback-datasource-juggler/issues/94
|
2014-03-25 15:47:07 +00:00
|
|
|
var expectedProduct = this.product;
|
|
|
|
this.get('/api/products?filter[where][categoryId]=' + this.category.id)
|
|
|
|
.expect(200, function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-03-25 15:47:07 +00:00
|
|
|
expect(res.body).to.eql([
|
|
|
|
{
|
|
|
|
id: expectedProduct.id,
|
2016-04-01 09:14:26 +00:00
|
|
|
name: expectedProduct.name,
|
|
|
|
},
|
2014-03-25 15:47:07 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-03-25 15:47:07 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows to find related object via URL scope', function(done) {
|
|
|
|
var expectedProduct = this.product;
|
|
|
|
this.get('/api/categories/' + this.category.id + '/products')
|
|
|
|
.expect(200, function(err, res) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-03-25 15:47:07 +00:00
|
|
|
expect(res.body).to.eql([
|
|
|
|
{
|
|
|
|
id: expectedProduct.id,
|
2016-04-01 09:14:26 +00:00
|
|
|
name: expectedProduct.name,
|
|
|
|
},
|
2014-03-25 15:47:07 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-03-25 15:47:07 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('includes requested related models in `find`', function(done) {
|
|
|
|
var expectedProduct = this.product;
|
|
|
|
var url = '/api/categories/findOne?filter[where][id]=' +
|
|
|
|
this.category.id + '&filter[include]=products';
|
|
|
|
|
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-03-25 15:47:07 +00:00
|
|
|
expect(res.body).to.have.property('products');
|
|
|
|
expect(res.body.products).to.eql([
|
|
|
|
{
|
|
|
|
id: expectedProduct.id,
|
2016-04-01 09:14:26 +00:00
|
|
|
name: expectedProduct.name,
|
|
|
|
},
|
2014-03-25 15:47:07 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-03-25 15:47:07 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it.skip('includes requested related models in `findById`', function(done) {
|
2017-01-06 11:12:35 +00:00
|
|
|
// TODO https://github.com/strongloop/loopback-datasource-juggler/issues/93
|
2014-03-25 15:47:07 +00:00
|
|
|
var expectedProduct = this.product;
|
|
|
|
// Note: the URL format is not final
|
|
|
|
var url = '/api/categories/' + this.category.id + '?include=products';
|
|
|
|
|
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-03-25 15:47:07 +00:00
|
|
|
expect(res.body).to.have.property('products');
|
|
|
|
expect(res.body.products).to.eql([
|
|
|
|
{
|
|
|
|
id: expectedProduct.id,
|
2016-04-01 09:14:26 +00:00
|
|
|
name: expectedProduct.name,
|
|
|
|
},
|
2014-03-25 15:47:07 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-03-25 15:47:07 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-08-22 13:39:53 +00:00
|
|
|
|
|
|
|
describe('embedsOne', function() {
|
2014-08-22 15:05:11 +00:00
|
|
|
before(function defineGroupAndPosterModels() {
|
2016-06-03 20:51:48 +00:00
|
|
|
var group = app.registry.createModel('group',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'string'},
|
|
|
|
{plural: 'groups'}
|
2014-08-22 13:39:53 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(group, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
|
|
|
var poster = app.registry.createModel(
|
2014-08-22 15:05:11 +00:00
|
|
|
'poster',
|
2016-11-15 21:46:23 +00:00
|
|
|
{url: 'string'}
|
2014-08-22 13:39:53 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(poster, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
group.embedsOne(poster, {as: 'cover'});
|
2014-08-22 13:39:53 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-22 13:39:53 +00:00
|
|
|
before(function createImage(done) {
|
|
|
|
var test = this;
|
2016-11-15 21:46:23 +00:00
|
|
|
app.models.group.create({name: 'Group 1'},
|
2014-08-22 15:05:11 +00:00
|
|
|
function(err, group) {
|
2014-08-22 13:39:53 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-22 13:39:53 +00:00
|
|
|
test.group = group;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-21 15:23:25 +00:00
|
|
|
done();
|
2014-08-22 13:39:53 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-22 13:39:53 +00:00
|
|
|
after(function(done) {
|
|
|
|
this.app.models.group.destroyAll(done);
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2015-03-21 15:23:25 +00:00
|
|
|
it('creates an embedded model', function(done) {
|
|
|
|
var url = '/api/groups/' + this.group.id + '/cover';
|
|
|
|
|
|
|
|
this.post(url)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({url: 'http://image.url'})
|
2015-03-21 15:23:25 +00:00
|
|
|
.expect(200, function(err, res) {
|
|
|
|
expect(res.body).to.be.eql(
|
2016-11-15 21:46:23 +00:00
|
|
|
{url: 'http://image.url'}
|
2015-03-21 15:23:25 +00:00
|
|
|
);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-21 15:23:25 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-22 13:39:53 +00:00
|
|
|
it('includes the embedded models', function(done) {
|
|
|
|
var url = '/api/groups/' + this.group.id;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-22 13:39:53 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-22 13:39:53 +00:00
|
|
|
expect(res.body.name).to.be.equal('Group 1');
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body.poster).to.be.eql(
|
2016-11-15 21:46:23 +00:00
|
|
|
{url: 'http://image.url'}
|
2014-08-22 15:05:11 +00:00
|
|
|
);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-22 13:39:53 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-22 13:39:53 +00:00
|
|
|
it('returns the embedded model', function(done) {
|
|
|
|
var url = '/api/groups/' + this.group.id + '/cover';
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-22 13:39:53 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql(
|
2016-11-15 21:46:23 +00:00
|
|
|
{url: 'http://image.url'}
|
2014-08-22 15:05:11 +00:00
|
|
|
);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-22 13:39:53 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2015-03-21 15:23:25 +00:00
|
|
|
it('updates an embedded model', function(done) {
|
|
|
|
var url = '/api/groups/' + this.group.id + '/cover';
|
|
|
|
|
|
|
|
this.put(url)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({url: 'http://changed.url'})
|
2015-03-21 15:23:25 +00:00
|
|
|
.expect(200, function(err, res) {
|
|
|
|
expect(res.body.url).to.be.equal('http://changed.url');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-21 15:23:25 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the updated embedded model', function(done) {
|
|
|
|
var url = '/api/groups/' + this.group.id + '/cover';
|
|
|
|
|
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-21 15:23:25 +00:00
|
|
|
expect(res.body).to.be.eql(
|
2016-11-15 21:46:23 +00:00
|
|
|
{url: 'http://changed.url'}
|
2015-03-21 15:23:25 +00:00
|
|
|
);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-03-21 15:23:25 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deletes an embedded model', function(done) {
|
|
|
|
var url = '/api/groups/' + this.group.id + '/cover';
|
|
|
|
this.del(url).expect(204, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('deleted the embedded model', function(done) {
|
|
|
|
var url = '/api/groups/' + this.group.id + '/cover';
|
|
|
|
this.get(url).expect(404, done);
|
|
|
|
});
|
2014-08-22 13:39:53 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
describe('embedsMany', function() {
|
|
|
|
before(function defineProductAndCategoryModels() {
|
2016-06-03 20:51:48 +00:00
|
|
|
var todoList = app.registry.createModel(
|
2014-07-30 11:24:40 +00:00
|
|
|
'todoList',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'string'},
|
|
|
|
{plural: 'todo-lists'}
|
2014-07-30 11:24:40 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(todoList, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
|
|
|
var todoItem = app.registry.createModel(
|
|
|
|
'todoItem',
|
2016-11-15 21:46:23 +00:00
|
|
|
{content: 'string'}, {forceId: false}
|
2016-06-03 20:51:48 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(todoItem, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
todoList.embedsMany(todoItem, {as: 'items'});
|
2014-07-30 11:24:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
before(function createTodoList(done) {
|
|
|
|
var test = this;
|
2016-11-15 21:46:23 +00:00
|
|
|
app.models.todoList.create({name: 'List A'},
|
2014-07-30 11:24:40 +00:00
|
|
|
function(err, list) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
test.todoList = list;
|
2016-11-15 21:46:23 +00:00
|
|
|
list.items.build({content: 'Todo 1'});
|
|
|
|
list.items.build({content: 'Todo 2'});
|
2014-07-30 11:24:40 +00:00
|
|
|
list.save(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function(done) {
|
|
|
|
this.app.models.todoList.destroyAll(done);
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
it('includes the embedded models', function(done) {
|
|
|
|
var url = '/api/todo-lists/' + this.todoList.id;
|
|
|
|
|
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
expect(res.body.name).to.be.equal('List A');
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body.todoItems).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{content: 'Todo 1', id: 1},
|
|
|
|
{content: 'Todo 2', id: 2},
|
2014-07-30 11:24:40 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
it('returns the embedded models', function(done) {
|
|
|
|
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{content: 'Todo 1', id: 1},
|
|
|
|
{content: 'Todo 2', id: 2},
|
2014-07-30 11:24:40 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
it('filters the embedded models', function(done) {
|
|
|
|
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
|
|
|
url += '?filter[where][id]=2';
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{content: 'Todo 2', id: 2},
|
2014-07-30 11:24:40 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
it('creates embedded models', function(done) {
|
|
|
|
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
var expected = {content: 'Todo 3', id: 3};
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
this.post(url)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({content: 'Todo 3'})
|
2014-07-30 11:24:40 +00:00
|
|
|
.expect(200, function(err, res) {
|
|
|
|
expect(res.body).to.be.eql(expected);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
it('returns the embedded models', function(done) {
|
|
|
|
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{content: 'Todo 1', id: 1},
|
|
|
|
{content: 'Todo 2', id: 2},
|
|
|
|
{content: 'Todo 3', id: 3},
|
2014-07-30 11:24:40 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
it('returns an embedded model by (internal) id', function(done) {
|
|
|
|
var url = '/api/todo-lists/' + this.todoList.id + '/items/3';
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
expect(res.body).to.be.eql(
|
2016-11-15 21:46:23 +00:00
|
|
|
{content: 'Todo 3', id: 3}
|
2014-07-30 11:24:40 +00:00
|
|
|
);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
it('removes an embedded model', function(done) {
|
|
|
|
var expectedProduct = this.product;
|
|
|
|
var url = '/api/todo-lists/' + this.todoList.id + '/items/2';
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
this.del(url)
|
|
|
|
.expect(200, function(err, res) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
it('returns the embedded models - verify', function(done) {
|
|
|
|
var url = '/api/todo-lists/' + this.todoList.id + '/items';
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{content: 'Todo 1', id: 1},
|
|
|
|
{content: 'Todo 3', id: 3},
|
2014-07-30 11:24:40 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-30 14:54:14 +00:00
|
|
|
it('returns a 404 response when embedded model is not found', function(done) {
|
|
|
|
var url = '/api/todo-lists/' + this.todoList.id + '/items/2';
|
|
|
|
this.get(url).expect(404, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-30 14:54:14 +00:00
|
|
|
expect(res.body.error.status).to.be.equal(404);
|
|
|
|
expect(res.body.error.message).to.be.equal('Unknown "todoItem" id "2".');
|
2014-12-18 20:26:27 +00:00
|
|
|
expect(res.body.error.code).to.be.equal('MODEL_NOT_FOUND');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-30 14:54:14 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
it.skip('checks if an embedded model exists - ok', function(done) {
|
|
|
|
var url = '/api/todo-lists/' + this.todoList.id + '/items/3';
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
this.head(url)
|
|
|
|
.expect(200, function(err, res) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
it.skip('checks if an embedded model exists - fail', function(done) {
|
|
|
|
var url = '/api/todo-lists/' + this.todoList.id + '/items/2';
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 11:24:40 +00:00
|
|
|
this.head(url)
|
|
|
|
.expect(404, function(err, res) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
describe('referencesMany', function() {
|
|
|
|
before(function defineProductAndCategoryModels() {
|
2016-06-03 20:51:48 +00:00
|
|
|
var recipe = app.registry.createModel(
|
2014-07-30 13:01:22 +00:00
|
|
|
'recipe',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'string'}
|
2014-07-30 13:01:22 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(recipe, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
|
|
|
var ingredient = app.registry.createModel(
|
2014-07-30 13:01:22 +00:00
|
|
|
'ingredient',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'string'}
|
2014-07-30 13:01:22 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(ingredient, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
|
|
|
var photo = app.registry.createModel(
|
2014-08-01 09:25:28 +00:00
|
|
|
'photo',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'string'}
|
2014-08-01 09:25:28 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(photo, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
recipe.referencesMany(ingredient);
|
2014-08-01 09:25:28 +00:00
|
|
|
// contrived example for test:
|
2016-11-15 21:46:23 +00:00
|
|
|
recipe.hasOne(photo, {as: 'picture', options: {
|
|
|
|
http: {path: 'image'},
|
2016-04-01 09:14:26 +00:00
|
|
|
}});
|
2014-07-30 13:01:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
before(function createRecipe(done) {
|
|
|
|
var test = this;
|
2016-11-15 21:46:23 +00:00
|
|
|
app.models.recipe.create({name: 'Recipe'},
|
2014-07-30 13:01:22 +00:00
|
|
|
function(err, recipe) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
test.recipe = recipe;
|
2014-11-21 01:46:21 +00:00
|
|
|
recipe.ingredients.create({
|
2016-11-15 21:46:23 +00:00
|
|
|
name: 'Chocolate'},
|
2014-07-30 13:01:22 +00:00
|
|
|
function(err, ing) {
|
|
|
|
test.ingredient1 = ing.id;
|
2016-11-15 21:46:23 +00:00
|
|
|
recipe.picture.create({name: 'Photo 1'}, done);
|
2014-07-30 13:01:22 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
before(function createIngredient(done) {
|
|
|
|
var test = this;
|
2016-11-15 21:46:23 +00:00
|
|
|
app.models.ingredient.create({name: 'Sugar'}, function(err, ing) {
|
2014-07-30 13:01:22 +00:00
|
|
|
test.ingredient2 = ing.id;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function(done) {
|
|
|
|
var app = this.app;
|
|
|
|
app.models.recipe.destroyAll(function() {
|
2014-08-01 09:25:28 +00:00
|
|
|
app.models.ingredient.destroyAll(function() {
|
|
|
|
app.models.photo.destroyAll(done);
|
|
|
|
});
|
2014-07-30 13:01:22 +00:00
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('keeps an array of ids', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id;
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
expect(res.body.ingredientIds).to.eql([test.ingredient1]);
|
|
|
|
expect(res.body).to.not.have.property('ingredients');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('creates referenced models', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.post(url)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({name: 'Butter'})
|
2014-07-30 13:01:22 +00:00
|
|
|
.expect(200, function(err, res) {
|
|
|
|
expect(res.body.name).to.be.eql('Butter');
|
|
|
|
test.ingredient3 = res.body.id;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('has created models', function(done) {
|
|
|
|
var url = '/api/ingredients';
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Chocolate', id: test.ingredient1},
|
|
|
|
{name: 'Sugar', id: test.ingredient2},
|
|
|
|
{name: 'Butter', id: test.ingredient3},
|
2014-07-30 13:01:22 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('returns the referenced models', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Chocolate', id: test.ingredient1},
|
|
|
|
{name: 'Butter', id: test.ingredient3},
|
2014-07-30 13:01:22 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('filters the referenced models', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
|
|
|
url += '?filter[where][name]=Butter';
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Butter', id: test.ingredient3},
|
2014-07-30 13:01:22 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('includes the referenced models', function(done) {
|
|
|
|
var url = '/api/recipes/findOne?filter[where][id]=' + this.recipe.id;
|
|
|
|
url += '&filter[include]=ingredients';
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
expect(res.body.ingredientIds).to.eql([
|
2016-04-01 09:14:26 +00:00
|
|
|
test.ingredient1, test.ingredient3,
|
2014-07-30 13:01:22 +00:00
|
|
|
]);
|
|
|
|
expect(res.body.ingredients).to.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Chocolate', id: test.ingredient1},
|
|
|
|
{name: 'Butter', id: test.ingredient3},
|
2014-07-30 13:01:22 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns a referenced model by id', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients/';
|
|
|
|
url += this.ingredient3;
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
expect(res.body).to.be.eql(
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Butter', id: test.ingredient3}
|
2014-07-30 13:01:22 +00:00
|
|
|
);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('keeps an array of ids - verify', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id;
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
var expected = [test.ingredient1, test.ingredient3];
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
expect(res.body.ingredientIds).to.eql(expected);
|
|
|
|
expect(res.body).to.not.have.property('ingredients');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('destroys a referenced model', function(done) {
|
|
|
|
var expectedProduct = this.product;
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients/';
|
|
|
|
url += this.ingredient3;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.del(url)
|
|
|
|
.expect(200, function(err, res) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('has destroyed a referenced model', function(done) {
|
|
|
|
var url = '/api/ingredients';
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Chocolate', id: test.ingredient1},
|
|
|
|
{name: 'Sugar', id: test.ingredient2},
|
2014-07-30 13:01:22 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('returns the referenced models - verify', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Chocolate', id: test.ingredient1},
|
2014-07-30 13:01:22 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('creates/links a reference by id', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
|
|
|
url += '/rel/' + this.ingredient2;
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.put(url)
|
|
|
|
.expect(200, function(err, res) {
|
|
|
|
expect(res.body).to.be.eql(
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Sugar', id: test.ingredient2}
|
2014-07-30 13:01:22 +00:00
|
|
|
);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('returns the referenced models - verify', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Chocolate', id: test.ingredient1},
|
|
|
|
{name: 'Sugar', id: test.ingredient2},
|
2014-07-30 13:01:22 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('removes/unlinks a reference by id', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
|
|
|
url += '/rel/' + this.ingredient1;
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.del(url)
|
|
|
|
.expect(200, function(err, res) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('returns the referenced models - verify', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients';
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Sugar', id: test.ingredient2},
|
2014-07-30 13:01:22 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
it('has not destroyed an unlinked model', function(done) {
|
|
|
|
var url = '/api/ingredients';
|
|
|
|
var test = this;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 01:46:21 +00:00
|
|
|
expect(res.body).to.be.eql([
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'Chocolate', id: test.ingredient1},
|
|
|
|
{name: 'Sugar', id: test.ingredient2},
|
2014-07-30 13:01:22 +00:00
|
|
|
]);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-07-30 13:01:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-01 09:25:28 +00:00
|
|
|
it('uses a custom relation path', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/image';
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-01 09:25:28 +00:00
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(err).to.not.exist();
|
2014-08-01 09:25:28 +00:00
|
|
|
expect(res.body.name).to.equal('Photo 1');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-01 09:25:28 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-30 14:54:14 +00:00
|
|
|
it.skip('checks if a referenced model exists - ok', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients/';
|
|
|
|
url += this.ingredient1;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-30 14:54:14 +00:00
|
|
|
this.head(url)
|
|
|
|
.expect(200, function(err, res) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-30 14:54:14 +00:00
|
|
|
it.skip('checks if an referenced model exists - fail', function(done) {
|
|
|
|
var url = '/api/recipes/' + this.recipe.id + '/ingredients/';
|
|
|
|
url += this.ingredient3;
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-30 14:54:14 +00:00
|
|
|
this.head(url)
|
|
|
|
.expect(404, function(err, res) {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-07-30 13:01:22 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
describe('nested relations', function() {
|
2014-08-22 15:05:11 +00:00
|
|
|
before(function defineModels() {
|
2016-06-03 20:51:48 +00:00
|
|
|
var Book = app.registry.createModel(
|
2014-08-04 16:27:50 +00:00
|
|
|
'Book',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'string'},
|
|
|
|
{plural: 'books'}
|
2014-08-04 16:27:50 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(Book, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
|
|
|
var Page = app.registry.createModel(
|
2014-08-04 16:27:50 +00:00
|
|
|
'Page',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'string'},
|
|
|
|
{plural: 'pages'}
|
2014-08-04 16:27:50 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(Page, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
|
|
|
var Image = app.registry.createModel(
|
2014-08-04 16:27:50 +00:00
|
|
|
'Image',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'string'},
|
|
|
|
{plural: 'images'}
|
2014-08-04 16:27:50 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(Image, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
|
|
|
var Note = app.registry.createModel(
|
2014-08-04 16:27:50 +00:00
|
|
|
'Note',
|
2016-11-15 21:46:23 +00:00
|
|
|
{text: 'string'},
|
|
|
|
{plural: 'notes'}
|
2014-08-04 16:27:50 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(Note, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
|
|
|
var Chapter = app.registry.createModel(
|
2015-01-01 07:26:58 +00:00
|
|
|
'Chapter',
|
2016-11-15 21:46:23 +00:00
|
|
|
{name: 'string'},
|
|
|
|
{plural: 'chapters'}
|
2015-01-01 07:26:58 +00:00
|
|
|
);
|
2016-11-15 21:46:23 +00:00
|
|
|
app.model(Chapter, {dataSource: 'db'});
|
2016-06-03 20:51:48 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
Book.hasMany(Page);
|
2015-01-01 07:26:58 +00:00
|
|
|
Book.hasMany(Chapter);
|
2014-08-04 16:27:50 +00:00
|
|
|
Page.hasMany(Note);
|
2015-01-01 07:26:58 +00:00
|
|
|
Chapter.hasMany(Note);
|
2014-08-04 16:27:50 +00:00
|
|
|
Image.belongsTo(Book);
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-12-10 04:04:56 +00:00
|
|
|
// fake a remote method that match the filter in Model.nestRemoting()
|
2014-12-10 11:43:55 +00:00
|
|
|
Page.prototype['__throw__errors'] = function() {
|
2014-12-10 04:04:56 +00:00
|
|
|
throw new Error('This should not crash the app');
|
|
|
|
};
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
Page.remoteMethod('__throw__errors', {isStatic: false, http: {path: '/throws', verb: 'get'}});
|
2014-12-10 04:04:56 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
Book.nestRemoting('pages');
|
2015-01-01 07:26:58 +00:00
|
|
|
Book.nestRemoting('chapters');
|
2014-08-04 16:27:50 +00:00
|
|
|
Image.nestRemoting('book');
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(Book.prototype['__findById__pages']).to.be.a('function');
|
|
|
|
expect(Image.prototype['__get__book']).to.be.a('function');
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
Page.beforeRemote('prototype.__findById__notes', function(ctx, result, next) {
|
|
|
|
ctx.res.set('x-before', 'before');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
next();
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
Page.afterRemote('prototype.__findById__notes', function(ctx, result, next) {
|
|
|
|
ctx.res.set('x-after', 'after');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
next();
|
|
|
|
});
|
2014-08-04 16:27:50 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
before(function createBook(done) {
|
|
|
|
var test = this;
|
2016-11-15 21:46:23 +00:00
|
|
|
app.models.Book.create({name: 'Book 1'},
|
2014-08-04 16:27:50 +00:00
|
|
|
function(err, book) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
test.book = book;
|
2016-11-15 21:46:23 +00:00
|
|
|
book.pages.create({name: 'Page 1'},
|
2014-11-21 02:35:36 +00:00
|
|
|
function(err, page) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
test.page = page;
|
2016-11-15 21:46:23 +00:00
|
|
|
page.notes.create({text: 'Page Note 1'},
|
2014-11-21 02:35:36 +00:00
|
|
|
function(err, note) {
|
|
|
|
test.note = note;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-08-04 16:27:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2015-01-01 07:26:58 +00:00
|
|
|
before(function createChapters(done) {
|
2015-01-04 10:24:29 +00:00
|
|
|
var test = this;
|
2016-11-15 21:46:23 +00:00
|
|
|
test.book.chapters.create({name: 'Chapter 1'},
|
2015-01-01 07:26:58 +00:00
|
|
|
function(err, chapter) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-01-01 07:26:58 +00:00
|
|
|
test.chapter = chapter;
|
2016-11-15 21:46:23 +00:00
|
|
|
chapter.notes.create({text: 'Chapter Note 1'}, function(err, note) {
|
2015-01-01 07:26:58 +00:00
|
|
|
test.cnote = note;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-01-01 07:26:58 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
before(function createCover(done) {
|
|
|
|
var test = this;
|
2016-11-15 21:46:23 +00:00
|
|
|
app.models.Image.create({name: 'Cover 1', book: test.book},
|
2014-08-04 16:27:50 +00:00
|
|
|
function(err, image) {
|
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
test.image = image;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
it('has regular relationship routes - pages', function(done) {
|
2014-08-04 16:27:50 +00:00
|
|
|
var test = this;
|
|
|
|
this.get('/api/books/' + test.book.id + '/pages')
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(res.body).to.be.an('array');
|
2014-08-04 16:27:50 +00:00
|
|
|
expect(res.body).to.have.length(1);
|
|
|
|
expect(res.body[0].name).to.equal('Page 1');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
done();
|
2014-11-21 02:35:36 +00:00
|
|
|
});
|
2014-08-04 16:27:50 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
it('has regular relationship routes - notes', function(done) {
|
|
|
|
var test = this;
|
|
|
|
this.get('/api/pages/' + test.page.id + '/notes/' + test.note.id)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
expect(res.headers['x-before']).to.equal('before');
|
|
|
|
expect(res.headers['x-after']).to.equal('after');
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(res.body).to.be.an('object');
|
2014-08-05 10:14:39 +00:00
|
|
|
expect(res.body.text).to.equal('Page Note 1');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
done();
|
2014-11-21 02:35:36 +00:00
|
|
|
});
|
2014-08-05 10:14:39 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
it('has a basic error handler', function(done) {
|
|
|
|
var test = this;
|
2014-08-04 16:47:15 +00:00
|
|
|
this.get('/api/books/unknown/pages/' + test.page.id + '/notes')
|
2014-08-04 16:27:50 +00:00
|
|
|
.expect(404, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(res.body.error).to.be.an('object');
|
2014-08-04 16:27:50 +00:00
|
|
|
var expected = 'could not find a model with id unknown';
|
|
|
|
expect(res.body.error.message).to.equal(expected);
|
2014-12-18 20:26:27 +00:00
|
|
|
expect(res.body.error.code).to.be.equal('MODEL_NOT_FOUND');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
done();
|
2014-11-21 02:35:36 +00:00
|
|
|
});
|
2014-08-04 16:27:50 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
it('enables nested relationship routes - belongsTo find', function(done) {
|
|
|
|
var test = this;
|
|
|
|
this.get('/api/images/' + test.image.id + '/book/pages')
|
|
|
|
.end(function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(res.body).to.be.an('array');
|
2014-08-04 16:27:50 +00:00
|
|
|
expect(res.body).to.have.length(1);
|
|
|
|
expect(res.body[0].name).to.equal('Page 1');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
done();
|
2014-11-21 02:35:36 +00:00
|
|
|
});
|
2014-08-04 16:27:50 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
it('enables nested relationship routes - belongsTo findById', function(done) {
|
|
|
|
var test = this;
|
|
|
|
this.get('/api/images/' + test.image.id + '/book/pages/' + test.page.id)
|
|
|
|
.end(function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(res.body).to.be.an('object');
|
2014-08-04 16:27:50 +00:00
|
|
|
expect(res.body.name).to.equal('Page 1');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
done();
|
2014-11-21 02:35:36 +00:00
|
|
|
});
|
2014-08-04 16:27:50 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
it('enables nested relationship routes - hasMany find', function(done) {
|
|
|
|
var test = this;
|
|
|
|
this.get('/api/books/' + test.book.id + '/pages/' + test.page.id + '/notes')
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(res.body).to.be.an('array');
|
2014-08-04 16:27:50 +00:00
|
|
|
expect(res.body).to.have.length(1);
|
|
|
|
expect(res.body[0].text).to.equal('Page Note 1');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
done();
|
2014-11-21 02:35:36 +00:00
|
|
|
});
|
2014-08-04 16:27:50 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2014-08-04 16:27:50 +00:00
|
|
|
it('enables nested relationship routes - hasMany findById', function(done) {
|
|
|
|
var test = this;
|
|
|
|
this.get('/api/books/' + test.book.id + '/pages/' + test.page.id + '/notes/' + test.note.id)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-08-05 10:14:39 +00:00
|
|
|
expect(res.headers['x-before']).to.equal('before');
|
|
|
|
expect(res.headers['x-after']).to.equal('after');
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(res.body).to.be.an('object');
|
2014-08-04 16:27:50 +00:00
|
|
|
expect(res.body.text).to.equal('Page Note 1');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-21 02:35:36 +00:00
|
|
|
done();
|
|
|
|
});
|
2014-08-04 16:27:50 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2015-01-01 07:26:58 +00:00
|
|
|
it('should nest remote hooks of ModelTo - hasMany findById', function(done) {
|
|
|
|
var test = this;
|
|
|
|
this.get('/api/books/' + test.book.id + '/chapters/' + test.chapter.id + '/notes/' + test.cnote.id)
|
|
|
|
.expect(200, function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2016-11-22 14:30:04 +00:00
|
|
|
expect(res.headers['x-before']).to.empty();
|
|
|
|
expect(res.headers['x-after']).to.empty();
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-01-01 07:26:58 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-14 07:33:29 +00:00
|
|
|
it('should have proper http.path for remoting', function() {
|
|
|
|
[app.models.Book, app.models.Image].forEach(function(Model) {
|
|
|
|
Model.sharedClass.methods().forEach(function(method) {
|
|
|
|
var http = Array.isArray(method.http) ? method.http : [method.http];
|
|
|
|
http.forEach(function(opt) {
|
|
|
|
// destroyAll has been shared but missing http property
|
|
|
|
if (opt.path === undefined) return;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-11-14 07:33:29 +00:00
|
|
|
expect(opt.path, method.stringName).to.match(/^\/.*/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-12-10 04:04:56 +00:00
|
|
|
|
2014-12-10 11:43:55 +00:00
|
|
|
it('should catch error if nested function throws', function(done) {
|
2014-12-10 04:04:56 +00:00
|
|
|
var test = this;
|
|
|
|
this.get('/api/books/' + test.book.id + '/pages/' + this.page.id + '/throws')
|
|
|
|
.end(function(err, res) {
|
2016-01-01 00:53:40 +00:00
|
|
|
if (err) return done(err);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-12-10 04:04:56 +00:00
|
|
|
expect(res.body).to.be.an('object');
|
|
|
|
expect(res.body.error).to.be.an('object');
|
|
|
|
expect(res.body.error.name).to.equal('Error');
|
2016-05-24 20:46:32 +00:00
|
|
|
expect(res.body.error.statusCode).to.equal(500);
|
2014-12-10 04:04:56 +00:00
|
|
|
expect(res.body.error.message).to.equal('This should not crash the app');
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2014-12-10 04:04:56 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2014-08-04 16:27:50 +00:00
|
|
|
});
|
2014-11-21 01:46:21 +00:00
|
|
|
|
2015-01-30 06:33:34 +00:00
|
|
|
describe('hasOne', function() {
|
|
|
|
var cust;
|
|
|
|
|
|
|
|
before(function createCustomer(done) {
|
|
|
|
var test = this;
|
2016-11-15 21:46:23 +00:00
|
|
|
app.models.customer.create({name: 'John'}, function(err, c) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2015-01-30 06:33:34 +00:00
|
|
|
cust = c;
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-01-30 06:33:34 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function(done) {
|
|
|
|
var self = this;
|
|
|
|
this.app.models.customer.destroyAll(function(err) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2015-01-30 06:33:34 +00:00
|
|
|
self.app.models.profile.destroyAll(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create the referenced model', function(done) {
|
|
|
|
var url = '/api/customers/' + cust.id + '/profile';
|
|
|
|
|
|
|
|
this.post(url)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({points: 10})
|
2015-01-30 06:33:34 +00:00
|
|
|
.expect(200, function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2015-01-30 06:33:34 +00:00
|
|
|
expect(res.body.points).to.be.eql(10);
|
|
|
|
expect(res.body.customerId).to.be.eql(cust.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-01-30 06:33:34 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should find the referenced model', function(done) {
|
|
|
|
var url = '/api/customers/' + cust.id + '/profile';
|
|
|
|
this.get(url)
|
|
|
|
.expect(200, function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2015-01-30 06:33:34 +00:00
|
|
|
expect(res.body.points).to.be.eql(10);
|
|
|
|
expect(res.body.customerId).to.be.eql(cust.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-01-30 06:33:34 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not create the referenced model twice', function(done) {
|
|
|
|
var url = '/api/customers/' + cust.id + '/profile';
|
|
|
|
this.post(url)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({points: 20})
|
2015-01-30 06:33:34 +00:00
|
|
|
.expect(500, function(err, res) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update the referenced model', function(done) {
|
|
|
|
var url = '/api/customers/' + cust.id + '/profile';
|
|
|
|
this.put(url)
|
2016-11-15 21:46:23 +00:00
|
|
|
.send({points: 100})
|
2015-01-30 06:33:34 +00:00
|
|
|
.expect(200, function(err, res) {
|
2016-05-05 04:09:06 +00:00
|
|
|
if (err) return done(err);
|
|
|
|
|
2015-01-30 06:33:34 +00:00
|
|
|
expect(res.body.points).to.be.eql(100);
|
|
|
|
expect(res.body.customerId).to.be.eql(cust.id);
|
2016-05-05 04:09:06 +00:00
|
|
|
|
2015-01-30 06:33:34 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should delete the referenced model', function(done) {
|
|
|
|
var url = '/api/customers/' + cust.id + '/profile';
|
|
|
|
this.del(url)
|
|
|
|
.expect(204, function(err, res) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not find the referenced model', function(done) {
|
|
|
|
var url = '/api/customers/' + cust.id + '/profile';
|
|
|
|
this.get(url)
|
|
|
|
.expect(404, function(err, res) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-12-20 22:58:29 +00:00
|
|
|
});
|