Skip to content

Commit 679a1ec

Browse files
durranbaileympearson
authored andcommitted
test(NODE-4160): add aws lambda examples
1 parent 5d004f2 commit 679a1ec

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"check:bench": "node test/benchmarks/driverBench",
9999
"check:coverage": "nyc npm run test:all",
100100
"check:integration-coverage": "nyc npm run check:test",
101+
"check:lambda": "mocha --config test/examples/mocha_lambda.json test/examples/lambda.test.js",
101102
"check:lint": "npm run build:dts && npm run check:dts && npm run check:eslint && npm run check:tsd",
102103
"check:eslint": "eslint -v && eslint --max-warnings=0 --ext '.js,.ts' src test",
103104
"check:tsd": "tsd --version && tsd",

test/examples/handler.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// begin lambda connection
2+
const { MongoClient } = require('mongodb');
3+
4+
// MongoClient now auto-connects so no need to store the connect()
5+
// promise anywhere and reference it.
6+
const client = new MongoClient(process.env.MONGODB_URI);
7+
8+
module.exports.handler = async function () {
9+
const databases = await client.db('admin').command({ listDatabases: 1 });
10+
return {
11+
statusCode: 200,
12+
databases: databases
13+
};
14+
};
15+
// end lambda connection
16+
17+
module.exports.client = client;

test/examples/lambda.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { expect } = require('chai');
2+
const { client, handler } = require('./handler');
3+
4+
describe('AWS Lambda Examples', function () {
5+
describe('#handler', function () {
6+
context('when using standard authentication', function () {
7+
let response;
8+
9+
before(async function () {
10+
response = await handler();
11+
});
12+
13+
after(async function () {
14+
await client.close();
15+
});
16+
17+
it('returns the databases', async function () {
18+
expect(response.databases).to.exist;
19+
});
20+
21+
it('returns the status code', async function () {
22+
expect(response.statusCode).to.equal(200);
23+
});
24+
});
25+
});
26+
});

test/examples/mocha_lambda.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/mocharc.json",
3+
"require": [
4+
"test/examples/setup.js"
5+
],
6+
"extension": ["js"],
7+
"ui": "test/tools/runner/metadata_ui.js",
8+
"recursive": true,
9+
"timeout": 6000,
10+
"failZero": true,
11+
"reporter": "test/tools/reporter/mongodb_reporter.js",
12+
"sort": true,
13+
"color": true
14+
}

test/examples/setup.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const path = require('path');
2+
const Module = require('module');
3+
const loader = Module._load;
4+
5+
Module._load = function(request, loc) {
6+
if (request === 'mongodb') {
7+
arguments[0] = path.join(__dirname, '..', '..', 'lib');
8+
}
9+
return loader.apply(this, arguments);
10+
};

0 commit comments

Comments
 (0)