11import { expect } from 'chai' ;
22
3- import { MongoServerError } from '../../mongodb ' ;
3+ import { type MongoClient , MongoServerError } from '../../../src ' ;
44import { setupDatabase } from '../shared' ;
55
66describe ( 'Errors' , function ( ) {
77 before ( function ( ) {
88 return setupDatabase ( this . configuration ) ;
99 } ) ;
1010
11- let client ;
11+ let client : MongoClient ;
1212
1313 beforeEach ( function ( ) {
1414 client = this . configuration . newClient ( this . configuration . writeConcernMax ( ) , { maxPoolSize : 1 } ) ;
@@ -19,65 +19,55 @@ describe('Errors', function () {
1919 return client . close ( ) ;
2020 } ) ;
2121
22- it ( 'should fail insert due to unique index' , function ( done ) {
22+ it ( 'should fail insert due to unique index' , async function ( ) {
2323 const db = client . db ( this . configuration . db ) ;
24- db . createCollection ( 'test_failing_insert_due_to_unique_index' , ( err , collection ) => {
25- expect ( err ) . to . not . exist ;
26- collection . createIndexes (
27- [
28- {
29- name : 'test_failing_insert_due_to_unique_index' ,
30- key : { a : 1 } ,
31- unique : true
32- }
33- ] ,
34- { writeConcern : { w : 1 } } ,
35- err => {
36- expect ( err ) . to . not . exist ;
24+ const collection = await db . createCollection ( 'test_failing_insert_due_to_unique_index' ) ;
25+ await collection . createIndexes ( [
26+ {
27+ name : 'test_failing_insert_due_to_unique_index' ,
28+ key : { a : 1 } ,
29+ unique : true
30+ }
31+ ] ) ;
3732
38- collection . insertOne ( { a : 2 } , { writeConcern : { w : 1 } } , err => {
39- expect ( err ) . to . not . exist ;
33+ await collection . insertOne ( { a : 2 } , { writeConcern : { w : 1 } } ) ;
4034
41- collection . insertOne ( { a : 2 } , { writeConcern : { w : 1 } } , err => {
42- expect ( err . code ) . to . equal ( 11000 ) ;
43- done ( ) ;
44- } ) ;
45- } ) ;
46- }
47- ) ;
48- } ) ;
35+ const err = await collection . insertOne ( { a : 2 } , { writeConcern : { w : 1 } } ) . catch ( err => err ) ;
36+ expect ( err ) . to . be . instanceOf ( MongoServerError ) ;
37+ expect ( err . code ) . to . equal ( 11000 ) ;
4938 } ) ;
5039
51- it ( 'should fail insert due to unique index strict' , function ( done ) {
52- const db = client . db ( this . configuration . db ) ;
53- db . dropCollection ( 'test_failing_insert_due_to_unique_index_strict' , ( ) => {
54- db . createCollection ( 'test_failing_insert_due_to_unique_index_strict' , err => {
55- expect ( err ) . to . not . exist ;
56- const collection = db . collection ( 'test_failing_insert_due_to_unique_index_strict' ) ;
57- collection . createIndexes (
58- [
59- {
60- name : 'test_failing_insert_due_to_unique_index_strict' ,
61- key : { a : 1 } ,
62- unique : true
63- }
64- ] ,
65- { writeConcern : { w : 1 } } ,
66- err => {
67- expect ( err ) . to . not . exist ;
68- collection . insertOne ( { a : 2 } , { writeConcern : { w : 1 } } , err => {
69- expect ( err ) . to . not . exist ;
70-
71- collection . insertOne ( { a : 2 } , { writeConcern : { w : 1 } } , err => {
72- expect ( err . code ) . to . equal ( 11000 ) ;
73- done ( ) ;
74- } ) ;
75- } ) ;
76- }
77- ) ;
78- } ) ;
79- } ) ;
80- } ) ;
40+ // TODO(NODE-7219): remove as it duplicates "should fail insert due to unique index"
41+ // it('should fail insert due to unique index strict', function (done) {
42+ // const db = client.db(this.configuration.db);
43+ // db.dropCollection('test_failing_insert_due_to_unique_index_strict', () => {
44+ // db.createCollection('test_failing_insert_due_to_unique_index_strict', err => {
45+ // expect(err).to.not.exist;
46+ // const collection = db.collection('test_failing_insert_due_to_unique_index_strict');
47+ // collection.createIndexes(
48+ // [
49+ // {
50+ // name: 'test_failing_insert_due_to_unique_index_strict',
51+ // key: { a: 1 },
52+ // unique: true
53+ // }
54+ // ],
55+ // { writeConcern: { w: 1 } },
56+ // err => {
57+ // expect(err).to.not.exist;
58+ // collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => {
59+ // expect(err).to.not.exist;
60+ //
61+ // collection.insertOne({ a: 2 }, { writeConcern: { w: 1 } }, err => {
62+ // expect(err.code).to.equal(11000);
63+ // done();
64+ // });
65+ // });
66+ // }
67+ // );
68+ // });
69+ // });
70+ // });
8171
8272 const PROJECTION_ERRORS = new Set ( [
8373 'Projection cannot have a mix of inclusion and exclusion.' ,
0 commit comments