From 7f170a0542d5e000b5bc2691f79018546e6bf01b Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 4 Oct 2013 21:21:12 -0700 Subject: [PATCH] Add more tests and fix toJSON --- lib/model-definition.js | 36 +++++----- test/model-definition.test.js | 121 ++++++++++++++++++++++++++++++++-- 2 files changed, 129 insertions(+), 28 deletions(-) diff --git a/lib/model-definition.js b/lib/model-definition.js index 3ec785b1..bb45fdf1 100644 --- a/lib/model-definition.js +++ b/lib/model-definition.js @@ -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; } diff --git a/test/model-definition.test.js b/test/model-definition.test.js index e907f1d9..ea9cfc8e 100644 --- a/test/model-definition.test.js +++ b/test/model-definition.test.js @@ -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(); + }); + });