-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
document.unit.test.js
72 lines (61 loc) · 1.71 KB
/
document.unit.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Module dependencies.
*/
'use strict';
const start = require('./common');
const assert = require('assert');
const storeShard = require('../lib/plugins/sharding').storeShard;
const mongoose = start.mongoose;
describe('sharding', function() {
it('should handle shard keys properly (gh-2127)', function(done) {
const mockSchema = {
options: {
shardKey: {date: 1}
}
};
const Stub = function() {
this.schema = mockSchema;
this.$__ = {};
};
Stub.prototype.__proto__ = mongoose.Document.prototype;
const d = new Stub();
const currentTime = new Date();
d._doc = {date: currentTime};
storeShard.call(d);
assert.equal(d.$__.shardval.date, currentTime);
done();
});
});
describe('toObject()', function() {
let Stub;
beforeEach(function() {
Stub = function() {
const schema = this.schema = {
options: {toObject: {minimize: false, virtuals: true}},
virtuals: {virtual: 'test'}
};
this._doc = {empty: {}};
this.get = function(path) { return schema.virtuals[path]; };
this.$__ = {};
};
Stub.prototype = Object.create(mongoose.Document.prototype);
});
it('should inherit options from schema', function(done) {
const d = new Stub();
assert.deepEqual(d.toObject(), {empty: {}, virtual: 'test'});
done();
});
it('can overwrite schema-set default options', function(done) {
const d = new Stub();
assert.deepEqual(d.toObject({minimize: true, virtuals: false}), {});
done();
});
it('doesnt crash with empty object (gh-3130)', function(done) {
const d = new Stub();
d._doc = undefined;
assert.doesNotThrow(function() {
d.toObject();
});
done();
});
});