deferrable.test.js
3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
'use strict';
const _ = require('lodash'),
chai = require('chai'),
expect = chai.expect,
Support = require(__dirname + '/../support'),
Sequelize = require(__dirname + '/../../../index'),
config = require(__dirname + '/../../config/config')
;
if (!Support.sequelize.dialect.supports.deferrableConstraints) {
return;
}
describe(Support.getTestDialectTeaser('Sequelize'), () => {
describe('Deferrable', () => {
beforeEach(function() {
this.run = function(deferrable, options) {
options = options || {};
const taskTableName = options.taskTableName || 'tasks_' + config.rand();
const transactionOptions = _.assign({}, { deferrable: Sequelize.Deferrable.SET_DEFERRED }, options);
const userTableName = 'users_' + config.rand();
const User = this.sequelize.define(
'User', { name: Sequelize.STRING }, { tableName: userTableName }
);
const Task = this.sequelize.define(
'Task', {
title: Sequelize.STRING,
user_id: {
allowNull: false,
type: Sequelize.INTEGER,
references: {
model: userTableName,
key: 'id',
deferrable
}
}
}, {
tableName: taskTableName
}
);
return User.sync({ force: true }).bind(this).then(() => {
return Task.sync({ force: true });
}).then(function() {
return this.sequelize.transaction(transactionOptions, t => {
return Task
.create({ title: 'a task', user_id: -1 }, { transaction: t })
.then(task => {
return [task, User.create({}, { transaction: t })];
})
.spread((task, user) => {
task.user_id = user.id;
return task.save({ transaction: t });
});
});
});
};
});
describe('NOT', () => {
it('does not allow the violation of the foreign key constraint', function() {
return expect(this.run(Sequelize.Deferrable.NOT)).to.eventually.be.rejectedWith(Sequelize.ForeignKeyConstraintError);
});
});
describe('INITIALLY_IMMEDIATE', () => {
it('allows the violation of the foreign key constraint if the transaction is deferred', function() {
return this
.run(Sequelize.Deferrable.INITIALLY_IMMEDIATE)
.then(task => {
expect(task.title).to.equal('a task');
expect(task.user_id).to.equal(1);
});
});
it('does not allow the violation of the foreign key constraint if the transaction is not deffered', function() {
return expect(this.run(Sequelize.Deferrable.INITIALLY_IMMEDIATE, {
deferrable: undefined
})).to.eventually.be.rejectedWith(Sequelize.ForeignKeyConstraintError);
});
it('allows the violation of the foreign key constraint if the transaction deferres only the foreign key constraint', function() {
const taskTableName = 'tasks_' + config.rand();
return this
.run(Sequelize.Deferrable.INITIALLY_IMMEDIATE, {
deferrable: Sequelize.Deferrable.SET_DEFERRED([taskTableName + '_user_id_fkey']),
taskTableName
})
.then(task => {
expect(task.title).to.equal('a task');
expect(task.user_id).to.equal(1);
});
});
});
describe('INITIALLY_DEFERRED', () => {
it('allows the violation of the foreign key constraint', function() {
return this
.run(Sequelize.Deferrable.INITIALLY_DEFERRED)
.then(task => {
expect(task.title).to.equal('a task');
expect(task.user_id).to.equal(1);
});
});
});
});
});