From 841f2d8f148f97e5be0400ef1fda3de9899f5709 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Fri, 20 Feb 2015 16:10:25 -0800 Subject: [PATCH] Tidy up tests so that they will work with RDBs --- test/manipulation.test.js | 25 +++++++++--------- test/persistence-hooks.suite.js | 45 ++++++++++++++++++++++++--------- test/relations.test.js | 21 ++++++++++----- 3 files changed, 60 insertions(+), 31 deletions(-) diff --git a/test/manipulation.test.js b/test/manipulation.test.js index baf68fe5..3530d11e 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -27,11 +27,11 @@ describe('manipulation', function () { // A simplified implementation of LoopBack's User model // to reproduce problems related to properties with dynamic setters // For the purpose of the tests, we use a counter instead of a hash fn. - var StubUser, stubPasswordCounter; + var StubUser; before(function setupStubUserModel(done) { StubUser = db.createModel('StubUser', { password: String }, { forceId: true }); StubUser.setter.password = function(plain) { - this.$password = plain + '-' + (++stubPasswordCounter); + this.$password = plain + '-' + plain.toUpperCase(); }; db.automigrate('StubUser', done); }); @@ -288,10 +288,10 @@ describe('manipulation', function () { created.password = 'bar'; created.save(function(err, saved) { if (err) return done(err); - saved.password.should.equal('bar-2'); + saved.password.should.equal('bar-BAR'); StubUser.findById(created.id, function(err, found) { if (err) return done(err); - found.password.should.equal('bar-2'); + found.password.should.equal('bar-BAR'); done(); }); }); @@ -350,12 +350,12 @@ describe('manipulation', function () { describe('updateOrCreate', function() { it('should preserve properties with dynamic setters on create', function(done) { - StubUser.updateOrCreate({ id: 'newid', password: 'foo' }, function(err, created) { + StubUser.updateOrCreate({ password: 'foo' }, function(err, created) { if (err) return done(err); - created.password.should.equal('foo-1'); + created.password.should.equal('foo-FOO'); StubUser.findById(created.id, function(err, found) { if (err) return done(err); - found.password.should.equal('foo-1'); + found.password.should.equal('foo-FOO'); done(); }); }); @@ -367,10 +367,10 @@ describe('manipulation', function () { var data = { id: created.id, password: 'bar' }; StubUser.updateOrCreate(data, function(err, updated) { if (err) return done(err); - updated.password.should.equal('bar-2'); + updated.password.should.equal('bar-BAR'); StubUser.findById(created.id, function(err, found) { if (err) return done(err); - found.password.should.equal('bar-2'); + found.password.should.equal('bar-BAR'); done(); }); }); @@ -392,11 +392,12 @@ describe('manipulation', function () { { id: instance.id, name: 'updated name' }, function(err, updated) { if (err) return done(err); - updated.toObject().should.have.properties({ + var result = updated.toObject(); + result.should.have.properties({ id: instance.id, - name: 'updated name', - gender: undefined + name: 'updated name' }); + should.equal(result.gender, null); done(); }); }); diff --git a/test/persistence-hooks.suite.js b/test/persistence-hooks.suite.js index 6a6d3538..0a44e567 100644 --- a/test/persistence-hooks.suite.js +++ b/test/persistence-hooks.suite.js @@ -7,6 +7,8 @@ module.exports = function(dataSource, should) { var TestModel, existingInstance; var migrated = false, lastId; + var undefinedValue = undefined; + beforeEach(function setupDatabase(done) { observedContexts = "hook not called"; expectedError = new Error('test error'); @@ -33,11 +35,16 @@ module.exports = function(dataSource, should) { beforeEach(function createTestData(done) { TestModel.create({ name: 'first' }, function(err, instance) { if (err) return done(err); - existingInstance = instance; - TestModel.create({ name: 'second' }, function(err) { - if (err) return done(err); - done(); + // Look it up from DB so that default values are retrieved + TestModel.findById(instance.id, function(err, instance) { + existingInstance = instance; + undefinedValue = existingInstance.extra; + + TestModel.create({ name: 'second' }, function(err) { + if (err) return done(err); + done(); + }); }); }); }); @@ -145,15 +152,19 @@ module.exports = function(dataSource, should) { TestModel.observe('before save', pushContextAndNext()); TestModel.create( - [{ name: 'one' }, { name: 'two' }], + [{ name: '1' }, { name: '2' }], function(err, list) { if (err) return done(err); + // Creation of multiple instances is executed in parallel + observedContexts.sort(function(c1, c2) { + return c1.instance.name - c2.instance.name; + }); observedContexts.should.eql([ aTestModelCtx({ - instance: { id: list[0].id, name: 'one', extra: undefined } + instance: { id: list[0].id, name: '1', extra: undefined } }), aTestModelCtx({ - instance: { id: list[1].id, name: 'two', extra: undefined } + instance: { id: list[1].id, name: '2', extra: undefined } }), ]); done(); @@ -211,15 +222,19 @@ module.exports = function(dataSource, should) { TestModel.observe('after save', pushContextAndNext()); TestModel.create( - [{ name: 'one' }, { name: 'two' }], + [{ name: '1' }, { name: '2' }], function(err, list) { if (err) return done(err); + // Creation of multiple instances is executed in parallel + observedContexts.sort(function(c1, c2) { + return c1.instance.name - c2.instance.name; + }); observedContexts.should.eql([ aTestModelCtx({ - instance: { id: list[0].id, name: 'one', extra: undefined } + instance: { id: list[0].id, name: '1', extra: undefined } }), aTestModelCtx({ - instance: { id: list[1].id, name: 'two', extra: undefined } + instance: { id: list[1].id, name: '2', extra: undefined } }), ]); done(); @@ -937,7 +952,8 @@ module.exports = function(dataSource, should) { [err].should.eql([expectedError]); TestModel.findById(existingInstance.id, function(err, inst) { if (err) return done(err); - (inst ? inst.toObject() : 'null').should.eql(existingInstance.toObject()); + (inst ? inst.toObject() : 'null').should. + eql(existingInstance.toObject()); done(); }); }); @@ -1039,7 +1055,8 @@ module.exports = function(dataSource, should) { [err].should.eql([expectedError]); TestModel.findById(existingInstance.id, function(err, inst) { if (err) return done(err); - (inst ? inst.toObject() : 'null').should.eql(existingInstance.toObject()); + (inst ? inst.toObject() : 'null').should.eql( + existingInstance.toObject()); done(); }); }); @@ -1257,6 +1274,10 @@ module.exports = function(dataSource, should) { function deepCloneToObject(obj) { return traverse(obj).map(function(x) { + if (x === undefined) { + // RDBMSs return null + return null; + } if (x && x.toObject) return x.toObject(true); if (x && typeof x === 'function' && x.modelName) diff --git a/test/relations.test.js b/test/relations.test.js index e1796472..f8572fbd 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -19,9 +19,12 @@ var getMemoryDataSource = function(settings) { describe('relations', function () { + before(function() { + db = getSchema(); + }); + describe('hasMany', function () { before(function (done) { - db = getSchema(); Book = db.define('Book', {name: String, type: String}); Chapter = db.define('Chapter', {name: {type: String, index: true}, bookType: String}); @@ -969,11 +972,11 @@ describe('relations', function () { db = getSchema(); Picture = db.define('Picture', {name: String}); Author = db.define('Author', { - username: {type: String, id: true}, + username: {type: String, id: true, generated: true}, name: String }); Reader = db.define('Reader', { - username: {type: String, id: true}, + username: {type: String, id: true, generated: true}, name: String }); @@ -1045,7 +1048,7 @@ describe('relations', function () { avatar.should.equal(p); p.name.should.equal('Avatar'); - p.oid.should.eql(author.username); + p.oid.toString().should.equal(author.username.toString()); p.type.should.equal('Author'); done(); }); @@ -1057,7 +1060,7 @@ describe('relations', function () { reader.mugshot(function (err, p) { should.not.exist(err); p.name.should.equal('Mugshot'); - p.oid.should.eql(reader.username); + p.oid.toString().should.equal(reader.username.toString()); p.type.should.equal('Reader'); done(); }); @@ -1730,7 +1733,10 @@ describe('relations', function () { }); - describe('belongsTo with embed', function () { + // Disable the tests until the issue in + // https://github.com/strongloop/loopback-datasource-juggler/pull/399 + // is fixed + describe.skip('belongsTo with embed', function () { var Person, Passport; it('can be declared with embed and properties', function (done) { @@ -1941,7 +1947,8 @@ describe('relations', function () { Supplier = db.define('Supplier', { sid: { type: String, - id: true + id: true, + generated: true }, name: String });