|
| 1 | + |
| 2 | +/** |
| 3 | + * Module dependencies. |
| 4 | + */ |
| 5 | + |
| 6 | +var start = require('./common'), |
| 7 | + mongoose = start.mongoose, |
| 8 | + assert = require('power-assert'), |
| 9 | + Schema = mongoose.Schema; |
| 10 | + |
| 11 | +describe('schema alias option', function() { |
| 12 | + it('works with all basic schema types', function() { |
| 13 | + var db = start(); |
| 14 | + |
| 15 | + var schema = new Schema({ |
| 16 | + string: { type: String, alias: 'StringAlias' }, |
| 17 | + number: { type: Number, alias: 'NumberAlias' }, |
| 18 | + date: { type: Date, alias: 'DateAlias' }, |
| 19 | + buffer: { type: Buffer, alias: 'BufferAlias' }, |
| 20 | + boolean: { type: Boolean, alias: 'BooleanAlias' }, |
| 21 | + mixed: { type: Schema.Types.Mixed, alias: 'MixedAlias' }, |
| 22 | + objectId: { type: Schema.Types.ObjectId, alias: 'ObjectIdAlias'}, |
| 23 | + array: { type: [], alias: 'ArrayAlias' } |
| 24 | + }); |
| 25 | + |
| 26 | + var S = db.model('AliasSchemaType', schema); |
| 27 | + S.create({ |
| 28 | + string: 'hello', |
| 29 | + number: 1, |
| 30 | + date: new Date(), |
| 31 | + buffer: new Buffer('World'), |
| 32 | + boolean: false, |
| 33 | + mixed: [1, [], 'three', { four: 5 }], |
| 34 | + objectId: new Schema.Types.ObjectId(), |
| 35 | + array: ['a', 'b', 'c', 'd'] |
| 36 | + }, function(err, s) { |
| 37 | + assert.ifError(err); |
| 38 | + |
| 39 | + // Comparing with aliases |
| 40 | + assert.equal(s.string, s.StringAlias); |
| 41 | + assert.equal(s.number, s.NumberAlias); |
| 42 | + assert.equal(s.date, s.DateAlias); |
| 43 | + assert.equal(s.buffer, s.BufferAlias); |
| 44 | + assert.equal(s.boolean, s.BooleanAlias); |
| 45 | + assert.equal(s.mixed, s.MixedAlias); |
| 46 | + assert.equal(s.objectId, s.ObjectIdAlias); |
| 47 | + assert.equal(s.array, s.ArrayAlias); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('works with nested schema types', function() { |
| 52 | + var db = start(); |
| 53 | + |
| 54 | + var schema = new Schema({ |
| 55 | + nested: { |
| 56 | + type: { |
| 57 | + string: { type: String, alias: 'StringAlias' }, |
| 58 | + number: { type: Number, alias: 'NumberAlias' }, |
| 59 | + date: { type: Date, alias: 'DateAlias' }, |
| 60 | + buffer: { type: Buffer, alias: 'BufferAlias' }, |
| 61 | + boolean: { type: Boolean, alias: 'BooleanAlias' }, |
| 62 | + mixed: { type: Schema.Types.Mixed, alias: 'MixedAlias' }, |
| 63 | + objectId: { type: Schema.Types.ObjectId, alias: 'ObjectIdAlias'}, |
| 64 | + array: { type: [], alias: 'ArrayAlias' } |
| 65 | + }, |
| 66 | + alias: 'NestedAlias' |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + var S = db.model('AliasNestedSchemaType', schema); |
| 71 | + S.create({ |
| 72 | + nested: { |
| 73 | + string: 'hello', |
| 74 | + number: 1, |
| 75 | + date: new Date(), |
| 76 | + buffer: new Buffer('World'), |
| 77 | + boolean: false, |
| 78 | + mixed: [1, [], 'three', { four: 5 }], |
| 79 | + objectId: new Schema.Types.ObjectId(), |
| 80 | + array: ['a', 'b', 'c', 'd'] |
| 81 | + } |
| 82 | + }, function(err, s) { |
| 83 | + assert.ifError(err); |
| 84 | + |
| 85 | + // Comparing with aliases |
| 86 | + assert.equal(s.nested, s.NestedAlias); |
| 87 | + assert.equal(s.nested.string, s.StringAlias); |
| 88 | + assert.equal(s.nested.number, s.NumberAlias); |
| 89 | + assert.equal(s.nested.date, s.DateAlias); |
| 90 | + assert.equal(s.nested.buffer, s.BufferAlias); |
| 91 | + assert.equal(s.nested.boolean, s.BooleanAlias); |
| 92 | + assert.equal(s.nested.mixed, s.MixedAlias); |
| 93 | + assert.equal(s.nested.objectId, s.ObjectIdAlias); |
| 94 | + assert.equal(s.nested.array, s.ArrayAlias); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('throws when alias option is invalid', function() { |
| 99 | + assert.throws(function() { |
| 100 | + new Schema({ |
| 101 | + foo: { type: String, alias: 456 } |
| 102 | + }); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments