@@ -45,6 +45,26 @@ using bsoncxx::builder::basic::make_document;
45
45
46
46
using namespace mongocxx ;
47
47
48
+ mongocxx::collection
49
+ make_test_coll (mongocxx::client& client, bsoncxx::stdx::string_view db_name, bsoncxx::stdx::string_view coll_name) {
50
+ write_concern wc_majority;
51
+ wc_majority.acknowledge_level (write_concern::level::k_majority);
52
+
53
+ read_concern rc_majority;
54
+ rc_majority.acknowledge_level (read_concern::level::k_majority);
55
+
56
+ auto db = client[db_name];
57
+ auto coll = db[coll_name];
58
+
59
+ coll.drop ();
60
+ coll = db.create_collection (coll_name);
61
+
62
+ coll.write_concern (wc_majority);
63
+ coll.read_concern (rc_majority);
64
+
65
+ return coll;
66
+ }
67
+
48
68
// Create a single-item document.
49
69
// E.g. doc("foo", 123) creates {"foo":123}.
50
70
template <typename T>
@@ -91,9 +111,9 @@ auto const watch_interpose = [](mongoc_collection_t const*, bson_t const*, bson_
91
111
92
112
auto const destroy_interpose = [](mongoc_change_stream_t *) -> void {};
93
113
94
- TEST_CASE (" Change stream options" ) {
114
+ TEST_CASE (" Change stream options" , " [change_stream] " ) {
95
115
instance::current ();
96
- client mongodb_client {uri{}, test_util::add_test_server_api ()};
116
+ client client {uri{}, test_util::add_test_server_api ()};
97
117
98
118
if (!test_util::is_replica_set ()) {
99
119
SKIP (" change streams require replica set" );
@@ -108,25 +128,20 @@ TEST_CASE("Change stream options") {
108
128
cs_opts.resume_after (resume_after.view ());
109
129
cs_opts.start_after (start_after.view ());
110
130
111
- auto cs = mongodb_client .watch (cs_opts);
131
+ auto cs = client .watch (cs_opts);
112
132
REQUIRE_THROWS (cs.begin ());
113
133
}
114
134
}
115
135
116
- TEST_CASE (" Spec Prose Tests" ) {
136
+ TEST_CASE (" Spec Prose Tests" , " [change_stream] " ) {
117
137
instance::current ();
118
138
client client{uri{}, test_util::add_test_server_api ()};
119
139
120
140
if (!test_util::is_replica_set ()) {
121
141
SKIP (" change streams require replica set" );
122
142
}
123
143
124
- auto db = client[" db" ];
125
- auto coll = db[" coll" ];
126
- coll.drop ();
127
-
128
- write_concern wc_majority;
129
- wc_majority.majority (std::chrono::seconds (30 ));
144
+ auto coll = make_test_coll (client, " db" , " coll" );
130
145
131
146
// As a sanity check, we implement the first prose test. The behavior tested
132
147
// by the prose tests is implemented and tested by the C driver, so we won't
@@ -143,21 +158,18 @@ TEST_CASE("Spec Prose Tests") {
143
158
auto doc2 = make_document (kvp (" b" , 2 ));
144
159
auto doc3 = make_document (kvp (" c" , 3 ));
145
160
146
- options::insert insert_opts{};
147
- insert_opts.write_concern (wc_majority);
148
-
149
161
{
150
- auto res = coll.insert_one (doc1.view (), insert_opts );
162
+ auto res = coll.insert_one (doc1.view ());
151
163
REQUIRE (res);
152
164
REQUIRE (res->result ().inserted_count () == 1 );
153
165
}
154
166
{
155
- auto res = coll.insert_one (doc2.view (), insert_opts );
167
+ auto res = coll.insert_one (doc2.view ());
156
168
REQUIRE (res);
157
169
REQUIRE (res->result ().inserted_count () == 1 );
158
170
}
159
171
{
160
- auto res = coll.insert_one (doc3.view (), insert_opts );
172
+ auto res = coll.insert_one (doc3.view ());
161
173
REQUIRE (res);
162
174
REQUIRE (res->result ().inserted_count () == 1 );
163
175
}
@@ -188,14 +200,13 @@ TEST_CASE("Spec Prose Tests") {
188
200
}
189
201
}
190
202
191
- TEST_CASE (" Mock streams and error-handling" ) {
203
+ TEST_CASE (" Mock streams and error-handling" , " [change_stream] " ) {
192
204
MOCK_CHANGE_STREAM;
193
205
194
206
instance::current ();
195
- client mongodb_client {uri{}, test_util::add_test_server_api ()};
207
+ client client {uri{}, test_util::add_test_server_api ()};
196
208
options::change_stream options{};
197
- database db = mongodb_client[" streams" ];
198
- collection events = db[" events" ];
209
+ collection events = make_test_coll (client, " streams" , " events" );
199
210
200
211
// nop watch and destroy
201
212
collection_watch->interpose (watch_interpose).forever ();
@@ -360,9 +371,9 @@ TEST_CASE("Mock streams and error-handling") {
360
371
return nullptr ;
361
372
});
362
373
363
- mongodb_client [" db" ][" collection" ].watch (cs_pipeline, cs_opts);
364
- mongodb_client [" db" ].watch (cs_pipeline, cs_opts);
365
- mongodb_client .watch (cs_pipeline, cs_opts);
374
+ client [" db" ][" collection" ].watch (cs_pipeline, cs_opts);
375
+ client [" db" ].watch (cs_pipeline, cs_opts);
376
+ client .watch (cs_pipeline, cs_opts);
366
377
367
378
// Ensure the interpose was called.
368
379
REQUIRE (collection_watch_called);
@@ -372,35 +383,33 @@ TEST_CASE("Mock streams and error-handling") {
372
383
}
373
384
374
385
// Put this before other tests which assume the collections already exists.
375
- TEST_CASE (" Create streams.events and assert we can read a single event" , " [min36 ]" ) {
386
+ TEST_CASE (" Create streams.events and assert we can read a single event" , " [change_stream ]" ) {
376
387
instance::current ();
377
- client mongodb_client {uri{}, test_util::add_test_server_api ()};
388
+ client client {uri{}, test_util::add_test_server_api ()};
378
389
if (!test_util::is_replica_set ()) {
379
390
SKIP (" change streams require replica set" );
380
391
}
381
392
382
- collection events = mongodb_client[" streams" ][" events" ];
383
- events.drop ();
393
+ collection events = make_test_coll (client, " streams" , " events" );
384
394
385
395
events.insert_one (make_document (kvp (" dummy" , " doc" )));
386
396
change_stream stream = events.watch ();
387
397
events.insert_one (make_document (kvp (" another" , " event" )));
388
398
REQUIRE (std::distance (stream.begin (), stream.end ()) == 1 );
389
399
390
400
// because we watch events2 in a test
391
- auto events2 = mongodb_client[" streams" ][" events2" ];
392
- events2.drop ();
401
+ auto events2 = make_test_coll (client, " streams" , " events2" );
393
402
}
394
403
395
- TEST_CASE (" Give an invalid pipeline" , " [min36 ]" ) {
404
+ TEST_CASE (" Give an invalid pipeline" , " [change_stream ]" ) {
396
405
instance::current ();
397
- client mongodb_client {uri{}, test_util::add_test_server_api ()};
406
+ client client {uri{}, test_util::add_test_server_api ()};
398
407
if (!test_util::is_replica_set ()) {
399
408
SKIP (" change streams require replica set" );
400
409
}
401
410
402
411
options::change_stream options{};
403
- collection events = mongodb_client[ " streams" ][ " events" ] ;
412
+ collection events = make_test_coll (client, " streams" , " events" ) ;
404
413
405
414
pipeline p;
406
415
p.match (make_document (kvp (" $foo" , -1 )));
@@ -421,16 +430,15 @@ TEST_CASE("Give an invalid pipeline", "[min36]") {
421
430
}
422
431
}
423
432
424
- TEST_CASE (" Documentation Examples" , " [min36 ]" ) {
433
+ TEST_CASE (" Documentation Examples" , " [change_stream ]" ) {
425
434
instance::current ();
426
435
mongocxx::pool pool{uri{}, options::pool (test_util::add_test_server_api ())};
427
- auto mongodb_client = pool.acquire ();
436
+ auto client = pool.acquire ();
428
437
if (!test_util::is_replica_set ()) {
429
438
SKIP (" change streams require replica set" );
430
439
}
431
440
432
- collection events = (*mongodb_client)[" streams" ][" events" ];
433
- collection inventory = events; // doc examples use this name
441
+ collection inventory = make_test_coll (*client, " streams" , " events" );
434
442
435
443
std::atomic_bool insert_thread_done;
436
444
insert_thread_done.store (false );
@@ -525,20 +533,19 @@ TEST_CASE("Documentation Examples", "[min36]") {
525
533
526
534
insert_thread_done = true ;
527
535
insert_thread.join ();
528
- inventory.drop ();
529
536
}
530
537
531
- TEST_CASE (" Watch 2 collections" , " [min36 ]" ) {
538
+ TEST_CASE (" Watch 2 collections" , " [change_stream ]" ) {
532
539
instance::current ();
533
- client mongodb_client {uri{}, test_util::add_test_server_api ()};
540
+ client client {uri{}, test_util::add_test_server_api ()};
534
541
if (!test_util::is_replica_set ()) {
535
542
SKIP (" change streams require replica set" );
536
543
}
537
544
538
545
options::change_stream options{};
539
546
540
- collection events = mongodb_client[ " streams" ][ " events" ] ;
541
- collection events2 = mongodb_client[ " streams" ][ " events2" ] ;
547
+ collection events = make_test_coll (client, " streams" , " events" ) ;
548
+ collection events2 = make_test_coll (client, " streams" , " events2" ) ;
542
549
543
550
change_stream x = events.watch ();
544
551
change_stream x2 = events.watch ();
@@ -575,15 +582,15 @@ TEST_CASE("Watch 2 collections", "[min36]") {
575
582
}
576
583
}
577
584
578
- TEST_CASE (" Watch a Collection" , " [min36 ]" ) {
585
+ TEST_CASE (" Watch a Collection" , " [change_stream ]" ) {
579
586
instance::current ();
580
- client mongodb_client {uri{}, test_util::add_test_server_api ()};
587
+ client client {uri{}, test_util::add_test_server_api ()};
581
588
if (!test_util::is_replica_set ()) {
582
589
SKIP (" change streams require replica set" );
583
590
}
584
591
585
592
options::change_stream options{};
586
- collection events = mongodb_client[ " streams" ][ " events" ] ;
593
+ collection events = make_test_coll (client, " streams" , " events" ) ;
587
594
588
595
change_stream x = events.watch ();
589
596
0 commit comments