-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
253 lines (213 loc) · 8.93 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"use strict";
const assert = require('chai').assert;
const expect = require('chai').expect;
const faker = require('Faker');
const mongoose = require('mongoose');
const rfc6902 = require('rfc6902');
const Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost:34000/testmongoosestream',{
useUnifiedTopology: true,
useNewUrlParser: true,
});
const modulePlus = require('./index.js');
//=====================================================
// ============================================ helpers
//=====================================================
function genRandomId() {
return (new Date).getTime().toString(32).toUpperCase();
}
//=====================================================
// ============================================== tests
//=====================================================
describe("mongoose model stream", function() { //log the function
//=====================================================
it('should work with module using auto timestamps', function(done){
//=====================================================
//=========================================== generator
const Module = modulePlus('TEST'+genRandomId(),
new Schema({ text: String }, { timestamps: true }));
//===================================== expected events
const Test = ['create','update','update'];
const Values = Test.map(() => faker.Name.findName());
let CountOfTestIndex = 0;
let outputVal = { };
//===================================== start listening
Module.stream.subscribe(
function Data(doc){
assert.equal(typeof doc.getChange, "function","document missing getChange function")
assert.equal(typeof doc.getTarget, "function","document missing getTarget function")
const docObj = doc.toObject()
assert.equal(typeof docObj.getChange, "function","doc.toObject missing getChange function")
assert.equal(typeof docObj.getTarget, "function","doc.toObject missing getTarget function")
const docJson = doc.toJSON()
assert.equal(typeof docJson.getChange, "undefined","doc.toJSON has getChange function")
assert.equal(typeof docJson.getTarget, "undefined","doc.toJSON has getTarget function")
if (0 === CountOfTestIndex) {
const change = doc.getChange()
assert.equal(change.text,Values[CountOfTestIndex],"text mismatch")
assert.equal(change._id,doc.target,"_id mismatch")
} else if (1 === CountOfTestIndex) {
const change = doc.toObject().getChange()
assert.equal(change.text,Values[CountOfTestIndex],"text mismatch")
}
rfc6902.applyPatch(outputVal,doc.toObject().patchs)
assert.equal(outputVal.text, Values[CountOfTestIndex],"Checking the core value");
assert.equal(doc.target+"", outputVal._id+"","Checking the id value");
assert.equal(doc.action, 0 === CountOfTestIndex ? "CREATE":"UPDATE","Should be the first event");
assert.equal(Object.prototype.toString.call(doc.createdAt), "[object Date]","Should have createdAt");
if(++CountOfTestIndex === Test.length){
Module.stream.onCompleted();
}
}, function Fail(err){
done(err);
}, function Done(){
done();
}); // END subscribe
//======================================= start example
setTimeout(function () {
//+++++++++++++++++++++++++++++++++++++++++++++ create
Module.create({ text: Values[0] })
.then(function (chatMessage) {
chatMessage.text = Values[1];
//+++++++++++++++++++++++++++++++++++++++++++++ update
return chatMessage.save();
}).then(function (chatMessage) {
chatMessage.text = Values[2];
//+++++++++++++++++++++++++++++++++++++++++++++ updatd
chatMessage.save();
}) // END then
.catch( err => { throw err }); // END catch
}, 150); // END setTimeout
}); // END it should work with module using auto timestamps
//=====================================================
it('should with out auto timestamps', function(done){
//=====================================================
//=========================================== generator
const Module = modulePlus('TEST'+genRandomId(), new Schema({ text: String }));
//===================================== expected events
const Test = ['create','update','update'];
const Values = Test.map( () => faker.Name.findName() );
let CountOfTestIndex = 0;
let outputVal = { };
//===================================== start listening
Module.stream.subscribe(
function Data(doc){
rfc6902.applyPatch(outputVal,doc.toObject().patchs)
assert.equal(outputVal.text, Values[CountOfTestIndex],"Checking the core value");
assert.equal(doc.target+"", outputVal._id+"","Checking the id value");
assert.equal(doc.action, 0 === CountOfTestIndex ? "CREATE":"UPDATE","Should be the first event");
assert.equal(Object.prototype.toString.call(doc.createdAt), "[object Date]","Should have createdAt");
if(++CountOfTestIndex === Test.length){
Module.stream.onCompleted();
}
}, function Fail(err){
done(err);
}, function Done(){
done();
}); // END subscribe
//======================================= start example
setTimeout(function () {
//+++++++++++++++++++++++++++++++++++++++++++++ create
Module.create({ text: Values[0] })
.then(function (chatMessage) {
chatMessage.text = Values[1];
//+++++++++++++++++++++++++++++++++++++++++++++ update
return chatMessage.save();
}).then(function (chatMessage) {
chatMessage.text = Values[2];
//+++++++++++++++++++++++++++++++++++++++++++++ updatd
chatMessage.save();
}) // END then
.catch( err => { throw err }); // END catch
}, 150); // END setTimeout
}); // END it should work with module using auto timestamps
//=====================================================
it('should emit when removing', function(done){
//=====================================================
//=========================================== generator
const Module = modulePlus('TEST'+genRandomId(), new Schema({ text: String }));
//===================================== expected events
const text = faker.Name.findName();
let step = 1;
let outputVal = { };
//===================================== start listening
Module.stream.subscribe(
function Data(doc){
if (1 === step) {
rfc6902.applyPatch(outputVal,doc.toObject().patchs)
assert.equal(outputVal.text, text,"Checking the core value");
assert.equal(doc.target+"", outputVal._id+"","Checking the id value");
assert.equal(doc.action, "CREATE","Should be the first event");
assert.equal(Object.prototype.toString.call(doc.createdAt), "[object Date]","Should have createdAt");
step++;
} else if (2 === step) {
assert.equal(doc.action, "DELETE","Should be the first event");
Module.stream.onCompleted();
}
}, function Fail(err){
done(err);
}, function Done(){
done();
}); // END subscribe
//======================================= start example
setTimeout(function () {
//+++++++++++++++++++++++++++++++++++++++++++++ create
Module.create({ text })
.then(function (chatMessage) {
//+++++++++++++++++++++++++++++++++++++++++++++ remove
return chatMessage.remove();
})
.catch(done)
}, 150); // END setTimeout
}); // END it should work with module using auto timestamps
//=====================================================
it('should emit with tags', function(done){
//=====================================================
//=========================================== generator
const Module = modulePlus('TEST'+genRandomId(), new Schema({ text: { type: String, required: true } }));
const tagVals = ["create1","save2","save3","saveBy4","update5"]
let tagsProcessed = 0
Module.stream.subscribe(
function Data(doc){
assert.equal(doc.tag, tagVals[tagsProcessed],"Tag didn't match");
tagsProcessed++
if (tagVals.length === tagsProcessed ) {
done();
}
}, function Fail(err){
done(err);
}, function Done(){
//done();
}); // END subscribe
//===================================== run doc changes
setTimeout(function () {
const genText = () => faker.Name.findName()
const updateTextTag = genText()
//+++++++++++++++++++++++++++++++++++++++++++++ create
Module.create({ text:updateTextTag },
{ tag :tagVals[0] })
.then(doc => {
//+++++++++++++++++++++++++++++++++++++++++++++ save
doc.text = genText()
return doc.save({tag:tagVals[1]})
})
.then(doc => {
//+++++++++++++++++++++++++++++++++++++++++++++ save
doc.text = genText()
return doc.save(tagVals[2])
})
.then(doc => {
//console.log("Here",doc)
//+++++++++++++++++++++++++++++++++++++++++++++ saveBy
doc.text = genText()
return doc.saveBy(doc._id,tagVals[3])
})
.then(doc => {
//+++++++++++++++++++++++++++++++++++++++++++++ update
const updateTextTag = genText()
return Module.updateOne({_id:doc._id},{$set: { text: updateTextTag }},{ tag : tagVals[4] })
})
.catch(done)
}, 150); // END setTimeout
}) // END it
}); // END describe - mongoose model stream