Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test transactions #6022

Merged
merged 2 commits into from
Sep 5, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 233 additions & 0 deletions spec/MongoStorageAdapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const MongoStorageAdapter = require('../lib/Adapters/Storage/Mongo/MongoStorageA
const { MongoClient } = require('mongodb');
const databaseURI =
'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase';
const request = require('../lib/request');
const Config = require('../lib/Config');
const TestUtils = require('../lib/TestUtils');

const fakeClient = {
s: { options: { dbName: null } },
Expand Down Expand Up @@ -316,4 +319,234 @@ describe_only_db('mongo')('MongoStorageAdapter', () => {
undefined
);
});

if (
process.env.MONGODB_VERSION === '4.0.4' &&
process.env.MONGODB_TOPOLOGY === 'replicaset' &&
process.env.MONGODB_STORAGE_ENGINE === 'wiredTiger'
) {
describe('transactions', () => {
const headers = {
'Content-Type': 'application/json',
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
};

beforeAll(async () => {
await reconfigureServer({
databaseAdapter: undefined,
databaseURI:
'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase?replicaSet=replicaset',
});
});

beforeEach(async () => {
await TestUtils.destroyAllDataPermanently(true);
});

it('should use transaction in a batch with transaction = true', async () => {
const myObject = new Parse.Object('MyObject');
await myObject.save();

const databaseAdapter = Config.get(Parse.applicationId).database
.adapter;
spyOn(
databaseAdapter.database.serverConfig,
'command'
).and.callThrough();

await request({
method: 'POST',
headers: headers,
url: 'http://localhost:8378/1/batch',
body: JSON.stringify({
requests: [
{
method: 'PUT',
path: '/1/classes/MyObject/' + myObject.id,
body: { myAttribute: 'myValue' },
},
],
transaction: true,
}),
});

let found = false;
databaseAdapter.database.serverConfig.command.calls
.all()
.forEach(call => {
found = true;
expect(call.args[2].session.transaction.state).not.toBe(
'NO_TRANSACTION'
);
});
expect(found).toBe(true);
});

it('should not use transaction in a batch with transaction = false', async () => {
const myObject = new Parse.Object('MyObject');
await myObject.save();

const databaseAdapter = Config.get(Parse.applicationId).database
.adapter;
spyOn(
databaseAdapter.database.serverConfig,
'command'
).and.callThrough();

await request({
method: 'POST',
headers: headers,
url: 'http://localhost:8378/1/batch',
body: JSON.stringify({
requests: [
{
method: 'PUT',
path: '/1/classes/MyObject/' + myObject.id,
body: { myAttribute: 'myValue' },
},
],
transaction: false,
}),
});

let found = false;
databaseAdapter.database.serverConfig.command.calls
.all()
.forEach(call => {
found = true;
expect(call.args[2].session).toBe(undefined);
});
expect(found).toBe(true);
});

it('should not use transaction in a batch with no transaction option sent', async () => {
const myObject = new Parse.Object('MyObject');
await myObject.save();

const databaseAdapter = Config.get(Parse.applicationId).database
.adapter;
spyOn(
databaseAdapter.database.serverConfig,
'command'
).and.callThrough();

await request({
method: 'POST',
headers: headers,
url: 'http://localhost:8378/1/batch',
body: JSON.stringify({
requests: [
{
method: 'PUT',
path: '/1/classes/MyObject/' + myObject.id,
body: { myAttribute: 'myValue' },
},
],
}),
});

let found = false;
databaseAdapter.database.serverConfig.command.calls
.all()
.forEach(call => {
found = true;
expect(call.args[2].session).toBe(undefined);
});
expect(found).toBe(true);
});

it('should not use transaction in a put request', async () => {
const myObject = new Parse.Object('MyObject');
await myObject.save();

const databaseAdapter = Config.get(Parse.applicationId).database
.adapter;
spyOn(
databaseAdapter.database.serverConfig,
'command'
).and.callThrough();

await request({
method: 'PUT',
headers: headers,
url: 'http://localhost:8378/1/classes/MyObject/' + myObject.id,
body: { myAttribute: 'myValue' },
});

let found = false;
databaseAdapter.database.serverConfig.command.calls
.all()
.forEach(call => {
found = true;
expect(call.args[2].session).toBe(undefined);
});
expect(found).toBe(true);
});

it('should not use transaction when using the sdk', async () => {
const databaseAdapter = Config.get(Parse.applicationId).database
.adapter;
spyOn(
databaseAdapter.database.serverConfig,
'command'
).and.callThrough();
spyOn(
databaseAdapter.database.serverConfig,
'cursor'
).and.callThrough();
spyOn(
databaseAdapter.database.serverConfig,
'update'
).and.callThrough();
spyOn(
databaseAdapter.database.serverConfig,
'insert'
).and.callThrough();
spyOn(
databaseAdapter.database.serverConfig,
'remove'
).and.callThrough();

const myObject = new Parse.Object('MyObject');
await myObject.save();

myObject.set('myAttribute', 'myValue');
await myObject.save();

let found = false;
databaseAdapter.database.serverConfig.command.calls
.all()
.forEach(call => {
found = true;
expect(call.args[2].session).toBe(undefined);
});
expect(found).toBe(true);
databaseAdapter.database.serverConfig.cursor.calls
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including an assertion of the number of calls would be a good idea, since you're not using found.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am actually not sure that the other operations will be called. The right one is the one with the found. I added the others just in case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the freedom to commit the separation of these tests. What do you think?

.all()
.forEach(call => {
expect(call.args[2]).toBe(undefined);
});
databaseAdapter.database.serverConfig.insert.calls
.all()
.forEach(call => {
expect(call.args[2].session.transaction.state).toBe(
'NO_TRANSACTION'
);
});
databaseAdapter.database.serverConfig.remove.calls
.all()
.forEach(call => {
expect(call.args[2].session).toBe(undefined);
});
databaseAdapter.database.serverConfig.update.calls
.all()
.forEach(call => {
expect(call.args[2].session.transaction.state).toBe(
'NO_TRANSACTION'
);
});
});
});
}
});