|
6 | 6 | const sinon = require('sinon'); |
7 | 7 | const start = require('./common'); |
8 | 8 |
|
| 9 | +const CastError = require('../lib/error/cast'); |
9 | 10 | const assert = require('assert'); |
10 | 11 | const { once } = require('events'); |
11 | 12 | const random = require('./util').random; |
@@ -4707,6 +4708,46 @@ describe('Model', function() { |
4707 | 4708 | assert.equal(err.validationErrors[0].path, 'age'); |
4708 | 4709 | assert.equal(err.results[0].path, 'age'); |
4709 | 4710 | }); |
| 4711 | + |
| 4712 | + it('bulkWrite should return both write errors and validation errors in error.results (gh-15265)', async function() { |
| 4713 | + const userSchema = new Schema({ _id: Number, age: { type: Number } }); |
| 4714 | + const User = db.model('User', userSchema); |
| 4715 | + |
| 4716 | + const createdUser = await User.create({ _id: 1, name: 'Test' }); |
| 4717 | + |
| 4718 | + const err = await User.bulkWrite([ |
| 4719 | + { |
| 4720 | + updateOne: { |
| 4721 | + filter: { _id: createdUser._id }, |
| 4722 | + update: { $set: { age: 'NaN' } } |
| 4723 | + } |
| 4724 | + }, |
| 4725 | + { |
| 4726 | + insertOne: { |
| 4727 | + document: { _id: 3, age: 14 } |
| 4728 | + } |
| 4729 | + }, |
| 4730 | + { |
| 4731 | + insertOne: { |
| 4732 | + document: { _id: 1, age: 13 } |
| 4733 | + } |
| 4734 | + }, |
| 4735 | + { |
| 4736 | + insertOne: { |
| 4737 | + document: { _id: 1, age: 14 } |
| 4738 | + } |
| 4739 | + } |
| 4740 | + ], { ordered: false, throwOnValidationError: true }) |
| 4741 | + .then(() => null) |
| 4742 | + .catch(err => err); |
| 4743 | + |
| 4744 | + assert.ok(err); |
| 4745 | + assert.strictEqual(err.mongoose.results.length, 4); |
| 4746 | + assert.ok(err.mongoose.results[0] instanceof CastError); |
| 4747 | + assert.strictEqual(err.mongoose.results[1], null); |
| 4748 | + assert.equal(err.mongoose.results[2].constructor.name, 'WriteError'); |
| 4749 | + assert.equal(err.mongoose.results[3].constructor.name, 'WriteError'); |
| 4750 | + }); |
4710 | 4751 | }); |
4711 | 4752 |
|
4712 | 4753 | it('deleteOne with cast error (gh-5323)', async function() { |
@@ -7060,6 +7101,41 @@ describe('Model', function() { |
7060 | 7101 | assert.deepStrictEqual(docs.map(doc => doc.age), [12, 12]); |
7061 | 7102 | }); |
7062 | 7103 |
|
| 7104 | + it('insertMany should return both write errors and validation errors in error.results (gh-15265)', async function() { |
| 7105 | + const userSchema = new Schema({ _id: Number, age: { type: Number } }); |
| 7106 | + const User = db.model('User', userSchema); |
| 7107 | + await User.insertOne({ _id: 1, age: 12 }); |
| 7108 | + |
| 7109 | + const err = await User.insertMany([ |
| 7110 | + { _id: 1, age: 'NaN' }, |
| 7111 | + { _id: 3, age: 14 }, |
| 7112 | + { _id: 1, age: 13 }, |
| 7113 | + { _id: 1, age: 14 } |
| 7114 | + ], { ordered: false }).then(() => null).catch(err => err); |
| 7115 | + |
| 7116 | + assert.ok(err); |
| 7117 | + assert.strictEqual(err.results.length, 4); |
| 7118 | + assert.ok(err.results[0] instanceof ValidationError); |
| 7119 | + assert.ok(err.results[1] instanceof User); |
| 7120 | + assert.ok(err.results[2].err); |
| 7121 | + assert.ok(err.results[3].err); |
| 7122 | + }); |
| 7123 | + |
| 7124 | + it('insertMany should return both write errors and validation errors in error.results with rawResult (gh-15265)', async function() { |
| 7125 | + const userSchema = new Schema({ _id: Number, age: { type: Number } }); |
| 7126 | + const User = db.model('User', userSchema); |
| 7127 | + |
| 7128 | + const res = await User.insertMany([ |
| 7129 | + { _id: 1, age: 'NaN' }, |
| 7130 | + { _id: 3, age: 14 } |
| 7131 | + ], { ordered: false, rawResult: true }); |
| 7132 | + |
| 7133 | + assert.ok(res); |
| 7134 | + assert.strictEqual(res.mongoose.results.length, 2); |
| 7135 | + assert.ok(res.mongoose.results[0] instanceof ValidationError); |
| 7136 | + assert.ok(res.mongoose.results[1] instanceof User); |
| 7137 | + }); |
| 7138 | + |
7063 | 7139 | it('returns writeResult on success', async() => { |
7064 | 7140 |
|
7065 | 7141 | const userSchema = new Schema({ |
|
0 commit comments