File tree Expand file tree Collapse file tree 5 files changed +42
-14
lines changed Expand file tree Collapse file tree 5 files changed +42
-14
lines changed Original file line number Diff line number Diff line change @@ -85,6 +85,11 @@ def execute
85
85
86
86
private
87
87
88
+ def valid_doc? ( doc )
89
+ doc . respond_to? ( :keys ) ||
90
+ doc . respond_to? ( :document )
91
+ end
92
+
88
93
def write_concern
89
94
@write_concern ||= WriteConcern . get ( @options [ :write_concern ] ) ||
90
95
@collection . write_concern
Original file line number Diff line number Diff line change @@ -22,10 +22,6 @@ module Deletable
22
22
23
23
private
24
24
25
- def valid_doc? ( doc )
26
- doc . respond_to? ( :keys )
27
- end
28
-
29
25
def validate_delete_op! ( type , d )
30
26
raise Error ::InvalidBulkOperation . new ( type , d ) unless valid_doc? ( d )
31
27
end
Original file line number Diff line number Diff line change @@ -22,10 +22,6 @@ module Insertable
22
22
23
23
private
24
24
25
- def valid_doc? ( doc )
26
- doc . respond_to? ( :keys )
27
- end
28
-
29
25
def validate_insert_ops! ( type , inserts )
30
26
if inserts . empty?
31
27
raise Error ::InvalidBulkOperation . new ( type , inserts )
Original file line number Diff line number Diff line change @@ -75,12 +75,12 @@ def find_one(selector = nil)
75
75
# @since 2.0.0
76
76
def insert_one ( file )
77
77
files_collection . insert_one ( file . metadata )
78
- result = chunks_collection . insert_many ( file . chunks )
79
- if write_concern . get_last_error
80
- validate_md5! ( file )
81
- else
82
- result
78
+ inserts = file . chunks . reduce ( [ ] ) do |ops , chunk |
79
+ ops << { :insert_one => chunk }
83
80
end
81
+ result = chunks_collection . bulk_write ( inserts , ordered : true )
82
+ validate_md5! ( file ) if write_concern . get_last_error
83
+ file . id
84
84
end
85
85
86
86
# Create the GridFS.
Original file line number Diff line number Diff line change 65
65
66
66
context 'when inserting the file once' do
67
67
68
- before do
68
+ let! ( :result ) do
69
69
fs . insert_one ( file )
70
70
end
71
71
85
85
it 'includes the chunks and data with the file' do
86
86
expect ( from_db . data ) . to eq ( 'Hello!' )
87
87
end
88
+
89
+ it 'returns the file id' do
90
+ expect ( result ) . to eq ( file . id )
91
+ end
88
92
end
89
93
90
94
context 'when inserting the file more than once' do
101
105
} . to raise_error ( Mongo ::Error ::OperationFailure )
102
106
end
103
107
end
108
+
109
+ context 'when the file exceeds the max bson size' do
110
+
111
+ let ( :fs ) do
112
+ described_class . new ( authorized_client . database )
113
+ end
114
+
115
+ let ( :file ) do
116
+ str = 'y' * 16777216
117
+ Mongo ::Grid ::File . new ( str , :filename => 'large-file.txt' )
118
+ end
119
+
120
+ before do
121
+ fs . insert_one ( file )
122
+ end
123
+
124
+ after do
125
+ fs . files_collection . find . delete_many
126
+ fs . chunks_collection . find . delete_many
127
+ end
128
+
129
+ it 'successfully inserts the file' do
130
+ expect (
131
+ fs . find_one ( :filename => 'large-file.txt' ) . chunks
132
+ ) . to eq ( file . chunks )
133
+ end
134
+ end
104
135
end
105
136
106
137
describe '#delete_one' do
You can’t perform that action at this time.
0 commit comments