不要怂,就是干,撸起袖子干!

Commit 0f4a7b42 by Mick Hansen

Merge branch 'master' of github.com:sequelize/sequelize

2 parents d0453674 38d1887b
...@@ -32,6 +32,7 @@ AbstractDialect.prototype.supports = { ...@@ -32,6 +32,7 @@ AbstractDialect.prototype.supports = {
/* What is the dialect's keyword for INSERT IGNORE */ /* What is the dialect's keyword for INSERT IGNORE */
'IGNORE': '', 'IGNORE': '',
schemas: false, schemas: false,
transactions: true,
constraints: { constraints: {
restrict: true restrict: true
}, },
......
...@@ -25,26 +25,29 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -25,26 +25,29 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
}) })
describe('getAssociation', function() { describe('getAssociation', function() {
it('supports transactions', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING })
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING })
Group.belongsTo(User) if (current.dialect.supports.transactions) {
it('supports transactions', function(done) {
sequelize.sync({ force: true }).success(function() { Support.prepareTransactionTest(this.sequelize, function(sequelize) {
User.create({ username: 'foo' }).success(function(user) { var User = sequelize.define('User', { username: Support.Sequelize.STRING })
Group.create({ name: 'bar' }).success(function(group) { , Group = sequelize.define('Group', { name: Support.Sequelize.STRING })
sequelize.transaction().then(function(t) {
group.setUser(user, { transaction: t }).success(function() { Group.belongsTo(User)
Group.all().success(function(groups) {
groups[0].getUser().success(function(associatedUser) { sequelize.sync({ force: true }).success(function() {
expect(associatedUser).to.be.null User.create({ username: 'foo' }).success(function(user) {
Group.all({ transaction: t }).success(function(groups) { Group.create({ name: 'bar' }).success(function(group) {
groups[0].getUser({ transaction: t }).success(function(associatedUser) { sequelize.transaction().then(function(t) {
expect(associatedUser).to.be.not.null group.setUser(user, { transaction: t }).success(function() {
t.rollback().success(function() { Group.all().success(function(groups) {
done() groups[0].getUser().success(function(associatedUser) {
expect(associatedUser).to.be.null
Group.all({ transaction: t }).success(function(groups) {
groups[0].getUser({ transaction: t }).success(function(associatedUser) {
expect(associatedUser).to.be.not.null
t.rollback().success(function() {
done()
})
}) })
}) })
}) })
...@@ -56,7 +59,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -56,7 +59,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
}) })
}) })
}) })
}) }
it('does not modify the passed arguments', function () { it('does not modify the passed arguments', function () {
var User = this.sequelize.define('user', {}) var User = this.sequelize.define('user', {})
...@@ -126,22 +129,25 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -126,22 +129,25 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
}) })
describe('setAssociation', function() { describe('setAssociation', function() {
it('supports transactions', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING })
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING })
Group.belongsTo(User) if (current.dialect.supports.transactions) {
it('supports transactions', function(done) {
sequelize.sync({ force: true }).success(function() { Support.prepareTransactionTest(this.sequelize, function(sequelize) {
User.create({ username: 'foo' }).success(function(user) { var User = sequelize.define('User', { username: Support.Sequelize.STRING })
Group.create({ name: 'bar' }).success(function(group) { , Group = sequelize.define('Group', { name: Support.Sequelize.STRING })
sequelize.transaction().then(function(t) {
group.setUser(user, { transaction: t }).success(function() { Group.belongsTo(User)
Group.all().success(function(groups) {
groups[0].getUser().success(function(associatedUser) { sequelize.sync({ force: true }).success(function() {
expect(associatedUser).to.be.null User.create({ username: 'foo' }).success(function(user) {
t.rollback().success(function() { done() }) Group.create({ name: 'bar' }).success(function(group) {
sequelize.transaction().then(function(t) {
group.setUser(user, { transaction: t }).success(function() {
Group.all().success(function(groups) {
groups[0].getUser().success(function(associatedUser) {
expect(associatedUser).to.be.null
t.rollback().success(function() { done() })
})
}) })
}) })
}) })
...@@ -150,7 +156,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -150,7 +156,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
}) })
}) })
}) })
}) }
it('can set the association with declared primary keys...', function(done) { it('can set the association with declared primary keys...', function(done) {
var User = this.sequelize.define('UserXYZ', { user_id: {type: DataTypes.INTEGER, primaryKey: true }, username: DataTypes.STRING }) var User = this.sequelize.define('UserXYZ', { user_id: {type: DataTypes.INTEGER, primaryKey: true }, username: DataTypes.STRING })
...@@ -277,24 +283,26 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -277,24 +283,26 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
}) })
}) })
it('supports transactions', function(done) { if (current.dialect.supports.transactions) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { it('supports transactions', function(done) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING }) Support.prepareTransactionTest(this.sequelize, function(sequelize) {
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING }) var User = sequelize.define('User', { username: Support.Sequelize.STRING })
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING })
Group.belongsTo(User) Group.belongsTo(User)
sequelize.sync({ force: true }).success(function() { sequelize.sync({ force: true }).success(function() {
Group.create({ name: 'bar' }).success(function(group) { Group.create({ name: 'bar' }).success(function(group) {
sequelize.transaction().then(function(t) { sequelize.transaction().then(function(t) {
group.createUser({ username: 'foo' }, { transaction: t }).success(function() { group.createUser({ username: 'foo' }, { transaction: t }).success(function() {
group.getUser().success(function(user) { group.getUser().success(function(user) {
expect(user).to.be.null expect(user).to.be.null
group.getUser({ transaction: t }).success(function(user) { group.getUser({ transaction: t }).success(function(user) {
expect(user).not.to.be.null expect(user).not.to.be.null
t.rollback().success(function() { done() }) t.rollback().success(function() { done() })
})
}) })
}) })
}) })
...@@ -302,7 +310,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -302,7 +310,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
}) })
}) })
}) })
}) }
}) })
describe("foreign key", function () { describe("foreign key", function () {
......
...@@ -23,28 +23,30 @@ describe(Support.getTestDialectTeaser("HasOne"), function() { ...@@ -23,28 +23,30 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
}) })
describe('getAssocation', function() { describe('getAssocation', function() {
it('supports transactions', function(done) { if (current.dialect.supports.transactions) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { it('supports transactions', function(done) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING }) Support.prepareTransactionTest(this.sequelize, function(sequelize) {
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING }) var User = sequelize.define('User', { username: Support.Sequelize.STRING })
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING })
Group.hasOne(User)
Group.hasOne(User)
sequelize.sync({ force: true }).success(function() {
User.create({ username: 'foo' }).success(function(fakeUser) { sequelize.sync({ force: true }).success(function() {
User.create({ username: 'foo' }).success(function(user) { User.create({ username: 'foo' }).success(function(fakeUser) {
Group.create({ name: 'bar' }).success(function(group) { User.create({ username: 'foo' }).success(function(user) {
sequelize.transaction().then(function(t) { Group.create({ name: 'bar' }).success(function(group) {
group.setUser(user, { transaction: t }).success(function() { sequelize.transaction().then(function(t) {
Group.all().success(function(groups) { group.setUser(user, { transaction: t }).success(function() {
groups[0].getUser().success(function(associatedUser) { Group.all().success(function(groups) {
expect(associatedUser).to.be.null groups[0].getUser().success(function(associatedUser) {
Group.all({ transaction: t }).success(function(groups) { expect(associatedUser).to.be.null
groups[0].getUser({ transaction: t }).success(function(associatedUser) { Group.all({ transaction: t }).success(function(groups) {
expect(associatedUser).not.to.be.null groups[0].getUser({ transaction: t }).success(function(associatedUser) {
expect(associatedUser.id).to.equal(user.id) expect(associatedUser).not.to.be.null
expect(associatedUser.id).not.to.equal(fakeUser.id) expect(associatedUser.id).to.equal(user.id)
t.rollback().success(function() { done() }) expect(associatedUser.id).not.to.equal(fakeUser.id)
t.rollback().success(function() { done() })
})
}) })
}) })
}) })
...@@ -56,7 +58,7 @@ describe(Support.getTestDialectTeaser("HasOne"), function() { ...@@ -56,7 +58,7 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
}) })
}) })
}) })
}) }
it('does not modify the passed arguments', function () { it('does not modify the passed arguments', function () {
var User = this.sequelize.define('user', {}) var User = this.sequelize.define('user', {})
...@@ -98,36 +100,38 @@ describe(Support.getTestDialectTeaser("HasOne"), function() { ...@@ -98,36 +100,38 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
}) })
describe('setAssociation', function() { describe('setAssociation', function() {
it('supports transactions', function(done) { if (current.dialect.supports.transactions) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { it('supports transactions', function(done) {
var User = sequelize.define('User', { username: Support.Sequelize.STRING }) Support.prepareTransactionTest(this.sequelize, function(sequelize) {
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING }) var User = sequelize.define('User', { username: Support.Sequelize.STRING })
, Group = sequelize.define('Group', { name: Support.Sequelize.STRING })
Group.hasOne(User) Group.hasOne(User)
sequelize.sync({ force: true }).success(function() { sequelize.sync({ force: true }).success(function() {
User.create({ username: 'foo' }).success(function(user) { User.create({ username: 'foo' }).success(function(user) {
Group.create({ name: 'bar' }).success(function(group) { Group.create({ name: 'bar' }).success(function(group) {
sequelize.transaction().then(function(t) { sequelize.transaction().then(function(t) {
group group
.setUser(user, { transaction: t }) .setUser(user, { transaction: t })
.success(function() { .success(function() {
Group.all().success(function(groups) { Group.all().success(function(groups) {
groups[0].getUser().success(function(associatedUser) { groups[0].getUser().success(function(associatedUser) {
expect(associatedUser).to.be.null expect(associatedUser).to.be.null
t.rollback().success(function() { done() }) t.rollback().success(function() { done() })
})
}) })
}) })
}) .on('sql', function(sql, uuid) {
.on('sql', function(sql, uuid) { expect(uuid).to.not.equal('default')
expect(uuid).to.not.equal('default') })
}) })
}) })
}) })
}) })
}) })
}) })
}) }
it('can set an association with predefined primary keys', function(done) { it('can set an association with predefined primary keys', function(done) {
var User = this.sequelize.define('UserXYZZ', { userCoolIdTag: { type: Sequelize.INTEGER, primaryKey: true }, username: Sequelize.STRING }) var User = this.sequelize.define('UserXYZZ', { userCoolIdTag: { type: Sequelize.INTEGER, primaryKey: true }, username: Sequelize.STRING })
...@@ -227,24 +231,26 @@ describe(Support.getTestDialectTeaser("HasOne"), function() { ...@@ -227,24 +231,26 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
}) })
}) })
it('supports transactions', function(done) { if (current.dialect.supports.transactions) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { it('supports transactions', function(done) {
var User = sequelize.define('User', { username: Sequelize.STRING }) Support.prepareTransactionTest(this.sequelize, function(sequelize) {
, Group = sequelize.define('Group', { name: Sequelize.STRING }) var User = sequelize.define('User', { username: Sequelize.STRING })
, Group = sequelize.define('Group', { name: Sequelize.STRING })
User.hasOne(Group)
User.hasOne(Group)
sequelize.sync({ force: true }).success(function() {
User.create({ username: 'bob' }).success(function(user) { sequelize.sync({ force: true }).success(function() {
sequelize.transaction().then(function(t) { User.create({ username: 'bob' }).success(function(user) {
user.createGroup({ name: 'testgroup' }, { transaction: t }).success(function() { sequelize.transaction().then(function(t) {
User.all().success(function (users) { user.createGroup({ name: 'testgroup' }, { transaction: t }).success(function() {
users[0].getGroup().success(function (group) { User.all().success(function (users) {
expect(group).to.be.null; users[0].getGroup().success(function (group) {
User.all({ transaction: t }).success(function (users) { expect(group).to.be.null;
users[0].getGroup({ transaction: t }).success(function (group) { User.all({ transaction: t }).success(function (users) {
expect(group).to.be.not.null; users[0].getGroup({ transaction: t }).success(function (group) {
t.rollback().success(function() { done() }) expect(group).to.be.not.null;
t.rollback().success(function() { done() })
})
}) })
}) })
}) })
...@@ -254,7 +260,8 @@ describe(Support.getTestDialectTeaser("HasOne"), function() { ...@@ -254,7 +260,8 @@ describe(Support.getTestDialectTeaser("HasOne"), function() {
}) })
}) })
}) })
}) }
}) })
describe('foreign key', function () { describe('foreign key', function () {
......
...@@ -10,6 +10,8 @@ var chai = require('chai') ...@@ -10,6 +10,8 @@ var chai = require('chai')
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, _ = require('lodash') , _ = require('lodash')
, assert = require('assert') , assert = require('assert')
, current = Support.sequelize;
chai.use(datetime) chai.use(datetime)
chai.config.includeStack = true chai.config.includeStack = true
...@@ -41,35 +43,37 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -41,35 +43,37 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}); });
describe('findOrCreate', function () { describe('findOrCreate', function () {
it("supports transactions", function(done) { if (current.dialect.supports.transactions) {
var self = this; it("supports transactions", function(done) {
this.sequelize.transaction().then(function(t) { var self = this;
self.User.findOrCreate({ where: { username: 'Username' }, defaults: { data: 'some data' }}, { transaction: t }).then(function() { this.sequelize.transaction().then(function(t) {
self.User.count().success(function(count) { self.User.findOrCreate({ where: { username: 'Username' }, defaults: { data: 'some data' }}, { transaction: t }).then(function() {
expect(count).to.equal(0) self.User.count().success(function(count) {
t.commit().success(function() { expect(count).to.equal(0)
self.User.count().success(function(count) { t.commit().success(function() {
expect(count).to.equal(1) self.User.count().success(function(count) {
done() expect(count).to.equal(1)
done()
})
}) })
}) })
}) })
}) })
}) })
})
it("supports more than one models per transaction", function(done) { it("supports more than one models per transaction", function(done) {
var self = this; var self = this;
this.sequelize.transaction().then(function(t) { this.sequelize.transaction().then(function(t) {
self.User.findOrCreate({ where: { username: 'Username'}, defaults: { data: 'some data' }}, { transaction: t }).then(function() { self.User.findOrCreate({ where: { username: 'Username'}, defaults: { data: 'some data' }}, { transaction: t }).then(function() {
self.Account.findOrCreate({ where: { accountName: 'accountName'}}, { transaction: t}).then(function(){ self.Account.findOrCreate({ where: { accountName: 'accountName'}}, { transaction: t}).then(function(){
t.commit().success(function() { t.commit().success(function() {
done() done()
})
}) })
}) })
}) })
}) })
}) }
it("returns instance if already existent. Single find field.", function(done) { it("returns instance if already existent. Single find field.", function(done) {
var self = this, var self = this,
...@@ -137,55 +141,59 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -137,55 +141,59 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it("should release transaction when meeting errors", function(){ if (current.dialect.supports.transactions) {
var self = this it("should release transaction when meeting errors", function(){
var self = this
var test = function(times) { var test = function(times) {
if (times > 10) { if (times > 10) {
return true; return true;
}
return self.Student.findOrCreate({
where: {
no: 1
} }
}) return self.Student.findOrCreate({
.timeout(1000) where: {
.catch(Promise.TimeoutError,function(e){ no: 1
throw new Error(e) }
}) })
.catch(Sequelize.ValidationError,function(err){ .timeout(1000)
return test(times+1); .catch(Promise.TimeoutError,function(e){
}) throw new Error(e)
} })
.catch(Sequelize.ValidationError,function(err){
return test(times+1);
})
}
return test(0); return test(0);
}) })
}
describe('several concurrent calls', function () { describe('several concurrent calls', function () {
it('works with a transaction', function () { if (current.dialect.supports.transactions) {
return this.sequelize.transaction().bind(this).then(function (transaction) { it('works with a transaction', function () {
return Promise.join( return this.sequelize.transaction().bind(this).then(function (transaction) {
this.User.findOrCreate({ where: { uniqueName: 'winner' }}, { transaction: transaction }), return Promise.join(
this.User.findOrCreate({ where: { uniqueName: 'winner' }}, { transaction: transaction }), this.User.findOrCreate({ where: { uniqueName: 'winner' }}, { transaction: transaction }),
function (first, second) { this.User.findOrCreate({ where: { uniqueName: 'winner' }}, { transaction: transaction }),
var firstInstance = first[0] function (first, second) {
, firstCreated = first[1] var firstInstance = first[0]
, secondInstance = second[0] , firstCreated = first[1]
, secondCreated = second[1]; , secondInstance = second[0]
, secondCreated = second[1];
// Depending on execution order and MAGIC either the first OR the second call should return true
expect(firstCreated ? !secondCreated : secondCreated).to.be.ok // XOR // Depending on execution order and MAGIC either the first OR the second call should return true
expect(firstCreated ? !secondCreated : secondCreated).to.be.ok // XOR
expect(firstInstance).to.be.ok;
expect(secondInstance).to.be.ok; expect(firstInstance).to.be.ok;
expect(secondInstance).to.be.ok;
expect(firstInstance.id).to.equal(secondInstance.id);
expect(firstInstance.id).to.equal(secondInstance.id);
return transaction.commit();
} return transaction.commit();
); }
);
});
}); });
}); }
// Creating two concurrent transactions and selecting / inserting from the same table throws sqlite off // Creating two concurrent transactions and selecting / inserting from the same table throws sqlite off
(dialect !== 'sqlite' ? it : it.skip)('works without a transaction', function () { (dialect !== 'sqlite' ? it : it.skip)('works without a transaction', function () {
...@@ -287,22 +295,24 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -287,22 +295,24 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}); });
}); });
it('supports transactions', function(done) { if (current.dialect.supports.transactions) {
var self = this; it('supports transactions', function(done) {
this.sequelize.transaction().then(function(t) { var self = this;
self.User.create({ username: 'user' }, { transaction: t }).success(function() { this.sequelize.transaction().then(function(t) {
self.User.count().success(function(count) { self.User.create({ username: 'user' }, { transaction: t }).success(function() {
expect(count).to.equal(0) self.User.count().success(function(count) {
t.commit().success(function() { expect(count).to.equal(0)
self.User.count().success(function(count) { t.commit().success(function() {
expect(count).to.equal(1) self.User.count().success(function(count) {
done() expect(count).to.equal(1)
done()
})
}) })
}) })
}) })
}) })
}) })
}) }
it('is possible to use casting when creating an instance', function (done) { it('is possible to use casting when creating an instance', function (done) {
var self = this var self = this
...@@ -960,22 +970,24 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -960,22 +970,24 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
describe('bulkCreate', function() { describe('bulkCreate', function() {
it("supports transactions", function(done) { if (current.dialect.supports.transactions) {
var self = this; it("supports transactions", function(done) {
this.sequelize.transaction().then(function(t) { var self = this;
self.User this.sequelize.transaction().then(function(t) {
.bulkCreate([{ username: 'foo' }, { username: 'bar' }], { transaction: t }) self.User
.success(function() { .bulkCreate([{ username: 'foo' }, { username: 'bar' }], { transaction: t })
self.User.count().success(function(count1) { .success(function() {
self.User.count({ transaction: t }).success(function(count2) { self.User.count().success(function(count1) {
expect(count1).to.equal(0) self.User.count({ transaction: t }).success(function(count2) {
expect(count2).to.equal(2) expect(count1).to.equal(0)
t.rollback().success(function(){ done() }) expect(count2).to.equal(2)
t.rollback().success(function(){ done() })
})
}) })
}) })
}) })
}) })
}) }
it('properly handles disparate field lists', function(done) { it('properly handles disparate field lists', function(done) {
var self = this var self = this
......
...@@ -11,6 +11,8 @@ var chai = require('chai') ...@@ -11,6 +11,8 @@ var chai = require('chai')
, promised = require("chai-as-promised") , promised = require("chai-as-promised")
, _ = require('lodash') , _ = require('lodash')
, async = require('async') , async = require('async')
, current = Support.sequelize;
chai.use(promised); chai.use(promised);
chai.use(datetime) chai.use(datetime)
...@@ -33,24 +35,26 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -33,24 +35,26 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
describe('find', function() { describe('find', function() {
it('supports transactions', function(done) { if (current.dialect.supports.transactions) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { it('supports transactions', function(done) {
var User = sequelize.define('User', { username: Sequelize.STRING }) Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING })
User.sync({ force: true }).success(function() {
sequelize.transaction().then(function(t) { User.sync({ force: true }).success(function() {
User.create({ username: 'foo' }, { transaction: t }).success(function() { sequelize.transaction().then(function(t) {
User.find({ User.create({ username: 'foo' }, { transaction: t }).success(function() {
where: { username: 'foo' }
}).success(function(user1) {
User.find({ User.find({
where: { username: 'foo' }, where: { username: 'foo' }
}, { transaction: t }).success(function(user2) { }).success(function(user1) {
expect(user1).to.be.null User.find({
expect(user2).to.not.be.null where: { username: 'foo' },
}, { transaction: t }).success(function(user2) {
t.rollback().success(function() { expect(user1).to.be.null
done() expect(user2).to.not.be.null
t.rollback().success(function() {
done()
})
}) })
}) })
}) })
...@@ -58,7 +62,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -58,7 +62,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
}) })
}) }
describe('general / basic function', function() { describe('general / basic function', function() {
beforeEach(function(done) { beforeEach(function(done) {
......
...@@ -11,6 +11,8 @@ var chai = require('chai') ...@@ -11,6 +11,8 @@ var chai = require('chai')
, _ = require('lodash') , _ = require('lodash')
, moment = require('moment') , moment = require('moment')
, async = require('async') , async = require('async')
, current = Support.sequelize;
chai.use(datetime) chai.use(datetime)
chai.config.includeStack = true chai.config.includeStack = true
...@@ -33,22 +35,24 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -33,22 +35,24 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
describe('findAll', function() { describe('findAll', function() {
it('supports transactions', function(done) { if (current.dialect.supports.transactions) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { it('supports transactions', function(done) {
var User = sequelize.define('User', { username: Sequelize.STRING }) Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING })
User.sync({ force: true }).success(function() {
sequelize.transaction().then(function(t) { User.sync({ force: true }).success(function() {
User.create({ username: 'foo' }, { transaction: t }).success(function() { sequelize.transaction().then(function(t) {
User.findAll({ username: 'foo' }).success(function(users1) { User.create({ username: 'foo' }, { transaction: t }).success(function() {
User.findAll({ transaction: t }).success(function(users2) { User.findAll({ username: 'foo' }).success(function(users1) {
User.findAll({ username: 'foo' }, { transaction: t }).success(function(users3) { User.findAll({ transaction: t }).success(function(users2) {
expect(users1.length).to.equal(0) User.findAll({ username: 'foo' }, { transaction: t }).success(function(users3) {
expect(users2.length).to.equal(1) expect(users1.length).to.equal(0)
expect(users3.length).to.equal(1) expect(users2.length).to.equal(1)
expect(users3.length).to.equal(1)
t.rollback().success(function() {
done() t.rollback().success(function() {
done()
})
}) })
}) })
}) })
...@@ -57,7 +61,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -57,7 +61,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
}) })
}) }
describe('special where conditions/smartWhere object', function() { describe('special where conditions/smartWhere object', function() {
beforeEach(function(done) { beforeEach(function(done) {
...@@ -1364,26 +1368,28 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1364,26 +1368,28 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it('supports transactions', function(done) { if (current.dialect.supports.transactions) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { it('supports transactions', function(done) {
var User = sequelize.define('User', { username: Sequelize.STRING }) Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING })
User.sync({ force: true }).success(function() { User.sync({ force: true }).success(function() {
sequelize.transaction().then(function(t) { sequelize.transaction().then(function(t) {
User.create({ username: 'foo' }, { transaction: t }).success(function() { User.create({ username: 'foo' }, { transaction: t }).success(function() {
User.findAndCountAll().success(function(info1) { User.findAndCountAll().success(function(info1) {
User.findAndCountAll({ transaction: t }).success(function(info2) { User.findAndCountAll({ transaction: t }).success(function(info2) {
expect(info1.count).to.equal(0) expect(info1.count).to.equal(0)
expect(info2.count).to.equal(1) expect(info2.count).to.equal(1)
t.rollback().success(function(){ done() }) t.rollback().success(function(){ done() })
})
}) })
}) })
}) })
}) })
}) })
}) })
}) }
it("handles where clause [only]", function(done) { it("handles where clause [only]", function(done) {
this.User.findAndCountAll({where: "id != " + this.users[0].id}).success(function(info) { this.User.findAndCountAll({where: "id != " + this.users[0].id}).success(function(info) {
...@@ -1503,25 +1509,27 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1503,25 +1509,27 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
}) })
it('supports transactions', function(done) { if (current.dialect.supports.transactions) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { it('supports transactions', function(done) {
var User = sequelize.define('User', { username: Sequelize.STRING }) Support.prepareTransactionTest(this.sequelize, function(sequelize) {
var User = sequelize.define('User', { username: Sequelize.STRING })
User.sync({ force: true }).success(function() { User.sync({ force: true }).success(function() {
sequelize.transaction().then(function(t) { sequelize.transaction().then(function(t) {
User.create({ username: 'foo' }, { transaction: t }).success(function() { User.create({ username: 'foo' }, { transaction: t }).success(function() {
User.all().success(function(users1) { User.all().success(function(users1) {
User.all({ transaction: t }).success(function(users2) { User.all({ transaction: t }).success(function(users2) {
expect(users1.length).to.equal(0) expect(users1.length).to.equal(0)
expect(users2.length).to.equal(1) expect(users2.length).to.equal(1)
t.rollback().success(function(){ done() }) t.rollback().success(function(){ done() })
})
}) })
}) })
}) })
}) })
}) })
}) })
}) }
it("should return all users", function(done) { it("should return all users", function(done) {
this.User.all().on('success', function(users) { this.User.all().on('success', function(users) {
......
...@@ -3,6 +3,10 @@ var chai = require('chai') ...@@ -3,6 +3,10 @@ var chai = require('chai')
, Support = require(__dirname + '/support') , Support = require(__dirname + '/support')
, Promise = require(__dirname + '/../lib/promise') , Promise = require(__dirname + '/../lib/promise')
, Transaction = require(__dirname + '/../lib/transaction') , Transaction = require(__dirname + '/../lib/transaction')
, current = Support.sequelize;
if (current.dialect.supports.transactions) {
describe(Support.getTestDialectTeaser("Sequelize#transaction"), function () { describe(Support.getTestDialectTeaser("Sequelize#transaction"), function () {
this.timeout(4000); this.timeout(4000);
...@@ -182,3 +186,5 @@ describe(Support.getTestDialectTeaser("Sequelize#transaction"), function () { ...@@ -182,3 +186,5 @@ describe(Support.getTestDialectTeaser("Sequelize#transaction"), function () {
}) })
}) })
}) })
}
...@@ -4,7 +4,6 @@ var chai = require('chai') ...@@ -4,7 +4,6 @@ var chai = require('chai')
, expect = chai.expect , expect = chai.expect
, Support = require(__dirname + '/support') , Support = require(__dirname + '/support')
, dialect = Support.getTestDialect() , dialect = Support.getTestDialect()
, Transaction = require(__dirname + '/../lib/transaction')
, Sequelize = require(__dirname + '/../index') , Sequelize = require(__dirname + '/../index')
, Promise = Sequelize.Promise , Promise = Sequelize.Promise
, sinon = require('sinon'); , sinon = require('sinon');
......
...@@ -7,6 +7,8 @@ var chai = require('chai') ...@@ -7,6 +7,8 @@ var chai = require('chai')
, sinon = require('sinon') , sinon = require('sinon')
, current = Support.sequelize; , current = Support.sequelize;
if (current.dialect.supports.transactions) {
describe(Support.getTestDialectTeaser("Transaction"), function () { describe(Support.getTestDialectTeaser("Transaction"), function () {
this.timeout(4000); this.timeout(4000);
describe('constructor', function() { describe('constructor', function() {
...@@ -173,3 +175,5 @@ describe(Support.getTestDialectTeaser("Transaction"), function () { ...@@ -173,3 +175,5 @@ describe(Support.getTestDialectTeaser("Transaction"), function () {
}); });
} }
}); });
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!