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

Commit 023bab43 by Jan Aagaard Meier

Findorcreate with transaction working

1 parent 2302ec49
......@@ -1123,7 +1123,6 @@ module.exports = (function() {
queryOptions.exception = 'WHEN unique_violation THEN RETURN QUERY SELECT * FROM <%= table %> WHERE 1 <> 1;';
return self.create(values, queryOptions).bind(this).then(function(instance) {
console.log(this);
if (instance[self.primaryKeyAttribute] === null) {
// If the query returned an empty result for the primary key, we know that this was actually a unique constraint violation
throw new self.sequelize.UniqueConstraintError();
......@@ -1133,7 +1132,7 @@ module.exports = (function() {
}).catch(self.sequelize.UniqueConstraintError, function () {
// Someone must have created a matching instance inside the same transaction since we last did a find. Let's find it!
return self.find(options, {
transaction: this.transaction,
transaction: internalTransaction ? null : this.transaction,
}).then(function(instance) {
return [instance, false];
});
......
......@@ -33,25 +33,25 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
});
});
describe.only('findOrCreate', function () {
describe('findOrCreate', function () {
it("supports transactions", function(done) {
var self = this;
this.sequelize.transaction().then(function(t) {
self.User.findOrCreate({ where: { username: 'Username' }, defaults: { data: 'some data' }}, { transaction: t }).then(function() {
// self.User.count().success(function(count) {
// expect(count).to.equal(0)
self.User.count().success(function(count) {
expect(count).to.equal(0)
t.commit().success(function() {
self.User.count().success(function(count) {
expect(count).to.equal(1)
done()
// })
})
})
})
})
})
})
it.skip("returns instance if already existent. Single find field.", function(done) {
it("returns instance if already existent. Single find field.", function(done) {
var self = this,
data = {
username: 'Username'
......@@ -69,7 +69,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
it.skip("Returns instance if already existent. Multiple find fields.", function(done) {
it("Returns instance if already existent. Multiple find fields.", function(done) {
var self = this,
data = {
username: 'Username',
......@@ -87,7 +87,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
it.skip("creates new instance with default value.", function(done) {
it("creates new instance with default value.", function(done) {
var data = {
username: 'Username'
},
......@@ -103,7 +103,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
it.skip("supports .or() (only using default values)", function (done) {
it("supports .or() (only using default values)", function (done) {
this.User.findOrCreate({
where: Sequelize.or({username: 'Fooobzz'}, {secretValue: 'Yolo'}),
defaults: {username: 'Fooobzz', secretValue: 'Yolo'}
......@@ -132,15 +132,14 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(first[0].id).to.equal(second[0].id);
return transaction.rollback();
return transaction.commit();
}
);
});
});
it('works without a transaction', function () {
var self = this;
if (dialect !== 'sqlite') { // 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 () {
return Promise.join(
this.User.findOrCreate({ where: { uniqueName: 'winner' }}),
this.User.findOrCreate({ where: { uniqueName: 'winner' }}),
......@@ -153,7 +152,6 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect(first[0].id).to.equal(second[0].id);
}
);
}
});
});
});
......
......@@ -74,12 +74,9 @@ describe(Support.getTestDialectTeaser("Sequelize Errors"), function () {
return this.sequelize.sync({ force: true }).bind(this).then(function () {
return User.create({ first_name: 'jan', last_name: 'meier' });
}).then(function () {
return Promise.join(
User.create({ first_name: 'jan', last_name: 'meier' }).catch(this.sequelize.UniqueConstraintError, spy),
function () {
return User.create({ first_name: 'jan', last_name: 'meier' }).catch(this.sequelize.UniqueConstraintError, spy);
}).then(function () {
expect(spy).to.have.been.calledOnce;
}
);
});
});
});
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!