Skip to content

Commit 52267a5

Browse files
authored
test(NODE-7254): migrate node-specific/operation_examples tests (#4743)
1 parent a8d7c5f commit 52267a5

File tree

1 file changed

+32
-67
lines changed

1 file changed

+32
-67
lines changed

test/integration/node-specific/operation_examples.test.ts

Lines changed: 32 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import { expect } from 'chai';
22

3-
import {
4-
Code,
5-
enumToString,
6-
type MongoClient,
7-
ProfilingLevel,
8-
ReturnDocument
9-
} from '../../mongodb';
3+
import { Code, type MongoClient, ProfilingLevel, ReturnDocument } from '../../../src';
4+
import { enumToString } from '../../../src/utils';
105
import { sleep as delay } from '../../tools/utils';
116
import { setupDatabase } from '../shared';
127

@@ -3989,77 +3984,47 @@ describe('Operations', function () {
39893984
});
39903985

39913986
/**
3992-
* A simple example showing the listening to a capped collection using a Promise.
3987+
* A simple example showing the listening to a capped collection.
39933988
*
39943989
* example-class Db
39953990
* example-method createCollection
39963991
*/
3997-
it('Should correctly add capped collection options to cursor With Promises', {
3998-
metadata: { requires: { topology: ['single'] } },
3999-
4000-
test: function (done) {
4001-
const configuration = this.configuration;
4002-
const client = configuration.newClient(configuration.writeConcernMax(), {
4003-
maxPoolSize: 1
4004-
});
4005-
4006-
client.connect().then(function (client) {
4007-
const db = client.db(configuration.db);
4008-
// LINE var MongoClient = require('mongodb').MongoClient,
4009-
// LINE test = require('assert');
4010-
// LINE const client = new MongoClient('mongodb://localhost:27017/test');
4011-
// LINE client.connect().then(() => {
4012-
// LINE var db = client.db('test);
4013-
// REPLACE configuration.writeConcernMax() WITH {w:1}
4014-
// REMOVE-LINE done();
4015-
// BEGIN
4016-
// Create a capped collection with a maximum of 1000 documents
4017-
let collection;
4018-
4019-
db.createCollection('a_simple_collection_2_with_promise', {
4020-
capped: true,
4021-
size: 100000,
4022-
max: 1000,
4023-
writeConcern: { w: 1 }
4024-
})
4025-
.then(function (_collection) {
4026-
collection = _collection;
4027-
4028-
const docs: Array<{ a: number }> = [];
4029-
for (let i = 0; i < 1000; i++) docs.push({ a: i });
3992+
it('Should correctly add capped collection options to cursor', async function () {
3993+
const configuration = this.configuration;
3994+
const client = configuration.newClient(configuration.writeConcernMax(), {
3995+
maxPoolSize: 1
3996+
});
40303997

4031-
// Insert a document in the capped collection
4032-
return collection.insertMany(docs, configuration.writeConcernMax());
4033-
})
4034-
.then(function (result) {
4035-
expect(result).to.exist;
3998+
await client.connect();
3999+
const db = client.db(configuration.db);
40364000

4037-
let total = 0;
4001+
const collection = await db.createCollection('a_simple_collection_2_with_promise', {
4002+
capped: true,
4003+
size: 100000,
4004+
max: 1000,
4005+
writeConcern: { w: 1 }
4006+
});
4007+
const docs: Array<{ a: number }> = [];
4008+
for (let i = 0; i < 10000; i++) docs.push({ a: i });
40384009

4039-
// Get the cursor
4040-
const cursor = collection
4041-
.find({ a: { $gte: 0 } })
4042-
.addCursorFlag('tailable', true)
4043-
.addCursorFlag('awaitData', true);
4010+
// Insert a document in the capped collection
4011+
await collection.insertMany(docs, configuration.writeConcernMax());
4012+
let total = 0;
40444013

4045-
const stream = cursor.stream();
4046-
stream.on('data', function (d) {
4047-
expect(d).to.exist;
4048-
total = total + 1;
4014+
// Get the cursor
4015+
const cursor = collection
4016+
.find({ a: { $gte: 0 } })
4017+
.addCursorFlag('tailable', true)
4018+
.addCursorFlag('awaitData', true);
40494019

4050-
if (total === 1000) {
4051-
cursor.close();
4052-
}
4053-
});
4020+
const stream = cursor.stream();
40544021

4055-
cursor.on('close', function () {
4056-
// TODO: forced because the cursor is still open/active
4057-
client.close(true, done);
4058-
});
4059-
});
4060-
});
4061-
// END
4022+
for await (const d of stream) {
4023+
expect(d).to.have.property('_id');
4024+
total = total + 1;
4025+
if (total === 1000) await cursor.close();
40624026
}
4027+
await client.close();
40634028
});
40644029

40654030
describe('Transaction Examples', function () {

0 commit comments

Comments
 (0)