Skip to content

Commit

Permalink
test(populate): repro #5858
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Dec 19, 2017
1 parent 1996a06 commit ed8e874
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5859,6 +5859,70 @@ describe('model: populate:', function() {
catch(done);
});

it('subPopulate under discriminators race condition (gh-5858)', function() {
var options = { discriminatorKey: 'kind' };
var activitySchema = new Schema({ title: { type: String } }, options);

var dateActivitySchema = new Schema({
postedBy: {
type: Schema.Types.ObjectId,
ref: 'gh5858',
required: true
}
}, options);

var eventActivitySchema = new Schema({ test: String }, options);

var logSchema = new Schema({
seq: Number,
activity: {
type: Schema.Types.ObjectId,
refPath: 'kind',
required: true
},
kind: String
}, options);

var User = db.model('gh5858', { name: String });
var Activity = db.model('gh5858_0', activitySchema);
var DateActivity = Activity.discriminator('gh5858_1', dateActivitySchema);
var EventActivity = Activity.discriminator('gh5858_2', eventActivitySchema);
var Log = db.model('gh5858_3', logSchema);

var dateActivity;
var eventActivity;
return User.create({ name: 'val' }).
then(function(user) {
return DateActivity.create({ title: 'test', postedBy: user._id });
}).
then(function(_dateActivity) {
dateActivity = _dateActivity;
return EventActivity.create({ title: 'test' });
}).
then(function(_eventActivity) {
eventActivity = _eventActivity;
return Log.create([
{ seq: 1, activity: eventActivity._id, kind: 'gh5858_2' },
{ seq: 2, activity: dateActivity._id, kind: 'gh5858_1' }
]);
}).
then(function() {
return Log.find({}).
populate({
path: 'activity',
populate: { path: 'postedBy' }
}).
sort({ seq:-1 });
}).
then(function(results) {
assert.equal(results.length, 2);
assert.equal(results[0].activity.kind, 'gh5858_1' );
assert.equal(results[0].activity.postedBy.name, 'val');
assert.equal(results[1].activity.kind, 'gh5858_2' );
assert.equal(results[1].activity.postedBy, null);
});
});

it('specify model in populate (gh-4264)', function(done) {
var PersonSchema = new Schema({
name: String,
Expand Down

0 comments on commit ed8e874

Please sign in to comment.