Skip to content

Commit

Permalink
Add test for Automatticgh-14098
Browse files Browse the repository at this point in the history
  • Loading branch information
csy1204 committed Nov 19, 2023
1 parent f94affd commit 364fc10
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion test/model.populate.test.js
Expand Up @@ -8254,11 +8254,68 @@ describe('model: populate:', function() {
path: 'companyId',
justOne: true
}
});
}).lean();
console.log(populatedRides)

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

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Missing semicolon
assert.deepEqual(populatedRides[0].files, []);
assert.deepEqual(populatedRides[1].files, []);
});

it('doesnt insert empty document when lean populating a path within an underneath non-existent document array (gh-14098)', async function() {
const userSchema = new mongoose.Schema({
fullName: String,
company: String
});
const User = db.model('User', userSchema);

const fileSchema = new mongoose.Schema({
_id: String,
uploaderId: {
type: mongoose.ObjectId,
ref: 'User'
}
}, { toObject: { virtuals: true }, toJSON: { virtuals: true } });
fileSchema.virtual('uploadedBy', {
ref: 'User',
localField: 'uploaderId',
foreignField: '_id',
justOne: true
});

const contentSchema = new mongoose.Schema({
memo: String,
files: { type: [fileSchema], default: [] }
}, { toObject: { virtuals: true }, toJSON: { virtuals: true }, _id: false });

const postSchema = new mongoose.Schema({
title: String,
content: { type: contentSchema }
}, { toObject: { virtuals: true }, toJSON: { virtuals: true } });
const Post = db.model('Test1', postSchema);

const user = await User.create({ fullName: 'John Doe', company: 'GitHub' });
await Post.create([
{ title: 'London-Paris' },
{
title: 'Berlin-Moscow',
content: {
memo: 'Not Easy',
files: [{ _id: '123', uploaderId: user._id }]
}
}
]);
await Post.updateMany({}, { $unset: { 'content.files': 1 } });
const populatedRides = await Post.find({}).populate({
path: 'content.files.uploadedBy',
justOne: true
}).lean();

console.log(populatedRides[0].content);
console.log(populatedRides[1].content);

assert.equal(populatedRides[0].content.files, undefined);
assert.equal(populatedRides[1].content.files, undefined);
})

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

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Missing semicolon

it('sets empty array if populating undefined path (gh-8455)', async function() {
const TestSchema = new Schema({
thingIds: [mongoose.ObjectId]
Expand Down

0 comments on commit 364fc10

Please sign in to comment.