sequelize.test.js
33.2 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
'use strict';
const { expect, assert } = require('chai');
const Support = require('./support');
const DataTypes = require('../../lib/data-types');
const dialect = Support.getTestDialect();
const _ = require('lodash');
const Sequelize = require('../../index');
const config = require('../config/config');
const Transaction = require('../../lib/transaction');
const sinon = require('sinon');
const current = Support.sequelize;
const qq = str => {
if (dialect === 'postgres' || dialect === 'mssql') {
return `"${str}"`;
}
if (dialect === 'mysql' || dialect === 'mariadb' || dialect === 'sqlite') {
return `\`${str}\``;
}
return str;
};
describe(Support.getTestDialectTeaser('Sequelize'), () => {
describe('constructor', () => {
it('should pass the global options correctly', () => {
const sequelize = Support.createSequelizeInstance({ logging: false, define: { underscored: true } }),
DAO = sequelize.define('dao', { name: DataTypes.STRING });
expect(DAO.options.underscored).to.be.ok;
});
it('should correctly set the host and the port', () => {
const sequelize = Support.createSequelizeInstance({ host: '127.0.0.1', port: 1234 });
expect(sequelize.config.port).to.equal(1234);
expect(sequelize.config.host).to.equal('127.0.0.1');
});
it('should set operators aliases on dialect queryGenerator', () => {
const operatorsAliases = { fake: true };
const sequelize = Support.createSequelizeInstance({ operatorsAliases });
expect(sequelize).to.have.property('dialect');
expect(sequelize.dialect).to.have.property('queryGenerator');
expect(sequelize.dialect.queryGenerator).to.have.property('OperatorsAliasMap');
expect(sequelize.dialect.queryGenerator.OperatorsAliasMap).to.be.eql(operatorsAliases);
});
if (dialect === 'sqlite') {
it('should work with connection strings (1)', () => {
new Sequelize('sqlite://test.sqlite');
});
it('should work with connection strings (2)', () => {
new Sequelize('sqlite://test.sqlite/');
});
it('should work with connection strings (3)', () => {
new Sequelize('sqlite://test.sqlite/lol?reconnect=true');
});
}
if (dialect === 'postgres') {
const getConnectionUri = o => `${o.protocol}://${o.username}:${o.password}@${o.host}${o.port ? `:${o.port}` : ''}/${o.database}`;
it('should work with connection strings (postgres protocol)', () => {
const connectionUri = getConnectionUri({ ...config[dialect], protocol: 'postgres' });
// postgres://...
new Sequelize(connectionUri);
});
it('should work with connection strings (postgresql protocol)', () => {
const connectionUri = getConnectionUri({ ...config[dialect], protocol: 'postgresql' });
// postgresql://...
new Sequelize(connectionUri);
});
}
});
if (dialect !== 'sqlite') {
describe('authenticate', () => {
describe('with valid credentials', () => {
it('triggers the success event', async function() {
await this.sequelize.authenticate();
});
});
describe('with an invalid connection', () => {
beforeEach(function() {
const options = { ...this.sequelize.options, port: '99999' };
this.sequelizeWithInvalidConnection = new Sequelize('wat', 'trololo', 'wow', options);
});
it('triggers the error event', async function() {
try {
await this
.sequelizeWithInvalidConnection
.authenticate();
} catch (err) {
expect(err).to.not.be.null;
}
});
it('triggers an actual RangeError or ConnectionError', async function() {
try {
await this
.sequelizeWithInvalidConnection
.authenticate();
} catch (err) {
expect(
err instanceof RangeError ||
err instanceof Sequelize.ConnectionError
).to.be.ok;
}
});
it('triggers the actual adapter error', async function() {
try {
await this
.sequelizeWithInvalidConnection
.authenticate();
} catch (err) {
console.log(err);
expect(
err.message.includes('connect ECONNREFUSED') ||
err.message.includes('invalid port number') ||
err.message.match(/should be >=? 0 and < 65536/) ||
err.message.includes('Login failed for user') ||
err.message.includes('must be > 0 and < 65536')
).to.be.ok;
}
});
});
describe('with invalid credentials', () => {
beforeEach(function() {
this.sequelizeWithInvalidCredentials = new Sequelize('localhost', 'wtf', 'lol', this.sequelize.options);
});
it('triggers the error event', async function() {
try {
await this
.sequelizeWithInvalidCredentials
.authenticate();
} catch (err) {
expect(err).to.not.be.null;
}
});
it('triggers an actual sequlize error', async function() {
try {
await this
.sequelizeWithInvalidCredentials
.authenticate();
} catch (err) {
expect(err).to.be.instanceof(Sequelize.Error);
}
});
it('triggers the error event when using replication', async () => {
try {
await new Sequelize('sequelize', null, null, {
dialect,
replication: {
read: {
host: 'localhost',
username: 'omg',
password: 'lol'
}
}
}).authenticate();
} catch (err) {
expect(err).to.not.be.null;
}
});
});
});
describe('validate', () => {
it('is an alias for .authenticate()', function() {
expect(this.sequelize.validate).to.equal(this.sequelize.authenticate);
});
});
}
describe('getDialect', () => {
it('returns the defined dialect', function() {
expect(this.sequelize.getDialect()).to.equal(dialect);
});
});
describe('getDatabaseName', () => {
it('returns the database name', function() {
expect(this.sequelize.getDatabaseName()).to.equal(this.sequelize.config.database);
});
});
describe('isDefined', () => {
it('returns false if the dao wasn\'t defined before', function() {
expect(this.sequelize.isDefined('Project')).to.be.false;
});
it('returns true if the dao was defined before', function() {
this.sequelize.define('Project', {
name: DataTypes.STRING
});
expect(this.sequelize.isDefined('Project')).to.be.true;
});
});
describe('model', () => {
it('throws an error if the dao being accessed is undefined', function() {
expect(() => {
this.sequelize.model('Project');
}).to.throw(/project has not been defined/i);
});
it('returns the dao factory defined by daoName', function() {
const project = this.sequelize.define('Project', {
name: DataTypes.STRING
});
expect(this.sequelize.model('Project')).to.equal(project);
});
});
describe('set', () => {
it('should be configurable with global functions', function() {
const defaultSetterMethod = sinon.spy(),
overrideSetterMethod = sinon.spy(),
defaultGetterMethod = sinon.spy(),
overrideGetterMethod = sinon.spy(),
customSetterMethod = sinon.spy(),
customOverrideSetterMethod = sinon.spy(),
customGetterMethod = sinon.spy(),
customOverrideGetterMethod = sinon.spy();
this.sequelize.options.define = {
'setterMethods': {
'default': defaultSetterMethod,
'override': overrideSetterMethod
},
'getterMethods': {
'default': defaultGetterMethod,
'override': overrideGetterMethod
}
};
const testEntity = this.sequelize.define('TestEntity', {}, {
'setterMethods': {
'custom': customSetterMethod,
'override': customOverrideSetterMethod
},
'getterMethods': {
'custom': customGetterMethod,
'override': customOverrideGetterMethod
}
});
// Create Instance to test
const instance = testEntity.build();
// Call Getters
instance.default;
instance.custom;
instance.override;
expect(defaultGetterMethod).to.have.been.calledOnce;
expect(customGetterMethod).to.have.been.calledOnce;
expect(overrideGetterMethod.callCount).to.be.eql(0);
expect(customOverrideGetterMethod).to.have.been.calledOnce;
// Call Setters
instance.default = 'test';
instance.custom = 'test';
instance.override = 'test';
expect(defaultSetterMethod).to.have.been.calledOnce;
expect(customSetterMethod).to.have.been.calledOnce;
expect(overrideSetterMethod.callCount).to.be.eql(0);
expect(customOverrideSetterMethod).to.have.been.calledOnce;
});
});
if (dialect === 'mysql') {
describe('set', () => {
it("should return an promised error if transaction isn't defined", async function() {
await expect(this.sequelize.set({ foo: 'bar' }))
.to.be.rejectedWith(TypeError, 'options.transaction is required');
});
it('one value', async function() {
const t = await this.sequelize.transaction();
this.t = t;
await this.sequelize.set({ foo: 'bar' }, { transaction: t });
const data = await this.sequelize.query('SELECT @foo as `foo`', { plain: true, transaction: this.t });
expect(data).to.be.ok;
expect(data.foo).to.be.equal('bar');
await this.t.commit();
});
it('multiple values', async function() {
const t = await this.sequelize.transaction();
this.t = t;
await this.sequelize.set({
foo: 'bar',
foos: 'bars'
}, { transaction: t });
const data = await this.sequelize.query('SELECT @foo as `foo`, @foos as `foos`', { plain: true, transaction: this.t });
expect(data).to.be.ok;
expect(data.foo).to.be.equal('bar');
expect(data.foos).to.be.equal('bars');
await this.t.commit();
});
});
}
describe('define', () => {
it('adds a new dao to the dao manager', function() {
const count = this.sequelize.modelManager.all.length;
this.sequelize.define('foo', { title: DataTypes.STRING });
expect(this.sequelize.modelManager.all.length).to.equal(count + 1);
});
it('adds a new dao to sequelize.models', function() {
expect(this.sequelize.models.bar).to.equal(undefined);
const Bar = this.sequelize.define('bar', { title: DataTypes.STRING });
expect(this.sequelize.models.bar).to.equal(Bar);
});
it('overwrites global options', () => {
const sequelize = Support.createSequelizeInstance({ define: { collate: 'utf8_general_ci' } });
const DAO = sequelize.define('foo', { bar: DataTypes.STRING }, { collate: 'utf8_bin' });
expect(DAO.options.collate).to.equal('utf8_bin');
});
it('overwrites global rowFormat options', () => {
const sequelize = Support.createSequelizeInstance({ define: { rowFormat: 'compact' } });
const DAO = sequelize.define('foo', { bar: DataTypes.STRING }, { rowFormat: 'default' });
expect(DAO.options.rowFormat).to.equal('default');
});
it('inherits global collate option', () => {
const sequelize = Support.createSequelizeInstance({ define: { collate: 'utf8_general_ci' } });
const DAO = sequelize.define('foo', { bar: DataTypes.STRING });
expect(DAO.options.collate).to.equal('utf8_general_ci');
});
it('inherits global rowFormat option', () => {
const sequelize = Support.createSequelizeInstance({ define: { rowFormat: 'default' } });
const DAO = sequelize.define('foo', { bar: DataTypes.STRING });
expect(DAO.options.rowFormat).to.equal('default');
});
it('uses the passed tableName', async function() {
const Photo = this.sequelize.define('Foto', { name: DataTypes.STRING }, { tableName: 'photos' });
await Photo.sync({ force: true });
let tableNames = await this.sequelize.getQueryInterface().showAllTables();
if (dialect === 'mssql' || dialect === 'mariadb') {
tableNames = tableNames.map(v => v.tableName);
}
expect(tableNames).to.include('photos');
});
});
describe('truncate', () => {
it('truncates all models', async function() {
const Project = this.sequelize.define(`project${Support.rand()}`, {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
title: DataTypes.STRING
});
await this.sequelize.sync({ force: true });
const project = await Project.create({ title: 'bla' });
expect(project).to.exist;
expect(project.title).to.equal('bla');
expect(project.id).to.equal(1);
await this.sequelize.truncate();
const projects = await Project.findAll({});
expect(projects).to.exist;
expect(projects).to.have.length(0);
});
});
describe('sync', () => {
it('synchronizes all models', async function() {
const Project = this.sequelize.define(`project${Support.rand()}`, { title: DataTypes.STRING });
const Task = this.sequelize.define(`task${Support.rand()}`, { title: DataTypes.STRING });
await Project.sync({ force: true });
await Task.sync({ force: true });
await Project.create({ title: 'bla' });
const task = await Task.create({ title: 'bla' });
expect(task).to.exist;
expect(task.title).to.equal('bla');
});
it('works with correct database credentials', async function() {
const User = this.sequelize.define('User', { username: DataTypes.STRING });
await User.sync();
expect(true).to.be.true;
});
it('fails with incorrect match condition', async function() {
const sequelize = new Sequelize('cyber_bird', 'user', 'pass', {
dialect: this.sequelize.options.dialect
});
sequelize.define('Project', { title: Sequelize.STRING });
sequelize.define('Task', { title: Sequelize.STRING });
await expect(sequelize.sync({ force: true, match: /$phoenix/ }))
.to.be.rejectedWith('Database "cyber_bird" does not match sync match parameter "/$phoenix/"');
});
if (dialect !== 'sqlite') {
it('fails for incorrect connection even when no models are defined', async function() {
const sequelize = new Sequelize('cyber_bird', 'user', 'pass', {
dialect: this.sequelize.options.dialect
});
await expect(sequelize.sync({ force: true })).to.be.rejected;
});
it('fails with incorrect database credentials (1)', async function() {
this.sequelizeWithInvalidCredentials = new Sequelize('omg', 'bar', null, _.omit(this.sequelize.options, ['host']));
const User2 = this.sequelizeWithInvalidCredentials.define('User', { name: DataTypes.STRING, bio: DataTypes.TEXT });
try {
await User2.sync();
expect.fail();
} catch (err) {
if (dialect === 'postgres' || dialect === 'postgres-native') {
assert([
'fe_sendauth: no password supplied',
'role "bar" does not exist',
'FATAL: role "bar" does not exist',
'password authentication failed for user "bar"'
].includes(err.message.trim()));
} else if (dialect === 'mssql') {
expect(err.message).to.equal('Login failed for user \'bar\'.');
} else {
expect(err.message.toString()).to.match(/.*Access denied.*/);
}
}
});
it('fails with incorrect database credentials (2)', async function() {
const sequelize = new Sequelize('db', 'user', 'pass', {
dialect: this.sequelize.options.dialect
});
sequelize.define('Project', { title: Sequelize.STRING });
sequelize.define('Task', { title: Sequelize.STRING });
await expect(sequelize.sync({ force: true })).to.be.rejected;
});
it('fails with incorrect database credentials (3)', async function() {
const sequelize = new Sequelize('db', 'user', 'pass', {
dialect: this.sequelize.options.dialect,
port: 99999
});
sequelize.define('Project', { title: Sequelize.STRING });
sequelize.define('Task', { title: Sequelize.STRING });
await expect(sequelize.sync({ force: true })).to.be.rejected;
});
it('fails with incorrect database credentials (4)', async function() {
const sequelize = new Sequelize('db', 'user', 'pass', {
dialect: this.sequelize.options.dialect,
port: 99999,
pool: {}
});
sequelize.define('Project', { title: Sequelize.STRING });
sequelize.define('Task', { title: Sequelize.STRING });
await expect(sequelize.sync({ force: true })).to.be.rejected;
});
it('returns an error correctly if unable to sync a foreign key referenced model', async function() {
this.sequelize.define('Application', {
authorID: {
type: Sequelize.BIGINT,
allowNull: false,
references: {
model: 'User',
key: 'id'
}
}
});
await expect(this.sequelize.sync()).to.be.rejected;
});
it('handles this dependant foreign key constraints', async function() {
const block = this.sequelize.define('block', {
id: { type: DataTypes.INTEGER, primaryKey: true },
name: DataTypes.STRING
}, {
tableName: 'block',
timestamps: false,
paranoid: false
});
block.hasMany(block, {
as: 'childBlocks',
foreignKey: 'parent',
joinTableName: 'link_block_block',
useJunctionTable: true,
foreignKeyConstraint: true
});
block.belongsTo(block, {
as: 'parentBlocks',
foreignKey: 'child',
joinTableName: 'link_block_block',
useJunctionTable: true,
foreignKeyConstraint: true
});
await this.sequelize.sync();
});
}
it('return the sequelize instance after syncing', async function() {
const sequelize = await this.sequelize.sync();
expect(sequelize).to.deep.equal(this.sequelize);
});
it('return the single dao after syncing', async function() {
const block = this.sequelize.define('block', {
id: { type: DataTypes.INTEGER, primaryKey: true },
name: DataTypes.STRING
}, {
tableName: 'block',
timestamps: false,
paranoid: false
});
const result = await block.sync();
expect(result).to.deep.equal(block);
});
it('handles alter: true with underscore correctly', async function() {
this.sequelize.define('access_metric', {
user_id: {
type: DataTypes.INTEGER
}
}, {
underscored: true
});
await this.sequelize.sync({
alter: true
});
});
describe("doesn't emit logging when explicitly saying not to", () => {
afterEach(function() {
this.sequelize.options.logging = false;
});
beforeEach(function() {
this.spy = sinon.spy();
this.sequelize.options.logging = () => { this.spy(); };
this.User = this.sequelize.define('UserTest', { username: DataTypes.STRING });
});
it('through Sequelize.sync()', async function() {
this.spy.resetHistory();
await this.sequelize.sync({ force: true, logging: false });
expect(this.spy.notCalled).to.be.true;
});
it('through DAOFactory.sync()', async function() {
this.spy.resetHistory();
await this.User.sync({ force: true, logging: false });
expect(this.spy.notCalled).to.be.true;
});
});
describe('match', () => {
it('will return an error not matching', function() {
expect(
this.sequelize.sync({
force: true,
match: /alibabaizshaek/
})
).to.be.rejected;
});
});
});
describe('drop should work', () => {
it('correctly succeeds', async function() {
const User = this.sequelize.define('Users', { username: DataTypes.STRING });
await User.sync({ force: true });
await User.drop();
});
});
describe('define', () => {
it('raises an error if no values are defined', function() {
expect(() => {
this.sequelize.define('omnomnom', {
bla: { type: DataTypes.ARRAY }
});
}).to.throw(Error, 'ARRAY is missing type definition for its values.');
});
});
describe('define', () => {
[
{ type: DataTypes.ENUM, values: ['scheduled', 'active', 'finished'] },
DataTypes.ENUM('scheduled', 'active', 'finished')
].forEach(status => {
describe('enum', () => {
beforeEach(async function() {
this.sequelize = Support.createSequelizeInstance({
typeValidation: true
});
this.Review = this.sequelize.define('review', { status });
await this.Review.sync({ force: true });
});
it('raises an error if no values are defined', function() {
expect(() => {
this.sequelize.define('omnomnom', {
bla: { type: DataTypes.ENUM }
});
}).to.throw(Error, 'Values for ENUM have not been defined.');
});
it('correctly stores values', async function() {
const review = await this.Review.create({ status: 'active' });
expect(review.status).to.equal('active');
});
it('correctly loads values', async function() {
await this.Review.create({ status: 'active' });
const reviews = await this.Review.findAll();
expect(reviews[0].status).to.equal('active');
});
it("doesn't save an instance if value is not in the range of enums", async function() {
try {
await this.Review.create({ status: 'fnord' });
} catch (err) {
expect(err).to.be.instanceOf(Error);
expect(err.message).to.equal('"fnord" is not a valid choice in ["scheduled","active","finished"]');
}
});
});
});
describe('table', () => {
[
{ id: { type: DataTypes.BIGINT, primaryKey: true } },
{ id: { type: DataTypes.STRING, allowNull: true, primaryKey: true } },
{ id: { type: DataTypes.BIGINT, allowNull: false, primaryKey: true, autoIncrement: true } }
].forEach(customAttributes => {
it('should be able to override options on the default attributes', async function() {
const Picture = this.sequelize.define('picture', _.cloneDeep(customAttributes));
await Picture.sync({ force: true });
Object.keys(customAttributes).forEach(attribute => {
Object.keys(customAttributes[attribute]).forEach(option => {
const optionValue = customAttributes[attribute][option];
if (typeof optionValue === 'function' && optionValue() instanceof DataTypes.ABSTRACT) {
expect(Picture.rawAttributes[attribute][option] instanceof optionValue).to.be.ok;
} else {
expect(Picture.rawAttributes[attribute][option]).to.be.equal(optionValue);
}
});
});
});
});
});
if (current.dialect.supports.transactions) {
describe('transaction', () => {
beforeEach(async function() {
const sequelize = await Support.prepareTransactionTest(this.sequelize);
this.sequelizeWithTransaction = sequelize;
});
it('is a transaction method available', () => {
expect(Support.Sequelize).to.respondTo('transaction');
});
it('passes a transaction object to the callback', async function() {
const t = await this.sequelizeWithTransaction.transaction();
expect(t).to.be.instanceOf(Transaction);
});
it('allows me to define a callback on the result', async function() {
const t = await this.sequelizeWithTransaction.transaction();
await t.commit();
});
if (dialect === 'sqlite') {
it('correctly scopes transaction from other connections', async function() {
const TransactionTest = this.sequelizeWithTransaction.define('TransactionTest', { name: DataTypes.STRING }, { timestamps: false });
const count = async transaction => {
const sql = this.sequelizeWithTransaction.getQueryInterface().queryGenerator.selectQuery('TransactionTests', { attributes: [['count(*)', 'cnt']] });
const result = await this.sequelizeWithTransaction.query(sql, { plain: true, transaction });
return result.cnt;
};
await TransactionTest.sync({ force: true });
const t1 = await this.sequelizeWithTransaction.transaction();
this.t1 = t1;
await this.sequelizeWithTransaction.query(`INSERT INTO ${qq('TransactionTests')} (${qq('name')}) VALUES ('foo');`, { transaction: t1 });
await expect(count()).to.eventually.equal(0);
await expect(count(this.t1)).to.eventually.equal(1);
await this.t1.commit();
await expect(count()).to.eventually.equal(1);
});
} else {
it('correctly handles multiple transactions', async function() {
const TransactionTest = this.sequelizeWithTransaction.define('TransactionTest', { name: DataTypes.STRING }, { timestamps: false });
const aliasesMapping = new Map([['_0', 'cnt']]);
const count = async transaction => {
const sql = this.sequelizeWithTransaction.getQueryInterface().queryGenerator.selectQuery('TransactionTests', { attributes: [['count(*)', 'cnt']] });
const result = await this.sequelizeWithTransaction.query(sql, { plain: true, transaction, aliasesMapping });
return parseInt(result.cnt, 10);
};
await TransactionTest.sync({ force: true });
const t1 = await this.sequelizeWithTransaction.transaction();
this.t1 = t1;
await this.sequelizeWithTransaction.query(`INSERT INTO ${qq('TransactionTests')} (${qq('name')}) VALUES ('foo');`, { transaction: t1 });
const t2 = await this.sequelizeWithTransaction.transaction();
this.t2 = t2;
await this.sequelizeWithTransaction.query(`INSERT INTO ${qq('TransactionTests')} (${qq('name')}) VALUES ('bar');`, { transaction: t2 });
await expect(count()).to.eventually.equal(0);
await expect(count(this.t1)).to.eventually.equal(1);
await expect(count(this.t2)).to.eventually.equal(1);
await this.t2.rollback();
await expect(count()).to.eventually.equal(0);
await this.t1.commit();
await expect(count()).to.eventually.equal(1);
});
}
it('supports nested transactions using savepoints', async function() {
const User = this.sequelizeWithTransaction.define('Users', { username: DataTypes.STRING });
await User.sync({ force: true });
const t1 = await this.sequelizeWithTransaction.transaction();
const user = await User.create({ username: 'foo' }, { transaction: t1 });
const t2 = await this.sequelizeWithTransaction.transaction({ transaction: t1 });
await user.update({ username: 'bar' }, { transaction: t2 });
await t2.commit();
const newUser = await user.reload({ transaction: t1 });
expect(newUser.username).to.equal('bar');
await t1.commit();
});
describe('supports rolling back to savepoints', () => {
beforeEach(async function() {
this.User = this.sequelizeWithTransaction.define('user', {});
await this.sequelizeWithTransaction.sync({ force: true });
});
it('rolls back to the first savepoint, undoing everything', async function() {
const transaction = await this.sequelizeWithTransaction.transaction();
this.transaction = transaction;
const sp1 = await this.sequelizeWithTransaction.transaction({ transaction });
this.sp1 = sp1;
await this.User.create({}, { transaction: this.transaction });
const sp2 = await this.sequelizeWithTransaction.transaction({ transaction: this.transaction });
this.sp2 = sp2;
await this.User.create({}, { transaction: this.transaction });
const users0 = await this.User.findAll({ transaction: this.transaction });
expect(users0).to.have.length(2);
await this.sp1.rollback();
const users = await this.User.findAll({ transaction: this.transaction });
expect(users).to.have.length(0);
await this.transaction.rollback();
});
it('rolls back to the most recent savepoint, only undoing recent changes', async function() {
const transaction = await this.sequelizeWithTransaction.transaction();
this.transaction = transaction;
const sp1 = await this.sequelizeWithTransaction.transaction({ transaction });
this.sp1 = sp1;
await this.User.create({}, { transaction: this.transaction });
const sp2 = await this.sequelizeWithTransaction.transaction({ transaction: this.transaction });
this.sp2 = sp2;
await this.User.create({}, { transaction: this.transaction });
const users0 = await this.User.findAll({ transaction: this.transaction });
expect(users0).to.have.length(2);
await this.sp2.rollback();
const users = await this.User.findAll({ transaction: this.transaction });
expect(users).to.have.length(1);
await this.transaction.rollback();
});
});
it('supports rolling back a nested transaction', async function() {
const User = this.sequelizeWithTransaction.define('Users', { username: DataTypes.STRING });
await User.sync({ force: true });
const t1 = await this.sequelizeWithTransaction.transaction();
const user = await User.create({ username: 'foo' }, { transaction: t1 });
const t2 = await this.sequelizeWithTransaction.transaction({ transaction: t1 });
await user.update({ username: 'bar' }, { transaction: t2 });
await t2.rollback();
const newUser = await user.reload({ transaction: t1 });
expect(newUser.username).to.equal('foo');
await t1.commit();
});
it('supports rolling back outermost transaction', async function() {
const User = this.sequelizeWithTransaction.define('Users', { username: DataTypes.STRING });
await User.sync({ force: true });
const t1 = await this.sequelizeWithTransaction.transaction();
const user = await User.create({ username: 'foo' }, { transaction: t1 });
const t2 = await this.sequelizeWithTransaction.transaction({ transaction: t1 });
await user.update({ username: 'bar' }, { transaction: t2 });
await t1.rollback();
const users = await User.findAll();
expect(users.length).to.equal(0);
});
});
}
});
describe('databaseVersion', () => {
it('should database/dialect version', async function() {
const version = await this.sequelize.databaseVersion();
expect(typeof version).to.equal('string');
expect(version).to.be.ok;
});
});
describe('paranoid deletedAt non-null default value', () => {
it('should use defaultValue of deletedAt in paranoid clause and restore', async function() {
const epochObj = new Date(0),
epoch = Number(epochObj);
const User = this.sequelize.define('user', {
username: DataTypes.STRING,
deletedAt: {
type: DataTypes.DATE,
defaultValue: epochObj
}
}, {
paranoid: true
});
await this.sequelize.sync({ force: true });
const user = await User.create({ username: 'user1' });
expect(Number(user.deletedAt)).to.equal(epoch);
const user0 = await User.findOne({
where: {
username: 'user1'
}
});
expect(user0).to.exist;
expect(Number(user0.deletedAt)).to.equal(epoch);
const destroyedUser = await user0.destroy();
expect(destroyedUser.deletedAt).to.exist;
expect(Number(destroyedUser.deletedAt)).not.to.equal(epoch);
const fetchedDestroyedUser = await User.findByPk(destroyedUser.id, { paranoid: false });
expect(fetchedDestroyedUser.deletedAt).to.exist;
expect(Number(fetchedDestroyedUser.deletedAt)).not.to.equal(epoch);
const restoredUser = await fetchedDestroyedUser.restore();
expect(Number(restoredUser.deletedAt)).to.equal(epoch);
await User.destroy({ where: {
username: 'user1'
} });
const count = await User.count();
expect(count).to.equal(0);
await User.restore();
const nonDeletedUsers = await User.findAll();
expect(nonDeletedUsers.length).to.equal(1);
nonDeletedUsers.forEach(u => {
expect(Number(u.deletedAt)).to.equal(epoch);
});
});
});
});