1
+ var request = require ( 'supertest' ) ;
2
+ var should = require ( 'should' ) ;
3
+ var testUtils = require ( "../../test/testUtils" ) ;
4
+ var plugins = require ( "../../plugins/pluginManager" ) ;
5
+ var db = "" ;
6
+
7
+
8
+ function runTest ( options ) {
9
+ it ( 'Running with callbacks correctly' , function ( done ) {
10
+ if ( options . op === "insert" ) {
11
+ db . collection ( "testCommands" ) . insert ( options . query , function ( err , res ) {
12
+ if ( err ) {
13
+ done ( err ) ;
14
+ }
15
+ else {
16
+ res . should . have . property ( "acknowledged" , true ) ;
17
+ res . should . have . property ( "insertedCount" , 1 ) ;
18
+ res . should . have . property ( "insertedId" ) ;
19
+ if ( options . query . _id ) {
20
+ res . should . have . property ( "insertedId" , options . query . _id ) ;
21
+ }
22
+ done ( ) ;
23
+ }
24
+ } ) ;
25
+ }
26
+ else if ( options . op === "findAndModify" ) {
27
+ db . collection ( "testCommands" ) . findAndModify ( options . query , options . sort || { } , options . update , options . options , function ( err , res ) {
28
+ should . not . exist ( err ) ;
29
+ console . log ( JSON . stringify ( res ) ) ;
30
+
31
+
32
+ if ( options . options . remove ) {
33
+ res = res || { } ;
34
+ if ( options . query . _id ) {
35
+ res . should . have . property ( "_id" , options . query . _id ) ;
36
+ }
37
+ else {
38
+ res . should . have . property ( "_id" ) ;
39
+ }
40
+
41
+ if ( options . query . name ) {
42
+ res . should . have . property ( "name" , options . query . name ) ;
43
+ }
44
+ else {
45
+ res . should . have . property ( "name" ) ;
46
+ }
47
+ }
48
+ else {
49
+ res . should . have . property ( "value" ) ;
50
+ if ( options . options . new ) {
51
+ if ( options . update . $set . name ) {
52
+ res . value . should . have . property ( "name" , options . update . $set . name ) ;
53
+ }
54
+ }
55
+ else {
56
+ if ( options . query . name ) {
57
+ res . value . should . have . property ( "name" , options . query . name ) ;
58
+ }
59
+ }
60
+ if ( options . query . _id ) {
61
+ res . value . should . have . property ( "_id" , options . query . _id ) ;
62
+ }
63
+ }
64
+ done ( ) ;
65
+ } ) ;
66
+ }
67
+ else {
68
+ done ( "unkonown op: " + options . op ) ;
69
+ }
70
+ } ) ;
71
+
72
+ it ( 'Running with callbacks + error' , function ( done ) {
73
+ if ( options . queryError ) {
74
+ if ( options . op === "insert" ) {
75
+ db . collection ( "testCommands" ) . insert ( options . queryError , function ( err , res ) {
76
+ if ( err ) {
77
+ err . should . have . property . code ;
78
+ done ( ) ;
79
+ }
80
+ else {
81
+ done ( "Should have failed with error but succeeded" ) ;
82
+ }
83
+ } ) ;
84
+ }
85
+ else if ( options . op === "findAndModify" ) {
86
+ db . collection ( "testCommands" ) . findAndModify ( options . query , options . sort || [ ] , options . update , options . options , function ( err , res ) {
87
+ should . not . exist ( err ) ;
88
+ console . log ( " " + JSON . stringify ( res ) ) ;
89
+ res . should . have . property ( "value" ) ;
90
+ if ( options . query . name ) {
91
+ res . value . should . have . property ( "name" , options . query ) ;
92
+ }
93
+ if ( options . query . _id ) {
94
+ res . value . should . have . property ( "_id" , options . query . _id ) ;
95
+ }
96
+ done ( ) ;
97
+ } ) ;
98
+ }
99
+ else {
100
+ done ( "unkonown op: " + options . op ) ;
101
+ }
102
+ }
103
+ else {
104
+ done ( ) ;
105
+ }
106
+
107
+ } ) ;
108
+
109
+ it ( 'Running with promises correctly' , async function ( ) {
110
+ if ( options . op === "insert" ) {
111
+
112
+ var res = await db . collection ( "testCommands2" ) . insert ( options . query ) ;
113
+ console . log ( " " + JSON . stringify ( res ) ) ;
114
+ res . should . have . property ( "acknowledged" , true ) ;
115
+ //res.should.have.property("insertedCount", 1);
116
+ res . should . have . property ( "insertedId" ) ;
117
+ if ( options . query . _id ) {
118
+ res . should . have . property ( "insertedId" , options . query . _id ) ;
119
+ }
120
+
121
+ }
122
+ else if ( options . op === "findAndModify" ) {
123
+ var res = await db . collection ( "testCommands2" ) . findAndModify ( options . query , options . sort || [ ] , options . update , options . options ) ;
124
+ console . log ( " " + JSON . stringify ( res ) ) ;
125
+
126
+ if ( options . options . remove ) {
127
+ if ( options . query . _id ) {
128
+ res . should . have . property ( "_id" , options . query . _id ) ;
129
+ }
130
+ else {
131
+ res . should . have . property ( "_id" ) ;
132
+ }
133
+
134
+ if ( options . query . name ) {
135
+ res . should . have . property ( "name" , options . query . name ) ;
136
+ }
137
+ else {
138
+ res . should . have . property ( "name" ) ;
139
+ }
140
+ }
141
+ else {
142
+ res . should . have . property ( "value" ) ;
143
+ if ( options . options . new ) {
144
+ res . value . should . have . property ( "name" , options . update . $set . name ) ;
145
+ }
146
+ else {
147
+ if ( options . query . name ) {
148
+ res . value . should . have . property ( "name" , options . query . name ) ;
149
+ }
150
+ }
151
+ if ( options . query . _id ) {
152
+ res . value . should . have . property ( "_id" , options . query . _id ) ;
153
+ }
154
+ else {
155
+ res . value . should . have . property ( "_id" ) ;
156
+ }
157
+ }
158
+
159
+ }
160
+ else {
161
+ throw new Error ( "unkonown op: " + options . op ) ;
162
+ }
163
+ } ) ;
164
+
165
+ it ( 'Running with promises + error' , async function ( ) {
166
+ if ( options . queryError ) {
167
+ if ( options . op === "insert" ) {
168
+ try {
169
+ var res = await db . collection ( "testCommands2" ) . insert ( options . queryError ) ;
170
+ throw new Error ( "Should have failed with error but succeeded" ) ;
171
+ }
172
+ catch ( err ) {
173
+ err . should . have . property . code ;
174
+ }
175
+ }
176
+ else if ( options . op === "findAndModify" ) {
177
+ try {
178
+ var res = await db . collection ( "testCommands2" ) . findAndModify ( options . query , options . sort || [ ] , options . update , options . options ) ;
179
+ throw new Error ( "Should have failed with error but succeeded" ) ;
180
+ }
181
+ catch ( err ) {
182
+ err . should . have . property . code ;
183
+ }
184
+ }
185
+ else {
186
+ throw new Error ( "unkonown op: " + options . op ) ;
187
+ }
188
+ }
189
+
190
+ } ) ;
191
+ }
192
+ describe ( 'Testing Simple database operations' , function ( ) {
193
+
194
+ describe ( 'Setting up db connection' , function ( ) {
195
+ before ( 'Create db connection' , async function ( ) {
196
+ testUtils . db = await plugins . dbConnection ( "countly" ) ;
197
+ testUtils . client = testUtils . db . client ;
198
+ } ) ;
199
+ it ( 'Setting db' , function ( done ) {
200
+ db = testUtils . client . db ( "countly" ) ;
201
+ done ( ) ;
202
+ } ) ;
203
+ } ) ;
204
+
205
+ describe ( 'Cleanup' , function ( ) {
206
+ it ( 'should remove collection with callback' , function ( done ) {
207
+ db . collection ( "testCommands" ) . drop ( function ( err , res ) {
208
+ if ( err ) {
209
+ console . log ( err ) ;
210
+ }
211
+ res . should . be . true ;
212
+ done ( ) ;
213
+ } ) ;
214
+
215
+ } ) ;
216
+ it ( 'should remove collection with promise' , async function ( ) {
217
+ var res = await db . collection ( "testCommands2" ) . drop ( ) ;
218
+ res . should . be . true ;
219
+ } ) ;
220
+ } ) ;
221
+
222
+ describe ( "testing insert operation" , function ( ) {
223
+ describe ( 'should insert simple document without _id' , function ( ) {
224
+ runTest ( { "op" : "insert" , query : { name : "test" } } ) ;
225
+ } ) ;
226
+ describe ( 'should insert simple document with _id' , function ( ) {
227
+ runTest ( { "op" : "insert" , query : { _id : "aaaaa" , name : "test" } , queryError : { "_id" : "aaaaa" } } ) ;
228
+ } ) ;
229
+ } ) ;
230
+
231
+ describe ( 'Testing findAndModify' , function ( ) {
232
+ describe ( 'Find and modify new + upsert(not existing)' , function ( ) {
233
+ runTest ( { "op" : "findAndModify" , query : { _id : "bbbb" } , "update" : { $set : { name : "test_b" } } , "options" : { new : true , upsert : true } } ) ;
234
+ } ) ;
235
+ describe ( 'Find and modify new + upsert(existing)' , function ( ) {
236
+ runTest ( { "op" : "findAndModify" , query : { _id : "bbbb" } , "update" : { $set : { name : "test_c" } } , "options" : { new : true , upsert : true } } ) ;
237
+ } ) ;
238
+ describe ( 'Find and modify upsert (existing)' , function ( ) {
239
+ runTest ( { "op" : "findAndModify" , query : { _id : "bbbb" } , "update" : { $set : { name : "test_d" } } , "options" : { upsert : true } } ) ;
240
+ } ) ;
241
+ describe ( 'Find and modify upsert:false,new:true' , function ( ) {
242
+ runTest ( { "op" : "findAndModify" , query : { name : "test" } , "update" : { $set : { name : "test2" } } , "options" : { new : true } } ) ;
243
+ } ) ;
244
+ describe ( 'Find and modify upsert:false,new:false' , function ( ) {
245
+ runTest ( { "op" : "findAndModify" , query : { name : "test" } , "update" : { $set : { name : "test2" } } , "options" : { new : false } } ) ;
246
+ } ) ;
247
+ describe ( 'Find and modify remove:true' , function ( ) {
248
+ runTest ( { "op" : "findAndModify" , query : { _id : "aaaaa" } , "update" : { } , "options" : { remove : true } } ) ;
249
+ } ) ;
250
+ } ) ;
251
+
252
+ describe ( 'Testing aggregation pipeline' , function ( ) {
253
+ describe ( 'Run aggregation and get ' , function ( ) {
254
+ runTest ( { "op" : "findAndModify" , query : { _id : "bbbb" } , "update" : { $set : { name : "test_c" } } , "options" : { new : true , upsert : true } } ) ;
255
+ } ) ;
256
+ } ) ;
257
+
258
+ describe ( "test working with cursors" , function ( ) {
259
+ it ( "test cursor using await and going toArray()" , async function ( ) {
260
+ var cursor = db . collection ( "testCommands2" ) . find ( ) ;
261
+ var res = await cursor . toArray ( ) ;
262
+ console . log ( JSON . stringify ( res ) ) ;
263
+ res . length . should . be . above ( 0 ) ;
264
+ res . should . be . an . instanceOf ( Array ) ;
265
+ } ) ;
266
+ it ( "test cursor using await and going next()" , async function ( ) {
267
+ var cursor = await db . collection ( "testCommands2" ) . find ( { } ) ;
268
+ var doc = await cursor . next ( ) ;
269
+ while ( doc ) {
270
+ doc . should . have . property ( "_id" ) ;
271
+ doc . should . have . property ( "name" ) ;
272
+ doc = await cursor . next ( ) ;
273
+ }
274
+ } ) ;
275
+ } ) ;
276
+
277
+
278
+
279
+ describe ( 'Cleanup' , function ( ) {
280
+ it ( 'should remove collection with callback' , function ( done ) {
281
+ db . collection ( "testCommands" ) . drop ( function ( err , res ) {
282
+ if ( err ) {
283
+ console . log ( err ) ;
284
+ }
285
+ res . should . be . true ;
286
+ done ( ) ;
287
+ } ) ;
288
+
289
+ } ) ;
290
+ it ( 'should remove collection with promise' , async function ( ) {
291
+ var res = await db . collection ( "testCommands2" ) . drop ( ) ;
292
+ res . should . be . true ;
293
+ } ) ;
294
+ after ( 'Close db connection' , async function ( ) {
295
+ testUtils . client . close ( ) ;
296
+ } ) ;
297
+ } ) ;
298
+ } ) ;
0 commit comments