Skip to content

Commit

Permalink
Merge branch 'master' into changeCommentType
Browse files Browse the repository at this point in the history
  • Loading branch information
hasezoey committed Aug 30, 2022
2 parents 11edf8d + bbeaa9a commit 3aa2411
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 91 deletions.
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: 13 additions & 13 deletions docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ You can refer to [this table](https://docs.mongodb.com/drivers/node/current/comp

Below are the [semver](http://semver.org/) ranges representing which versions of mongoose are compatible with the listed versions of MongoDB server.

| MongoDB Sever | Mongoose |
| :-----------: | :----------------------------: |
| `6.x` | `^6.5.0` |
| `5.x` | `^6.0.0` |
| `4.4.x` | `^5.10.0 \| ^6.0.0` |
| `4.2.x` | `^5.7.0 \| ^6.0.0` |
| `4.0.x` | `^5.2.0 \| ^6.0.0` |
| `3.6.x` | `^5.0.0` |
| `3.4.x` | `^4.7.3 \| ^5.0.0` |
| `3.2.x` | `^4.3.0 \| 5.0.0` |
| `3.0.x` | `^3.8.22 \| ^4.0.0 \| ^5.0.0` |
| `2.6.x` | `^3.8.8 \| ^4.0.0` |
| `2.4.x` | `^3.8.0 \| ^4.0.0` |
| MongoDB Server | Mongoose |
| :------------: | :---------------------------: |
| `6.x` | `^6.5.0` |
| `5.x` | `^6.0.0` |
| `4.4.x` | `^5.10.0 \| ^6.0.0` |
| `4.2.x` | `^5.7.0 \| ^6.0.0` |
| `4.0.x` | `^5.2.0 \| ^6.0.0` |
| `3.6.x` | `^5.0.0` |
| `3.4.x` | `^4.7.3 \| ^5.0.0` |
| `3.2.x` | `^4.3.0 \| 5.0.0` |
| `3.0.x` | `^3.8.22 \| ^4.0.0 \| ^5.0.0` |
| `2.6.x` | `^3.8.8 \| ^4.0.0` |
| `2.4.x` | `^3.8.0 \| ^4.0.0` |

Note that Mongoose `5.x` dropped support for all versions of MongoDB before `3.0.0`. If you need to use MongoDB `2.6` or older, use Mongoose `4.x`.
3 changes: 3 additions & 0 deletions lib/cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const ALLOWED_GEOWITHIN_GEOJSON_TYPES = ['Polygon', 'MultiPolygon'];
* @param {Schema} schema
* @param {Object} obj Object to cast
* @param {Object} [options] the query options
* @param {Boolean|"throw"} [options.strict] Wheter to enable all strict options
* @param {Boolean|"throw"} [options.strictQuery] Enable strict Queries
* @param {Boolean} [options.upsert]
* @param {Query} [context] passed to setters
* @api private
*/
Expand Down
4 changes: 3 additions & 1 deletion lib/helpers/model/discriminator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const CUSTOMIZABLE_DISCRIMINATOR_OPTIONS = {
toJSON: true,
toObject: true,
_id: true,
id: true
id: true,
virtuals: true,
methods: true
};

/*!
Expand Down
1 change: 1 addition & 0 deletions lib/types/array/methods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ const methods = {
let ret;
const arr = utils.isMongooseArray(this) ? this.__array : this;

this._markModified();
_checkManualPopulation(this, Array.prototype.slice.call(arguments, 2));

if (arguments.length) {
Expand Down
28 changes: 22 additions & 6 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

const mongoose = require('../');
const mongoose = require('../index');
const Collection = mongoose.Collection;
const assert = require('assert');

Expand Down 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
6 changes: 6 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11753,6 +11753,12 @@ describe('document', function() {
const res = await Test.findById(doc);
assert.equal(res.docArr[0].counter, 1);
});
it('Splice call registers path modification', async function() {
await Test.create({ docArr: [{ counter: 0 }, { counter: 2 }, { counter: 3 }, { counter: 4 }] });
const doc = await Test.findOne();
doc.docArr.splice(1, 0, { counter: 1 });
assert.equal(doc.isModified('docArr'), true);
});
});

it('stores CastError if trying to $inc a non-numeric path', async function() {
Expand Down

0 comments on commit 3aa2411

Please sign in to comment.