Skip to content

Commit dd214be

Browse files
committed
Fixing tests
1 parent 97d8a58 commit dd214be

File tree

3 files changed

+106
-93
lines changed

3 files changed

+106
-93
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"pretest": "cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} mongodb-runner start",
9999
"test": "cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} TESTING=1 jasmine",
100100
"posttest": "cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} mongodb-runner stop",
101-
"coverage": "cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} TESTING=1 nyc jasmine",
101+
"coverage": "npm run pretest && cross-env MONGODB_VERSION=${MONGODB_VERSION:=4.0.4} MONGODB_TOPOLOGY=${MONGODB_TOPOLOGY:=standalone} MONGODB_STORAGE_ENGINE=${MONGODB_STORAGE_ENGINE:=mmapv1} TESTING=1 nyc jasmine && npm run posttest",
102102
"start": "node ./bin/parse-server",
103103
"prepare": "npm run build",
104104
"postinstall": "node -p 'require(\"./postinstall.js\")()'"

spec/GridStoreAdapter.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe_only_db('mongo')('GridStoreAdapter', () => {
1313
const config = Config.get(Parse.applicationId);
1414
const gridStoreAdapter = new GridStoreAdapter(databaseURI);
1515
const db = await gridStoreAdapter._connect();
16-
db.dropDatabase();
16+
await db.dropDatabase();
1717
const filesController = new FilesController(
1818
gridStoreAdapter,
1919
Parse.applicationId,

spec/batch.spec.js

+104-91
Original file line numberDiff line numberDiff line change
@@ -178,83 +178,102 @@ describe('batch', () => {
178178
});
179179

180180
it('should handle a batch request with transaction = true', done => {
181-
spyOn(databaseAdapter, 'createObject').and.callThrough();
181+
const myObject = new Parse.Object('MyObject'); // This is important because transaction only works on pre-existing collections
182+
myObject
183+
.save()
184+
.then(() => {
185+
return myObject.destroy();
186+
})
187+
.then(() => {
188+
spyOn(databaseAdapter, 'createObject').and.callThrough();
182189

183-
request({
184-
method: 'POST',
185-
headers: headers,
186-
url: 'http://localhost:8378/1/batch',
187-
body: JSON.stringify({
188-
requests: [
189-
{
190-
method: 'POST',
191-
path: '/1/classes/MyObject',
192-
body: { key: 'value1' },
193-
},
194-
{
195-
method: 'POST',
196-
path: '/1/classes/MyObject',
197-
body: { key: 'value2' },
198-
},
199-
],
200-
transaction: true,
201-
}),
202-
}).then(response => {
203-
expect(response.data.length).toEqual(2);
204-
expect(response.data[0].success.objectId).toBeDefined();
205-
expect(response.data[0].success.createdAt).toBeDefined();
206-
expect(response.data[1].success.objectId).toBeDefined();
207-
expect(response.data[1].success.createdAt).toBeDefined();
208-
const query = new Parse.Query('MyObject');
209-
query.find().then(results => {
210-
expect(databaseAdapter.createObject.calls.count()).toBe(2);
211-
expect(databaseAdapter.createObject.calls.argsFor(0)[3]).toBe(
212-
databaseAdapter.createObject.calls.argsFor(1)[3]
213-
);
214-
expect(results.map(result => result.get('key')).sort()).toEqual([
215-
'value1',
216-
'value2',
217-
]);
218-
done();
190+
request({
191+
method: 'POST',
192+
headers: headers,
193+
url: 'http://localhost:8378/1/batch',
194+
body: JSON.stringify({
195+
requests: [
196+
{
197+
method: 'POST',
198+
path: '/1/classes/MyObject',
199+
body: { key: 'value1' },
200+
},
201+
{
202+
method: 'POST',
203+
path: '/1/classes/MyObject',
204+
body: { key: 'value2' },
205+
},
206+
],
207+
transaction: true,
208+
}),
209+
}).then(response => {
210+
expect(response.data.length).toEqual(2);
211+
expect(response.data[0].success.objectId).toBeDefined();
212+
expect(response.data[0].success.createdAt).toBeDefined();
213+
expect(response.data[1].success.objectId).toBeDefined();
214+
expect(response.data[1].success.createdAt).toBeDefined();
215+
const query = new Parse.Query('MyObject');
216+
query.find().then(results => {
217+
expect(databaseAdapter.createObject.calls.count()).toBe(2);
218+
expect(databaseAdapter.createObject.calls.argsFor(0)[3]).toBe(
219+
databaseAdapter.createObject.calls.argsFor(1)[3]
220+
);
221+
expect(results.map(result => result.get('key')).sort()).toEqual(
222+
['value1', 'value2']
223+
);
224+
done();
225+
});
226+
});
219227
});
220-
});
221228
});
222229

223230
it('should not save anything when one operation fails in a transaction', done => {
224-
request({
225-
method: 'POST',
226-
headers: headers,
227-
url: 'http://localhost:8378/1/batch',
228-
body: JSON.stringify({
229-
requests: [
230-
{
231-
method: 'POST',
232-
path: '/1/classes/MyObject',
233-
body: { key: 'value1' },
234-
},
235-
{
236-
method: 'POST',
237-
path: '/1/classes/MyObject',
238-
body: { key: 10 },
239-
},
240-
],
241-
transaction: true,
242-
}),
243-
}).catch(error => {
244-
expect(error.data).toBeDefined();
245-
const query = new Parse.Query('MyObject');
246-
query.find().then(results => {
247-
expect(results.length).toBe(0);
248-
done();
231+
const myObject = new Parse.Object('MyObject'); // This is important because transaction only works on pre-existing collections
232+
myObject
233+
.save()
234+
.then(() => {
235+
return myObject.destroy();
236+
})
237+
.then(() => {
238+
request({
239+
method: 'POST',
240+
headers: headers,
241+
url: 'http://localhost:8378/1/batch',
242+
body: JSON.stringify({
243+
requests: [
244+
{
245+
method: 'POST',
246+
path: '/1/classes/MyObject',
247+
body: { key: 'value1' },
248+
},
249+
{
250+
method: 'POST',
251+
path: '/1/classes/MyObject',
252+
body: { key: 10 },
253+
},
254+
],
255+
transaction: true,
256+
}),
257+
}).catch(error => {
258+
expect(error.data).toBeDefined();
259+
const query = new Parse.Query('MyObject');
260+
query.find().then(results => {
261+
expect(results.length).toBe(0);
262+
done();
263+
});
264+
});
249265
});
250-
});
251266
});
252267

253268
it('should generate separate session for each call', async () => {
254-
const myObject = new Parse.Object('MyObject2'); // This is important because transaction only works on pre-existing collections
269+
const myObject = new Parse.Object('MyObject'); // This is important because transaction only works on pre-existing collections
255270
await myObject.save();
256271
await myObject.destroy();
257272

273+
const myObject2 = new Parse.Object('MyObject2'); // This is important because transaction only works on pre-existing collections
274+
await myObject2.save();
275+
await myObject2.destroy();
276+
258277
spyOn(databaseAdapter, 'createObject').and.callThrough();
259278

260279
let myObjectCalls = 0;
@@ -289,32 +308,6 @@ describe('batch', () => {
289308
}
290309
});
291310

292-
let myObject2Calls = 0;
293-
Parse.Cloud.beforeSave('MyObject2', async () => {
294-
myObject2Calls++;
295-
if (myObject2Calls === 2) {
296-
await request({
297-
method: 'POST',
298-
headers: headers,
299-
url: 'http://localhost:8378/1/batch',
300-
body: JSON.stringify({
301-
requests: [
302-
{
303-
method: 'POST',
304-
path: '/1/classes/MyObject3',
305-
body: { key: 'value1' },
306-
},
307-
{
308-
method: 'POST',
309-
path: '/1/classes/MyObject3',
310-
body: { key: 'value2' },
311-
},
312-
],
313-
}),
314-
});
315-
}
316-
});
317-
318311
const response = await request({
319312
method: 'POST',
320313
headers: headers,
@@ -342,6 +335,26 @@ describe('batch', () => {
342335
expect(response.data[1].success.objectId).toBeDefined();
343336
expect(response.data[1].success.createdAt).toBeDefined();
344337

338+
await request({
339+
method: 'POST',
340+
headers: headers,
341+
url: 'http://localhost:8378/1/batch',
342+
body: JSON.stringify({
343+
requests: [
344+
{
345+
method: 'POST',
346+
path: '/1/classes/MyObject3',
347+
body: { key: 'value1' },
348+
},
349+
{
350+
method: 'POST',
351+
path: '/1/classes/MyObject3',
352+
body: { key: 'value2' },
353+
},
354+
],
355+
}),
356+
});
357+
345358
const query = new Parse.Query('MyObject');
346359
const results = await query.find();
347360
expect(results.map(result => result.get('key')).sort()).toEqual([

0 commit comments

Comments
 (0)