@@ -5687,6 +5687,112 @@ test_insert_one_reports_id (void)
56875687
56885688#undef ASSERT_INDEX_EXISTS
56895689
5690+ static void
5691+ test_get_last_error (void )
5692+ {
5693+ mongoc_client_t * client = test_framework_new_default_client ();
5694+ mongoc_collection_t * coll = get_test_collection (client , "test_get_last_error" );
5695+ bson_error_t error ;
5696+ bool ok ;
5697+
5698+ // Test mongoc_collection_update:
5699+ {
5700+ mongoc_collection_drop (coll , NULL );
5701+
5702+ // Clear error:
5703+ ASSERT_OR_PRINT (mongoc_collection_command_simple (coll , tmp_bson ("{'ping': 1}" ), NULL , NULL , & error ), error );
5704+ ASSERT (!mongoc_collection_get_last_error (coll ));
5705+
5706+ // Insert a document to remove:
5707+ ASSERT_OR_PRINT (mongoc_collection_insert_one (coll , tmp_bson ("{'_id': 0}" ), NULL , NULL , & error ), error );
5708+
5709+ // Update:
5710+ ok = mongoc_collection_update (
5711+ coll , MONGOC_UPDATE_NONE , tmp_bson ("{}" ), tmp_bson ("{'$set': {'foo': 'bar'}}" ), NULL , & error );
5712+ ASSERT_OR_PRINT (ok , error );
5713+ const bson_t * gle = mongoc_collection_get_last_error (coll );
5714+ ASSERT_MATCH (
5715+ gle ,
5716+ BSON_STR (
5717+ {"nInserted" : 0 , "nMatched" : 1 , "nModified" : 1 , "nRemoved" : 0 , "nUpserted" : 0 , "writeErrors" : []}));
5718+ }
5719+
5720+ // Test mongoc_collection_remove:
5721+ {
5722+ mongoc_collection_drop (coll , NULL );
5723+
5724+ // Clear error:
5725+ ASSERT_OR_PRINT (mongoc_collection_command_simple (coll , tmp_bson ("{'ping': 1}" ), NULL , NULL , & error ), error );
5726+ ASSERT (!mongoc_collection_get_last_error (coll ));
5727+
5728+ // Insert a document to remove:
5729+ ASSERT_OR_PRINT (mongoc_collection_insert_one (coll , tmp_bson ("{'_id': 0}" ), NULL , NULL , & error ), error );
5730+
5731+ ok = mongoc_collection_remove (coll , MONGOC_REMOVE_NONE , tmp_bson ("{}" ), NULL , & error );
5732+ ASSERT_OR_PRINT (ok , error );
5733+ const bson_t * gle = mongoc_collection_get_last_error (coll );
5734+ ASSERT_MATCH (
5735+ gle ,
5736+ BSON_STR (
5737+ {"nInserted" : 0 , "nMatched" : 0 , "nModified" : 0 , "nRemoved" : 1 , "nUpserted" : 0 , "writeErrors" : []}));
5738+ }
5739+
5740+ // Test mongoc_collection_delete:
5741+ {
5742+ mongoc_collection_drop (coll , NULL );
5743+
5744+ // Clear error:
5745+ ASSERT_OR_PRINT (mongoc_collection_command_simple (coll , tmp_bson ("{'ping': 1}" ), NULL , NULL , & error ), error );
5746+ ASSERT (!mongoc_collection_get_last_error (coll ));
5747+
5748+ // Insert a document to delete:
5749+ ASSERT_OR_PRINT (mongoc_collection_insert_one (coll , tmp_bson ("{'_id': 0}" ), NULL , NULL , & error ), error );
5750+
5751+ ok = mongoc_collection_delete (coll , MONGOC_DELETE_NONE , tmp_bson ("{}" ), NULL , & error );
5752+ ASSERT_OR_PRINT (ok , error );
5753+ const bson_t * gle = mongoc_collection_get_last_error (coll );
5754+ ASSERT_MATCH (
5755+ gle ,
5756+ BSON_STR (
5757+ {"nInserted" : 0 , "nMatched" : 0 , "nModified" : 0 , "nRemoved" : 1 , "nUpserted" : 0 , "writeErrors" : []}));
5758+ }
5759+
5760+ // Test mongoc_collection_insert_bulk:
5761+ {
5762+ mongoc_collection_drop (coll , NULL );
5763+
5764+ // Clear error:
5765+ ASSERT_OR_PRINT (mongoc_collection_command_simple (coll , tmp_bson ("{'ping': 1}" ), NULL , NULL , & error ), error );
5766+ ASSERT (!mongoc_collection_get_last_error (coll ));
5767+
5768+ bson_t * docs [] = {tmp_bson ("{'_id': 1}" )};
5769+ ok = mongoc_collection_insert_bulk (coll , MONGOC_INSERT_NONE , (const bson_t * * ) docs , 1u , NULL , & error );
5770+ ASSERT_OR_PRINT (ok , error );
5771+ const bson_t * gle = mongoc_collection_get_last_error (coll );
5772+ ASSERT_MATCH (
5773+ gle ,
5774+ BSON_STR (
5775+ {"nInserted" : 1 , "nMatched" : 0 , "nModified" : 0 , "nRemoved" : 0 , "nUpserted" : 0 , "writeErrors" : []}));
5776+ }
5777+
5778+ // Test mongoc_collection_insert:
5779+ {
5780+ mongoc_collection_drop (coll , NULL );
5781+
5782+ // Clear error:
5783+ ASSERT_OR_PRINT (mongoc_collection_command_simple (coll , tmp_bson ("{'ping': 1}" ), NULL , NULL , & error ), error );
5784+ ASSERT (!mongoc_collection_get_last_error (coll ));
5785+
5786+ ok = mongoc_collection_insert (coll , MONGOC_INSERT_NONE , tmp_bson ("{'_id': 1}" ), NULL , & error );
5787+ ASSERT_OR_PRINT (ok , error );
5788+ const bson_t * gle = mongoc_collection_get_last_error (coll );
5789+ ASSERT_MATCH (gle , BSON_STR ({"insertedCount" : 1 , "insertedId" : 1 }));
5790+ }
5791+
5792+ mongoc_collection_destroy (coll );
5793+ mongoc_client_destroy (client );
5794+ }
5795+
56905796void
56915797test_collection_install (TestSuite * suite )
56925798{
@@ -5846,4 +5952,5 @@ test_collection_install (TestSuite *suite)
58465952 // requires failpoint
58475953 test_framework_skip_if_no_failpoint );
58485954 TestSuite_AddLive (suite , "/Collection/insert_one_reports_id" , test_insert_one_reports_id );
5955+ TestSuite_AddLive (suite , "/Collection/get_last_error" , test_get_last_error );
58495956}
0 commit comments