-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
166 lines (141 loc) · 4.63 KB
/
index.js
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
'use strict';
import sinon from 'sinon';
import { removeWhiteSpace } from './utils/testUtils';
import replaceEnum from '../src/index';
describe('replaceEnum() - enum replacement:', () => {
it('should run all queries within a transaction', async () => {
const queryInterface = queryInterfaceMock();
const transactionSpy = sinon.spy(queryInterface.sequelize, 'transaction');
await replaceEnum({
queryInterface,
tableName: 'table1',
columnName: 'column1',
defaultValue: 'A',
newValues: ['A', 'B', 'C'],
enumName: 'enum1'
});
expect(transactionSpy.calledOnce).to.be.true;
});
it('should run all queries within a sub-transaction', async () => {
const queryInterface = queryInterfaceMock();
const transactionSpy = sinon.spy(queryInterface.sequelize, 'transaction');
await replaceEnum({
queryInterface,
tableName: 'table1',
columnName: 'column1',
defaultValue: 'A',
newValues: ['A', 'B', 'C'],
enumName: 'enum1',
sequelizeOptions: { transaction: { mockParentTransaction: true } }
});
expect(transactionSpy.calledOnceWith(
sinon.match({ transaction: { mockParentTransaction: true } }),
sinon.match.func
)).to.be.true;
});
it('should pass correct queries to queryInterface', async () => {
const queryInterface = queryInterfaceMock();
await replaceEnum({
queryInterface,
tableName: 'table1',
columnName: 'column1',
defaultValue: 'A',
newValues: ['A', 'B', 'C'],
enumName: 'enum1'
});
expect(
queryInterface.getQueries().map((q) => removeWhiteSpace(q.sql))
).to.deep.equal([
`CREATE TYPE "enum1_new" AS ENUM (\'A\', \'B\', \'C\')`,
`ALTER TABLE "table1" ALTER COLUMN "column1" DROP DEFAULT`,
` ALTER TABLE "table1" ALTER COLUMN "column1" TYPE "enum1_new"` +
` USING ("column1"::text::"enum1_new") `,
`DROP TYPE "enum1"`,
`ALTER TYPE "enum1_new" RENAME TO "enum1"`,
` ALTER TABLE "table1" ALTER COLUMN "column1"` +
` SET DEFAULT 'A'::"enum1" `
]);
});
it('should pass correct queries to queryInterface when not using a default value', async () => {
const queryInterface = queryInterfaceMock();
await replaceEnum({
queryInterface,
tableName: 'table1',
columnName: 'column1',
newValues: ['A', 'B', 'C'],
enumName: 'enum1'
});
expect(
queryInterface.getQueries().map((q) => removeWhiteSpace(q.sql))
).to.deep.equal([
`CREATE TYPE "enum1_new" AS ENUM (\'A\', \'B\', \'C\')`,
` ALTER TABLE "table1" ALTER COLUMN "column1" TYPE "enum1_new"` +
` USING ("column1"::text::"enum1_new") `,
`DROP TYPE "enum1"`,
`ALTER TYPE "enum1_new" RENAME TO "enum1"`
]);
});
it('should pass correct options - transaction', async () => {
const queryInterface = queryInterfaceMock();
await replaceEnum({
queryInterface,
tableName: 'table1',
columnName: 'column1',
defaultValue: 'A',
newValues: ['A', 'B', 'C'],
enumName: 'enum1'
});
const queries = queryInterface.getQueries();
expect(queries).to.have.length(6, 'should create 6 queries');
queries.forEach((query) => {
expect(query.options).to.deep.equal({
transaction: {
mockTransaction: true,
sequelizeOptions: {}
}
});
});
});
it('should pass correct options - transaction and subtransaction', async () => {
const queryInterface = queryInterfaceMock();
await replaceEnum({
queryInterface,
tableName: 'table1',
columnName: 'column1',
defaultValue: 'A',
newValues: ['A', 'B', 'C'],
enumName: 'enum1',
sequelizeOptions: { transaction: { mockParentTransaction: true } }
});
const queries = queryInterface.getQueries();
expect(queries).to.have.length(6, 'should create 6 queries');
queries.forEach((query) => {
expect(query.options).to.deep.equal({
transaction: {
mockTransaction: true,
sequelizeOptions: { transaction: { mockParentTransaction: true } }
}
});
});
});
});
function queryInterfaceMock() {
const queries = [];
return {
sequelize: {
query(sql, options) {
queries.push({ sql, options });
return Promise.resolve();
},
async transaction(...args) {
const sequelizeOptions = (args.length > 1) ? args[0] : null;
const callback = args.length ? args[args.length - 1] : null;
await callback({ mockTransaction: true, sequelizeOptions });
return Promise.resolve();
}
},
getQueries() {
return queries;
}
};
}