7
7
ignoreNsNotFound,
8
8
assert : test
9
9
} = require ( '../shared' ) ;
10
- const { Long, MongoBatchReExecutionError, MongoDriverError } = require ( '../../../src' ) ;
10
+ const {
11
+ Long,
12
+ MongoBatchReExecutionError,
13
+ MongoDriverError,
14
+ MongoInvalidArgumentError
15
+ } = require ( '../../../src' ) ;
11
16
const crypto = require ( 'crypto' ) ;
12
17
const chai = require ( 'chai' ) ;
13
18
@@ -22,6 +27,84 @@ describe('Bulk', function () {
22
27
before ( function ( ) {
23
28
return setupDatabase ( this . configuration ) ;
24
29
} ) ;
30
+ describe ( 'BulkOperationBase' , ( ) => {
31
+ describe ( '#raw()' , function ( ) {
32
+ let client ;
33
+ beforeEach ( async function ( ) {
34
+ client = this . configuration . newClient ( ) ;
35
+ await client . connect ( ) ;
36
+ } ) ;
37
+ afterEach ( async function ( ) {
38
+ await client . close ( ) ;
39
+ } ) ;
40
+ context ( 'when called with an undefined operation' , function ( ) {
41
+ it ( 'should throw a MongoInvalidArgument error ' , async function ( ) {
42
+ const bulkOp = client . db ( 'test' ) . collection ( 'test' ) . initializeUnorderedBulkOp ( ) ;
43
+ expect ( ( ) => bulkOp . raw ( undefined ) ) . to . throw ( MongoInvalidArgumentError ) ;
44
+ expect ( ( ) => bulkOp . raw ( true ) ) . to . throw ( MongoInvalidArgumentError ) ;
45
+ expect ( ( ) => bulkOp . raw ( 3 ) ) . to . throw ( MongoInvalidArgumentError ) ;
46
+ } ) ;
47
+
48
+ it ( 'should throw an error with the specifc message: "Operation must be an object with an operation key"' , async function ( ) {
49
+ const bulkOp = client . db ( 'test' ) . collection ( 'test' ) . initializeUnorderedBulkOp ( ) ;
50
+ expect ( ( ) => bulkOp . raw ( undefined ) )
51
+ . to . throw ( MongoInvalidArgumentError )
52
+ . to . match ( / O p e r a t i o n m u s t b e a n o b j e c t w i t h a n o p e r a t i o n k e y / ) ;
53
+ } ) ;
54
+ } ) ;
55
+
56
+ context ( 'when called with a valid operation' , function ( ) {
57
+ it ( 'should not throw a MongoInvalidArgument error' , async function ( ) {
58
+ try {
59
+ client . db ( 'test' ) . collection ( 'test' ) . initializeUnorderedBulkOp ( ) . raw ( { insertOne : { } } ) ;
60
+ } catch ( error ) {
61
+ expect ( error ) . not . to . exist ;
62
+ }
63
+ } ) ;
64
+ } ) ;
65
+ } ) ;
66
+ } ) ;
67
+
68
+ describe ( 'Collection' , function ( ) {
69
+ describe ( '#insertMany()' , function ( ) {
70
+ let client ;
71
+ beforeEach ( async function ( ) {
72
+ client = this . configuration . newClient ( ) ;
73
+ await client . connect ( ) ;
74
+ } ) ;
75
+ afterEach ( async function ( ) {
76
+ await client . close ( ) ;
77
+ } ) ;
78
+ context ( 'when passed an invalid docs argument' , function ( ) {
79
+ it ( 'should throw a MongoInvalidArgument error' , async function ( ) {
80
+ try {
81
+ const docs = [ ] ;
82
+ docs [ 1 ] = { color : 'red' } ;
83
+ await client . db ( 'test' ) . collection ( 'test' ) . insertMany ( docs ) ;
84
+ expect . fail ( 'Expected insertMany to throw error, failed to throw error' ) ;
85
+ } catch ( error ) {
86
+ expect ( error ) . to . be . instanceOf ( MongoInvalidArgumentError ) ;
87
+ expect ( error . message ) . to . equal (
88
+ 'Collection.insertMany() cannot be called with an array that has null/undefined values'
89
+ ) ;
90
+ }
91
+ } ) ;
92
+ } ) ;
93
+ context ( 'when passed a valid document list' , function ( ) {
94
+ it ( 'insertMany should not throw a MongoInvalidArgument error when called with a valid operation' , async function ( ) {
95
+ try {
96
+ let result = await client
97
+ . db ( 'test' )
98
+ . collection ( 'test' )
99
+ . insertMany ( [ { color : 'blue' } ] ) ;
100
+ expect ( result ) . to . exist ;
101
+ } catch ( error ) {
102
+ expect ( error ) . not . to . exist ;
103
+ }
104
+ } ) ;
105
+ } ) ;
106
+ } ) ;
107
+ } ) ;
25
108
26
109
context ( 'promise tests' , ( ) => {
27
110
it ( 'Should correctly execute unordered bulk operation in promise form' , function ( done ) {
0 commit comments