2013-07-16 17:42:47 +00:00
|
|
|
describe('loopback', function() {
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
var nameCounter = 0;
|
|
|
|
var uniqueModelName;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
uniqueModelName = 'TestModel-' + (++nameCounter);
|
|
|
|
});
|
|
|
|
|
2014-01-02 09:31:47 +00:00
|
|
|
describe('exports', function() {
|
|
|
|
it('ValidationError', function() {
|
|
|
|
expect(loopback.ValidationError).to.be.a('function')
|
|
|
|
.and.have.property('name', 'ValidationError');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-07-16 17:42:47 +00:00
|
|
|
describe('loopback.createDataSource(options)', function(){
|
2013-06-11 16:01:44 +00:00
|
|
|
it('Create a data source with a connector.', function() {
|
2013-07-16 17:42:47 +00:00
|
|
|
var dataSource = loopback.createDataSource({
|
|
|
|
connector: loopback.Memory
|
2013-06-11 16:01:44 +00:00
|
|
|
});
|
2013-07-23 19:59:40 +00:00
|
|
|
assert(dataSource.connector);
|
2013-06-08 00:37:30 +00:00
|
|
|
});
|
|
|
|
});
|
2014-01-23 23:27:31 +00:00
|
|
|
|
|
|
|
describe('loopback.autoAttach', function () {
|
|
|
|
it('doesn\'t overwrite model with datasource configured', function () {
|
|
|
|
var ds1 = loopback.createDataSource('db1', {
|
|
|
|
connector: loopback.Memory
|
|
|
|
});
|
|
|
|
|
|
|
|
// setup default data sources
|
|
|
|
loopback.setDefaultDataSourceForType('db', ds1);
|
|
|
|
|
|
|
|
var ds2 = loopback.createDataSource('db2', {
|
|
|
|
connector: loopback.Memory
|
|
|
|
});
|
|
|
|
|
|
|
|
var model1 = ds2.createModel('m1', {});
|
|
|
|
|
|
|
|
var model2 = loopback.createModel('m2');
|
|
|
|
model2.autoAttach = 'db';
|
|
|
|
|
|
|
|
// auto attach data sources to models
|
|
|
|
loopback.autoAttach();
|
|
|
|
|
|
|
|
assert(model1.dataSource === ds2);
|
|
|
|
assert(model2.dataSource === ds1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-07-16 17:42:47 +00:00
|
|
|
describe('loopback.remoteMethod(Model, fn, [options]);', function() {
|
2013-06-11 16:01:44 +00:00
|
|
|
it("Setup a remote method.", function() {
|
2013-07-16 17:42:47 +00:00
|
|
|
var Product = loopback.createModel('product', {price: Number});
|
2013-06-11 16:01:44 +00:00
|
|
|
|
2013-06-07 19:57:51 +00:00
|
|
|
Product.stats = function(fn) {
|
2013-06-11 16:01:44 +00:00
|
|
|
// ...
|
2013-06-07 19:57:51 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 17:42:47 +00:00
|
|
|
loopback.remoteMethod(
|
2013-06-07 19:57:51 +00:00
|
|
|
Product.stats,
|
|
|
|
{
|
|
|
|
returns: {arg: 'stats', type: 'array'},
|
|
|
|
http: {path: '/info', verb: 'get'}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2013-06-11 16:01:44 +00:00
|
|
|
assert.equal(Product.stats.returns.arg, 'stats');
|
|
|
|
assert.equal(Product.stats.returns.type, 'array');
|
|
|
|
assert.equal(Product.stats.http.path, '/info');
|
|
|
|
assert.equal(Product.stats.http.verb, 'get');
|
|
|
|
assert.equal(Product.stats.shared, true);
|
2013-06-07 19:57:51 +00:00
|
|
|
});
|
|
|
|
});
|
2013-12-10 01:11:01 +00:00
|
|
|
|
|
|
|
describe('loopback.createModel(name, properties, options)', function () {
|
|
|
|
describe('options.base', function () {
|
|
|
|
it('should extend from options.base', function () {
|
|
|
|
var MyModel = loopback.createModel('MyModel', {}, {
|
|
|
|
foo: {
|
|
|
|
bar: 'bat'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var MyCustomModel = loopback.createModel('MyCustomModel', {}, {
|
|
|
|
base: 'MyModel',
|
|
|
|
foo: {
|
|
|
|
bat: 'baz'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
assert(MyCustomModel.super_ === MyModel);
|
|
|
|
assert.deepEqual(MyCustomModel.settings.foo, { bar: 'bat', bat: 'baz' });
|
|
|
|
assert(MyCustomModel.super_.modelName === MyModel.modelName);
|
|
|
|
});
|
|
|
|
});
|
2014-01-23 22:26:45 +00:00
|
|
|
|
|
|
|
describe('loopback.getModel and getModelByType', function () {
|
|
|
|
it('should be able to get model by name', function () {
|
|
|
|
var MyModel = loopback.createModel('MyModel', {}, {
|
|
|
|
foo: {
|
|
|
|
bar: 'bat'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var MyCustomModel = loopback.createModel('MyCustomModel', {}, {
|
|
|
|
base: 'MyModel',
|
|
|
|
foo: {
|
|
|
|
bat: 'baz'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
assert(loopback.getModel('MyModel') === MyModel);
|
|
|
|
assert(loopback.getModel('MyCustomModel') === MyCustomModel);
|
|
|
|
assert(loopback.getModel('Invalid') === undefined);
|
|
|
|
});
|
|
|
|
it('should be able to get model by type', function () {
|
|
|
|
var MyModel = loopback.createModel('MyModel', {}, {
|
|
|
|
foo: {
|
|
|
|
bar: 'bat'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var MyCustomModel = loopback.createModel('MyCustomModel', {}, {
|
|
|
|
base: 'MyModel',
|
|
|
|
foo: {
|
|
|
|
bat: 'baz'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
assert(loopback.getModelByType(MyModel) === MyCustomModel);
|
|
|
|
assert(loopback.getModelByType(MyCustomModel) === MyCustomModel);
|
|
|
|
});
|
|
|
|
});
|
2013-12-10 01:11:01 +00:00
|
|
|
});
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
|
2014-06-09 09:18:52 +00:00
|
|
|
describe('loopback.createModel(config)', function() {
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
it('creates the model', function() {
|
2014-06-09 09:18:52 +00:00
|
|
|
var model = loopback.createModel({
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
name: uniqueModelName
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.prototype).to.be.instanceof(loopback.Model);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('interprets extra first-level keys as options', function() {
|
2014-06-09 09:18:52 +00:00
|
|
|
var model = loopback.createModel({
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
name: uniqueModelName,
|
|
|
|
base: 'User'
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.prototype).to.be.instanceof(loopback.User);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('prefers config.options.key over config.key', function() {
|
2014-06-09 09:18:52 +00:00
|
|
|
var model = loopback.createModel({
|
Add createModelFromConfig and configureModel()
Add new API allowing developers to split the model definition and
configuration into two steps:
1. Build models from JSON config, export them for re-use:
```js
var Customer = loopback.createModelFromConfig({
name: 'Customer',
base: 'User',
properties: {
address: 'string'
}
});
```
2. Attach existing models to a dataSource and a loopback app,
modify certain model aspects like relations:
```js
loopback.configureModel(Customer, {
dataSource: db,
relations: { /* ... */ }
});
```
Rework `app.model` to use `loopback.configureModel` under the hood.
Here is the new usage:
```js
var Customer = require('./models').Customer;
app.model(Customer, {
dataSource: 'db',
relations: { /* ... */ }
});
```
In order to preserve backwards compatibility with loopback 1.x,
`app.model(name, config)` calls both `createModelFromConfig`
and `configureModel`.
2014-06-05 15:41:12 +00:00
|
|
|
name: uniqueModelName,
|
|
|
|
base: 'User',
|
|
|
|
options: {
|
|
|
|
base: 'Application'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.prototype).to.be.instanceof(loopback.Application);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('loopback.configureModel(ModelCtor, config)', function() {
|
|
|
|
it('adds new relations', function() {
|
|
|
|
var model = loopback.Model.extend(uniqueModelName);
|
|
|
|
|
|
|
|
loopback.configureModel(model, {
|
|
|
|
relations: {
|
|
|
|
owner: {
|
|
|
|
type: 'belongsTo',
|
|
|
|
model: 'User'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.settings.relations).to.have.property('owner');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates existing relations', function() {
|
|
|
|
var model = loopback.Model.extend(uniqueModelName, {}, {
|
|
|
|
relations: {
|
|
|
|
owner: {
|
|
|
|
type: 'belongsTo',
|
|
|
|
model: 'User'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
loopback.configureModel(model, {
|
|
|
|
relations: {
|
|
|
|
owner: {
|
|
|
|
model: 'Application'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(model.settings.relations.owner).to.eql({
|
|
|
|
type: 'belongsTo',
|
|
|
|
model: 'Application'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates relations before attaching to a dataSource', function() {
|
|
|
|
var db = loopback.createDataSource({ connector: loopback.Memory });
|
|
|
|
var model = loopback.Model.extend(uniqueModelName);
|
|
|
|
|
|
|
|
loopback.configureModel(model, {
|
|
|
|
dataSource: db,
|
|
|
|
relations: {
|
|
|
|
owner: {
|
|
|
|
type: 'belongsTo',
|
|
|
|
model: 'User'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var owner = model.prototype.owner;
|
|
|
|
expect(owner, 'model.prototype.owner').to.be.a('function');
|
|
|
|
expect(owner._targetClass).to.equal('User');
|
|
|
|
});
|
|
|
|
});
|
2013-07-23 19:59:40 +00:00
|
|
|
});
|