-
Notifications
You must be signed in to change notification settings - Fork 1.8k
docs(examples): updating examples to use async/await #1764
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
71e8fa2
docs(examples): updating examples to use async/await
daprahamian a7c6350
adding transaction tests
daprahamian 005e4dd
blocking console.logs
daprahamian 900949b
adding semver back in as a dev dep
daprahamian be98691
add consistent causal constistency name
daprahamian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,36 @@ | ||
| 'use strict'; | ||
|
|
||
| const setupDatabase = require('../functional/shared').setupDatabase; | ||
| const MongoClient = require('../../lib/mongo_client'); | ||
|
|
||
| describe('examples(project-fields-from-query):', function() { | ||
| let client; | ||
| let collection; | ||
|
|
||
| before(async function() { | ||
| await setupDatabase(this.configuration); | ||
| }); | ||
|
|
||
| beforeEach(async function() { | ||
| client = await MongoClient.connect(this.configuration.url()); | ||
| collection = client.db(this.configuration.db).collection('arrayFilterUpdateExample'); | ||
| }); | ||
|
|
||
| afterEach(async function() { | ||
| await client.close(); | ||
| client = undefined; | ||
| collection = undefined; | ||
| }); | ||
|
|
||
| it('supports array filters when updating', { | ||
| metadata: { requires: { mongodb: '>=3.6.x', topology: ['single'] } }, | ||
| test: async function() { | ||
| // 3. Exploiting the power of arrays | ||
| await collection.updateOne( | ||
| { _id: 1 }, | ||
| { $set: { 'a.$[i].b': 2 } }, | ||
| { arrayFilters: [{ 'i.b': 0 }] } | ||
| ); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or 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,49 @@ | ||
| 'use strict'; | ||
|
|
||
| const setupDatabase = require('../functional/shared').setupDatabase; | ||
| const expect = require('chai').expect; | ||
| const MongoClient = require('../../lib/mongo_client'); | ||
|
|
||
| describe('examples(causal-consistency):', function() { | ||
| let client; | ||
| let collection; | ||
| let session; | ||
|
|
||
| before(async function() { | ||
| await setupDatabase(this.configuration); | ||
| }); | ||
|
|
||
| beforeEach(async function() { | ||
| client = await MongoClient.connect(this.configuration.url()); | ||
| collection = client.db(this.configuration.db).collection('arrayFilterUpdateExample'); | ||
| }); | ||
|
|
||
| afterEach(async function() { | ||
| if (session) { | ||
| await session.endSession(); | ||
| session = undefined; | ||
| } | ||
|
|
||
| await client.close(); | ||
| client = undefined; | ||
| collection = undefined; | ||
| }); | ||
|
|
||
| it('CausalConsistency', { | ||
| metadata: { | ||
| requires: { topology: ['single'], mongodb: '>=3.6.0' }, | ||
| sessions: { skipLeakTests: true } | ||
| }, | ||
|
|
||
| test: async function() { | ||
| const session = client.startSession({ causalConsistency: true }); | ||
|
|
||
| collection.insertOne({ darmok: 'jalad' }, { session }); | ||
| collection.updateOne({ darmok: 'jalad' }, { $set: { darmok: 'tanagra' } }, { session }); | ||
|
|
||
| const results = await collection.find({}, { session }).toArray(); | ||
|
|
||
| expect(results).to.exist; | ||
| } | ||
| }); | ||
| }); | ||
This file contains hidden or 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,77 @@ | ||
| 'use strict'; | ||
|
|
||
| const setupDatabase = require('../functional/shared').setupDatabase; | ||
| const expect = require('chai').expect; | ||
| const MongoClient = require('../../lib/mongo_client'); | ||
|
|
||
| describe('examples(insert):', 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('Insert a Single Document', { | ||
| metadata: { requires: { topology: ['single'], mongodb: '>= 2.8.0' } }, | ||
| test: async function() { | ||
| // Start Example 1 | ||
| await db.collection('inventory').insertOne({ | ||
| item: 'canvas', | ||
| qty: 100, | ||
| tags: ['cotton'], | ||
| size: { h: 28, w: 35.5, uom: 'cm' } | ||
| }); | ||
| // End Example 1 | ||
|
|
||
| // Start Example 2 | ||
| const cursor = db.collection('inventory').find({ item: 'canvas' }); | ||
| // End Example 2 | ||
|
|
||
| expect(await cursor.count()).to.equal(1); | ||
| } | ||
| }); | ||
|
|
||
| it('Insert Multiple Documents', { | ||
| metadata: { requires: { topology: ['single'], mongodb: '>= 2.8.0' } }, | ||
| test: async function() { | ||
| // Start Example 3 | ||
| await db.collection('inventory').insertMany([ | ||
| { | ||
| item: 'journal', | ||
| qty: 25, | ||
| tags: ['blank', 'red'], | ||
| size: { h: 14, w: 21, uom: 'cm' } | ||
| }, | ||
| { | ||
| item: 'mat', | ||
| qty: 85, | ||
| tags: ['gray'], | ||
| size: { h: 27.9, w: 35.5, uom: 'cm' } | ||
| }, | ||
| { | ||
| item: 'mousepad', | ||
| qty: 25, | ||
| tags: ['gel', 'blue'], | ||
| size: { h: 19, w: 22.85, uom: 'cm' } | ||
| } | ||
| ]); | ||
| // End Example 3 | ||
|
|
||
| expect(await db.collection('inventory').count({})).to.equal(3); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or 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,222 @@ | ||
| 'use strict'; | ||
|
|
||
| const setupDatabase = require('../functional/shared').setupDatabase; | ||
| const expect = require('chai').expect; | ||
| const MongoClient = require('../../lib/mongo_client'); | ||
|
|
||
| describe('examples(project-fields-from-query):', 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({}); | ||
| // Start Example 42 | ||
| await db.collection('inventory').insertMany([ | ||
| { | ||
| item: 'journal', | ||
| status: 'A', | ||
| size: { h: 14, w: 21, uom: 'cm' }, | ||
| instock: [{ warehouse: 'A', qty: 5 }] | ||
| }, | ||
| { | ||
| item: 'notebook', | ||
| status: 'A', | ||
| size: { h: 8.5, w: 11, uom: 'in' }, | ||
| instock: [{ warehouse: 'C', qty: 5 }] | ||
| }, | ||
| { | ||
| item: 'paper', | ||
| status: 'D', | ||
| size: { h: 8.5, w: 11, uom: 'in' }, | ||
| instock: [{ warehouse: 'A', qty: 60 }] | ||
| }, | ||
| { | ||
| item: 'planner', | ||
| status: 'D', | ||
| size: { h: 22.85, w: 30, uom: 'cm' }, | ||
| instock: [{ warehouse: 'A', qty: 40 }] | ||
| }, | ||
| { | ||
| item: 'postcard', | ||
| status: 'A', | ||
| size: { h: 10, w: 15.25, uom: 'cm' }, | ||
| instock: [{ warehouse: 'B', qty: 15 }, { warehouse: 'C', qty: 35 }] | ||
| } | ||
| ]); | ||
| // End Example 42 | ||
| }); | ||
|
|
||
| afterEach(async function() { | ||
| await client.close(); | ||
| client = undefined; | ||
| db = undefined; | ||
| }); | ||
|
|
||
| it('Return All Fields in Matching Documents', { | ||
| metadata: { requires: { topology: ['single'], mongodb: '>= 2.8.0' } }, | ||
| test: async function() { | ||
| // Start Example 43 | ||
| const cursor = db.collection('inventory').find({ | ||
| status: 'A' | ||
| }); | ||
| // End Example 43 | ||
|
|
||
| expect(await cursor.count()).to.equal(3); | ||
| } | ||
| }); | ||
|
|
||
| it('Return the Specified Fields and the ``_id`` Field Only', { | ||
| metadata: { requires: { topology: ['single'], mongodb: '>= 2.8.0' } }, | ||
| test: async function() { | ||
| // Start Example 44 | ||
| const cursor = db | ||
| .collection('inventory') | ||
| .find({ | ||
| status: 'A' | ||
| }) | ||
| .project({ item: 1, status: 1 }); | ||
| // End Example 44 | ||
|
|
||
| const docs = await cursor.toArray(); | ||
| docs.forEach(function(doc) { | ||
| expect(doc).to.have.all.keys(['_id', 'item', 'status']); | ||
| expect(doc).to.not.have.all.keys(['size', 'instock']); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| it('Suppress ``_id`` Field', { | ||
| metadata: { requires: { topology: ['single'], mongodb: '>= 2.8.0' } }, | ||
| test: async function() { | ||
| // Start Example 45 | ||
| const cursor = db | ||
| .collection('inventory') | ||
| .find({ | ||
| status: 'A' | ||
| }) | ||
| .project({ item: 1, status: 1, _id: 0 }); | ||
| // End Example 45 | ||
| const docs = await cursor.toArray(); | ||
| docs.forEach(function(doc) { | ||
| expect(doc).to.have.all.keys(['item', 'status']); | ||
| expect(doc).to.not.have.all.keys(['_id', 'size', 'instock']); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| it('Return All But the Excluded Fields', { | ||
| metadata: { requires: { topology: ['single'], mongodb: '>= 2.8.0' } }, | ||
| test: async function() { | ||
| // Start Example 46 | ||
| const cursor = db | ||
| .collection('inventory') | ||
| .find({ | ||
| status: 'A' | ||
| }) | ||
| .project({ status: 0, instock: 0 }); | ||
| // End Example 46 | ||
| const docs = await cursor.toArray(); | ||
| docs.forEach(function(doc) { | ||
| expect(doc).to.have.all.keys(['_id', 'item', 'size']); | ||
| expect(doc).to.not.have.all.keys(['status', 'instock']); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| it('Return Specific Fields in Embedded Documents', { | ||
| metadata: { requires: { topology: ['single'], mongodb: '>= 2.8.0' } }, | ||
| test: async function() { | ||
| // Start Example 47 | ||
| const cursor = db | ||
| .collection('inventory') | ||
| .find({ | ||
| status: 'A' | ||
| }) | ||
| .project({ item: 1, status: 1, 'size.uom': 1 }); | ||
| // End Example 47 | ||
| const docs = await cursor.toArray(); | ||
|
|
||
| docs.forEach(function(doc) { | ||
| expect(doc).to.have.all.keys(['_id', 'item', 'status', 'size']); | ||
| expect(doc).to.not.have.property('instock'); | ||
|
|
||
| const size = doc.size; | ||
| expect(size).to.have.property('uom'); | ||
| expect(size).to.not.have.all.keys(['h', 'w']); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| it('Suppress Specific Fields in Embedded Documents', { | ||
| metadata: { requires: { topology: ['single'], mongodb: '>= 2.8.0' } }, | ||
| test: async function() { | ||
| // Start Example 48 | ||
| const cursor = db | ||
| .collection('inventory') | ||
| .find({ | ||
| status: 'A' | ||
| }) | ||
| .project({ 'size.uom': 0 }); | ||
| // End Example 48 | ||
| const docs = await cursor.toArray(); | ||
| docs.forEach(function(doc) { | ||
| expect(doc).to.have.all.keys(['_id', 'item', 'status', 'size', 'instock']); | ||
| const size = doc.size; | ||
| expect(size).to.have.all.keys(['h', 'w']); | ||
| expect(size).to.not.have.property('uom'); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| it('Projection on Embedded Documents in an Array', { | ||
| metadata: { requires: { topology: ['single'], mongodb: '>= 2.8.0' } }, | ||
| test: async function() { | ||
| // Start Example 49 | ||
| const cursor = db | ||
| .collection('inventory') | ||
| .find({ | ||
| status: 'A' | ||
| }) | ||
| .project({ item: 1, status: 1, 'instock.qty': 1 }); | ||
| // End Example 49 | ||
| const docs = await cursor.toArray(); | ||
| docs.forEach(function(doc) { | ||
| expect(doc).to.have.all.keys(['_id', 'item', 'status', 'instock']); | ||
| expect(doc).to.not.have.property('size'); | ||
| doc.instock.forEach(function(subdoc) { | ||
| expect(subdoc).to.have.property('qty'); | ||
| expect(subdoc).to.not.have.property('warehouse'); | ||
| }); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| it('Project Specific Array Elements in the Returned Array', { | ||
| metadata: { requires: { topology: ['single'], mongodb: '>= 2.8.0' } }, | ||
| test: async function() { | ||
| // Start Example 50 | ||
| const cursor = db | ||
| .collection('inventory') | ||
| .find({ | ||
| status: 'A' | ||
| }) | ||
| .project({ item: 1, status: 1, instock: { $slice: -1 } }); | ||
| // End Example 50 | ||
| const docs = await cursor.toArray(); | ||
| docs.forEach(function(doc) { | ||
| expect(doc).to.have.all.keys(['_id', 'item', 'status', 'instock']); | ||
| expect(doc).to.not.have.property('size'); | ||
| expect(doc) | ||
| .to.have.property('instock') | ||
| .with.a.lengthOf(1); | ||
| }); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems a little inconsistent (pun intended), maybe
it('supports causal consistency', ...)?