Add more tests and fix toJSON
This commit is contained in:
parent
2a15bb917a
commit
7f170a0542
|
@ -56,7 +56,7 @@ require('./types')(ModelDefinition);
|
|||
ModelDefinition.prototype.tableName = function (connectorType) {
|
||||
var settings = this.settings;
|
||||
if(settings[connectorType]) {
|
||||
return settings[connectorType].table || this.name;
|
||||
return settings[connectorType].table || settings[connectorType].tableName || this.name;
|
||||
} else {
|
||||
return this.name;
|
||||
}
|
||||
|
@ -75,7 +75,7 @@ ModelDefinition.prototype.columnName = function (connectorType, propertyName) {
|
|||
this.build();
|
||||
var property = this.properties[propertyName];
|
||||
if(property && property[connectorType]) {
|
||||
return property[connectorType].columnName || propertyName;
|
||||
return property[connectorType].column || property[connectorType].columnName || propertyName;
|
||||
} else {
|
||||
return propertyName;
|
||||
}
|
||||
|
@ -111,7 +111,7 @@ ModelDefinition.prototype.columnNames = function (connectorType) {
|
|||
var cols = [];
|
||||
for(var p in props) {
|
||||
if(props[p][connectorType]) {
|
||||
cols.push(props[p][connectorType].columnName || p);
|
||||
cols.push(property[connectorType].column || props[p][connectorType].columnName || p);
|
||||
} else {
|
||||
cols.push(p);
|
||||
}
|
||||
|
@ -231,11 +231,7 @@ ModelDefinition.prototype.resolveType = function(type) {
|
|||
if (type.type) {
|
||||
return this.resolveType(type.type);
|
||||
} else {
|
||||
if(!this.anonymousTypesCount) {
|
||||
this.anonymousTypesCount = 0;
|
||||
}
|
||||
this.anonymousTypesCount++;
|
||||
return this.modelBuilder.define('AnonymousType' + this.anonymousTypesCount,
|
||||
return this.modelBuilder.define(this.modelBuilder.getSchemaName(null),
|
||||
type, {anonymous: true, idInjection: false});
|
||||
/*
|
||||
console.error(type);
|
||||
|
@ -278,10 +274,13 @@ ModelDefinition.prototype.build = function (forceRebuild) {
|
|||
var typeDef = {
|
||||
type: type
|
||||
};
|
||||
for (var a in this.rawProperties[p]) {
|
||||
// Skip the type property but don't delete it Model.extend() shares same instances of the properties from the base class
|
||||
if (a !== 'type') {
|
||||
typeDef[a] = this.rawProperties[p][a];
|
||||
var prop = this.rawProperties[p];
|
||||
if (typeof prop === 'object' && prop !== null) {
|
||||
for (var a in prop) {
|
||||
// Skip the type property but don't delete it Model.extend() shares same instances of the properties from the base class
|
||||
if (a !== 'type') {
|
||||
typeDef[a] = prop[a];
|
||||
}
|
||||
}
|
||||
}
|
||||
this.properties[p] = typeDef;
|
||||
|
@ -302,15 +301,10 @@ ModelDefinition.prototype.defineProperty = function (propertyName, propertyDefin
|
|||
|
||||
|
||||
function isModelClass(cls) {
|
||||
while (true) {
|
||||
if (!cls) {
|
||||
return false;
|
||||
}
|
||||
if (ModelBaseClass === cls) {
|
||||
return true;
|
||||
}
|
||||
cls = cls.prototype;
|
||||
if(!cls) {
|
||||
return false;
|
||||
}
|
||||
return cls.prototype instanceof ModelBaseClass;
|
||||
}
|
||||
|
||||
ModelDefinition.prototype.toJSON = function(forceRebuild) {
|
||||
|
@ -338,7 +332,7 @@ ModelDefinition.prototype.toJSON = function(forceRebuild) {
|
|||
if('function' === typeof val) {
|
||||
if(isModelClass(val)) {
|
||||
if(val.settings && val.settings.anonymous) {
|
||||
return val.definition && val.definition.toJSON();
|
||||
return val.definition && val.definition.toJSON().properties;
|
||||
} else {
|
||||
return val.modelName;
|
||||
}
|
||||
|
|
|
@ -14,15 +14,55 @@ describe('ModelDefinition class', function () {
|
|||
var modelBuilder = new ModelBuilder();
|
||||
|
||||
var User = new ModelDefinition(modelBuilder, 'User', {
|
||||
name: String,
|
||||
name: "string",
|
||||
bio: ModelBuilder.Text,
|
||||
approved: Boolean,
|
||||
joinedAt: Date,
|
||||
age: Number
|
||||
age: "number"
|
||||
});
|
||||
|
||||
// console.log(User.toJSON());
|
||||
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");
|
||||
|
||||
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"
|
||||
});
|
||||
|
||||
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();
|
||||
|
||||
|
||||
|
@ -46,11 +86,29 @@ describe('ModelDefinition class', function () {
|
|||
}
|
||||
});
|
||||
|
||||
// console.log(JSON.stringify(User.toJSON(), null, ' '));
|
||||
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();
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
@ -73,11 +131,60 @@ describe('ModelDefinition class', function () {
|
|||
|
||||
});
|
||||
|
||||
// console.log(JSON.stringify(User.toJSON(), null, ' '));
|
||||
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"
|
||||
});
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue