spec/support/crud/read.rb in mongo-2.4.0.rc0 vs spec/support/crud/read.rb in mongo-2.4.0.rc1

- old
+ new

@@ -30,10 +30,20 @@ :batch_size => 'batchSize', :limit => 'limit', :collation => 'collation' } + # Map of read preference mode names to their equivalent Ruby-formatted symbols. + # + # @since 2.4.0 + READ_PREFERENCE_MAP = { 'primary' => :primary, + 'secondary' => :secondary, + 'primaryPreferred' => :primary_preferred, + 'secondaryPreferred' => :secondary_preferred, + 'nearest' => :nearest + } + # The operation name. # # @return [ String ] name The operation name. # # @since 2.0.0 @@ -74,45 +84,10 @@ def has_results? !(name == 'aggregate' && pipeline.find {|op| op.keys.include?('$out') }) end - # Whether the operation requires server version >= 2.6 for its results to - # match expected results. - # - # @example Whether the operation requires >= 2.6 for its results to match. - # operation.requires_2_6?(collection) - # - # @param [ Collection ] collection The collection the operation is executed on. - # - # @return [ true, false ] If the operation requires 2.6 for its results to match. - # - # @since 2.0.0 - def requires_2_6?(collection) - name == 'aggregate' && pipeline.find {|op| op.keys.include?('$out') } - end - - # Whether this operation requires a certain server version to be run. - # - # @example Determine whether this operation requires a certain server feature. - # operation.feature_enabled?(collection) - # - # @param [ Collection ] collection The collection the operation - # should be executed on. - # - # @return [ true, false ] Whether this operation requires a certain server version. - # - # @since 2.4.0 - def feature_enabled?(collection) - if collation - return $mongo_client.cluster.servers.first.features.collation_enabled? - elsif requires_2_6?(collection) - return $mongo_client.cluster.servers.first.features.write_command_enabled? - end - true - end - private def count(collection) options = ARGUMENT_MAP.reduce({}) do |opts, (key, value)| opts.merge!(key => arguments[value]) if arguments[value] @@ -128,11 +103,12 @@ def distinct(collection) collection.distinct(field_name, filter, options) end def find(collection) - collection.find(filter, options.merge(modifiers: BSON::Document.new(modifiers) || {})).to_a + opts = modifiers ? options.merge(modifiers: BSON::Document.new(modifiers)) : options + (read_preference ? collection.with(read: read_preference) : collection).find(filter, opts).to_a end def options ARGUMENT_MAP.reduce({}) do |opts, (key, value)| arguments[value] ? opts.merge!(key => arguments[value]) : opts @@ -163,9 +139,15 @@ arguments['fieldName'] end def arguments @spec['arguments'] + end + + def read_preference + if @spec['read_preference'] && @spec['read_preference']['mode'] + { mode: READ_PREFERENCE_MAP[@spec['read_preference']['mode']] } + end end end end end end