Skip to content

test(NODE-6492): add integration tests for transaction write concern behavior #4490

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

Merged
merged 11 commits into from
Apr 2, 2025
Merged
Changes from all commits
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
75 changes: 75 additions & 0 deletions test/integration/transactions/transactions.prose.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { expect } from 'chai';
import * as semver from 'semver';

import { type MongoClient, type ObjectId } from '../../mongodb';

const metadata: MongoDBMetadataUI = {
requires: {
topology: '!single'
}
};

describe('Transactions Spec Prose', function () {
let client: MongoClient;
const started = [];

beforeEach(async function () {
if (
semver.satisfies(this.configuration.version, '<=4.2') &&
this.configuration.topologyType === 'Sharded'
) {
if (this.currentTest) {
this.currentTest.skipReason =
'Transactions on sharded clusters are only supported after 4.2';
this.skip();
}
}
started.length = 0;
client = this.configuration.newClient({}, { monitorCommands: true });

await client
.db()
.collection('txn-test')
.drop()
.catch(() => null);
await client.db().createCollection('txn-test');

client.on('commandStarted', ev => started.push(ev));
});

afterEach(async function () {
await client
?.db()
.collection('txn-test')
.drop()
.catch(() => null);
await client?.close();
});

describe('Options Inside Transaction', function () {
it(
'1.0 Write concern not inherited from collection object inside transaction.',
metadata,
async () => {
let _id: ObjectId;
const collection = client.db().collection('txn-test', { writeConcern: { w: 0 } });

await client.withSession(async session => {
session.startTransaction();
_id = (await collection.insertOne({ n: 1 }, { session })).insertedId;
await session.commitTransaction();
});

// keep finding until we get a result, otherwise the test will timeout.
expect(await collection.findOne({ _id })).to.have.property('n', 1);

const insertStarted = started.find(ev => ev.commandName === 'insert');
expect(insertStarted).to.not.have.nested.property('command.writeConcern');

// not in asked by the spec test but good to check, this is where the WC would be if it wasn't ignored.
const commitTransactionStarted = started.find(ev => ev.commandName === 'commitTransaction');
expect(commitTransactionStarted).to.not.have.nested.property('command.writeConcern');
}
);
});
});