-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
111 lines (97 loc) · 2.72 KB
/
index.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict';
var sinon = require('sinon');
var events = require('events');
var mongoose = {};
module.exports = mongoose;
// Mongoose-mock emits events
// when Models or Documents are created.
// This allows for the mock injector to get notifications
// about use of the mock and get a chance to access
// the mocked models and document produced.
events.EventEmitter.call(mongoose);
mongoose.__proto__ = events.EventEmitter.prototype; // jshint ignore:line
// ## Schema
var Schema = function () {
function Model(properties) {
var self = this;
if(properties) {
Object.keys(properties).forEach(function (key) {
self[key] = properties[key];
});
}
this.save = sinon.stub();
this.increment = sinon.stub();
this.remove = sinon.stub();
mongoose.emit('document', this);
}
Model.statics = {};
Model.methods = {};
Model.static = sinon.stub();
Model.method = sinon.stub();
Model.pre = sinon.stub();
Model.path = function() {
return {
validate: sinon.stub(),
};
};
Model.virtual = function() {
function SetterGetter() {
return {
set: function() {
return new SetterGetter();
},
get: function() {
return new SetterGetter();
}
};
}
return new SetterGetter();
};
Model.aggregate = sinon.stub();
Model.count = sinon.stub();
Model.create = sinon.stub();
Model.distinct = sinon.stub();
Model.ensureIndexes = sinon.stub();
Model.find = sinon.stub();
Model.findById = sinon.stub();
Model.findByIdAndRemove = sinon.stub();
Model.findByIdAndUpdate = sinon.stub();
Model.findOne = sinon.stub();
Model.findOneAndRemove = sinon.stub();
Model.findOneAndUpdate = sinon.stub();
Model.geoNear = sinon.stub();
Model.geoSearch = sinon.stub();
Model.index = sinon.stub();
Model.mapReduce = sinon.stub();
Model.plugin = sinon.stub();
Model.populate = sinon.stub();
Model.remove = sinon.stub();
Model.set = sinon.stub();
Model.update = sinon.stub();
Model.where = sinon.stub();
mongoose.emit('model', Model);
return Model;
};
// compiled models are stored in models_
// and may be retrieved by name.
var models_ = {};
function createModelFromSchema(name, Type) {
if (Type) {
if (Type.statics) {
Object.keys(Type.statics).forEach(function (key) {
Type[key] = Type.statics[key];
});
}
if (Type.methods) {
Object.keys(Type.methods).forEach(function (key) {
Type.prototype[key] = Type.methods[key];
});
}
models_[name] = Type;
}
return models_[name];
}
mongoose.Schema = Schema;
mongoose.Schema.Types = { ObjectId: ''}; // Defining mongoose types as dummies.
mongoose.model = createModelFromSchema;
mongoose.connect = sinon.stub;