Tidy up tests so that they will work with RDBs

This commit is contained in:
Raymond Feng 2015-02-20 16:10:25 -08:00
parent e659e2f603
commit 841f2d8f14
3 changed files with 60 additions and 31 deletions

View File

@ -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();
});
});

View File

@ -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)

View File

@ -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
});