|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var Schema = require('../../lib/schema'); |
| 4 | +var assert = require('assert'); |
| 5 | +var getSchemaTypes = require('../../lib/services/populate/getSchemaTypes'); |
| 6 | + |
| 7 | +describe('getSchemaTypes', function() { |
| 8 | + it('handles embedded discriminators (gh-5970)', function(done) { |
| 9 | + var ItemSchema = new Schema({ |
| 10 | + title: { |
| 11 | + type: String, |
| 12 | + required: true |
| 13 | + } |
| 14 | + }, {discriminatorKey: 'type'}); |
| 15 | + |
| 16 | + var ItemBookSchema = new Schema({ |
| 17 | + author: { |
| 18 | + type: String, |
| 19 | + ref: 'Ref1' |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + var ItemEBookSchema = new Schema({ |
| 24 | + author: { |
| 25 | + type: String, |
| 26 | + ref: 'Ref2' |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + var OtherItem = new Schema({ name: String }); |
| 31 | + |
| 32 | + var BundleSchema = new Schema({ |
| 33 | + items: [{ |
| 34 | + type: ItemSchema, |
| 35 | + required: false |
| 36 | + }] |
| 37 | + }); |
| 38 | + |
| 39 | + BundleSchema.path('items').discriminator('Book', ItemBookSchema); |
| 40 | + BundleSchema.path('items').discriminator('EBook', ItemEBookSchema); |
| 41 | + BundleSchema.path('items').discriminator('Other', OtherItem); |
| 42 | + |
| 43 | + var doc = { |
| 44 | + items: [ |
| 45 | + { type: 'Book', author: 'test 1' }, |
| 46 | + { type: 'EBook', author: 'test 2' }, |
| 47 | + { type: 'Other' }, |
| 48 | + ] |
| 49 | + } |
| 50 | + var schemaTypes = getSchemaTypes(BundleSchema, doc, 'items.author'); |
| 51 | + |
| 52 | + assert.ok(Array.isArray(schemaTypes)); |
| 53 | + // Make sure we only got the schema paths for Book and EBook, and none |
| 54 | + // for the 'Other' |
| 55 | + assert.equal(schemaTypes.length, 2); |
| 56 | + assert.equal(schemaTypes[0].options.ref, 'Ref1'); |
| 57 | + assert.equal(schemaTypes[1].options.ref, 'Ref2'); |
| 58 | + |
| 59 | + done(); |
| 60 | + }); |
| 61 | +}); |
0 commit comments