Skip to content

Commit

Permalink
test(populate): repro #5109
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jul 20, 2019
1 parent fb46fe4 commit 95c2a55
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8490,4 +8490,38 @@ describe('model: populate:', function() {
assert.equal(doc.childDocs[0].name, 'test');
});
});

it('handles refPath on discriminator when populating top-level model (gh-5109)', function() {
const options = { discriminatorKey: 'kind' };
const Post = db.model('gh5109_Post', new Schema({ time: Date, text: String }, options));

const MediaPost = Post.discriminator('gh5109_MediaPost', new Schema({
media: { type: Schema.Types.ObjectId, refPath: 'mediaType' },
mediaType: String // either 'Image' or 'Video'
}, options));
const Image = db.model('gh5109_Image',
new Schema({ url: String }));
const Video = db.model('gh5109_Video',
new Schema({ url: String, duration: Number }));

return co(function*() {
const image = yield Image.create({ url: 'test' });
const video = yield Video.create({ url: 'foo', duration: 42 });

yield MediaPost.create([
{ media: image._id, mediaType: 'gh5109_Image' },
{ media: video._id, mediaType: 'gh5109_Video' }
]);

const docs = yield Post.find().populate('media').sort({ mediaType: 1 });

assert.equal(docs.length, 2);
assert.ok(docs[0].populated('media'));
assert.ok(docs[1].populated('media'));

assert.equal(docs[0].media.url, 'test');
assert.equal(docs[1].media.url, 'foo');
assert.equal(docs[1].media.duration, 42);
});
});
});

0 comments on commit 95c2a55

Please sign in to comment.