1
+ #include <bcon.h>
1
2
#include <mongoc.h>
2
3
3
4
#include "TestSuite.h"
4
5
5
6
#include "test-libmongoc.h"
7
+ #include "mongoc-tests.h"
6
8
7
9
static char * gTestUri ;
8
10
9
11
12
+ static mongoc_database_t *
13
+ get_test_database (mongoc_client_t * client )
14
+ {
15
+ return mongoc_client_get_database (client , MONGOC_TEST_UNIQUE );
16
+ }
17
+
18
+
10
19
static void
11
20
test_insert (void )
12
21
{
22
+ mongoc_database_t * database ;
13
23
mongoc_collection_t * collection ;
14
24
mongoc_client_t * client ;
15
25
bson_context_t * context ;
@@ -19,10 +29,14 @@ test_insert (void)
19
29
unsigned i ;
20
30
bson_t b ;
21
31
32
+
22
33
client = mongoc_client_new (gTestUri );
23
34
ASSERT (client );
24
35
25
- collection = mongoc_client_get_collection (client , "test" , "test" );
36
+ database = get_test_database (client );
37
+ ASSERT (database );
38
+
39
+ collection = mongoc_database_get_collection (database , "test_insert" );
26
40
ASSERT (collection );
27
41
28
42
mongoc_collection_drop (collection , & error );
@@ -53,7 +67,11 @@ test_insert (void)
53
67
ASSERT (error .code == MONGOC_ERROR_BSON_INVALID );
54
68
bson_destroy (& b );
55
69
70
+ r = mongoc_collection_drop (collection , & error );
71
+ ASSERT (r );
72
+
56
73
mongoc_collection_destroy (collection );
74
+ mongoc_database_destroy (database );
57
75
bson_context_destroy (context );
58
76
mongoc_client_destroy (client );
59
77
}
@@ -63,6 +81,7 @@ static void
63
81
test_insert_bulk (void )
64
82
{
65
83
mongoc_collection_t * collection ;
84
+ mongoc_database_t * database ;
66
85
mongoc_client_t * client ;
67
86
bson_context_t * context ;
68
87
bson_error_t error ;
@@ -77,7 +96,10 @@ test_insert_bulk (void)
77
96
client = mongoc_client_new (gTestUri );
78
97
ASSERT (client );
79
98
80
- collection = mongoc_client_get_collection (client , "test" , "test" );
99
+ database = get_test_database (client );
100
+ ASSERT (database );
101
+
102
+ collection = mongoc_database_get_collection (database , "test_insert_bulk" );
81
103
ASSERT (collection );
82
104
83
105
mongoc_collection_drop (collection , & error );
@@ -138,7 +160,11 @@ test_insert_bulk (void)
138
160
bson_destroy (& b [i ]);
139
161
}
140
162
163
+ r = mongoc_collection_drop (collection , & error );
164
+ ASSERT (r );
165
+
141
166
mongoc_collection_destroy (collection );
167
+ mongoc_database_destroy (database );
142
168
bson_context_destroy (context );
143
169
mongoc_client_destroy (client );
144
170
}
@@ -148,6 +174,7 @@ static void
148
174
test_save (void )
149
175
{
150
176
mongoc_collection_t * collection ;
177
+ mongoc_database_t * database ;
151
178
mongoc_client_t * client ;
152
179
bson_context_t * context ;
153
180
bson_error_t error ;
@@ -159,7 +186,10 @@ test_save (void)
159
186
client = mongoc_client_new (gTestUri );
160
187
ASSERT (client );
161
188
162
- collection = mongoc_client_get_collection (client , "test" , "test" );
189
+ database = get_test_database (client );
190
+ ASSERT (database );
191
+
192
+ collection = mongoc_database_get_collection (database , "test_save" );
163
193
ASSERT (collection );
164
194
165
195
mongoc_collection_drop (collection , & error );
@@ -182,7 +212,11 @@ test_save (void)
182
212
183
213
bson_destroy (& b );
184
214
215
+ r = mongoc_collection_drop (collection , & error );
216
+ ASSERT (r );
217
+
185
218
mongoc_collection_destroy (collection );
219
+ mongoc_database_destroy (database );
186
220
bson_context_destroy (context );
187
221
mongoc_client_destroy (client );
188
222
}
@@ -192,18 +226,32 @@ static void
192
226
test_regex (void )
193
227
{
194
228
mongoc_collection_t * collection ;
229
+ mongoc_database_t * database ;
230
+ mongoc_write_concern_t * wr ;
195
231
mongoc_client_t * client ;
196
232
bson_error_t error = { 0 };
197
233
int64_t count ;
198
234
bson_t q = BSON_INITIALIZER ;
235
+ bson_t * doc ;
236
+ bool r ;
199
237
200
238
client = mongoc_client_new (gTestUri );
201
239
ASSERT (client );
202
240
203
- collection = mongoc_client_get_collection (client , "test" , "test" );
241
+ database = get_test_database (client );
242
+ ASSERT (database );
243
+
244
+ collection = mongoc_database_get_collection (database , "test_regex" );
204
245
ASSERT (collection );
205
246
206
- bson_append_regex (& q , "hello" , -1 , "^/wo" , NULL );
247
+ wr = mongoc_write_concern_new ();
248
+ mongoc_write_concern_set_journal (wr , true);
249
+
250
+ doc = BCON_NEW ("hello" , "/world" );
251
+ r = mongoc_collection_insert (collection , MONGOC_INSERT_NONE , doc , wr , & error );
252
+ ASSERT (r );
253
+
254
+ BSON_APPEND_REGEX (& q , "hello" , "^/wo" , "i" );
207
255
208
256
count = mongoc_collection_count (collection ,
209
257
MONGOC_QUERY_NONE ,
@@ -216,8 +264,15 @@ test_regex (void)
216
264
ASSERT (count > 0 );
217
265
ASSERT (!error .domain );
218
266
267
+ r = mongoc_collection_drop (collection , & error );
268
+ ASSERT (r );
269
+
270
+ mongoc_write_concern_destroy (wr );
271
+
219
272
bson_destroy (& q );
273
+ bson_destroy (doc );
220
274
mongoc_collection_destroy (collection );
275
+ mongoc_database_destroy (database );
221
276
mongoc_client_destroy (client );
222
277
}
223
278
@@ -226,6 +281,7 @@ static void
226
281
test_update (void )
227
282
{
228
283
mongoc_collection_t * collection ;
284
+ mongoc_database_t * database ;
229
285
mongoc_client_t * client ;
230
286
bson_context_t * context ;
231
287
bson_error_t error ;
@@ -240,7 +296,10 @@ test_update (void)
240
296
client = mongoc_client_new (gTestUri );
241
297
ASSERT (client );
242
298
243
- collection = mongoc_client_get_collection (client , "test" , "test" );
299
+ database = get_test_database (client );
300
+ ASSERT (database );
301
+
302
+ collection = mongoc_database_get_collection (database , "test_update" );
244
303
ASSERT (collection );
245
304
246
305
context = bson_context_new (BSON_CONTEXT_NONE );
@@ -301,7 +360,11 @@ test_update (void)
301
360
bson_destroy (& q );
302
361
bson_destroy (& u );
303
362
363
+ r = mongoc_collection_drop (collection , & error );
364
+ ASSERT (r );
365
+
304
366
mongoc_collection_destroy (collection );
367
+ mongoc_database_destroy (database );
305
368
bson_context_destroy (context );
306
369
mongoc_client_destroy (client );
307
370
}
@@ -311,6 +374,7 @@ static void
311
374
test_delete (void )
312
375
{
313
376
mongoc_collection_t * collection ;
377
+ mongoc_database_t * database ;
314
378
mongoc_client_t * client ;
315
379
bson_context_t * context ;
316
380
bson_error_t error ;
@@ -322,7 +386,10 @@ test_delete (void)
322
386
client = mongoc_client_new (gTestUri );
323
387
ASSERT (client );
324
388
325
- collection = mongoc_client_get_collection (client , "test" , "test" );
389
+ database = get_test_database (client );
390
+ ASSERT (database );
391
+
392
+ collection = mongoc_database_get_collection (database , "test_delete" );
326
393
ASSERT (collection );
327
394
328
395
context = bson_context_new (BSON_CONTEXT_NONE );
@@ -352,7 +419,11 @@ test_delete (void)
352
419
bson_destroy (& b );
353
420
}
354
421
422
+ r = mongoc_collection_drop (collection , & error );
423
+ ASSERT (r );
424
+
355
425
mongoc_collection_destroy (collection );
426
+ mongoc_database_destroy (database );
356
427
bson_context_destroy (context );
357
428
mongoc_client_destroy (client );
358
429
}
@@ -361,6 +432,7 @@ static void
361
432
test_index (void )
362
433
{
363
434
mongoc_collection_t * collection ;
435
+ mongoc_database_t * database ;
364
436
mongoc_client_t * client ;
365
437
mongoc_index_opt_t opt ;
366
438
bson_error_t error ;
@@ -372,7 +444,10 @@ test_index (void)
372
444
client = mongoc_client_new (gTestUri );
373
445
ASSERT (client );
374
446
375
- collection = mongoc_client_get_collection (client , "test" , "test" );
447
+ database = get_test_database (client );
448
+ ASSERT (database );
449
+
450
+ collection = mongoc_database_get_collection (database , "test_index" );
376
451
ASSERT (collection );
377
452
378
453
bson_init (& keys );
@@ -388,7 +463,11 @@ test_index (void)
388
463
389
464
bson_destroy (& keys );
390
465
466
+ r = mongoc_collection_drop (collection , & error );
467
+ ASSERT (r );
468
+
391
469
mongoc_collection_destroy (collection );
470
+ mongoc_database_destroy (database );
392
471
mongoc_client_destroy (client );
393
472
}
394
473
@@ -427,23 +506,34 @@ static void
427
506
test_drop (void )
428
507
{
429
508
mongoc_collection_t * collection ;
509
+ mongoc_database_t * database ;
430
510
mongoc_client_t * client ;
431
511
bson_error_t error ;
512
+ bson_t * doc ;
432
513
bool r ;
433
514
434
515
client = mongoc_client_new (gTestUri );
435
516
ASSERT (client );
436
517
437
- collection = mongoc_client_get_collection (client , "test" , "test" );
518
+ database = get_test_database (client );
519
+ ASSERT (database );
520
+
521
+ collection = mongoc_database_get_collection (database , "test_drop" );
438
522
ASSERT (collection );
439
523
524
+ doc = BCON_NEW ("hello" , "world" );
525
+ r = mongoc_collection_insert (collection , MONGOC_INSERT_NONE , doc , NULL , & error );
526
+ bson_destroy (doc );
527
+ ASSERT (r );
528
+
440
529
r = mongoc_collection_drop (collection , & error );
441
- assert (r == true);
530
+ ASSERT (r == true);
442
531
443
532
r = mongoc_collection_drop (collection , & error );
444
- assert (r == false);
533
+ ASSERT (r == false);
445
534
446
535
mongoc_collection_destroy (collection );
536
+ mongoc_database_destroy (database );
447
537
mongoc_client_destroy (client );
448
538
}
449
539
@@ -452,6 +542,7 @@ static void
452
542
test_aggregate (void )
453
543
{
454
544
mongoc_collection_t * collection ;
545
+ mongoc_database_t * database ;
455
546
mongoc_client_t * client ;
456
547
mongoc_cursor_t * cursor ;
457
548
const bson_t * doc ;
@@ -474,7 +565,10 @@ test_aggregate (void)
474
565
client = mongoc_client_new (gTestUri );
475
566
ASSERT (client );
476
567
477
- collection = mongoc_client_get_collection (client , "test" , "test" );
568
+ database = get_test_database (client );
569
+ ASSERT (database );
570
+
571
+ collection = mongoc_database_get_collection (database , "test_aggregate" );
478
572
ASSERT (collection );
479
573
480
574
mongoc_collection_drop (collection , & error );
@@ -506,8 +600,12 @@ test_aggregate (void)
506
600
ASSERT (!r );
507
601
ASSERT (!doc );
508
602
603
+ r = mongoc_collection_drop (collection , & error );
604
+ ASSERT (r );
605
+
509
606
mongoc_cursor_destroy (cursor );
510
607
mongoc_collection_destroy (collection );
608
+ mongoc_database_destroy (database );
511
609
mongoc_client_destroy (client );
512
610
bson_destroy (& b );
513
611
bson_destroy (& pipeline );
0 commit comments