Skip to content

Commit 5d09a1f

Browse files
authored
ci: Fix flaky direct access transaction tests (#9235)
1 parent 71a34ec commit 5d09a1f

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

spec/ParseServerRESTController.spec.js

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ const ParseServerRESTController = require('../lib/ParseServerRESTController')
22
.ParseServerRESTController;
33
const ParseServer = require('../lib/ParseServer').default;
44
const Parse = require('parse/node').Parse;
5-
const TestUtils = require('../lib/TestUtils');
65

76
let RESTController;
87

98
describe('ParseServerRESTController', () => {
9+
let createSpy;
1010
beforeEach(() => {
1111
RESTController = ParseServerRESTController(
1212
Parse.applicationId,
1313
ParseServer.promiseRouter({ appId: Parse.applicationId })
1414
);
15+
createSpy = spyOn(databaseAdapter, 'createObject').and.callThrough();
1516
});
1617

1718
it('should handle a get request', async () => {
@@ -133,24 +134,11 @@ describe('ParseServerRESTController', () => {
133134
process.env.PARSE_SERVER_TEST_DB === 'postgres'
134135
) {
135136
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-
149137
it('should handle a batch request with transaction = true', async () => {
150138
const myObject = new Parse.Object('MyObject'); // This is important because transaction only works on pre-existing collections
151139
await myObject.save();
152140
await myObject.destroy();
153-
spyOn(databaseAdapter, 'createObject').and.callThrough();
141+
createSpy.calls.reset();
154142
const response = await RESTController.request('POST', 'batch', {
155143
requests: [
156144
{
@@ -173,20 +161,22 @@ describe('ParseServerRESTController', () => {
173161
expect(response[1].success.createdAt).toBeDefined();
174162
const query = new Parse.Query('MyObject');
175163
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]
180168
);
181169
}
182170
expect(results.map(result => result.get('key')).sort()).toEqual(['value1', 'value2']);
183171
});
184172

185173
it('should not save anything when one operation fails in a transaction', async () => {
186174
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' });
188176
await myObject.destroy();
177+
createSpy.calls.reset();
189178
try {
179+
// Saving a number to a string field should fail
190180
await RESTController.request('POST', 'batch', {
191181
requests: [
192182
{
@@ -294,20 +284,21 @@ describe('ParseServerRESTController', () => {
294284
it('should generate separate session for each call', async () => {
295285
await reconfigureServer();
296286
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' });
298288
await myObject.destroy();
299289

300290
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' });
302292
await myObject2.destroy();
303293

304-
spyOn(databaseAdapter, 'createObject').and.callThrough();
294+
createSpy.calls.reset();
305295

306296
let myObjectCalls = 0;
307297
Parse.Cloud.beforeSave('MyObject', async () => {
308298
myObjectCalls++;
309299
if (myObjectCalls === 2) {
310300
try {
301+
// Saving a number to a string field should fail
311302
await RESTController.request('POST', 'batch', {
312303
requests: [
313304
{
@@ -459,14 +450,14 @@ describe('ParseServerRESTController', () => {
459450
const results3 = await query3.find();
460451
expect(results3.map(result => result.get('key')).sort()).toEqual(['value1', 'value2']);
461452

462-
expect(databaseAdapter.createObject.calls.count() >= 13).toEqual(true);
453+
expect(createSpy.calls.count() >= 13).toEqual(true);
463454
let transactionalSession;
464455
let transactionalSession2;
465456
let myObjectDBCalls = 0;
466457
let myObject2DBCalls = 0;
467458
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);
470461
switch (args[0]) {
471462
case 'MyObject':
472463
myObjectDBCalls++;

0 commit comments

Comments
 (0)