|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as semver from 'semver'; |
| 3 | + |
| 4 | +import { type MongoClient, type ObjectId } from '../../mongodb'; |
| 5 | + |
| 6 | +const metadata: MongoDBMetadataUI = { |
| 7 | + requires: { |
| 8 | + topology: '!single' |
| 9 | + } |
| 10 | +}; |
| 11 | + |
| 12 | +describe('Transactions Spec Prose', function () { |
| 13 | + let client: MongoClient; |
| 14 | + const started = []; |
| 15 | + |
| 16 | + beforeEach(async function () { |
| 17 | + if ( |
| 18 | + semver.satisfies(this.configuration.version, '<=4.2') && |
| 19 | + this.configuration.topologyType === 'Sharded' |
| 20 | + ) { |
| 21 | + if (this.currentTest) { |
| 22 | + this.currentTest.skipReason = |
| 23 | + 'Transactions on sharded clusters are only supported after 4.2'; |
| 24 | + this.skip(); |
| 25 | + } |
| 26 | + } |
| 27 | + started.length = 0; |
| 28 | + client = this.configuration.newClient({}, { monitorCommands: true }); |
| 29 | + |
| 30 | + await client |
| 31 | + .db() |
| 32 | + .collection('txn-test') |
| 33 | + .drop() |
| 34 | + .catch(() => null); |
| 35 | + await client.db().createCollection('txn-test'); |
| 36 | + |
| 37 | + client.on('commandStarted', ev => started.push(ev)); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(async function () { |
| 41 | + await client |
| 42 | + ?.db() |
| 43 | + .collection('txn-test') |
| 44 | + .drop() |
| 45 | + .catch(() => null); |
| 46 | + await client?.close(); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('Options Inside Transaction', function () { |
| 50 | + it( |
| 51 | + '1.0 Write concern not inherited from collection object inside transaction.', |
| 52 | + metadata, |
| 53 | + async () => { |
| 54 | + let _id: ObjectId; |
| 55 | + const collection = client.db().collection('txn-test', { writeConcern: { w: 0 } }); |
| 56 | + |
| 57 | + await client.withSession(async session => { |
| 58 | + session.startTransaction(); |
| 59 | + _id = (await collection.insertOne({ n: 1 }, { session })).insertedId; |
| 60 | + await session.commitTransaction(); |
| 61 | + }); |
| 62 | + |
| 63 | + // keep finding until we get a result, otherwise the test will timeout. |
| 64 | + expect(await collection.findOne({ _id })).to.have.property('n', 1); |
| 65 | + |
| 66 | + const insertStarted = started.find(ev => ev.commandName === 'insert'); |
| 67 | + expect(insertStarted).to.not.have.nested.property('command.writeConcern'); |
| 68 | + |
| 69 | + // not in asked by the spec test but good to check, this is where the WC would be if it wasn't ignored. |
| 70 | + const commitTransactionStarted = started.find(ev => ev.commandName === 'commitTransaction'); |
| 71 | + expect(commitTransactionStarted).to.not.have.nested.property('command.writeConcern'); |
| 72 | + } |
| 73 | + ); |
| 74 | + }); |
| 75 | +}); |
0 commit comments