@@ -70,11 +70,7 @@ describe('batch', () => {
70
70
} ) ;
71
71
72
72
it ( 'should handle a batch request without transaction' , done => {
73
- let calls = 0 ;
74
- Parse . Cloud . beforeSave ( 'MyObject' , ( { database } ) => {
75
- calls ++ ;
76
- expect ( database . _transactionalSession ) . toEqual ( null ) ;
77
- } ) ;
73
+ spyOn ( databaseAdapter , 'createObject' ) . and . callThrough ( ) ;
78
74
79
75
request ( {
80
76
method : 'POST' ,
@@ -102,7 +98,9 @@ describe('batch', () => {
102
98
expect ( response . data [ 1 ] . success . createdAt ) . toBeDefined ( ) ;
103
99
const query = new Parse . Query ( 'MyObject' ) ;
104
100
query . find ( ) . then ( results => {
105
- expect ( calls ) . toBe ( 2 ) ;
101
+ expect ( databaseAdapter . createObject . calls . count ( ) ) . toBe ( 2 ) ;
102
+ expect ( databaseAdapter . createObject . calls . argsFor ( 0 ) [ 3 ] ) . toEqual ( null ) ;
103
+ expect ( databaseAdapter . createObject . calls . argsFor ( 1 ) [ 3 ] ) . toEqual ( null ) ;
106
104
expect ( results . map ( result => result . get ( 'key' ) ) . sort ( ) ) . toEqual ( [
107
105
'value1' ,
108
106
'value2' ,
@@ -113,11 +111,7 @@ describe('batch', () => {
113
111
} ) ;
114
112
115
113
it ( 'should handle a batch request with transaction = false' , done => {
116
- let calls = 0 ;
117
- Parse . Cloud . beforeSave ( 'MyObject' , ( { database } ) => {
118
- calls ++ ;
119
- expect ( database . _transactionalSession ) . toEqual ( null ) ;
120
- } ) ;
114
+ spyOn ( databaseAdapter , 'createObject' ) . and . callThrough ( ) ;
121
115
122
116
request ( {
123
117
method : 'POST' ,
@@ -146,7 +140,9 @@ describe('batch', () => {
146
140
expect ( response . data [ 1 ] . success . createdAt ) . toBeDefined ( ) ;
147
141
const query = new Parse . Query ( 'MyObject' ) ;
148
142
query . find ( ) . then ( results => {
149
- expect ( calls ) . toBe ( 2 ) ;
143
+ expect ( databaseAdapter . createObject . calls . count ( ) ) . toBe ( 2 ) ;
144
+ expect ( databaseAdapter . createObject . calls . argsFor ( 0 ) [ 3 ] ) . toEqual ( null ) ;
145
+ expect ( databaseAdapter . createObject . calls . argsFor ( 1 ) [ 3 ] ) . toEqual ( null ) ;
150
146
expect ( results . map ( result => result . get ( 'key' ) ) . sort ( ) ) . toEqual ( [
151
147
'value1' ,
152
148
'value2' ,
@@ -170,17 +166,7 @@ describe('batch', () => {
170
166
} ) ;
171
167
172
168
it ( 'should handle a batch request with transaction = true' , done => {
173
- let calls = 0 ;
174
- let transactionalSession = null ;
175
- Parse . Cloud . beforeSave ( 'MyObject' , ( { database } ) => {
176
- calls ++ ;
177
- expect ( database . _transactionalSession ) . not . toEqual ( null ) ;
178
- if ( transactionalSession ) {
179
- expect ( database . _transactionalSession ) . toBe ( transactionalSession ) ;
180
- } else {
181
- transactionalSession = database . _transactionalSession ;
182
- }
183
- } ) ;
169
+ spyOn ( databaseAdapter , 'createObject' ) . and . callThrough ( ) ;
184
170
185
171
request ( {
186
172
method : 'POST' ,
@@ -209,7 +195,10 @@ describe('batch', () => {
209
195
expect ( response . data [ 1 ] . success . createdAt ) . toBeDefined ( ) ;
210
196
const query = new Parse . Query ( 'MyObject' ) ;
211
197
query . find ( ) . then ( results => {
212
- expect ( calls ) . toBe ( 2 ) ;
198
+ expect ( databaseAdapter . createObject . calls . count ( ) ) . toBe ( 2 ) ;
199
+ expect ( databaseAdapter . createObject . calls . argsFor ( 0 ) [ 3 ] ) . toBe (
200
+ databaseAdapter . createObject . calls . argsFor ( 1 ) [ 3 ]
201
+ ) ;
213
202
expect ( results . map ( result => result . get ( 'key' ) ) . sort ( ) ) . toEqual ( [
214
203
'value1' ,
215
204
'value2' ,
@@ -219,7 +208,37 @@ describe('batch', () => {
219
208
} ) ;
220
209
} ) ;
221
210
222
- it ( 'should generate separate session for each call' , done => {
211
+ it ( 'should not save anything when one operation fails in a transaction' , done => {
212
+ request ( {
213
+ method : 'POST' ,
214
+ headers : headers ,
215
+ url : 'http://localhost:8378/1/batch' ,
216
+ body : JSON . stringify ( {
217
+ requests : [
218
+ {
219
+ method : 'POST' ,
220
+ path : '/1/classes/MyObject' ,
221
+ body : { key : 'value1' } ,
222
+ } ,
223
+ {
224
+ method : 'POST' ,
225
+ path : '/1/classes/MyObject' ,
226
+ body : { key : 10 } ,
227
+ } ,
228
+ ] ,
229
+ transaction : true ,
230
+ } ) ,
231
+ } ) . catch ( error => {
232
+ expect ( error . data . error ) . toEqual ( 'Could not add field key' ) ;
233
+ const query = new Parse . Query ( 'MyObject' ) ;
234
+ query . find ( ) . then ( results => {
235
+ expect ( results . length ) . toBe ( 0 ) ;
236
+ done ( ) ;
237
+ } ) ;
238
+ } ) ;
239
+ } ) ;
240
+
241
+ xit ( 'should generate separate session for each call' , done => {
223
242
let myObjectCalls = 0 ;
224
243
//let myObjectTransactionalSession = null;
225
244
0 commit comments