-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
test.js
133 lines (108 loc) · 3.31 KB
/
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
'use strict'
var mongodb = require('mongodb')
var MongoClient = mongodb.MongoClient
var MongoEmitter = require('./')
var { test } = require('tape')
var abstractTests = require('mqemitter/abstractTest.js')
var clean = require('mongo-clean')
var dbname = 'mqemitter-test'
var url = 'mongodb://127.0.0.1/' + dbname
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, w: 1 }, function (err, client) {
if (err) {
throw err
}
var db = client.db(dbname)
clean(db, function (err) {
if (err) {
throw err
}
client.close()
abstractTests({
builder: function (opts) {
opts = opts || {}
opts.url = url
return MongoEmitter(opts)
},
test: test
})
test('with default database name', function (t) {
t.plan(2)
var mqEmitterMongoDB = MongoEmitter({
url: url
})
mqEmitterMongoDB.status.once('stream', function () {
t.equal(mqEmitterMongoDB._db.databaseName, dbname)
t.ok(true, 'database name is default db name')
t.end()
mqEmitterMongoDB.close()
})
})
test('should fetch last packet id', function (t) {
t.plan(5)
var started = 0
var lastId = new mongodb.ObjectId()
function startInstance (cb) {
var mqEmitterMongoDB = MongoEmitter({
url: url
})
mqEmitterMongoDB.status.once('stream', function () {
t.equal(mqEmitterMongoDB._db.databaseName, dbname)
t.ok(true, 'database name is default db name')
if (++started === 1) {
mqEmitterMongoDB.emit({ topic: 'last/packet', payload: 'I\'m the last', _id: lastId }, mqEmitterMongoDB.close.bind(mqEmitterMongoDB, cb))
} else {
t.equal(mqEmitterMongoDB._lastObj._stringId, lastId.toString(), 'Should fetch last Id')
mqEmitterMongoDB.close(cb)
}
})
}
startInstance(function () {
startInstance(t.end.bind(t))
})
})
test('with database option', function (t) {
t.plan(2)
var mqEmitterMongoDB = MongoEmitter({
url: url,
database: 'test-custom-db-name'
})
mqEmitterMongoDB.status.once('stream', function () {
t.equal(mqEmitterMongoDB._db.databaseName, 'test-custom-db-name')
t.ok(true, 'database name is custom db name')
t.end()
mqEmitterMongoDB.close()
})
})
test('with mongodb options', function (t) {
t.plan(2)
var mqEmitterMongoDB = MongoEmitter({
url: url,
mongo: {
keepAlive: false
}
})
mqEmitterMongoDB.status.once('stream', function () {
t.equal(mqEmitterMongoDB._db.s.client.s.options.keepAlive, false)
t.ok(true, 'database name is default db name')
t.end()
mqEmitterMongoDB.close()
})
})
// keep this test as last
test('doesn\'t throw db errors', function (t) {
t.plan(1)
client.close(true)
var mqEmitterMongoDB = MongoEmitter({
url: url,
db: db
})
mqEmitterMongoDB.status.on('error', function (err) {
t.ok(true, 'error event emitted')
if (err.message !== 'MongoClient must be connected to perform this operation') {
t.fail('throws error')
}
mqEmitterMongoDB.close()
})
})
})
})