-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(changeStream): update changestream examples
Updates changeStream examples to use async/await Fixes NODE-1533
- Loading branch information
1 parent
09c7d8e
commit d080143
Showing
2 changed files
with
124 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
'use strict'; | ||
|
||
const setupDatabase = require('../functional/shared').setupDatabase; | ||
const expect = require('chai').expect; | ||
const MongoClient = require('../../lib/mongo_client'); | ||
|
||
describe('examples(change-stream):', function() { | ||
let client; | ||
let db; | ||
|
||
before(async function() { | ||
await setupDatabase(this.configuration); | ||
}); | ||
|
||
beforeEach(async function() { | ||
client = await MongoClient.connect(this.configuration.url()); | ||
db = client.db(this.configuration.db); | ||
|
||
await db.collection('inventory').deleteMany({}); | ||
}); | ||
|
||
afterEach(async function() { | ||
await client.close(); | ||
client = undefined; | ||
db = undefined; | ||
}); | ||
|
||
it('Open A Change Stream', { | ||
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } }, | ||
test: async function() { | ||
setTimeout(async function() { | ||
await db.collection('inventory').insertOne({ a: 1 }); | ||
}); | ||
|
||
// Start Changestream Example 1 | ||
const collection = db.collection('inventory'); | ||
const changeStream = collection.watch(); | ||
const next = await changeStream.next(); | ||
// End Changestream Example 1 | ||
|
||
await changeStream.close(); | ||
|
||
expect(next) | ||
.to.have.property('operationType') | ||
.that.equals('insert'); | ||
} | ||
}); | ||
|
||
it('Lookup Full Document for Update Operations', { | ||
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } }, | ||
test: async function() { | ||
await db.collection('inventory').insertOne({ a: 1, b: 2 }); | ||
setTimeout(async function() { | ||
await db.collection('inventory').updateOne({ a: 1 }, { $set: { a: 2 } }); | ||
}); | ||
|
||
// Start Changestream Example 2 | ||
const collection = db.collection('inventory'); | ||
const changeStream = collection.watch({ fullDocument: 'updateLookup' }); | ||
const next = await changeStream.next(); | ||
// End Changestream Example 2 | ||
|
||
await changeStream.close(); | ||
|
||
expect(next) | ||
.to.have.property('operationType') | ||
.that.equals('update'); | ||
expect(next) | ||
.to.have.property('fullDocument') | ||
.that.has.all.keys(['_id', 'a', 'b']); | ||
} | ||
}); | ||
|
||
it('Resume a Change Stream', { | ||
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } }, | ||
test: async function() { | ||
setTimeout(async function() { | ||
await db.collection('inventory').insertOne({ a: 1 }); | ||
await db.collection('inventory').insertOne({ b: 2 }); | ||
}); | ||
|
||
// Start Changestream Example 3 | ||
const collection = db.collection('inventory'); | ||
const changeStream = collection.watch(); | ||
const change1 = await changeStream.next(); | ||
|
||
const resumeAfter = change1._id; | ||
changeStream.close(); | ||
|
||
const newChangeStream = collection.watch({ resumeAfter }); | ||
const change2 = await newChangeStream.next(); | ||
// End Changestream Example 3 | ||
|
||
await newChangeStream.close(); | ||
|
||
expect(change1).to.have.nested.property('fullDocument.a', 1); | ||
expect(change2).to.have.nested.property('fullDocument.b', 2); | ||
} | ||
}); | ||
|
||
it('Modify Change Stream Output', { | ||
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } }, | ||
test: async function() { | ||
setTimeout(async function() { | ||
await db.collection('inventory').insertOne({ username: 'alice' }); | ||
}); | ||
|
||
// Start Changestream Example 4 | ||
const pipeline = [ | ||
{ $match: { 'fullDocument.username': 'alice' } }, | ||
{ $addFields: { newField: 'this is an added field!' } } | ||
]; | ||
const collection = db.collection('inventory'); | ||
const changeStream = collection.watch(pipeline); | ||
const next = await changeStream.next(); | ||
// End Changestream Example 4 | ||
|
||
await changeStream.close(); | ||
|
||
expect(next).to.have.nested.property('fullDocument.username', 'alice'); | ||
expect(next).to.have.property('newField', 'this is an added field!'); | ||
} | ||
}); | ||
}); |
269 changes: 0 additions & 269 deletions
269
test/functional/operation_changestream_example_tests.js
This file was deleted.
Oops, something went wrong.