forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.capped.test.js
91 lines (78 loc) · 2.43 KB
/
collection.capped.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
/**
* Module dependencies.
*/
'use strict';
let start = require('./common'), mongoose = start.mongoose, assert = require('power-assert'), Schema = mongoose.Schema, random = require('../lib/utils').random;
/**
* setup
*/
const capped = new Schema({key: 'string', val: 'number'});
capped.set('capped', {size: 1000});
const coll = 'capped_' + random();
/**
* Test.
*/
describe('collections: capped:', function() {
let db;
before(function() {
db = start();
});
after(function(done) {
db.close(done);
});
it('schemas should have option size', function(done) {
assert.ok(capped.options.capped);
assert.equal(capped.options.capped.size, 1000);
done();
});
it('creation', function(done) {
const Capped = db.model('Capped', capped, coll);
Capped.collection.isCapped(function(err, isCapped) {
assert.ifError(err);
assert.ok(isCapped, 'should create a capped collection');
// use the existing capped collection in the db (no coll creation)
const Capped2 = db.model('Capped2', capped, coll);
Capped2.collection.isCapped(function(err1, isCapped1) {
assert.ifError(err1);
assert.ok(isCapped1, 'should reuse the capped collection in the db');
assert.equal(Capped.collection.name, Capped2.collection.name);
done();
});
});
});
it('creation using a number', function(done) {
const schema = new Schema({key: 'string'}, {capped: 8192});
const Capped = db.model('Capped3', schema);
Capped.collection.options(function(err, options) {
assert.ifError(err);
assert.ok(options.capped, 'should create a capped collection');
assert.equal(options.size, 8192);
done();
});
});
it('attempting to use existing non-capped collection as capped emits error', function(done) {
db = start();
const opts = {};
const conn = 'capped_existing_' + random();
db.on('open', function() {
db.db.createCollection(conn, opts, function(err) {
if (err) {
db.close();
}
assert.ifError(err);
let timer;
db.on('error', function(err1) {
clearTimeout(timer);
db.close();
assert.ok(/non-capped collection exists/.test(err1));
done();
});
db.model('CappedExisting', capped, conn);
timer = setTimeout(function() {
db.close();
throw new Error('capped test timeout');
}, 900);
});
});
});
});