Skip to content

Commit

Permalink
write test
Browse files Browse the repository at this point in the history
  • Loading branch information
IslandRhythms committed Oct 20, 2023
1 parent 7efa151 commit 627a53e
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4263,9 +4263,7 @@ Model.populate = async function populate(docs, paths) {
if (typeof paths === 'function' || typeof arguments[2] === 'function') {
throw new MongooseError('Model.populate() no longer accepts a callback');
}

const _this = this;

// normalized paths
paths = utils.populate(paths);
// data that should persist across subPopulate calls
Expand Down Expand Up @@ -4322,6 +4320,7 @@ const excludeIdRegGlobal = /\s?-_id\s?/g;

function populate(model, docs, options, callback) {
const populateOptions = options;
// playing hide and seek with strictPopulate
if (options.strictPopulate == null) {
if (options._localModel != null && options._localModel.schema._userProvidedOptions.strictPopulate != null) {
populateOptions.strictPopulate = options._localModel.schema._userProvidedOptions.strictPopulate;
Expand Down
1 change: 0 additions & 1 deletion lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2305,7 +2305,6 @@ Query.prototype._find = async function _find() {
_completeManyLean(_this.model.schema, docs, null, completeManyOptions) :
completeMany(_this.model, docs, fields, userProvidedFields, completeManyOptions);
}

const pop = helpers.preparePopulationOptionsMQ(_this, mongooseOptions);

if (mongooseOptions.lean) {
Expand Down
113 changes: 113 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10330,6 +10330,119 @@ describe('model: populate:', function() {
});

describe('strictPopulate', function() {
it('does not throw an error when using strictPopulate on a nested path (gh-13863)', async function() {
const testSchema = new mongoose.Schema({
name: String,
location: String,
occupation: String,

Check failure on line 10337 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma

Check failure on line 10337 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma
});

Check failure on line 10339 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed

Check failure on line 10339 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
const ASchema = new mongoose.Schema({
name: String,
age: Number,
weight: Number,
dModel: {
type: 'ObjectId',
ref: 'gh13863DModel'
},
fModel: {
type: 'ObjectId',
ref: 'gh13863FModel'
}
});

Check failure on line 10353 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed

Check failure on line 10353 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
const XSchema = new mongoose.Schema({
name: String,
car: String,
make: String,
model: String,
aModel: {
type: 'ObjectId',
ref: 'gh13863AModel'
},
testModel: {
type: 'ObjectId',
ref: 'gh13863Test'
},
cModel: {
type: 'ObjectId',
ref: 'gh13863CModel'
}
});

Check failure on line 10372 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed

Check failure on line 10372 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
const CSchema = new Schema({
name: String,
hobbies: String
})

Check failure on line 10376 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Missing semicolon

Check failure on line 10376 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Missing semicolon

Check failure on line 10377 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed

Check failure on line 10377 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
const DSchema = new Schema({
name: String,

Check failure on line 10379 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma

Check failure on line 10379 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma
});

Check failure on line 10381 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed

Check failure on line 10381 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
const FSchema = new Schema({
name: String,

Check failure on line 10383 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma

Check failure on line 10383 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Unexpected trailing comma
});

Check failure on line 10385 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed

Check failure on line 10385 in test/model.populate.test.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
const GSchema = new Schema({
name: String
});

const Test = db.model('gh13863Test', testSchema);
const AModel = db.model('gh13863AModel', ASchema);
const XModel = db.model('gh13863XModel', XSchema);
const CModel = db.model('gh13863CModel', CSchema);
const DModel = db.model('gh13863DModel', DSchema);
const FModel = db.model('gh13863FModel', FSchema);
const GModel = db.model('gh13863GModel', GSchema);

const gDoc = await GModel.create({
name: 'G-Man'
});
const dDoc = await DModel.create({
name: 'Dirty Dan',
});

const fDoc = await FModel.create({
name: 'Filthy Frank',
});

const testDoc = await Test.create({
name: 'Test Testserson',
location: 'Florida',
occupation: 'Tester'
});

const aDoc = await AModel.create({
name: 'A-men',
age: 4,
weight: 404,
dModel: dDoc._id,
fModel: fDoc._id
});

const cDoc = await CModel.create({
name: 'C-ya',
hobbies: 'Leaving'
});

await XModel.create({
name: 'XCOM',
aModel: aDoc._id,
cModel: cDoc._id,
testModel: testDoc._id
});

const res = await XModel.find().populate({
path: 'aModel testModel cModel',
populate: {
path: 'dModel fModel',
populate: 'gModel',
strictPopulate: false
}
});

assert.ok(res);
});
it('reports full path when throwing `strictPopulate` error with deep populate (gh-10923)', async function() {
const L2 = db.model('Test', new Schema({ name: String }));

Expand Down

0 comments on commit 627a53e

Please sign in to comment.