-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathmodel.test.js
More file actions
295 lines (269 loc) · 9.24 KB
/
Copy pathmodel.test.js
File metadata and controls
295 lines (269 loc) · 9.24 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright IBM Corp. 2014,2019. All Rights Reserved.
// Node module: generator-loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
/* global describe, beforeEach, it */
'use strict';
var path = require('path');
var helpers = require('yeoman-test');
var SANDBOX = path.resolve(__dirname, 'sandbox');
var fs = require('fs-extra');
var chai = require('chai');
var expect = chai.expect;
var assert = chai.assert;
var wsModels = require('loopback-workspace').models;
var common = require('./common');
var workspace = require('loopback-workspace');
var Workspace = workspace.models.Workspace;
describe('loopback:model generator', function() {
process.env.LB_CLI_SKIP_PROPERTY = true;
beforeEach(common.resetWorkspace);
beforeEach(function createSandbox(done) {
helpers.testDirectory(SANDBOX, done);
});
beforeEach(function createProject(done) {
common.createDummyProject(SANDBOX, 'test-app', done);
});
beforeEach(function addRestDataSource(done) {
wsModels.DataSourceDefinition.create({
name: 'rest',
connector: 'rest',
facetName: 'server',
}, done);
});
it('creates common/models/{name}.json', function() {
return helpers.run(path.join(__dirname, '../model'))
.cd(SANDBOX)
.withPrompts({
name: 'Product',
plural: 'pds',
dataSource: 'db',
}).then(function() {
var content = readProductJsonSync();
expect(content).to.have.property('name', 'Product');
expect(content).to.not.have.property('public');
expect(content).to.have.property('plural', 'pds');
});
});
it('adds an entry to server/models.json', function() {
var builtinModels = Object.keys(readModelsJsonSync('server'));
return helpers.run(path.join(__dirname, '../model'))
.cd(SANDBOX)
.withPrompts({
name: 'Product',
dataSource: 'db',
public: false,
}).then(function() {
var modelConfig = readModelsJsonSync('server');
var newModels = Object.keys(modelConfig);
var expectedModels = builtinModels.concat(['Product']);
expect(newModels).to.have.members(expectedModels);
expect(modelConfig.Product).to.eql({
dataSource: 'db',
public: false,
});
});
});
it('sets `base` option from the list', function() {
return helpers.run(path.join(__dirname, '../model'))
.cd(SANDBOX)
.withPrompts({
name: 'Product',
dataSource: 'db',
base: 'PersistedModel',
}).then(function() {
var product = readProductJsonSync();
expect(product).to.have.property('base', 'PersistedModel');
});
});
it('sets `dataSource` option to db by default', function() {
return helpers.run(path.join(__dirname, '../model'))
.cd(SANDBOX)
.withPrompts({
name: 'Product',
}).then(function() {
var product = readProductJsonSync();
expect(product).to.have.property('base', 'PersistedModel');
var modelConfig = readModelsJsonSync();
expect(modelConfig.Product.dataSource).to.eql('db');
});
});
it('sets custom `base` option', function() {
return helpers.run(path.join(__dirname, '../model'))
.cd(SANDBOX)
.withPrompts({
name: 'Product',
dataSource: 'rest',
base: null,
customBase: 'CustomModel',
}).then(function() {
var product = readProductJsonSync();
expect(product).to.have.property('base', 'CustomModel');
});
});
it('honors the model name in arg', function() {
return helpers.run(path.join(__dirname, '../model'))
.cd(SANDBOX)
.withArguments(['AdvancedProduct'])
.withPrompts({
dataSource: 'db',
base: 'Product',
}).then(function() {
var product = readAdvancedProductJsonSync();
expect(product).to.have.property('base', 'Product');
});
});
describe('in an empty project', function() {
beforeEach(common.resetWorkspace);
beforeEach(function createSandbox(done) {
helpers.testDirectory(SANDBOX, done);
});
beforeEach(function(done) {
process.env.WORKSPACE_DIR = SANDBOX;
Workspace.createFromTemplate('empty-server', 'empty', done);
});
it('should set dataSource to null', function() {
return helpers.run(path.join(__dirname, '../model'))
.cd(SANDBOX)
.withPrompts({
name: 'Product',
plural: 'pds',
}).then(function() {
var modelConfig = readModelsJsonSync();
expect(modelConfig.Product.dataSource).to.eql(null);
var product = readProductJsonSync();
expect(product).to.have.property('base', 'Model');
});
});
it('should set dataSource to 1st one if db does not exist', function() {
return wsModels.DataSourceDefinition.create({
name: 'db1',
connector: 'memory',
facetName: 'server',
}).then(returnResult);
function returnResult() {
return helpers.run(path.join(__dirname, '../model'))
.cd(SANDBOX)
.withPrompts({
name: 'Review',
plural: 'Reviews',
}).then(function() {
var modelConfig = readModelsJsonSync();
expect(modelConfig.Review.dataSource).to.eql('db1');
});
}
});
it('should set dataSource to db if it exists', function() {
return new Promise(function(resolve, reject) {
wsModels.DataSourceDefinition.create([{
name: 'db1',
connector: 'memory',
facetName: 'server',
}, {
name: 'db',
connector: 'memory',
facetName: 'server',
}], function(err) {
if (err) {
reject(err);
} else {
resolve(returnResult());
}
});
});
function returnResult() {
return helpers.run(path.join(__dirname, '../model'))
.cd(SANDBOX)
.withPrompts({
name: 'Review',
plural: 'Reviews',
}).then(function() {
var modelConfig = readModelsJsonSync();
expect(modelConfig.Review.dataSource).to.eql('db');
});
}
});
describe('with --bluemix', function() {
it('should not throw if no Bluemix datasources are found',
function() {
var srcPath = path.join(__dirname, 'fixtures',
'datasources-config-empty.json');
var destPath = path.join(SANDBOX, '.bluemix',
'datasources-config.json');
fs.copySync(srcPath, destPath);
const ctx = helpers.run(path.join(__dirname, '../model'));
return ctx.cd(SANDBOX)
.withPrompts({
name: 'Product',
})
.withOptions({
bluemix: true,
login: false,
})
.then(function() {
expect(ctx.generator.abort).to.equal(true);
});
});
it('should not throw on parsing datasources-config.json', function() {
var srcPath = path.join(__dirname, 'fixtures',
'datasources-config-empty.json');
var destPath = path.join(SANDBOX, '.bluemix',
'datasources-config.json');
fs.copySync(srcPath, destPath);
fs.writeFileSync(destPath, '');
const ctx = helpers.run(path.join(__dirname, '../model'));
return ctx.cd(SANDBOX)
.withPrompts({
name: 'Product',
})
.withOptions({
bluemix: true,
login: false,
})
.then(function() {
expect(ctx.generator.abort).to.equal(true);
});
});
it('should use Bluemix datasource', function() {
var srcPath = path.join(__dirname, 'fixtures',
'datasources-config-filled.json');
var destPath = path.join(SANDBOX, '.bluemix',
'datasources-config.json');
fs.copySync(srcPath, destPath);
const ctx = helpers.run(path.join(__dirname, '../model'));
return ctx.cd(SANDBOX)
.withPrompts({
name: 'Product',
dataSource: 'cloudant-demo-service',
})
.withOptions({
bluemix: true,
login: false,
})
.then(function() {
// eslint-disable-next-line max-len
assert('cloudant-demo-service' in ctx.generator.bluemixDataSourcesList);
var modelConfig = readModelsJsonSync();
expect(modelConfig.Product.dataSource).to
.eql('cloudant-demo-service');
});
});
});
});
function readProductJsonSync() {
var productJson = path.resolve(SANDBOX, 'common/models/product.json');
expect(fs.existsSync(productJson), 'file exists');
return JSON.parse(fs.readFileSync(productJson));
}
function readAdvancedProductJsonSync() {
var advancedProductJson = path.resolve(SANDBOX, 'common/models/advanced-product.json');
expect(fs.existsSync(advancedProductJson), 'file exists');
return JSON.parse(fs.readFileSync(advancedProductJson));
}
function readModelsJsonSync(facet) {
facet = facet || 'server';
var filepath = path.resolve(SANDBOX, facet, 'model-config.json');
var content = fs.readFileSync(filepath, 'utf-8');
return JSON.parse(content);
}
});