2016-04-01 22:25:16 +00:00
|
|
|
|
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
|
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
|
// This test written in mocha+should.js
|
2016-08-22 19:55:22 +00:00
|
|
|
|
'use strict';
|
2013-10-02 05:14:21 +00:00
|
|
|
|
var should = require('./init.js');
|
|
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
|
|
var jdb = require('../');
|
|
|
|
|
var ModelBuilder = jdb.ModelBuilder;
|
|
|
|
|
var DataSource = jdb.DataSource;
|
2013-12-11 22:45:27 +00:00
|
|
|
|
var Memory = require('../lib/connectors/memory');
|
2013-10-02 05:14:21 +00:00
|
|
|
|
|
|
|
|
|
var ModelDefinition = require('../lib/model-definition');
|
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
describe('ModelDefinition class', function() {
|
2015-04-02 07:49:04 +00:00
|
|
|
|
var memory;
|
|
|
|
|
beforeEach(function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
|
memory = new DataSource({connector: Memory});
|
2015-04-02 07:49:04 +00:00
|
|
|
|
});
|
2013-10-02 05:14:21 +00:00
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
it('should be able to define plain models', function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
2016-04-01 11:48:17 +00:00
|
|
|
|
name: 'string',
|
2014-01-24 17:09:53 +00:00
|
|
|
|
bio: ModelBuilder.Text,
|
|
|
|
|
approved: Boolean,
|
|
|
|
|
joinedAt: Date,
|
2016-04-01 13:23:42 +00:00
|
|
|
|
age: 'number',
|
2013-10-05 04:21:12 +00:00
|
|
|
|
});
|
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
User.build();
|
|
|
|
|
assert.equal(User.properties.name.type, String);
|
|
|
|
|
assert.equal(User.properties.bio.type, ModelBuilder.Text);
|
|
|
|
|
assert.equal(User.properties.approved.type, Boolean);
|
|
|
|
|
assert.equal(User.properties.joinedAt.type, Date);
|
|
|
|
|
assert.equal(User.properties.age.type, Number);
|
|
|
|
|
|
|
|
|
|
var json = User.toJSON();
|
2016-04-01 11:48:17 +00:00
|
|
|
|
assert.equal(json.name, 'User');
|
|
|
|
|
assert.equal(json.properties.name.type, 'String');
|
|
|
|
|
assert.equal(json.properties.bio.type, 'Text');
|
|
|
|
|
assert.equal(json.properties.approved.type, 'Boolean');
|
|
|
|
|
assert.equal(json.properties.joinedAt.type, 'Date');
|
|
|
|
|
assert.equal(json.properties.age.type, 'Number');
|
2015-01-20 19:58:52 +00:00
|
|
|
|
|
2014-08-17 16:01:52 +00:00
|
|
|
|
assert.deepEqual(User.toJSON(), json);
|
2015-01-20 19:58:52 +00:00
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
done();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
it('should be able to define additional properties', function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
2016-04-01 11:48:17 +00:00
|
|
|
|
name: 'string',
|
2014-01-24 17:09:53 +00:00
|
|
|
|
bio: ModelBuilder.Text,
|
|
|
|
|
approved: Boolean,
|
|
|
|
|
joinedAt: Date,
|
2016-04-01 13:23:42 +00:00
|
|
|
|
age: 'number',
|
2013-10-02 05:14:21 +00:00
|
|
|
|
});
|
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
User.build();
|
2015-01-20 19:58:52 +00:00
|
|
|
|
|
2014-08-31 10:17:53 +00:00
|
|
|
|
var json = User.toJSON();
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
|
User.defineProperty('id', {type: 'number', id: true});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
assert.equal(User.properties.name.type, String);
|
|
|
|
|
assert.equal(User.properties.bio.type, ModelBuilder.Text);
|
|
|
|
|
assert.equal(User.properties.approved.type, Boolean);
|
|
|
|
|
assert.equal(User.properties.joinedAt.type, Date);
|
|
|
|
|
assert.equal(User.properties.age.type, Number);
|
2015-01-20 19:58:52 +00:00
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
assert.equal(User.properties.id.type, Number);
|
2015-01-20 19:58:52 +00:00
|
|
|
|
|
2014-08-31 10:17:53 +00:00
|
|
|
|
json = User.toJSON();
|
2016-08-19 17:46:59 +00:00
|
|
|
|
assert.deepEqual(json.properties.id, {type: 'Number', id: true});
|
2015-01-20 19:58:52 +00:00
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
done();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
it('should be able to define nesting models', function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
|
|
|
|
name: String,
|
|
|
|
|
bio: ModelBuilder.Text,
|
|
|
|
|
approved: Boolean,
|
|
|
|
|
joinedAt: Date,
|
|
|
|
|
age: Number,
|
|
|
|
|
address: {
|
|
|
|
|
street: String,
|
|
|
|
|
city: String,
|
|
|
|
|
zipCode: String,
|
2016-04-01 11:48:17 +00:00
|
|
|
|
state: String,
|
|
|
|
|
},
|
2013-10-02 05:14:21 +00:00
|
|
|
|
});
|
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
User.build();
|
|
|
|
|
assert.equal(User.properties.name.type, String);
|
|
|
|
|
assert.equal(User.properties.bio.type, ModelBuilder.Text);
|
|
|
|
|
assert.equal(User.properties.approved.type, Boolean);
|
|
|
|
|
assert.equal(User.properties.joinedAt.type, Date);
|
|
|
|
|
assert.equal(User.properties.age.type, Number);
|
|
|
|
|
assert.equal(typeof User.properties.address.type, 'function');
|
|
|
|
|
|
|
|
|
|
var json = User.toJSON();
|
2016-04-01 11:48:17 +00:00
|
|
|
|
assert.equal(json.name, 'User');
|
|
|
|
|
assert.equal(json.properties.name.type, 'String');
|
|
|
|
|
assert.equal(json.properties.bio.type, 'Text');
|
|
|
|
|
assert.equal(json.properties.approved.type, 'Boolean');
|
|
|
|
|
assert.equal(json.properties.joinedAt.type, 'Date');
|
|
|
|
|
assert.equal(json.properties.age.type, 'Number');
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
|
assert.deepEqual(json.properties.address.type, {street: {type: 'String'},
|
|
|
|
|
city: {type: 'String'},
|
|
|
|
|
zipCode: {type: 'String'},
|
|
|
|
|
state: {type: 'String'}});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
it('should be able to define referencing models', function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
|
|
var Address = modelBuilder.define('Address', {
|
|
|
|
|
street: String,
|
|
|
|
|
city: String,
|
|
|
|
|
zipCode: String,
|
2016-04-01 11:48:17 +00:00
|
|
|
|
state: String,
|
2014-01-24 17:09:53 +00:00
|
|
|
|
});
|
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
|
|
|
|
name: String,
|
|
|
|
|
bio: ModelBuilder.Text,
|
|
|
|
|
approved: Boolean,
|
|
|
|
|
joinedAt: Date,
|
|
|
|
|
age: Number,
|
2016-04-01 11:48:17 +00:00
|
|
|
|
address: Address,
|
2013-10-02 05:14:21 +00:00
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
});
|
2013-10-05 04:21:12 +00:00
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
User.build();
|
|
|
|
|
assert.equal(User.properties.name.type, String);
|
|
|
|
|
assert.equal(User.properties.bio.type, ModelBuilder.Text);
|
|
|
|
|
assert.equal(User.properties.approved.type, Boolean);
|
|
|
|
|
assert.equal(User.properties.joinedAt.type, Date);
|
|
|
|
|
assert.equal(User.properties.age.type, Number);
|
|
|
|
|
assert.equal(User.properties.address.type, Address);
|
2013-10-05 04:21:12 +00:00
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
var json = User.toJSON();
|
2016-04-01 11:48:17 +00:00
|
|
|
|
assert.equal(json.name, 'User');
|
|
|
|
|
assert.equal(json.properties.name.type, 'String');
|
|
|
|
|
assert.equal(json.properties.bio.type, 'Text');
|
|
|
|
|
assert.equal(json.properties.approved.type, 'Boolean');
|
|
|
|
|
assert.equal(json.properties.joinedAt.type, 'Date');
|
|
|
|
|
assert.equal(json.properties.age.type, 'Number');
|
2013-10-05 04:21:12 +00:00
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
assert.equal(json.properties.address.type, 'Address');
|
2013-10-02 05:14:21 +00:00
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
done();
|
2013-10-02 05:14:21 +00:00
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
});
|
2013-10-05 04:21:12 +00:00
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
it('should be able to define referencing models by name', function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
|
var modelBuilder = new ModelBuilder();
|
2013-10-05 18:13:10 +00:00
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
var Address = modelBuilder.define('Address', {
|
|
|
|
|
street: String,
|
|
|
|
|
city: String,
|
|
|
|
|
zipCode: String,
|
2016-04-01 11:48:17 +00:00
|
|
|
|
state: String,
|
2013-10-05 18:13:10 +00:00
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
|
|
|
|
name: String,
|
|
|
|
|
bio: ModelBuilder.Text,
|
|
|
|
|
approved: Boolean,
|
|
|
|
|
joinedAt: Date,
|
|
|
|
|
age: Number,
|
2016-04-01 11:48:17 +00:00
|
|
|
|
address: 'Address',
|
2013-10-05 18:13:10 +00:00
|
|
|
|
|
2013-10-02 05:14:21 +00:00
|
|
|
|
});
|
2013-10-05 04:21:12 +00:00
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
User.build();
|
|
|
|
|
assert.equal(User.properties.name.type, String);
|
|
|
|
|
assert.equal(User.properties.bio.type, ModelBuilder.Text);
|
|
|
|
|
assert.equal(User.properties.approved.type, Boolean);
|
|
|
|
|
assert.equal(User.properties.joinedAt.type, Date);
|
|
|
|
|
assert.equal(User.properties.age.type, Number);
|
|
|
|
|
assert.equal(User.properties.address.type, Address);
|
|
|
|
|
|
|
|
|
|
var json = User.toJSON();
|
2016-04-01 11:48:17 +00:00
|
|
|
|
assert.equal(json.name, 'User');
|
|
|
|
|
assert.equal(json.properties.name.type, 'String');
|
|
|
|
|
assert.equal(json.properties.bio.type, 'Text');
|
|
|
|
|
assert.equal(json.properties.approved.type, 'Boolean');
|
|
|
|
|
assert.equal(json.properties.joinedAt.type, 'Date');
|
|
|
|
|
assert.equal(json.properties.age.type, 'Number');
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
|
|
assert.equal(json.properties.address.type, 'Address');
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
it('should report correct id names', function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
2016-08-19 17:46:59 +00:00
|
|
|
|
userId: {type: String, id: true},
|
2016-04-01 11:48:17 +00:00
|
|
|
|
name: 'string',
|
2014-01-24 17:09:53 +00:00
|
|
|
|
bio: ModelBuilder.Text,
|
|
|
|
|
approved: Boolean,
|
|
|
|
|
joinedAt: Date,
|
2016-04-01 13:23:42 +00:00
|
|
|
|
age: 'number',
|
2013-10-05 04:21:12 +00:00
|
|
|
|
});
|
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
|
assert.equal(User.idName(), 'userId');
|
|
|
|
|
assert.deepEqual(User.idNames(), ['userId']);
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
it('should sort id properties by its index', function() {
|
2015-01-21 12:16:34 +00:00
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
2016-08-19 17:46:59 +00:00
|
|
|
|
userId: {type: String, id: 2},
|
|
|
|
|
userType: {type: String, id: 1},
|
2016-04-01 11:48:17 +00:00
|
|
|
|
name: 'string',
|
2015-01-21 12:16:34 +00:00
|
|
|
|
bio: ModelBuilder.Text,
|
|
|
|
|
approved: Boolean,
|
|
|
|
|
joinedAt: Date,
|
2016-04-01 13:23:42 +00:00
|
|
|
|
age: 'number',
|
2015-01-21 12:16:34 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var ids = User.ids();
|
|
|
|
|
assert.ok(Array.isArray(ids));
|
|
|
|
|
assert.equal(ids.length, 2);
|
|
|
|
|
assert.equal(ids[0].id, 1);
|
|
|
|
|
assert.equal(ids[0].name, 'userType');
|
|
|
|
|
assert.equal(ids[1].id, 2);
|
|
|
|
|
assert.equal(ids[1].name, 'userId');
|
|
|
|
|
});
|
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
it('should report correct table/column names', function(done) {
|
2014-01-24 17:09:53 +00:00
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
2016-08-19 17:46:59 +00:00
|
|
|
|
userId: {type: String, id: true, oracle: {column: 'ID'}},
|
2016-04-01 13:23:42 +00:00
|
|
|
|
name: 'string',
|
2016-08-19 17:46:59 +00:00
|
|
|
|
}, {oracle: {table: 'USER'}});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
|
|
|
|
|
assert.equal(User.tableName('oracle'), 'USER');
|
|
|
|
|
assert.equal(User.tableName('mysql'), 'User');
|
|
|
|
|
assert.equal(User.columnName('oracle', 'userId'), 'ID');
|
|
|
|
|
assert.equal(User.columnName('mysql', 'userId'), 'userId');
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
it('should inherit prototype using option.base', function() {
|
2014-01-24 17:09:53 +00:00
|
|
|
|
var modelBuilder = memory.modelBuilder;
|
|
|
|
|
var parent = memory.createModel('parent', {}, {
|
|
|
|
|
relations: {
|
|
|
|
|
children: {
|
|
|
|
|
type: 'hasMany',
|
2016-04-01 11:48:17 +00:00
|
|
|
|
model: 'anotherChild',
|
|
|
|
|
},
|
|
|
|
|
},
|
2013-12-11 22:45:27 +00:00
|
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
|
var baseChild = modelBuilder.define('baseChild');
|
|
|
|
|
baseChild.attachTo(memory);
|
|
|
|
|
// the name of this must begin with a letter < b
|
|
|
|
|
// for this test to fail
|
|
|
|
|
var anotherChild = baseChild.extend('anotherChild');
|
|
|
|
|
|
|
|
|
|
assert(anotherChild.prototype instanceof baseChild);
|
|
|
|
|
});
|
2013-12-11 22:45:27 +00:00
|
|
|
|
|
2014-10-10 11:42:59 +00:00
|
|
|
|
it('should ignore inherited options.base', function() {
|
|
|
|
|
var modelBuilder = memory.modelBuilder;
|
|
|
|
|
var base = modelBuilder.define('base');
|
2016-08-19 17:46:59 +00:00
|
|
|
|
var child = base.extend('child', {}, {base: 'base'});
|
2014-10-10 11:42:59 +00:00
|
|
|
|
var grandChild = child.extend('grand-child');
|
|
|
|
|
assert.equal('child', grandChild.base.modelName);
|
|
|
|
|
assert(grandChild.prototype instanceof child);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should ignore inherited options.super', function() {
|
|
|
|
|
var modelBuilder = memory.modelBuilder;
|
|
|
|
|
var base = modelBuilder.define('base');
|
2016-08-19 17:46:59 +00:00
|
|
|
|
var child = base.extend('child', {}, {super: 'base'});
|
2014-10-10 11:42:59 +00:00
|
|
|
|
var grandChild = child.extend('grand-child');
|
|
|
|
|
assert.equal('child', grandChild.base.modelName);
|
|
|
|
|
assert(grandChild.prototype instanceof child);
|
|
|
|
|
});
|
|
|
|
|
|
2015-01-20 19:58:52 +00:00
|
|
|
|
it('should serialize protected properties into JSON', function() {
|
|
|
|
|
var modelBuilder = memory.modelBuilder;
|
|
|
|
|
var ProtectedModel = memory.createModel('protected', {}, {
|
2016-04-01 11:48:17 +00:00
|
|
|
|
protected: ['protectedProperty'],
|
2015-01-20 19:58:52 +00:00
|
|
|
|
});
|
|
|
|
|
var pm = new ProtectedModel({
|
2016-04-01 11:48:17 +00:00
|
|
|
|
id: 1, foo: 'bar', protectedProperty: 'protected',
|
2015-01-20 19:58:52 +00:00
|
|
|
|
});
|
|
|
|
|
var serialized = pm.toJSON();
|
|
|
|
|
assert.deepEqual(serialized, {
|
2016-04-01 11:48:17 +00:00
|
|
|
|
id: 1, foo: 'bar', protectedProperty: 'protected',
|
2015-01-20 19:58:52 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
it('should not serialize protected properties of nested models into JSON', function(done) {
|
2015-01-20 19:58:52 +00:00
|
|
|
|
var modelBuilder = memory.modelBuilder;
|
|
|
|
|
var Parent = memory.createModel('parent');
|
2016-08-19 17:46:59 +00:00
|
|
|
|
var Child = memory.createModel('child', {}, {protected: ['protectedProperty']});
|
2015-01-20 19:58:52 +00:00
|
|
|
|
Parent.hasMany(Child);
|
|
|
|
|
Parent.create({
|
2016-04-01 11:48:17 +00:00
|
|
|
|
name: 'parent',
|
2015-01-20 19:58:52 +00:00
|
|
|
|
}, function(err, parent) {
|
|
|
|
|
parent.children.create({
|
|
|
|
|
name: 'child',
|
2016-04-01 11:48:17 +00:00
|
|
|
|
protectedProperty: 'protectedValue',
|
2015-01-20 19:58:52 +00:00
|
|
|
|
}, function(err, child) {
|
2016-08-19 17:46:59 +00:00
|
|
|
|
Parent.find({include: 'children'}, function(err, parents) {
|
2015-01-20 19:58:52 +00:00
|
|
|
|
var serialized = parents[0].toJSON();
|
|
|
|
|
var child = serialized.children[0];
|
|
|
|
|
assert.equal(child.name, 'child');
|
|
|
|
|
assert.notEqual(child.protectedProperty, 'protectedValue');
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
it('should not serialize hidden properties into JSON', function() {
|
2014-04-11 18:39:57 +00:00
|
|
|
|
var modelBuilder = memory.modelBuilder;
|
|
|
|
|
var HiddenModel = memory.createModel('hidden', {}, {
|
2016-04-01 11:48:17 +00:00
|
|
|
|
hidden: ['secret'],
|
2014-04-11 18:39:57 +00:00
|
|
|
|
});
|
|
|
|
|
var hm = new HiddenModel({
|
|
|
|
|
id: 1,
|
|
|
|
|
foo: 'bar',
|
2016-04-01 11:48:17 +00:00
|
|
|
|
secret: 'secret',
|
2014-04-11 18:39:57 +00:00
|
|
|
|
});
|
|
|
|
|
var serialized = hm.toJSON();
|
|
|
|
|
assert.deepEqual(serialized, {
|
|
|
|
|
id: 1,
|
2016-04-01 11:48:17 +00:00
|
|
|
|
foo: 'bar',
|
2014-04-11 18:39:57 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
|
it('should not serialize hidden properties of nested models into JSON', function(done) {
|
2014-04-11 18:39:57 +00:00
|
|
|
|
var modelBuilder = memory.modelBuilder;
|
|
|
|
|
var Parent = memory.createModel('parent');
|
2016-08-19 17:46:59 +00:00
|
|
|
|
var Child = memory.createModel('child', {}, {hidden: ['secret']});
|
2014-04-11 18:39:57 +00:00
|
|
|
|
Parent.hasMany(Child);
|
|
|
|
|
Parent.create({
|
2016-04-01 11:48:17 +00:00
|
|
|
|
name: 'parent',
|
2014-04-11 18:39:57 +00:00
|
|
|
|
}, function(err, parent) {
|
|
|
|
|
parent.children.create({
|
|
|
|
|
name: 'child',
|
2016-04-01 11:48:17 +00:00
|
|
|
|
secret: 'secret',
|
2014-04-11 18:39:57 +00:00
|
|
|
|
}, function(err, child) {
|
2016-08-19 17:46:59 +00:00
|
|
|
|
Parent.find({include: 'children'}, function(err, parents) {
|
2014-04-11 18:39:57 +00:00
|
|
|
|
var serialized = parents[0].toJSON();
|
|
|
|
|
var child = serialized.children[0];
|
|
|
|
|
assert.equal(child.name, 'child');
|
|
|
|
|
assert.notEqual(child.secret, 'secret');
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-04-02 07:49:04 +00:00
|
|
|
|
|
2016-05-26 21:21:37 +00:00
|
|
|
|
it('should throw error for property names containing dot', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
|
(function() { memory.createModel('Dotted', {'dot.name': String}); })
|
2016-05-26 21:21:37 +00:00
|
|
|
|
.should
|
|
|
|
|
.throw(/dot\(s\).*Dotted.*dot\.name/);
|
2015-04-02 07:49:04 +00:00
|
|
|
|
});
|
|
|
|
|
|
2016-01-19 02:48:58 +00:00
|
|
|
|
it('should report deprecation warning for property named constructor', function() {
|
|
|
|
|
var message = 'deprecation not reported';
|
|
|
|
|
process.once('deprecation', function(err) { message = err.message; });
|
|
|
|
|
|
2016-08-19 17:46:59 +00:00
|
|
|
|
memory.createModel('Ctor', {'constructor': String});
|
2016-01-19 02:48:58 +00:00
|
|
|
|
|
|
|
|
|
message.should.match(/Property name should not be "constructor" in Model: Ctor/);
|
|
|
|
|
});
|
|
|
|
|
|
2016-05-26 21:21:37 +00:00
|
|
|
|
it('should throw error for dynamic property names containing dot',
|
|
|
|
|
function(done) {
|
|
|
|
|
var Model = memory.createModel('DynamicDotted');
|
2016-08-19 17:46:59 +00:00
|
|
|
|
Model.create({'dot.name': 'dot.value'}, function(err) {
|
2016-05-26 21:21:37 +00:00
|
|
|
|
err.should.be.instanceOf(Error);
|
|
|
|
|
err.message.should.match(/dot\(s\).*DynamicDotted.*dot\.name/);
|
|
|
|
|
done();
|
|
|
|
|
});
|
2015-04-02 07:49:04 +00:00
|
|
|
|
});
|
2015-05-04 13:00:06 +00:00
|
|
|
|
|
2016-01-19 02:48:58 +00:00
|
|
|
|
it('should throw error for dynamic property named constructor', function(done) {
|
|
|
|
|
var Model = memory.createModel('DynamicCtor');
|
2016-08-19 17:46:59 +00:00
|
|
|
|
Model.create({'constructor': 'myCtor'}, function(err) {
|
2016-01-19 02:48:58 +00:00
|
|
|
|
assert.equal(err.message, 'Property name "constructor" is not allowed in DynamicCtor data');
|
|
|
|
|
done();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2015-05-04 13:00:06 +00:00
|
|
|
|
it('should support "array" type shortcut', function() {
|
|
|
|
|
var Model = memory.createModel('TwoArrays', {
|
|
|
|
|
regular: Array,
|
2016-04-01 11:48:17 +00:00
|
|
|
|
sugar: 'array',
|
2015-05-04 13:00:06 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var props = Model.definition.properties;
|
|
|
|
|
props.regular.type.should.equal(props.sugar.type);
|
|
|
|
|
});
|
2015-08-26 22:23:35 +00:00
|
|
|
|
|
|
|
|
|
context('hasPK', function() {
|
|
|
|
|
context('with primary key defined', function() {
|
|
|
|
|
var Todo;
|
|
|
|
|
before(function prepModel() {
|
|
|
|
|
Todo = new ModelDefinition(new ModelBuilder(), 'Todo', {
|
2016-04-01 11:48:17 +00:00
|
|
|
|
content: 'string',
|
2015-08-26 22:23:35 +00:00
|
|
|
|
});
|
|
|
|
|
Todo.defineProperty('id', {
|
|
|
|
|
type: 'number',
|
2016-04-01 11:48:17 +00:00
|
|
|
|
id: true,
|
2015-08-26 22:23:35 +00:00
|
|
|
|
});
|
|
|
|
|
Todo.build();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return true', function() {
|
|
|
|
|
Todo.hasPK().should.be.ok;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
context('without primary key defined', function() {
|
|
|
|
|
var Todo;
|
|
|
|
|
before(function prepModel() {
|
|
|
|
|
Todo = new ModelDefinition(new ModelBuilder(), 'Todo', {
|
2016-04-01 11:48:17 +00:00
|
|
|
|
content: 'string',
|
2015-08-26 22:23:35 +00:00
|
|
|
|
});
|
|
|
|
|
Todo.build();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should return false', function() {
|
|
|
|
|
Todo.hasPK().should.not.be.ok;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2013-10-02 05:14:21 +00:00
|
|
|
|
});
|