55 type Collection ,
66 Long ,
77 MongoBatchReExecutionError ,
8+ MongoBulkWriteError ,
89 type MongoClient ,
910 MongoDriverError ,
1011 MongoInvalidArgumentError
@@ -31,7 +32,6 @@ describe('Bulk', function () {
3132 . createCollection ( 'test' )
3233 . catch ( ( ) => null ) ; // make ns exist
3334 } ) ;
34-
3535 afterEach ( async function ( ) {
3636 const cleanup = this . configuration . newClient ( ) ;
3737 await cleanup
@@ -91,6 +91,7 @@ describe('Bulk', function () {
9191 }
9292 } ) ;
9393 } ) ;
94+
9495 context ( 'when passed a valid document list' , function ( ) {
9596 it ( 'insertMany should not throw a MongoInvalidArgument error when called with a valid operation' , async function ( ) {
9697 try {
@@ -104,6 +105,121 @@ describe('Bulk', function () {
104105 }
105106 } ) ;
106107 } ) ;
108+
109+ context ( 'when inserting duplicate values' , function ( ) {
110+ let col ;
111+
112+ beforeEach ( async function ( ) {
113+ const db = client . db ( ) ;
114+ col = db . collection ( 'test' ) ;
115+ await col . createIndex ( [ { a : 1 } ] , { unique : true , sparse : false } ) ;
116+ } ) ;
117+
118+ async function assertFailsWithDuplicateFields ( input , isOrdered , expectedInsertedIds ) {
119+ const error = await col . insertMany ( input , { ordered : isOrdered } ) . catch ( error => error ) ;
120+ expect ( error ) . to . be . instanceOf ( MongoBulkWriteError ) ;
121+ expect ( error . result . insertedCount ) . to . equal ( Object . keys ( error . result . insertedIds ) . length ) ;
122+ expect ( error . result . insertedIds ) . to . deep . equal ( expectedInsertedIds ) ;
123+ }
124+
125+ context ( 'when the insert is ordered' , function ( ) {
126+ it ( 'contains the correct insertedIds on one duplicate insert' , async function ( ) {
127+ await assertFailsWithDuplicateFields (
128+ [
129+ { _id : 0 , a : 1 } ,
130+ { _id : 1 , a : 1 }
131+ ] ,
132+ true ,
133+ { 0 : 0 }
134+ ) ;
135+ } ) ;
136+
137+ it ( 'contains the correct insertedIds on multiple duplicate inserts' , async function ( ) {
138+ await assertFailsWithDuplicateFields (
139+ [
140+ { _id : 0 , a : 1 } ,
141+ { _id : 1 , a : 1 } ,
142+ { _id : 2 , a : 1 } ,
143+ { _id : 3 , b : 2 }
144+ ] ,
145+ true ,
146+ { 0 : 0 }
147+ ) ;
148+ } ) ;
149+ } ) ;
150+
151+ context ( 'when the insert is unordered' , function ( ) {
152+ it ( 'contains the correct insertedIds on multiple duplicate inserts' , async function ( ) {
153+ await assertFailsWithDuplicateFields (
154+ [
155+ { _id : 0 , a : 1 } ,
156+ { _id : 1 , a : 1 } ,
157+ { _id : 2 , a : 1 } ,
158+ { _id : 3 , b : 2 }
159+ ] ,
160+ false ,
161+ { 0 : 0 , 3 : 3 }
162+ ) ;
163+ } ) ;
164+ } ) ;
165+ } ) ;
166+ } ) ;
167+
168+ describe ( '#bulkWrite()' , function ( ) {
169+ context ( 'when inserting duplicate values' , function ( ) {
170+ let col ;
171+
172+ beforeEach ( async function ( ) {
173+ const db = client . db ( ) ;
174+ col = db . collection ( 'test' ) ;
175+ await col . createIndex ( [ { a : 1 } ] , { unique : true , sparse : false } ) ;
176+ } ) ;
177+
178+ async function assertFailsWithDuplicateFields ( input , isOrdered , expectedInsertedIds ) {
179+ const error = await col . bulkWrite ( input , { ordered : isOrdered } ) . catch ( error => error ) ;
180+ expect ( error ) . to . be . instanceOf ( MongoBulkWriteError ) ;
181+ expect ( error . result . insertedCount ) . to . equal ( Object . keys ( error . result . insertedIds ) . length ) ;
182+ expect ( error . result . insertedIds ) . to . deep . equal ( expectedInsertedIds ) ;
183+ }
184+
185+ context ( 'when the insert is ordered' , function ( ) {
186+ it ( 'contains the correct insertedIds on one duplicate insert' , async function ( ) {
187+ await assertFailsWithDuplicateFields (
188+ [ { insertOne : { _id : 0 , a : 1 } } , { insertOne : { _id : 1 , a : 1 } } ] ,
189+ true ,
190+ { 0 : 0 }
191+ ) ;
192+ } ) ;
193+
194+ it ( 'contains the correct insertedIds on multiple duplicate inserts' , async function ( ) {
195+ await assertFailsWithDuplicateFields (
196+ [
197+ { insertOne : { _id : 0 , a : 1 } } ,
198+ { insertOne : { _id : 1 , a : 1 } } ,
199+ { insertOne : { _id : 2 , a : 1 } } ,
200+ { insertOne : { _id : 3 , b : 2 } }
201+ ] ,
202+ true ,
203+ { 0 : 0 }
204+ ) ;
205+ } ) ;
206+ } ) ;
207+
208+ context ( 'when the insert is unordered' , function ( ) {
209+ it ( 'contains the correct insertedIds on multiple duplicate inserts' , async function ( ) {
210+ await assertFailsWithDuplicateFields (
211+ [
212+ { insertOne : { _id : 0 , a : 1 } } ,
213+ { insertOne : { _id : 1 , a : 1 } } ,
214+ { insertOne : { _id : 2 , a : 1 } } ,
215+ { insertOne : { _id : 3 , b : 2 } }
216+ ] ,
217+ false ,
218+ { 0 : 0 , 3 : 3 }
219+ ) ;
220+ } ) ;
221+ } ) ;
222+ } ) ;
107223 } ) ;
108224 } ) ;
109225
0 commit comments