deno.land / x / mongoose@6.7.5 / test / model.create.test.js

model.create.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
'use strict';
/** * Test dependencies. */
const start = require('./common');
const assert = require('assert');
const mongoose = start.mongoose;const Schema = mongoose.Schema;const DocumentObjectId = mongoose.Types.ObjectId;
/** * Setup */
const schema = new Schema({ title: { type: String }});
describe('model', function() { describe('create()', function() { let db; let B;
before(function() { db = start(); B = db.model('Test', schema); });
after(async function() { await db.close(); });
it('accepts an array and returns an array', async function() { const posts = await B.create([{ title: 'hi' }, { title: 'bye' }]);
assert.ok(posts instanceof Array); assert.equal(posts.length, 2); const post1 = posts[0]; const post2 = posts[1]; assert.ok(post1.get('_id') instanceof DocumentObjectId); assert.equal(post1.title, 'hi');
assert.ok(post2.get('_id') instanceof DocumentObjectId); assert.equal(post2.title, 'bye'); });
it('fires callback when passed 0 docs', function(done) { B.create(function(err, a) { assert.ifError(err); assert.ok(!a); done(); }); });
it('fires callback when empty array passed', function(done) { B.create([], function(err, a) { assert.ifError(err); assert.deepEqual(a, []); done(); }); });
it('supports passing options', async function() { const docs = await B.create([{}], { validateBeforeSave: false });
assert.ok(Array.isArray(docs)); assert.equal(docs.length, 1); });
it('returns a promise', function() { const p = B.create({ title: 'returns promise' }); assert.ok(p instanceof mongoose.Promise); });
it('creates in parallel', async function() { let countPre = 0; let countPost = 0;
const SchemaWithPreSaveHook = new Schema({ preference: String });
let startTime, endTime; SchemaWithPreSaveHook.pre('save', true, function hook(next, done) { setTimeout(function() { countPre++; if (countPre === 1) startTime = Date.now(); else if (countPre === 4) endTime = Date.now(); next(); done(); }, 100); }); SchemaWithPreSaveHook.post('save', function() { countPost++; });
db.deleteModel(/Test/); const MWPSH = db.model('Test', SchemaWithPreSaveHook); const docs = await MWPSH.create([ { preference: 'xx' }, { preference: 'yy' }, { preference: '1' }, { preference: '2' } ]);
assert.ok(docs instanceof Array); assert.equal(docs.length, 4);
const [doc1, doc2, doc3, doc4] = docs;
assert.ok(doc1); assert.ok(doc2); assert.ok(doc3); assert.ok(doc4); assert.equal(countPre, 4); assert.equal(countPost, 4); assert.ok(endTime - startTime < 4 * 100); // serial: >= 4 * 100 parallel: < 4 * 100 });
describe('callback is optional', function() { it('with one doc', async function() { const doc = await B.create({ title: 'optional callback' });
assert.equal(doc.title, 'optional callback'); });
it('with more than one doc', async function() { const docs = await B.create({ title: 'optional callback 2' }, { title: 'orient expressions' });
assert.equal(docs.length, 2); const doc1 = docs[0]; const doc2 = docs[1]; assert.equal(doc1.title, 'optional callback 2'); assert.equal(doc2.title, 'orient expressions'); });
it('with array of docs', async function() { const docs = await B.create([{ title: 'optional callback3' }, { title: '3' }]);
assert.ok(docs instanceof Array); assert.equal(docs.length, 2); const doc1 = docs[0]; const doc2 = docs[1]; assert.equal(doc1.title, 'optional callback3'); assert.equal(doc2.title, '3'); });
it('and should reject promise on error', async function() { const p = B.create({ title: 'optional callback 4' }); const doc = await p;
const err = await B.create({ _id: doc._id }).then(() => null, err => err); assert(err); });
it('if callback is falsy, will ignore it (gh-5061)', async function() { const doc = await B.create({ title: 'test' }, null);
assert.equal(doc.title, 'test'); });
it('when passed an empty array, returns an empty array', async function() { const userSchema = new Schema({ name: String }); const User = db.model('User', userSchema);
const users = await User.create([]); assert.deepEqual(users, []); });
it('treats undefined first arg as doc rather than callback (gh-9765)', function() { return B.create(void 0). then(function(doc) { assert.ok(doc); assert.ok(doc._id); }); }); }); });});
mongoose

Version Info

Tagged at
a year ago