@@ -937,6 +937,59 @@ describe('Bulk', function() {
937
937
}
938
938
} ) ;
939
939
940
+ it ( 'should provide descriptive error message for unordered batch with duplicate key errors on inserts' , function ( done ) {
941
+ const configuration = this . configuration ;
942
+ const client = configuration . newClient ( configuration . writeConcernMax ( ) , {
943
+ poolSize : 1
944
+ } ) ;
945
+
946
+ client . connect ( ( err , client ) => {
947
+ const db = client . db ( configuration . db ) ;
948
+ const col = db . collection ( 'err_batch_write_unordered_ops_legacy_6' ) ;
949
+
950
+ // Add unique index on a field causing all inserts to fail
951
+ col . createIndexes (
952
+ [
953
+ {
954
+ name : 'err_batch_write_unordered_ops_legacy_6' ,
955
+ key : { a : 1 } ,
956
+ unique : true
957
+ }
958
+ ] ,
959
+ err => {
960
+ expect ( err ) . to . not . exist ;
961
+
962
+ // Initialize the unordered Batch
963
+ const batch = col . initializeUnorderedBulkOp ( ) ;
964
+
965
+ // Add some operations to be executed in order
966
+ batch . insert ( { a : 1 } ) ;
967
+ batch . insert ( { a : 1 } ) ;
968
+
969
+ // Execute the operations
970
+ batch . execute ( configuration . writeConcernMax ( ) , ( err , result ) => {
971
+ expect ( err ) . to . exist ;
972
+ expect ( result ) . to . not . exist ;
973
+
974
+ // Test basic settings
975
+ result = err . result ;
976
+ expect ( result . nInserted ) . to . equal ( 1 ) ;
977
+ expect ( result . hasWriteErrors ( ) ) . to . equal ( true ) ;
978
+ expect ( result . getWriteErrorCount ( ) === 1 ) . to . equal ( true ) ;
979
+
980
+ // Individual error checking
981
+ const error = result . getWriteErrorAt ( 0 ) ;
982
+ expect ( error . code === 11000 ) . to . equal ( true ) ;
983
+ expect ( error . errmsg ) . to . exist ;
984
+ expect ( err . message ) . to . equal ( error . errmsg ) ;
985
+
986
+ client . close ( done ) ;
987
+ } ) ;
988
+ }
989
+ ) ;
990
+ } ) ;
991
+ } ) ;
992
+
940
993
it (
941
994
'should Correctly Execute Unordered Batch of with upserts causing duplicate key errors on updates' ,
942
995
{
0 commit comments