Skip to content
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

Update some tests and config to be more maintainable #12356

Merged
merged 15 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
wget -q https://downloads.mongodb.org/linux/mongodb-linux-x86_64-${{ matrix.mongodb-os }}-${{ matrix.mongodb }}.tgz
tar xf mongodb-linux-x86_64-${{ matrix.mongodb-os }}-${{ matrix.mongodb }}.tgz
mkdir -p ./data/db/27017 ./data/db/27000
printf "\n--timeout 8000" >> ./test/mocha.opts
printf "\ntimeout: 8000" >> ./.mocharc.yml
./mongodb-linux-x86_64-${{ matrix.mongodb-os }}-${{ matrix.mongodb }}/bin/mongod --setParameter ttlMonitorSleepSecs=1 --fork --dbpath ./data/db/27017 --syslog --port 27017
sleep 2
mongod --version
Expand Down Expand Up @@ -109,7 +109,7 @@ jobs:
wget -q https://downloads.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-5.0.2.tgz
tar xf mongodb-linux-x86_64-ubuntu2004-5.0.2.tgz
mkdir -p ./data/db/27017 ./data/db/27000
printf "\n--timeout 8000" >> ./test/mocha.opts
printf "\ntimeout: 8000" >> ./.mocharc.yml
./mongodb-linux-x86_64-ubuntu2004-5.0.2/bin/mongod --setParameter ttlMonitorSleepSecs=1 --fork --dbpath ./data/db/27017 --syslog --port 27017
sleep 2
mongod --version
Expand Down
2 changes: 2 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
reporter: spec # better to identify failing / slow tests than "dot"
ui: bdd # explicitly setting, even though it is mocha default
26 changes: 21 additions & 5 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,33 @@ module.exports = function(options) {
return conn;
};

function getUri(env, default_uri, db) {
const use = env ? env : default_uri;
const lastIndex = use.lastIndexOf('/');
// use length if lastIndex is 9 or lower, because that would mean it found the last character of "mongodb://"
return use.slice(0, lastIndex <= 9 ? use.length : lastIndex) + `/${db}`;
}

/**
* Testing Databases, used for consistency
*/

const databases = module.exports.databases = [
'mongoose_test',
'mongoose_test_2'
];

/*!
* testing uri
*/

module.exports.uri = process.env.MONGOOSE_TEST_URI || 'mongodb://127.0.0.1:27017/mongoose_test';
module.exports.uri = getUri(process.env.MONGOOSE_TEST_URI, 'mongodb://127.0.0.1:27017/', databases[0]);

/*!
* testing uri for 2nd db
*/

module.exports.uri2 = 'mongodb://127.0.0.1:27017/mongoose_test_2';
module.exports.uri2 = getUri(process.env.MONGOOSE_TEST_URI, 'mongodb://127.0.0.1:27017/', databases[1]);

/**
* expose mongoose
Expand Down Expand Up @@ -172,7 +188,7 @@ before(async function() {
const uri = await startReplicaSet();

module.exports.uri = uri;
module.exports.uri2 = uri.replace('mongoose_test', 'mongoose_test2');
module.exports.uri2 = uri.replace(databases[0], databases[1]);

process.env.REPLICA_SET = 'rs0';

Expand Down Expand Up @@ -223,7 +239,7 @@ async function startReplicaSet() {
args: ['--setParameter', 'ttlMonitorSleepSecs=1']
}
],
dbName: 'mongoose_test',
dbName: databases[0],
replSet: {
name: 'rs0',
count: 2,
Expand All @@ -236,5 +252,5 @@ async function startReplicaSet() {

await new Promise(resolve => setTimeout(resolve, 10000));

return replSet.getUri('mongoose_test');
return replSet.getUri(databases[0]);
}
46 changes: 24 additions & 22 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const MongooseError = require('../lib/error/index');

const mongoose = start.mongoose;
const Schema = mongoose.Schema;
const uri = start.uri;

/**
* Test.
Expand All @@ -26,7 +25,7 @@ describe('connections:', function() {

describe('openUri (gh-5304)', function() {
it('with mongoose.createConnection()', function() {
const conn = mongoose.createConnection('mongodb://127.0.0.1/mongoosetest');
const conn = mongoose.createConnection(start.uri.slice(0, start.uri.lastIndexOf('/')) + '/' + start.databases[0]);
assert.equal(conn.constructor.name, 'NativeConnection');

const Test = conn.model('Test', new Schema({ name: String }));
Expand All @@ -37,9 +36,12 @@ describe('connections:', function() {
return conn.asPromise().
then(function(conn) {
assert.equal(conn.constructor.name, 'NativeConnection');
assert.equal(conn.host, '127.0.0.1');
assert.equal(conn.port, 27017);
assert.equal(conn.name, 'mongoosetest');
// the regex below extract the first ip & port, because the created connection's properties only have the first anyway as "host" and "port"
const match = /mongodb:\/\/([\d.]+)(?::(\d+))?(?:,[\d.]+(?::\d+)?)*\/(\w+)/i.exec(start.uri);
assert.ok(match);
assert.equal(conn.host, match[1]);
assert.equal(conn.port, parseInt(match[2]));
assert.equal(conn.name, start.databases[0]);

return findPromise;
}).
Expand All @@ -57,7 +59,7 @@ describe('connections:', function() {
});

it('with autoCreate (gh-6489)', async function() {
const conn = await mongoose.createConnection(uri, {
const conn = await mongoose.createConnection(start.uri, {
// autoCreate: true
}).asPromise();

Expand All @@ -81,7 +83,7 @@ describe('connections:', function() {
});

it('with autoCreate = false (gh-8814)', async function() {
const conn = await mongoose.createConnection(uri, {
const conn = await mongoose.createConnection(start.uri, {
autoCreate: false
}).asPromise();

Expand All @@ -96,7 +98,7 @@ describe('connections:', function() {
});

it('autoCreate when collection already exists does not fail (gh-7122)', async function() {
const conn = await mongoose.createConnection(uri).asPromise();
const conn = await mongoose.createConnection(start.uri).asPromise();

const schema = new mongoose.Schema({
name: {
Expand Down Expand Up @@ -484,7 +486,7 @@ describe('connections:', function() {
});

it('uses default database in uri if options.dbName is not provided', function() {
return mongoose.createConnection('mongodb://127.0.0.1:27017/default-db-name').
return mongoose.createConnection(start.uri.slice(0, start.uri.lastIndexOf('/')) + '/default-db-name').
asPromise().
then(db => {
assert.equal(db.name, 'default-db-name');
Expand Down Expand Up @@ -560,7 +562,7 @@ describe('connections:', function() {

it('saves correctly', async function() {
const db = start();
const db2 = db.useDb('mongoose-test-2');
const db2 = db.useDb(start.databases[1]);

const schema = new Schema({
body: String,
Expand Down Expand Up @@ -595,7 +597,7 @@ describe('connections:', function() {

it('emits connecting events on both', async function() {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
const db2 = db.useDb(start.databases[1]);
let hit = false;

db2.on('connecting', async function() {
Expand All @@ -617,7 +619,7 @@ describe('connections:', function() {

it('emits connected events on both', function() {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
const db2 = db.useDb(start.databases[1]);
let hit = false;

db2.on('connected', function() {
Expand All @@ -638,7 +640,7 @@ describe('connections:', function() {

it('emits open events on both', function() {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
const db2 = db.useDb(start.databases[1]);
let hit = false;
db2.on('open', function() {
hit && close();
Expand All @@ -658,7 +660,7 @@ describe('connections:', function() {

it('emits disconnecting events on both, closing initial db', function(done) {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
const db2 = db.useDb(start.databases[1]);
let hit = false;
db2.on('disconnecting', function() {
hit && done();
Expand All @@ -676,7 +678,7 @@ describe('connections:', function() {

it('emits disconnecting events on both, closing secondary db', function(done) {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
const db2 = db.useDb(start.databases[1]);
let hit = false;
db2.on('disconnecting', function() {
hit && done();
Expand All @@ -694,7 +696,7 @@ describe('connections:', function() {

it('emits disconnected events on both, closing initial db', function(done) {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
const db2 = db.useDb(start.databases[1]);
let hit = false;
db2.on('disconnected', function() {
hit && done();
Expand All @@ -712,7 +714,7 @@ describe('connections:', function() {

it('emits disconnected events on both, closing secondary db', function(done) {
const db = mongoose.createConnection();
const db2 = db.useDb('mongoose-test-2');
const db2 = db.useDb(start.databases[1]);
let hit = false;
db2.on('disconnected', function() {
hit && done();
Expand All @@ -730,7 +732,7 @@ describe('connections:', function() {

it('closes correctly for all dbs, closing initial db', async function() {
const db = await start({ noErrorListener: true }).asPromise();
const db2 = db.useDb('mongoose-test-2');
const db2 = db.useDb(start.databases[1]);

const p = new Promise(resolve => {
db2.on('close', function() {
Expand All @@ -743,7 +745,7 @@ describe('connections:', function() {

it('handles re-opening base connection (gh-11240)', async function() {
const db = await start().asPromise();
const db2 = db.useDb('mongoose-test-2');
const db2 = db.useDb(start.databases[1]);

await db.close();

Expand All @@ -753,7 +755,7 @@ describe('connections:', function() {

it('closes correctly for all dbs, closing secondary db', function(done) {
const db = start();
const db2 = db.useDb('mongoose-test-2');
const db2 = db.useDb(start.databases[1]);

db.on('disconnected', function() {
done();
Expand All @@ -763,8 +765,8 @@ describe('connections:', function() {

it('cache connections to the same db', function() {
const db = start();
const db2 = db.useDb('mongoose-test-2', { useCache: true });
const db3 = db.useDb('mongoose-test-2', { useCache: true });
const db2 = db.useDb(start.databases[1], { useCache: true });
const db3 = db.useDb(start.databases[1], { useCache: true });

assert.strictEqual(db2, db3);
db.close();
Expand Down
22 changes: 10 additions & 12 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const mongoose = start.mongoose;
const Mongoose = mongoose.Mongoose;
const Schema = mongoose.Schema;

const uri = start.uri;

const options = {};

describe('mongoose module:', function() {
Expand All @@ -25,15 +23,15 @@ describe('mongoose module:', function() {
const goose = new Mongoose();
const db = goose.connection;

await goose.connect(process.env.MONGOOSE_TEST_URI || uri, options);
await goose.connect(start.uri, options);
await db.close();
});

it('with promise (gh-3790)', async function() {
const goose = new Mongoose();
const db = goose.connection;

await goose.connect(process.env.MONGOOSE_TEST_URI || uri, options);
await goose.connect(start.uri, options);

await db.close();
});
Expand Down Expand Up @@ -85,7 +83,7 @@ describe('mongoose module:', function() {
const User = mongoose.model('User', new Schema({ name: String }));


await mongoose.connect(uri);
await mongoose.connect(start.uri);
await User.findOne();
assert.equal(written.length, 1);
assert.ok(written[0].startsWith('users.findOne('));
Expand Down Expand Up @@ -190,7 +188,7 @@ describe('mongoose module:', function() {
name: { type: String, required: true }
}));

await mongoose.connect(uri, options);
await mongoose.connect(start.uri, options);

const err = await M.updateOne({}, { name: null }).then(() => null, err => err);
assert.ok(err.errors['name']);
Expand Down Expand Up @@ -472,7 +470,7 @@ describe('mongoose module:', function() {
const M = mongoose.model('gh6728', schema);


await mongoose.connect(uri);
await mongoose.connect(start.uri);

const doc = new M({ name: 'foo' });

Expand Down Expand Up @@ -525,7 +523,7 @@ describe('mongoose module:', function() {
let disconnections = 0;
let pending = 4;

mong.connect(process.env.MONGOOSE_TEST_URI || uri, options);
mong.connect(start.uri, options);
const db = mong.connection;

function cb() {
Expand All @@ -548,7 +546,7 @@ describe('mongoose module:', function() {
const events = [];
mong.events.on('createConnection', conn => events.push(conn));

const db2 = mong.createConnection(process.env.MONGOOSE_TEST_URI || uri, options);
const db2 = mong.createConnection(start.uri, options);

assert.equal(events.length, 1);
assert.equal(events[0], db2);
Expand All @@ -570,7 +568,7 @@ describe('mongoose module:', function() {
it('with callback', function(done) {
const mong = new Mongoose();

mong.connect(process.env.MONGOOSE_TEST_URI || uri, options);
mong.connect(start.uri, options);

mong.connection.on('open', function() {
mong.disconnect(function() {
Expand All @@ -582,7 +580,7 @@ describe('mongoose module:', function() {
it('with promise (gh-3790)', function(done) {
const _mongoose = new Mongoose();

_mongoose.connect(process.env.MONGOOSE_TEST_URI || uri, options);
_mongoose.connect(start.uri, options);

_mongoose.connection.on('open', function() {
_mongoose.disconnect().then(function() { done(); });
Expand Down Expand Up @@ -712,7 +710,7 @@ describe('mongoose module:', function() {
it('with single mongod', async function() {
const mong = new Mongoose();

await mong.connect(uri, options);
await mong.connect(start.uri, options);

await mong.connection.close();
});
Expand Down
2 changes: 0 additions & 2 deletions test/mocha.opts

This file was deleted.