forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.create.test.js
100 lines (83 loc) · 2.45 KB
/
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
/**
* Test dependencies.
*/
var start = require('./common')
, assert = require('assert')
, mongoose = start.mongoose
, random = require('../lib/utils').random
, Schema = mongoose.Schema
, DocumentObjectId = mongoose.Types.ObjectId
/**
* Setup
*/
var schema = Schema({
title: String
})
describe('model', function(){
describe('create()', function(){
var db;
var B;
before(function(){
db = start();
B = db.model('model-create', schema, 'model-create-'+random());
})
after(function(done){
db.close(done);
})
it('accepts an array', function(done){
B.create([{ title: 'hi'}, { title: 'bye'}], function (err, post1, post2) {
assert.ifError(err);
assert.ok(post1.get('_id') instanceof DocumentObjectId);
assert.equal(post1.title,'hi');
assert.ok(post2.get('_id') instanceof DocumentObjectId);
assert.equal(post2.title,'bye');
done();
});
});
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.ok(!a);
done();
});
});
it('returns a promise', function(done){
var p = B.create({ title: 'returns promise' }, function(){
assert.ok(p instanceof mongoose.Promise);
done();
});
})
describe('callback is optional', function(){
it('with one doc', function(done){
var p = B.create({ title: 'optional callback' });
p.then(function (doc) {
assert.equal('optional callback', doc.title);
done();
}, done).end();
})
it('with more than one doc', function(done){
var p = B.create({ title: 'optional callback 2' }, { title: 'orient expressions' });
p.then(function (doc1, doc2) {
assert.equal('optional callback 2', doc1.title);
assert.equal('orient expressions', doc2.title);
done();
}, done).end();
})
it('with array of docs', function(done){
var p = B.create([{ title: 'optional callback3' }, { title: '3' }]);
p.then(function (doc1, doc2) {
assert.equal('optional callback3', doc1.title);
assert.equal('3', doc2.title);
done();
}, done).end();
})
})
});
})