errors.test.js
2.38 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
'use strict';
const chai = require('chai');
const expect = chai.expect;
const Support = require('../../support');
const Sequelize = Support.Sequelize;
const dialect = Support.getTestDialect();
const queryProto = Support.sequelize.dialect.Query.prototype;
if (dialect === 'mysql') {
describe('[MYSQL Specific] ForeignKeyConstraintError - error message parsing', () => {
it('FK Errors with ` quotation char are parsed correctly', () => {
const fakeErr = new Error('Cannot delete or update a parent row: a foreign key constraint fails (`table`.`brothers`, CONSTRAINT `brothers_ibfk_1` FOREIGN KEY (`personId`) REFERENCES `people` (`id`) ON UPDATE CASCADE).');
fakeErr.code = 1451;
const parsedErr = queryProto.formatError(fakeErr);
expect(parsedErr).to.be.instanceOf(Sequelize.ForeignKeyConstraintError);
expect(parsedErr.parent).to.equal(fakeErr);
expect(parsedErr.reltype).to.equal('parent');
expect(parsedErr.table).to.equal('people');
expect(parsedErr.fields).to.be.an('array').to.deep.equal(['personId']);
expect(parsedErr.value).to.be.undefined;
expect(parsedErr.index).to.equal('brothers_ibfk_1');
});
it('FK Errors with " quotation char are parsed correctly', () => {
const fakeErr = new Error('Cannot delete or update a parent row: a foreign key constraint fails ("table"."brothers", CONSTRAINT "brothers_ibfk_1" FOREIGN KEY ("personId") REFERENCES "people" ("id") ON UPDATE CASCADE).');
fakeErr.code = 1451;
const parsedErr = queryProto.formatError(fakeErr);
expect(parsedErr).to.be.instanceOf(Sequelize.ForeignKeyConstraintError);
expect(parsedErr.parent).to.equal(fakeErr);
expect(parsedErr.reltype).to.equal('parent');
expect(parsedErr.table).to.equal('people');
expect(parsedErr.fields).to.be.an('array').to.deep.equal(['personId']);
expect(parsedErr.value).to.be.undefined;
expect(parsedErr.index).to.equal('brothers_ibfk_1');
});
it('newlines contained in err message are parsed correctly', () => {
const fakeErr = new Error("Duplicate entry '13888888888\r' for key 'num'");
fakeErr.code = 1062;
const parsedErr = queryProto.formatError(fakeErr);
expect(parsedErr).to.be.instanceOf(Sequelize.UniqueConstraintError);
expect(parsedErr.parent).to.equal(fakeErr);
expect(parsedErr.fields.num).to.equal('13888888888\r');
});
});
}