Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"mongodb-mock-server": "^1.0.0",
"mongodb-test-runner": "^1.1.18",
"prettier": "~1.12.0",
"semver": "5.4.1",
"sinon": "^4.3.0",
"worker-farm": "^1.5.0"
},
Expand Down
36 changes: 36 additions & 0 deletions test/examples/array_filters.js
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 }] }
);
}
});
});
49 changes: 49 additions & 0 deletions test/examples/causal_consistency.js
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', {
Copy link
Member

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', ...) ?

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;
}
});
});
77 changes: 77 additions & 0 deletions test/examples/insert.js
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);
}
});
});
222 changes: 222 additions & 0 deletions test/examples/project_fields_from_query_results.js
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);
});
}
});
});
Loading