forked from Automattic/mongoose
/
types.document.test.js
208 lines (167 loc) · 5.62 KB
/
types.document.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* Module dependencies.
*/
var should = require('should')
, start = require('./common')
, mongoose = start.mongoose
, EmbeddedDocument = require('../lib/types/embedded')
, DocumentArray = require('../lib/types/documentarray')
, Schema = mongoose.Schema
, SchemaType = mongoose.SchemaType
, ValidatorError = SchemaType.ValidatorError
, ValidationError = mongoose.Document.ValidationError
/**
* Setup.
*/
function Dummy () {
mongoose.Document.call(this, {});
}
Dummy.prototype.__proto__ = mongoose.Document.prototype;
Dummy.prototype.schema = new Schema;
function Subdocument () {
var arr = new DocumentArray;
arr._path = 'jsconf.ar'
arr._parent = new Dummy;
arr[0] = this;
EmbeddedDocument.call(this, {}, arr);
};
/**
* Inherits from EmbeddedDocument.
*/
Subdocument.prototype.__proto__ = EmbeddedDocument.prototype;
/**
* Set schema.
*/
Subdocument.prototype.schema = new Schema({
test: { type: String, required: true }
, work: { type: String, validate: /^good/ }
});
/**
* Schema.
*/
var RatingSchema = new Schema({
stars: Number
});
var MovieSchema = new Schema({
title: String
, ratings: [RatingSchema]
});
mongoose.model('Movie', MovieSchema);
/**
* Test.
*/
module.exports = {
'test that save fires errors': function(){
var a = new Subdocument();
a.set('test', '');
a.set('work', 'nope');
a.save(function(err){
a.parent._validationError.should.be.an.instanceof(ValidationError);
a.parent.errors['jsconf.ar.0.work'].name.should.eql('ValidatorError');
a.parent._validationError.toString().should.eql('ValidationError: Validator "required" failed for path test, Validator failed for path work');
});
},
'test that save fires with null if no errors': function(){
var a = new Subdocument();
a.set('test', 'cool');
a.set('work', 'goods');
a.save(function(err){
should.strictEqual(err, null);
});
},
'objects can be passed to #set': function () {
var a = new Subdocument();
a.set({ test: 'paradiddle', work: 'good flam'});
a.test.should.eql('paradiddle');
a.work.should.eql('good flam');
},
'Subdocuments can be passed to #set': function () {
var a = new Subdocument();
a.set({ test: 'paradiddle', work: 'good flam'});
a.test.should.eql('paradiddle');
a.work.should.eql('good flam');
var b = new Subdocument();
b.set(a);
b.test.should.eql('paradiddle');
b.work.should.eql('good flam');
},
'cached _ids': function () {
var db = start();
var Movie = db.model('Movie');
db.close();
var m = new Movie;
should.equal(m.id, m.__id);
var old = m.id;
m._id = new mongoose.Types.ObjectId;
should.equal(m.id, m.__id);
should.strictEqual(true, old !== m.__id);
var m2= new Movie;
delete m2._doc._id;
m2.init({ _id: new mongoose.Types.ObjectId });
should.equal(m2.id, m2.__id);
should.strictEqual(true, m.__id !== m2.__id);
should.strictEqual(true, m.id !== m2.id);
should.strictEqual(true, m.__id !== m2.__id);
}
/*
'Subdocument#remove': function (beforeExit) {
var db = start();
var Movie = db.model('Movie');
var super8 = new Movie({ title: 'Super 8' });
var id1 = '4e3d5fc7da5d7eb635063c96';
var id2 = '4e3d5fc7da5d7eb635063c97';
var id3 = '4e3d5fc7da5d7eb635063c98';
var id4 = '4e3d5fc7da5d7eb635063c99';
super8.ratings.push({ stars: 9, _id: id1 });
super8.ratings.push({ stars: 8, _id: id2 });
super8.ratings.push({ stars: 7, _id: id3 });
super8.ratings.push({ stars: 6, _id: id4 });
console.error('pre save', super8);
super8.save(function (err) {
should.strictEqual(err, null);
super8.title.should.eql('Super 8');
super8.ratings.id(id1).stars.valueOf().should.eql(9);
super8.ratings.id(id2).stars.valueOf().should.eql(8);
super8.ratings.id(id3).stars.valueOf().should.eql(7);
super8.ratings.id(id4).stars.valueOf().should.eql(6);
super8.ratings.id(id1).stars = 5;
super8.ratings.id(id2).remove();
super8.ratings.id(id3).stars = 4;
super8.ratings.id(id4).stars = 3;
super8.save(function (err) {
should.strictEqual(err, null);
Movie.findById(super8._id, function (err, movie) {
should.strictEqual(err, null);
movie.title.should.eql('Super 8');
movie.ratings.length.should.equal(3);
movie.ratings.id(id1).stars.valueOf().should.eql(5);
movie.ratings.id(id3).stars.valueOf().should.eql(4);
movie.ratings.id(id4).stars.valueOf().should.eql(3);
console.error('after save + findbyId', movie);
movie.ratings.id(id1).stars = 2;
movie.ratings.id(id3).remove();
movie.ratings.id(id4).stars = 1;
console.error('after modified', movie);
movie.save(function (err) {
should.strictEqual(err, null);
Movie.findById(super8._id, function (err, movie) {
db.close();
should.strictEqual(err, null);
movie.ratings.length.should.equal(2);
movie.ratings.id(id1).stars.valueOf().should.eql(2);
movie.ratings.id(id4).stars.valueOf().should.eql(1);
console.error('after resave + findById', movie);
});
});
});
});
});
beforeExit(function () {
var db = start();
Movie.remove({}, function (err) {
db.close();
});
});
}
*/
};