Fix transaction

This commit is contained in:
Diana Lau 2017-07-13 22:35:01 -04:00
parent ffa776cf6a
commit 5a00628526
2 changed files with 48 additions and 1 deletions

View File

@ -85,7 +85,13 @@ Transaction.begin = function(connector, options, cb) {
return cb(err); return cb(err);
} }
var tx = connection; var tx = connection;
if (!(connection instanceof Transaction)) {
// When the connector and juggler node module have different version of this module as a dependency,
// the transaction is not an instanceof Transaction.
// i.e. (connection instanceof Transaction) == false
// Check for existence of required functions and properties, instead of prototype inheritance.
if (connection.connector == undefined || connection.connection == undefined ||
connection.commit == undefined || connection.rollback == undefined) {
tx = new Transaction(connector, connection); tx = new Transaction(connector, connection);
} }
cb(err, tx); cb(err, tx);

View File

@ -194,4 +194,45 @@ describe('transactions', function() {
}); });
}); });
}); });
describe('transaction instance', function() {
function TestTransaction(connector, connection) {
this.connector = connector;
this.connection = connection;
}
Object.assign(TestTransaction.prototype, Transaction.prototype);
TestTransaction.prototype.foo = true;
function beginTransaction(isolationLevel, cb) {
return cb(null, new TestTransaction(testConnector, {}));
};
it('should do nothing when transaction is like a Transaction', function(done) {
testConnector.initialize(db, function(err, resultConnector) {
resultConnector.beginTransaction = beginTransaction;
Transaction.begin(resultConnector, Transaction.READ_COMMITTED,
function(err, result) {
if (err) done(err);
expect(result).to.be.instanceof(TestTransaction);
expect(result.foo).to.equal(true);
done();
});
});
});
it('should create new instance when transaction is not like a Transaction',
function(done) {
testConnector.initialize(db, function(err, resultConnector) {
resultConnector.beginTransaction = beginTransaction;
delete TestTransaction.prototype.commit;
Transaction.begin(resultConnector, Transaction.READ_COMMITTED,
function(err, result) {
if (err) done(err);
expect(result).to.not.be.instanceof(TestTransaction);
expect(result).to.be.instanceof(Transaction);
expect(result.foo).to.equal(undefined);
done();
});
});
});
});
}); });