deno.land / x / mongoose@6.7.5 / test / model.create.test.js
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184'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); }); }); }); });});
Version Info