Skip to content

Commit

Permalink
Fix minor issues and add test cases for #8543
Browse files Browse the repository at this point in the history
  • Loading branch information
samgladstone committed Jan 30, 2020
1 parent 27f45a5 commit 834200d
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/schema/SingleNestedPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ SingleNestedPath.prototype.doValidateSync = function(value, scope, options) {
*/

SingleNestedPath.prototype.discriminator = function(name, schema, value) {
discriminator(this.caster, name, schema, value);
schema = discriminator(this.caster, name, schema, value);

this.caster.discriminators[name] = _createConstructor(schema, this.caster);

Expand Down
4 changes: 2 additions & 2 deletions test/docs/discriminators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ describe('discriminator docs', function () {
var event1 = new ClickedLinkEvent({ _id: 'custom id', time: '4pm' });
// Woops, clickedLinkSchema overwrites the `time` path, but **not**
// the `_id` path because that was implicitly added.
assert.ok(typeof event1._id === 'string');
assert.ok(typeof event1.time === 'string');
assert.strictEqual(typeof event1._id, 'string');
assert.strictEqual(typeof event1.time, 'string');

// acquit:ignore:start
done();
Expand Down
6 changes: 3 additions & 3 deletions test/model.discriminator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ describe('model', function() {
});

it('inherits statics', function(done) {
assert.strictEqual(Employee.findByGender, EmployeeSchema.statics.findByGender);
assert.strictEqual(Employee.findByGender, PersonSchema.statics.findByGender);
assert.strictEqual(Employee.findByDepartment, EmployeeSchema.statics.findByDepartment);
assert.equal(Person.findByDepartment, undefined);
done();
Expand Down Expand Up @@ -1329,7 +1329,7 @@ describe('model', function() {
// Delete every model
afterEach(function() { mongoose.deleteModel(/.+/); });

it('does not modify _id path of the passed in schema the _id is not auto generated', function() {
it('does not modify _id path of the passed in schema the _id is not auto generated (gh-8543)', function() {
const model = mongoose.model('Model', new mongoose.Schema({ _id: Number }));
const passedInSchema = new mongoose.Schema({});
model.discriminator('Discrimintaor', passedInSchema);
Expand All @@ -1338,7 +1338,7 @@ describe('model', function() {

function throwErrorOnClone() { throw new Error('clone() was called on the unrelated schema'); };

it('when the base schema has an _id that is not auto generated', function() {
it('when the base schema has an _id that is not auto generated (gh-8543) (gh-8546)', function() {
const unrelatedSchema = new mongoose.Schema({});
unrelatedSchema.clone = throwErrorOnClone;
mongoose.model('UnrelatedModel', unrelatedSchema);
Expand Down
146 changes: 146 additions & 0 deletions test/schema.singlenestedpath.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
'use strict';

/**
* Module dependencies.
*/

const mongoose = require('./common').mongoose;

const assert = require('assert');

const Schema = mongoose.Schema;

describe('SingleNestedPath', function() {
describe('discriminator()', function() {
describe('recursive nested discriminators', function() {
it('allow multiple levels of data in the schema', function() {
const singleEventSchema = new Schema({
message: String,
}, { _id: false, discriminatorKey: 'kind' });

const subEventSchema = new Schema({
sub_events: [singleEventSchema]
}, {_id: false});

subEventSchema.path('sub_events').discriminator('SubEvent', subEventSchema);

let currentEventLevel = subEventSchema;
for (let i = 0; i < 5; i++) {
const subEventSchemaDiscriminators = currentEventLevel.path('sub_events').schema.discriminators;
assert.ok(subEventSchemaDiscriminators);
assert.ok(subEventSchemaDiscriminators.SubEvent)
currentEventLevel = subEventSchemaDiscriminators.SubEvent;
}
});

it('allow multiple levels of data in a document', function() {
const singleEventSchema = new Schema({
message: String,
}, { _id: false, discriminatorKey: 'kind' });

const subEventSchema = new Schema({
sub_events: [singleEventSchema]
}, {_id: false});

subEventSchema.path('sub_events').discriminator('SubEvent', subEventSchema);

const SubEvent = mongoose.model('MultiLevelDataDoc', subEventSchema);
const multiLevel = {
// To create a recursive document, the schema was modified, so kind & message are added
kind: 'SubEvent',
message: 'level 1',
sub_events: [{
kind: 'SubEvent',
message: 'level 2',
sub_events: [{
kind: 'SubEvent',
message: 'level 3',
sub_events: [{
kind: 'SubEvent',
message: 'level 4',
sub_events: [{
kind: 'SubEvent',
message: 'level 5',
sub_events: [],
}],
}],
}],
}]
};
const subEvent = SubEvent(multiLevel);

assert.deepStrictEqual(multiLevel, subEvent.toJSON());
});

it('allow multiple levels of data in the schema when the base schema has _id without auto', function() {
const singleEventSchema = new Schema({
_id: { type: Number, required: true },
message: String,
}, { discriminatorKey: 'kind' });

const subEventSchema = new Schema({
sub_events: [singleEventSchema]
});

subEventSchema.path('sub_events').discriminator('SubEvent', subEventSchema);

// To create a recursive document, the schema was modified, so the _id property is now a number
assert.equal(subEventSchema.path('_id').instance, 'Number');

let currentEventLevel = subEventSchema;
for (let i = 0; i < 5; i++) {
const subEventSchemaDiscriminators = currentEventLevel.path('sub_events').schema.discriminators;
assert.ok(subEventSchemaDiscriminators);
assert.ok(subEventSchemaDiscriminators.SubEvent)
currentEventLevel = subEventSchemaDiscriminators.SubEvent;
assert.equal(currentEventLevel.path('_id').instance, 'Number');
}
});

it('allow multiple levels of data in a document when the base schema has _id without auto', function() {
const singleEventSchema = new Schema({
_id: { type: Number, required: true },
message: String,
}, { discriminatorKey: 'kind' });

const subEventSchema = new Schema({
sub_events: [singleEventSchema]
});

subEventSchema.path('sub_events').discriminator('SubEvent', subEventSchema);

const SubEvent = mongoose.model('MultiLevelDataWithIdDoc', subEventSchema);
const multiLevel = {
// To create a recursive document, the schema was modified, so kind & message are added & _id is now Number
_id: 1,
kind: 'SubEvent',
message: 'level 1',
sub_events: [{
_id: 1,
kind: 'SubEvent',
message: 'level 2',
sub_events: [{
_id: 1,
kind: 'SubEvent',
message: 'level 3',
sub_events: [{
_id: 1,
kind: 'SubEvent',
message: 'level 4',
sub_events: [{
_id: 1,
kind: 'SubEvent',
message: 'level 5',
sub_events: [],
}],
}],
}],
}]
};
const subEvent = SubEvent(multiLevel);

assert.deepStrictEqual(multiLevel, subEvent.toJSON());
});
})
});
});

0 comments on commit 834200d

Please sign in to comment.