forked from strongloop/loopback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelations.integration.js
227 lines (210 loc) · 7.03 KB
/
relations.integration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var loopback = require('../');
var lt = require('loopback-testing');
var path = require('path');
var SIMPLE_APP = path.join(__dirname, 'fixtures', 'simple-integration-app');
var app = require(path.join(SIMPLE_APP, 'app.js'));
var assert = require('assert');
var expect = require('chai').expect;
describe('relations - integration', function () {
lt.beforeEach.withApp(app);
lt.beforeEach.givenModel('store');
beforeEach(function(done) {
this.widgetName = 'foo';
this.store.widgets.create({
name: this.widgetName
}, function() {
done();
});
});
afterEach(function(done) {
this.app.models.widget.destroyAll(done);
});
describe('/store/:id/widgets', function () {
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() {
this.widgets = this.res.body;
this.widget = this.res.body[0];
});
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 = {
name: this.newWidgetName
};
});
beforeEach(function(done) {
this.http = this.post(this.url, this.newWidget);
this.http.send(this.newWidget);
this.http.end(function(err) {
if(err) return done(err);
this.req = this.http.req;
this.res = this.http.res;
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;
});
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);
});
});
it('should have a single widget with storeId', function (done) {
this.app.models.widget.count({
storeId: this.store.id
}, function(err, count) {
if(err) return done(err);
assert.equal(count, 2);
done();
});
});
});
});
describe('/widgets/:id/store', function () {
beforeEach(function (done) {
var self = this;
this.store.widgets.create({
name: this.widgetName
}, function(err, widget) {
self.widget = widget;
self.url = '/api/widgets/' + self.widget.id + '/store';
done();
});
});
lt.describe.whenCalledRemotely('GET', '/api/widgets/:id/store', function () {
it('should succeed with statusCode 200', function () {
assert.equal(this.res.statusCode, 200);
assert.equal(this.res.body.id, this.store.id);
});
});
});
describe('hasAndBelongsToMany', function() {
beforeEach(function defineProductAndCategoryModels() {
var product = app.model(
'product',
{ properties: { id: 'string', name: 'string' }, dataSource: 'db' }
);
var category = app.model(
'category',
{ properties: { id: 'string', name: 'string' }, dataSource: 'db' }
);
product.hasAndBelongsToMany(category);
category.hasAndBelongsToMany(product);
});
lt.beforeEach.givenModel('category');
beforeEach(function createProductsInCategory(done) {
var test = this;
this.category.products.create({
name: 'a-product'
}, function(err, product) {
if (err) return done(err);
test.product = product;
done();
});
});
beforeEach(function createAnotherCategoryAndProduct(done) {
app.models.category.create({ name: 'another-category' },
function(err, cat) {
if (err) return done(err);
cat.products.create({ name: 'another-product' }, done);
});
});
afterEach(function(done) {
this.app.models.product.destroyAll(done);
});
it.skip('allows to find related objects via where filter', function(done) {
//TODO https://github.com/strongloop/loopback-datasource-juggler/issues/94
var expectedProduct = this.product;
// Note: the URL format is not final
this.get('/api/products?filter[where][categoryId]=' + this.category.id)
.expect(200, function(err, res) {
if (err) return done(err);
expect(res.body).to.eql([
{
id: expectedProduct.id,
name: expectedProduct.name
}
]);
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);
expect(res.body).to.eql([
{
id: expectedProduct.id,
name: expectedProduct.name
}
]);
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) {
expect(res.body).to.have.property('products');
expect(res.body.products).to.eql([
{
id: expectedProduct.id,
name: expectedProduct.name
}
]);
done();
});
});
it.skip('includes requested related models in `findById`', function(done) {
//TODO https://github.com/strongloop/loopback-datasource-juggler/issues/93
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) {
expect(res.body).to.have.property('products');
expect(res.body.products).to.eql([
{
id: expectedProduct.id,
name: expectedProduct.name
}
]);
done();
});
});
});
});