Permalink
Browse files

Merge pull request #842 from estolfo/map-reduce-fixes

Fix map reduce querying when a db is specified in out
  • Loading branch information...
2 parents cd19a31 + ab8873b commit 09bdc4122a137a8a4ed24fa49f25845affe42107 @estolfo estolfo committed on GitHub Dec 7, 2016
@@ -80,7 +80,7 @@ def initialize(map, reduce, view, options)
def command_specification
{
selector: find_command,
- db_name: database.name,
+ db_name: query_database,
read: read
}
end
@@ -94,7 +94,7 @@ def command_specification
#
# @since 2.2.0
def query_specification
- { selector: {}, options: {}, db_name: database.name, coll_name: query_collection }
+ { selector: {}, options: {}, db_name: query_database, coll_name: query_collection }
end
# Get the specification to pass to the map/reduce operation.
@@ -116,6 +116,8 @@ def specification
private
+ OUT_ACTIONS = [ :replace, :merge, :reduce ].freeze
+
def write?(spec)
if out = spec[:selector][:out]
out.is_a?(String) ||
@@ -141,8 +143,14 @@ def map_reduce_command
command
end
+ def query_database
+ options[:out].respond_to?(:keys) && options[:out][:db] ? options[:out][:db] : database.name
+ end
+
def query_collection
- options[:out].respond_to?(:keys) ? options[:out].values.first : options[:out]
+ if options[:out].respond_to?(:keys)
+ options[:out][OUT_ACTIONS.find { |action| options[:out][action] }]
+ end || options[:out]
end
end
end
@@ -133,6 +133,23 @@
it 'fetches the results from the collection' do
expect(new_map_reduce.count).to eq(2)
end
+
+ context 'when another db is specified', if: (!auth_enabled? && list_command_enabled?) do
+
+ let(:new_map_reduce) do
+ map_reduce.out(db: 'another-db', replace: 'output_collection')
+ end
+
+ it 'iterates over the documents in the result' do
+ new_map_reduce.each do |document|
+ expect(document[:value]).to_not be_nil
+ end
+ end
+
+ it 'fetches the results from the collection' do
+ expect(new_map_reduce.count).to eq(2)
+ end
+ end
end
context 'when the option is to merge' do
@@ -150,6 +167,23 @@
it 'fetches the results from the collection' do
expect(new_map_reduce.count).to eq(2)
end
+
+ context 'when another db is specified', if: (!auth_enabled? && list_command_enabled?) do
+
+ let(:new_map_reduce) do
+ map_reduce.out(db: 'another-db', merge: 'output_collection')
+ end
+
+ it 'iterates over the documents in the result' do
+ new_map_reduce.each do |document|
+ expect(document[:value]).to_not be_nil
+ end
+ end
+
+ it 'fetches the results from the collection' do
+ expect(new_map_reduce.count).to eq(2)
+ end
+ end
end
context 'when the option is to reduce' do
@@ -167,6 +201,23 @@
it 'fetches the results from the collection' do
expect(new_map_reduce.count).to eq(2)
end
+
+ context 'when another db is specified', if: (!auth_enabled? && list_command_enabled?) do
+
+ let(:new_map_reduce) do
+ map_reduce.out(db: 'another-db', reduce: 'output_collection')
+ end
+
+ it 'iterates over the documents in the result' do
+ new_map_reduce.each do |document|
+ expect(document[:value]).to_not be_nil
+ end
+ end
+
+ it 'fetches the results from the collection' do
+ expect(new_map_reduce.count).to eq(2)
+ end
+ end
end
context 'when the option is a collection name' do
View
@@ -192,7 +192,11 @@ def failing_delete_doc
# @since 2.2.0
def auth_enabled?
$mongo_client ||= initialize_scanned_client!
- begin; $mongo_client.use(:admin).command(getCmdLineOpts: 1); rescue; return true; end
+ begin
+ $mongo_client.use(:admin).command(getCmdLineOpts: 1).first["argv"].include?("--auth")
+ rescue
+ return true
+ end
false
end

0 comments on commit 09bdc41

Please sign in to comment.