@@ -1286,6 +1286,136 @@ test_aggregate (void)
1286
1286
}
1287
1287
1288
1288
1289
+ typedef struct {
1290
+ bool with_batch_size ;
1291
+ bool with_options ;
1292
+ } test_aggregate_context_t ;
1293
+
1294
+
1295
+ static const char *
1296
+ options_json (test_aggregate_context_t * c )
1297
+ {
1298
+ if (c -> with_batch_size && c -> with_options ) {
1299
+ return "{'foo': 1, 'batchSize': 11}" ;
1300
+ } else if (c -> with_batch_size ) {
1301
+ return "{'batchSize': 11}" ;
1302
+ } else if (c -> with_options ) {
1303
+ return "{'foo': 1}" ;
1304
+ } else {
1305
+ return "{}" ;
1306
+ }
1307
+ }
1308
+
1309
+
1310
+ static void
1311
+ test_aggregate_legacy (void * data )
1312
+ {
1313
+ test_aggregate_context_t * context = (test_aggregate_context_t * ) data ;
1314
+ mock_server_t * server ;
1315
+ mongoc_client_t * client ;
1316
+ mongoc_collection_t * collection ;
1317
+ future_t * future ;
1318
+ request_t * request ;
1319
+ mongoc_cursor_t * cursor ;
1320
+ const bson_t * doc ;
1321
+
1322
+ /* wire protocol version 0 */
1323
+ server = mock_server_with_autoismaster (0 );
1324
+ mock_server_run (server );
1325
+ client = mongoc_client_new_from_uri (mock_server_get_uri (server ));
1326
+ collection = mongoc_client_get_collection (client , "db" , "collection" );
1327
+
1328
+ cursor = mongoc_collection_aggregate (
1329
+ collection ,
1330
+ MONGOC_QUERY_NONE ,
1331
+ tmp_bson ("[{'a': 1}]" ),
1332
+ tmp_bson (options_json (context )),
1333
+ NULL );
1334
+
1335
+ future = future_cursor_next (cursor , & doc );
1336
+
1337
+ /* no "cursor" argument */
1338
+ request = mock_server_receives_command (
1339
+ server , "db" , MONGOC_QUERY_NONE ,
1340
+ "{'aggregate': 'collection',"
1341
+ " 'pipeline': [{'a': 1}]},"
1342
+ " 'cursor': {'$exists': false} %s" ,
1343
+ context -> with_options ? ", 'foo': 1" : "" );
1344
+
1345
+ mock_server_replies_simple (request , "{'ok': 1, 'result': [{'_id': 123}]}" );
1346
+ assert (future_get_bool (future ));
1347
+ ASSERT_MATCH (doc , "{'_id': 123}" );
1348
+
1349
+ /* cursor is completed */
1350
+ assert (!mongoc_cursor_next (cursor , & doc ));
1351
+
1352
+ mongoc_cursor_destroy (cursor );
1353
+ request_destroy (request );
1354
+ future_destroy (future );
1355
+ mongoc_collection_destroy (collection );
1356
+ mongoc_client_destroy (client );
1357
+ mock_server_destroy (server );
1358
+ }
1359
+
1360
+
1361
+ static void
1362
+ test_aggregate_modern (void * data )
1363
+ {
1364
+ test_aggregate_context_t * context = (test_aggregate_context_t * ) data ;
1365
+ mock_server_t * server ;
1366
+ mongoc_client_t * client ;
1367
+ mongoc_collection_t * collection ;
1368
+ future_t * future ;
1369
+ request_t * request ;
1370
+ mongoc_cursor_t * cursor ;
1371
+ const bson_t * doc ;
1372
+
1373
+ /* wire protocol version 1 */
1374
+ server = mock_server_with_autoismaster (1 );
1375
+ mock_server_run (server );
1376
+ client = mongoc_client_new_from_uri (mock_server_get_uri (server ));
1377
+ collection = mongoc_client_get_collection (client , "db" , "collection" );
1378
+
1379
+ future = future_collection_aggregate (
1380
+ collection ,
1381
+ MONGOC_QUERY_NONE ,
1382
+ tmp_bson ("[{'a': 1}]" ),
1383
+ tmp_bson (options_json (context )),
1384
+ NULL );
1385
+
1386
+ /* "cursor" argument always sent if wire version >= 1 */
1387
+ request = mock_server_receives_command (
1388
+ server , "db" , MONGOC_QUERY_NONE ,
1389
+ "{'aggregate': 'collection',"
1390
+ " 'pipeline': [{'a': 1}],"
1391
+ " 'cursor': %s %s}" ,
1392
+ context -> with_batch_size ? "{'batchSize': 11}" : "{'$empty': true}" ,
1393
+ context -> with_options ? ", 'foo': 1" : "" );
1394
+
1395
+ mock_server_replies_simple (request ,
1396
+ "{'ok': 1,"
1397
+ " 'cursor': {"
1398
+ " 'id': 0,"
1399
+ " 'ns': 'db.collection',"
1400
+ " 'firstBatch': [{'_id': 123}]"
1401
+ "}}" );
1402
+
1403
+ assert ((cursor = future_get_mongoc_cursor_ptr (future )));
1404
+ assert (mongoc_cursor_next (cursor , & doc ));
1405
+ ASSERT_MATCH (doc , "{'_id': 123}" );
1406
+
1407
+ /* cursor is completed */
1408
+ assert (!mongoc_cursor_next (cursor , & doc ));
1409
+
1410
+ mongoc_cursor_destroy (cursor );
1411
+ request_destroy (request );
1412
+ future_destroy (future );
1413
+ mongoc_collection_destroy (collection );
1414
+ mongoc_client_destroy (client );
1415
+ mock_server_destroy (server );
1416
+ }
1417
+
1418
+
1289
1419
static void
1290
1420
test_validate (void )
1291
1421
{
@@ -1747,9 +1877,48 @@ test_get_index_info (void)
1747
1877
mongoc_client_destroy (client );
1748
1878
}
1749
1879
1880
+
1881
+ static void
1882
+ test_aggregate_install (TestSuite * suite ) {
1883
+ static test_aggregate_context_t test_aggregate_contexts [2 ][2 ][2 ];
1884
+
1885
+ int wire_version , with_batch_size , with_options ;
1886
+ char * legacy_or_modern ;
1887
+ TestFuncWC func ;
1888
+ char * name ;
1889
+ test_aggregate_context_t * context ;
1890
+
1891
+ for (wire_version = 0 ; wire_version < 2 ; wire_version ++ ) {
1892
+ for (with_batch_size = 0 ; with_batch_size < 2 ; with_batch_size ++ ) {
1893
+ for (with_options = 0 ; with_options < 2 ; with_options ++ ) {
1894
+ legacy_or_modern = wire_version ? "legacy" : "modern" ;
1895
+ func = wire_version ? test_aggregate_legacy : test_aggregate_modern ;
1896
+
1897
+ context = & test_aggregate_contexts
1898
+ [wire_version ][with_batch_size ][with_options ];
1899
+
1900
+ context -> with_batch_size = (bool ) with_batch_size ;
1901
+ context -> with_options = (bool ) with_options ;
1902
+
1903
+ name = bson_strdup_printf (
1904
+ "/Collection/aggregate/%s/%s/%s" ,
1905
+ legacy_or_modern ,
1906
+ context -> with_batch_size ? "batch_size" : "no_batch_size" ,
1907
+ context -> with_options ? "with_options" : "no_options" );
1908
+
1909
+ TestSuite_AddWC (suite , name , func , NULL , (void * ) context );
1910
+ bson_free (name );
1911
+ }
1912
+ }
1913
+ }
1914
+ }
1915
+
1916
+
1750
1917
void
1751
1918
test_collection_install (TestSuite * suite )
1752
1919
{
1920
+ test_aggregate_install (suite );
1921
+
1753
1922
TestSuite_Add (suite , "/Collection/insert_bulk" , test_insert_bulk );
1754
1923
1755
1924
TestSuite_Add (suite ,
@@ -1787,7 +1956,6 @@ test_collection_install (TestSuite *suite)
1787
1956
TestSuite_Add (suite , "/Collection/count_with_opts" , test_count_with_opts );
1788
1957
TestSuite_Add (suite , "/Collection/drop" , test_drop );
1789
1958
TestSuite_Add (suite , "/Collection/aggregate" , test_aggregate );
1790
- TestSuite_Add (suite , "/Collection/validate" , test_validate );
1791
1959
TestSuite_Add (suite , "/Collection/rename" , test_rename );
1792
1960
TestSuite_Add (suite , "/Collection/stats" , test_stats );
1793
1961
TestSuite_Add (suite , "/Collection/find_and_modify" , test_find_and_modify );
0 commit comments