Skip to content

Commit c093a45

Browse files
add tests for pkFactory in bulk write
1 parent e30c6d3 commit c093a45

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

test/integration/crud/bulk.test.ts

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as crypto from 'crypto';
33

44
import {
55
type Collection,
6+
Double,
67
Long,
78
MongoBatchReExecutionError,
89
MongoBulkWriteError,
@@ -65,16 +66,84 @@ describe('Bulk', function () {
6566
context('when called with a valid operation', function () {
6667
it('should not throw a MongoInvalidArgument error', async function () {
6768
try {
68-
client.db('test').collection('test').initializeUnorderedBulkOp().raw({ insertOne: {} });
69+
client
70+
.db('test')
71+
.collection('test')
72+
.initializeUnorderedBulkOp()
73+
.raw({ insertOne: { document: {} } });
6974
} catch (error) {
7075
expect(error).not.to.exist;
7176
}
7277
});
7378
});
79+
80+
it('supports the legacy specification (no nested document field)', async function () {
81+
await client
82+
.db('test')
83+
.collection('test')
84+
.initializeUnorderedBulkOp()
85+
// @ts-expect-error Not allowed in TS, but allowed for legacy compat
86+
.raw({ insertOne: { name: 'john doe' } })
87+
.execute();
88+
const result = await client.db('test').collection('test').findOne({ name: 'john doe' });
89+
expect(result).to.exist;
90+
});
7491
});
7592
});
7693

7794
describe('Collection', function () {
95+
describe('when a pkFactory is set on the client', function () {
96+
let client: MongoClient;
97+
const pkFactory = {
98+
count: 0,
99+
createPk: function () {
100+
return new Double(this.count++);
101+
}
102+
};
103+
let collection: Collection;
104+
105+
beforeEach(async function () {
106+
client = this.configuration.newClient({}, { pkFactory, promoteValues: false });
107+
collection = client.db('integration').collection('pk_factory_tests');
108+
await collection.deleteMany({});
109+
});
110+
111+
afterEach(() => client.close());
112+
113+
it('insertMany() generates _ids using the pkFactory', async function () {
114+
await collection.insertMany([{ name: 'john doe' }]);
115+
const result = await collection.findOne({ name: 'john doe' });
116+
expect(result).to.have.property('_id').to.be.instanceOf(Double);
117+
});
118+
119+
it('bulkWrite() generates _ids using the pkFactory', async function () {
120+
await collection.bulkWrite([{ insertOne: { document: { name: 'john doe' } } }]);
121+
const result = await collection.findOne({ name: 'john doe' });
122+
expect(result).to.have.property('_id').to.be.instanceOf(Double);
123+
});
124+
125+
it('ordered bulk operations generate _ids using pkFactory', async function () {
126+
await collection.initializeOrderedBulkOp().insert({ name: 'john doe' }).execute();
127+
const result = await collection.findOne({ name: 'john doe' });
128+
expect(result).to.have.property('_id').to.be.instanceOf(Double);
129+
});
130+
131+
it('unordered bulk operations generate _ids using pkFactory', async function () {
132+
await collection.initializeUnorderedBulkOp().insert({ name: 'john doe' }).execute();
133+
const result = await collection.findOne({ name: 'john doe' });
134+
expect(result).to.have.property('_id').to.be.instanceOf(Double);
135+
});
136+
137+
it('bulkOperation.raw() with the legacy syntax (no nested document field) generates _ids using pkFactory', async function () {
138+
await collection
139+
.initializeOrderedBulkOp()
140+
// @ts-expect-error Not allowed by TS, but still permitted.
141+
.raw({ insertOne: { name: 'john doe' } })
142+
.execute();
143+
const result = await collection.findOne({ name: 'john doe' });
144+
expect(result).to.have.property('_id').to.be.instanceOf(Double);
145+
});
146+
});
78147
describe('#insertMany()', function () {
79148
context('when passed an invalid docs argument', function () {
80149
it('should throw a MongoInvalidArgument error', async function () {

0 commit comments

Comments
 (0)