@@ -22,6 +22,27 @@ describe('replaceEnum() - enum replacement:', () => {
22
22
expect ( transactionSpy . calledOnce ) . to . be . true ;
23
23
} ) ;
24
24
25
+ it ( 'should run all queries within a sub-transaction' , async ( ) => {
26
+ const queryInterface = queryInterfaceMock ( ) ;
27
+
28
+ const transactionSpy = sinon . spy ( queryInterface . sequelize , 'transaction' ) ;
29
+
30
+ await replaceEnum ( {
31
+ queryInterface,
32
+ tableName : 'table1' ,
33
+ columnName : 'column1' ,
34
+ defaultValue : 'A' ,
35
+ newValues : [ 'A' , 'B' , 'C' ] ,
36
+ enumName : 'enum1' ,
37
+ sequelizeOptions : { transaction : { mockParentTransaction : true } }
38
+ } ) ;
39
+
40
+ expect ( transactionSpy . calledOnceWith (
41
+ sinon . match ( { transaction : { mockParentTransaction : true } } ) ,
42
+ sinon . match . func
43
+ ) ) . to . be . true ;
44
+ } ) ;
45
+
25
46
it ( 'should pass correct queries to queryInterface' , async ( ) => {
26
47
const queryInterface = queryInterfaceMock ( ) ;
27
48
@@ -84,11 +105,40 @@ describe('replaceEnum() - enum replacement:', () => {
84
105
85
106
expect ( queries ) . to . have . length ( 6 , 'should create 6 queries' ) ;
86
107
87
- queries . forEach ( ( query ) =>
108
+ queries . forEach ( ( query ) => {
88
109
expect ( query . options ) . to . deep . equal ( {
89
- transaction : { mockTransaction : true }
90
- } )
91
- ) ;
110
+ transaction : {
111
+ mockTransaction : true ,
112
+ sequelizeOptions : { }
113
+ }
114
+ } ) ;
115
+ } ) ;
116
+ } ) ;
117
+
118
+ it ( 'should pass correct options - transaction and subtransaction' , async ( ) => {
119
+ const queryInterface = queryInterfaceMock ( ) ;
120
+ await replaceEnum ( {
121
+ queryInterface,
122
+ tableName : 'table1' ,
123
+ columnName : 'column1' ,
124
+ defaultValue : 'A' ,
125
+ newValues : [ 'A' , 'B' , 'C' ] ,
126
+ enumName : 'enum1' ,
127
+ sequelizeOptions : { transaction : { mockParentTransaction : true } }
128
+ } ) ;
129
+
130
+ const queries = queryInterface . getQueries ( ) ;
131
+
132
+ expect ( queries ) . to . have . length ( 6 , 'should create 6 queries' ) ;
133
+
134
+ queries . forEach ( ( query ) => {
135
+ expect ( query . options ) . to . deep . equal ( {
136
+ transaction : {
137
+ mockTransaction : true ,
138
+ sequelizeOptions : { transaction : { mockParentTransaction : true } }
139
+ }
140
+ } ) ;
141
+ } ) ;
92
142
} ) ;
93
143
} ) ;
94
144
@@ -101,8 +151,11 @@ function queryInterfaceMock() {
101
151
queries . push ( { sql, options } ) ;
102
152
return Promise . resolve ( ) ;
103
153
} ,
104
- async transaction ( callback ) {
105
- await callback ( { mockTransaction : true } ) ;
154
+ async transaction ( ...args ) {
155
+ const sequelizeOptions = ( args . length > 1 ) ? args [ 0 ] : null ;
156
+ const callback = args . length ? args [ args . length - 1 ] : null ;
157
+
158
+ await callback ( { mockTransaction : true , sequelizeOptions } ) ;
106
159
return Promise . resolve ( ) ;
107
160
}
108
161
} ,
0 commit comments