2013-10-02 05:14:21 +00:00
|
|
|
// This test written in mocha+should.js
|
|
|
|
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');
|
|
|
|
|
|
|
|
describe('ModelDefinition class', function () {
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should be able to define plain models', function (done) {
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
|
|
|
name: "string",
|
|
|
|
bio: ModelBuilder.Text,
|
|
|
|
approved: Boolean,
|
|
|
|
joinedAt: Date,
|
|
|
|
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();
|
|
|
|
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-08-17 16:01:52 +00:00
|
|
|
|
|
|
|
assert.deepEqual(User.toJSON(), json);
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
done();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to define additional properties', function (done) {
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
|
|
|
name: "string",
|
|
|
|
bio: ModelBuilder.Text,
|
|
|
|
approved: Boolean,
|
|
|
|
joinedAt: Date,
|
|
|
|
age: "number"
|
2013-10-02 05:14:21 +00:00
|
|
|
});
|
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
User.build();
|
|
|
|
|
|
|
|
User.defineProperty("id", {type: "number", id: true});
|
|
|
|
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.id.type, Number);
|
|
|
|
done();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to define nesting models', function (done) {
|
|
|
|
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,
|
|
|
|
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();
|
|
|
|
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");
|
|
|
|
|
|
|
|
assert.deepEqual(json.properties.address.type, { street: { type: 'String' },
|
|
|
|
city: { type: 'String' },
|
|
|
|
zipCode: { type: 'String' },
|
|
|
|
state: { type: 'String' } });
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be able to define referencing models', function (done) {
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
var Address = modelBuilder.define('Address', {
|
|
|
|
street: String,
|
|
|
|
city: String,
|
|
|
|
zipCode: String,
|
|
|
|
state: String
|
|
|
|
});
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
|
|
|
name: String,
|
|
|
|
bio: ModelBuilder.Text,
|
|
|
|
approved: Boolean,
|
|
|
|
joinedAt: Date,
|
|
|
|
age: Number,
|
|
|
|
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();
|
|
|
|
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
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should be able to define referencing models by name', function (done) {
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
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();
|
|
|
|
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");
|
|
|
|
|
|
|
|
assert.equal(json.properties.address.type, 'Address');
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report correct id names', function (done) {
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
|
|
|
userId: {type: String, id: true},
|
|
|
|
name: "string",
|
|
|
|
bio: ModelBuilder.Text,
|
|
|
|
approved: Boolean,
|
|
|
|
joinedAt: Date,
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should report correct table/column names', function (done) {
|
|
|
|
var modelBuilder = new ModelBuilder();
|
|
|
|
|
|
|
|
var User = new ModelDefinition(modelBuilder, 'User', {
|
|
|
|
userId: {type: String, id: true, oracle: {column: 'ID'}},
|
|
|
|
name: "string"
|
|
|
|
}, {oracle: {table: 'USER'}});
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should inherit prototype using option.base', function () {
|
|
|
|
var memory = new DataSource({connector: Memory});
|
|
|
|
var modelBuilder = memory.modelBuilder;
|
|
|
|
var parent = memory.createModel('parent', {}, {
|
|
|
|
relations: {
|
|
|
|
children: {
|
|
|
|
type: 'hasMany',
|
|
|
|
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-04-11 18:39:57 +00:00
|
|
|
it('should not serialize hidden properties into JSON', function () {
|
|
|
|
var memory = new DataSource({connector: Memory});
|
|
|
|
var modelBuilder = memory.modelBuilder;
|
|
|
|
var HiddenModel = memory.createModel('hidden', {}, {
|
|
|
|
hidden: ['secret']
|
|
|
|
});
|
|
|
|
var hm = new HiddenModel({
|
|
|
|
id: 1,
|
|
|
|
foo: 'bar',
|
|
|
|
secret: 'secret'
|
|
|
|
});
|
|
|
|
var serialized = hm.toJSON();
|
|
|
|
assert.deepEqual(serialized, {
|
|
|
|
id: 1,
|
|
|
|
foo: 'bar'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not serialize hidden properties of nested models into JSON', function (done) {
|
|
|
|
var memory = new DataSource({connector: Memory});
|
|
|
|
var modelBuilder = memory.modelBuilder;
|
|
|
|
var Parent = memory.createModel('parent');
|
|
|
|
var Child = memory.createModel('child', {}, {hidden: ['secret']});
|
|
|
|
Parent.hasMany(Child);
|
|
|
|
Parent.create({
|
|
|
|
name: 'parent'
|
|
|
|
}, function(err, parent) {
|
|
|
|
parent.children.create({
|
|
|
|
name: 'child',
|
|
|
|
secret: 'secret'
|
|
|
|
}, function(err, child) {
|
|
|
|
Parent.find({include: 'children'}, function(err, parents) {
|
|
|
|
var serialized = parents[0].toJSON();
|
|
|
|
var child = serialized.children[0];
|
|
|
|
assert.equal(child.name, 'child');
|
|
|
|
assert.notEqual(child.secret, 'secret');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-10-02 05:14:21 +00:00
|
|
|
});
|
|
|
|
|