@@ -2,16 +2,17 @@ const ParseServerRESTController = require('../lib/ParseServerRESTController')
2
2
. ParseServerRESTController ;
3
3
const ParseServer = require ( '../lib/ParseServer' ) . default ;
4
4
const Parse = require ( 'parse/node' ) . Parse ;
5
- const TestUtils = require ( '../lib/TestUtils' ) ;
6
5
7
6
let RESTController ;
8
7
9
8
describe ( 'ParseServerRESTController' , ( ) => {
9
+ let createSpy ;
10
10
beforeEach ( ( ) => {
11
11
RESTController = ParseServerRESTController (
12
12
Parse . applicationId ,
13
13
ParseServer . promiseRouter ( { appId : Parse . applicationId } )
14
14
) ;
15
+ createSpy = spyOn ( databaseAdapter , 'createObject' ) . and . callThrough ( ) ;
15
16
} ) ;
16
17
17
18
it ( 'should handle a get request' , async ( ) => {
@@ -133,24 +134,11 @@ describe('ParseServerRESTController', () => {
133
134
process . env . PARSE_SERVER_TEST_DB === 'postgres'
134
135
) {
135
136
describe ( 'transactions' , ( ) => {
136
- beforeEach ( async ( ) => {
137
- await TestUtils . destroyAllDataPermanently ( true ) ;
138
- if ( process . env . MONGODB_TOPOLOGY === 'replicaset' ) {
139
- await reconfigureServer ( {
140
- databaseAdapter : undefined ,
141
- databaseURI :
142
- 'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase?replicaSet=replicaset' ,
143
- } ) ;
144
- } else {
145
- await reconfigureServer ( ) ;
146
- }
147
- } ) ;
148
-
149
137
it ( 'should handle a batch request with transaction = true' , async ( ) => {
150
138
const myObject = new Parse . Object ( 'MyObject' ) ; // This is important because transaction only works on pre-existing collections
151
139
await myObject . save ( ) ;
152
140
await myObject . destroy ( ) ;
153
- spyOn ( databaseAdapter , 'createObject' ) . and . callThrough ( ) ;
141
+ createSpy . calls . reset ( ) ;
154
142
const response = await RESTController . request ( 'POST' , 'batch' , {
155
143
requests : [
156
144
{
@@ -173,20 +161,22 @@ describe('ParseServerRESTController', () => {
173
161
expect ( response [ 1 ] . success . createdAt ) . toBeDefined ( ) ;
174
162
const query = new Parse . Query ( 'MyObject' ) ;
175
163
const results = await query . find ( ) ;
176
- expect ( databaseAdapter . createObject . calls . count ( ) % 2 ) . toBe ( 0 ) ;
177
- for ( let i = 0 ; i + 1 < databaseAdapter . createObject . calls . length ; i = i + 2 ) {
178
- expect ( databaseAdapter . createObject . calls . argsFor ( i ) [ 3 ] ) . toBe (
179
- databaseAdapter . createObject . calls . argsFor ( i + 1 ) [ 3 ]
164
+ expect ( createSpy . calls . count ( ) ) . toBe ( 2 ) ;
165
+ for ( let i = 0 ; i + 1 < createSpy . calls . length ; i = i + 2 ) {
166
+ expect ( createSpy . calls . argsFor ( i ) [ 3 ] ) . toBe (
167
+ createSpy . calls . argsFor ( i + 1 ) [ 3 ]
180
168
) ;
181
169
}
182
170
expect ( results . map ( result => result . get ( 'key' ) ) . sort ( ) ) . toEqual ( [ 'value1' , 'value2' ] ) ;
183
171
} ) ;
184
172
185
173
it ( 'should not save anything when one operation fails in a transaction' , async ( ) => {
186
174
const myObject = new Parse . Object ( 'MyObject' ) ; // This is important because transaction only works on pre-existing collections
187
- await myObject . save ( ) ;
175
+ await myObject . save ( { key : 'stringField' } ) ;
188
176
await myObject . destroy ( ) ;
177
+ createSpy . calls . reset ( ) ;
189
178
try {
179
+ // Saving a number to a string field should fail
190
180
await RESTController . request ( 'POST' , 'batch' , {
191
181
requests : [
192
182
{
@@ -294,20 +284,21 @@ describe('ParseServerRESTController', () => {
294
284
it ( 'should generate separate session for each call' , async ( ) => {
295
285
await reconfigureServer ( ) ;
296
286
const myObject = new Parse . Object ( 'MyObject' ) ; // This is important because transaction only works on pre-existing collections
297
- await myObject . save ( ) ;
287
+ await myObject . save ( { key : 'stringField' } ) ;
298
288
await myObject . destroy ( ) ;
299
289
300
290
const myObject2 = new Parse . Object ( 'MyObject2' ) ; // This is important because transaction only works on pre-existing collections
301
- await myObject2 . save ( ) ;
291
+ await myObject2 . save ( { key : 'stringField' } ) ;
302
292
await myObject2 . destroy ( ) ;
303
293
304
- spyOn ( databaseAdapter , 'createObject' ) . and . callThrough ( ) ;
294
+ createSpy . calls . reset ( ) ;
305
295
306
296
let myObjectCalls = 0 ;
307
297
Parse . Cloud . beforeSave ( 'MyObject' , async ( ) => {
308
298
myObjectCalls ++ ;
309
299
if ( myObjectCalls === 2 ) {
310
300
try {
301
+ // Saving a number to a string field should fail
311
302
await RESTController . request ( 'POST' , 'batch' , {
312
303
requests : [
313
304
{
@@ -459,14 +450,14 @@ describe('ParseServerRESTController', () => {
459
450
const results3 = await query3 . find ( ) ;
460
451
expect ( results3 . map ( result => result . get ( 'key' ) ) . sort ( ) ) . toEqual ( [ 'value1' , 'value2' ] ) ;
461
452
462
- expect ( databaseAdapter . createObject . calls . count ( ) >= 13 ) . toEqual ( true ) ;
453
+ expect ( createSpy . calls . count ( ) >= 13 ) . toEqual ( true ) ;
463
454
let transactionalSession ;
464
455
let transactionalSession2 ;
465
456
let myObjectDBCalls = 0 ;
466
457
let myObject2DBCalls = 0 ;
467
458
let myObject3DBCalls = 0 ;
468
- for ( let i = 0 ; i < databaseAdapter . createObject . calls . count ( ) ; i ++ ) {
469
- const args = databaseAdapter . createObject . calls . argsFor ( i ) ;
459
+ for ( let i = 0 ; i < createSpy . calls . count ( ) ; i ++ ) {
460
+ const args = createSpy . calls . argsFor ( i ) ;
470
461
switch ( args [ 0 ] ) {
471
462
case 'MyObject' :
472
463
myObjectDBCalls ++ ;
0 commit comments