diff --git a/test/mysql.discover.test.js b/test/mysql.discover.test.js index c5686b7..0750b9d 100644 --- a/test/mysql.discover.test.js +++ b/test/mysql.discover.test.js @@ -211,55 +211,67 @@ describe('Discover model foreign keys', function () { }); describe('Discover LDL schema from a table', function () { - it('should return an LDL schema for INVENTORY', function (done) { - db.discoverSchema('INVENTORY', {owner: 'STRONGLOOP'}, function (err, schema) { - var productId = 'productId' in schema.properties ? 'productId' : 'productid'; - var locationId = 'locationId' in schema.properties ? 'locationId' : 'locationid'; - console.error('schema:', schema); - assert.strictEqual(schema.name, 'Inventory'); - assert.ok(/STRONGLOOP/i.test(schema.options.mysql.schema)); - assert.strictEqual(schema.options.mysql.table, 'INVENTORY'); - assert(schema.properties[productId]); - // TODO: schema shows this field is default NULL, which means it isn't required - // assert(schema.properties[productId].required); - assert.strictEqual(schema.properties[productId].type, 'String'); - assert.strictEqual(schema.properties[productId].mysql.columnName, 'productId'); - assert(schema.properties[locationId]); - assert.strictEqual(schema.properties[locationId].type, 'String'); - assert.strictEqual(schema.properties[locationId].mysql.columnName, 'locationId'); - assert(schema.properties.available); - assert.strictEqual(schema.properties.available.required, false); - assert.strictEqual(schema.properties.available.type, 'Number'); - assert(schema.properties.total); - assert.strictEqual(schema.properties.total.type, 'Number'); - done(null, schema); + var schema; + before(function (done) { + db.discoverSchema('INVENTORY', {owner: 'STRONGLOOP'}, function (err, schema_) { + schema = schema_; + done(err); }); }); + it('should return an LDL schema for INVENTORY', function () { + var productId = 'productId' in schema.properties ? 'productId' : 'productid'; + var locationId = 'locationId' in schema.properties ? 'locationId' : 'locationid'; + console.error('schema:', schema); + assert.strictEqual(schema.name, 'Inventory'); + assert.ok(/STRONGLOOP/i.test(schema.options.mysql.schema)); + assert.strictEqual(schema.options.mysql.table, 'INVENTORY'); + assert(schema.properties[productId]); + // TODO: schema shows this field is default NULL, which means it isn't required + // assert(schema.properties[productId].required); + assert.strictEqual(schema.properties[productId].type, 'String'); + assert.strictEqual(schema.properties[productId].mysql.columnName, 'productId'); + assert(schema.properties[locationId]); + assert.strictEqual(schema.properties[locationId].type, 'String'); + assert.strictEqual(schema.properties[locationId].mysql.columnName, 'locationId'); + assert(schema.properties.available); + assert.strictEqual(schema.properties.available.required, false); + assert.strictEqual(schema.properties.available.type, 'Number'); + assert(schema.properties.total); + assert.strictEqual(schema.properties.total.type, 'Number'); + }); }); describe('Discover and build models', function () { - it('should discover and build models', function (done) { - db.discoverAndBuildModels('INVENTORY', {owner: 'STRONGLOOP', visited: {}, associations: true}, function (err, models) { - assert(models.Inventory, 'Inventory model should be discovered and built'); - var schema = models.Inventory.definition; - var productId = 'productId' in schema.properties ? 'productId' : 'productid'; - var locationId = 'locationId' in schema.properties ? 'locationId' : 'locationid'; - assert(/STRONGLOOP/i.test(schema.settings.mysql.schema)); - assert.strictEqual(schema.settings.mysql.table, 'INVENTORY'); - assert(schema.properties[productId]); - assert.strictEqual(schema.properties[productId].type, String); - assert.strictEqual(schema.properties[productId].mysql.columnName, 'productId'); - assert(schema.properties[locationId]); - assert.strictEqual(schema.properties[locationId].type, String); - assert.strictEqual(schema.properties[locationId].mysql.columnName, 'locationId'); - assert(schema.properties.available); - assert.strictEqual(schema.properties.available.type, Number); - assert(schema.properties.total); - assert.strictEqual(schema.properties.total.type, Number); - models.Inventory.findOne(function (err, inv) { - assert(!err, 'error should not be reported'); - done(); - }); + var models; + before(function (done) { + db.discoverAndBuildModels('INVENTORY', {owner: 'STRONGLOOP', visited: {}, associations: true}, function (err, models_) { + models = models_; + done(err); + }); + }); + it('should discover and build models', function () { + assert(models.Inventory, 'Inventory model should be discovered and built'); + var schema = models.Inventory.definition; + var productId = 'productId' in schema.properties ? 'productId' : 'productid'; + var locationId = 'locationId' in schema.properties ? 'locationId' : 'locationid'; + assert(/STRONGLOOP/i.test(schema.settings.mysql.schema)); + assert.strictEqual(schema.settings.mysql.table, 'INVENTORY'); + assert(schema.properties[productId]); + assert.strictEqual(schema.properties[productId].type, String); + assert.strictEqual(schema.properties[productId].mysql.columnName, 'productId'); + assert(schema.properties[locationId]); + assert.strictEqual(schema.properties[locationId].type, String); + assert.strictEqual(schema.properties[locationId].mysql.columnName, 'locationId'); + assert(schema.properties.available); + assert.strictEqual(schema.properties.available.type, Number); + assert(schema.properties.total); + assert.strictEqual(schema.properties.total.type, Number); + }); + it('should be able to find an instance', function (done) { + assert(models.Inventory, 'Inventory model must exist'); + models.Inventory.findOne(function (err, inv) { + assert(!err, 'error should not be reported'); + done(); }); }); });