forked from mongodb/mongo-ruby-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk_write_error_message_spec.rb
More file actions
88 lines (79 loc) · 3.2 KB
/
bulk_write_error_message_spec.rb
File metadata and controls
88 lines (79 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'spec_helper'
describe 'BulkWriteError message' do
let(:client) { authorized_client }
let(:collection_name) { 'bulk_write_error_message_spec' }
let(:collection) { client[collection_name] }
before do
collection.delete_many
end
context 'a bulk write with one error' do
it 'reports code name, code and message' do
collection.insert_many([
{ _id: 1 },
{ _id: 1 },
{ _id: 1 },
], ordered: true)
raise('Should have raised')
rescue Mongo::Error::BulkWriteError => e
e.message.should =~ /\A\[11000\]: (insertDocument :: caused by :: 11000 )?E11000 duplicate key error (collection|index):/
end
end
context 'a bulk write with multiple errors' do
it 'reports code name, code and message' do
collection.insert_many([
{ _id: 1 },
{ _id: 1 },
{ _id: 1 },
], ordered: false)
raise('Should have raised')
rescue Mongo::Error::BulkWriteError => e
e.message.should =~ /\AMultiple errors: \[11000\]: (insertDocument :: caused by :: 11000 )?E11000 duplicate key error (collection|index):.*\[11000\]: (insertDocument :: caused by :: 11000 )?E11000 duplicate key error (collection|index):/
end
end
context 'a bulk write with validation errors' do
let(:collection_name) { 'bulk_write_error_validation_message_spec' }
let(:collection) do
client[:collection_name].drop
client[:collection_name,
{
'validator' => {
'x' => { '$type' => 'string' },
}
}].create
client[:collection_name]
end
it 'reports code name, code, message, and details' do
collection.insert_one({ _id: 1, x: '1' })
collection.insert_many([
{ _id: 1, x: '1' },
{ _id: 2, x: 1 },
], ordered: false)
raise('Should have raised')
rescue Mongo::Error::BulkWriteError => e
e.message.should =~ /\AMultiple errors: \[11000\]: (insertDocument :: caused by :: 11000 )?E11000 duplicate key error (collection|index):.*; \[121\]: Document failed validation( -- .*)?/
# The duplicate key error should not print details because it's not a
# WriteError or a WriteConcernError
e.message.scan(' -- ').length.should be <= 1
end
end
context 'when include_server_address_in_errors is enabled' do
around do |example|
original = Mongo.include_server_address_in_errors
Mongo.include_server_address_in_errors = true
example.run
ensure
Mongo.include_server_address_in_errors = original
end
it 'includes the server address suffix in the bulk error message' do
collection.insert_one(_id: 1)
begin
collection.insert_many([ { _id: 1 }, { _id: 2 }, { _id: 2 } ])
rescue Mongo::Error::BulkWriteError => e
expect(e.message).to match(/\(on [^)]+:\d+(?:, [^)]+:\d+)*\)\z/)
expect(e.server_addresses).not_to be_empty
else
raise 'expected BulkWriteError'
end
end
end
end