diff --git a/lib/moped/session.rb b/lib/moped/session.rb index 54203fa..e7b52ab 100644 --- a/lib/moped/session.rb +++ b/lib/moped/session.rb @@ -172,12 +172,26 @@ def logout # Setup validation of allowed write concern options. # # @since 2.0.0 - option(:write).allow({ w: Optionable.any(Integer) }, { "w" => Optionable.any(Integer) }) - option(:write).allow({ w: Optionable.any(String) }, { "w" => Optionable.any(String) }) - option(:write).allow({ j: true }, { "j" => true }) - option(:write).allow({ j: false }, { "j" => false }) - option(:write).allow({ fsync: true }, { "fsync" => true }) - option(:write).allow({ fsync: false }, { "fsync" => false }) + option(:write).allow(Optionable.any(Hash)) + + class WriteOption + include Optionable + + option(:w).allow(Optionable.any(Integer)) + option('w').allow(Optionable.any(Integer)) + option(:w).allow(Optionable.any(String)) + option('w').allow(Optionable.any(String)) + option(:j).allow(true, false) + option('j').allow(true, false) + option(:fsync).allow(true, false) + option('fsync').allow(true, false) + option(:wtimeout).allow(Optionable.any(Integer)) + option('wtimeout').allow(Optionable.any(Integer)) + + def initialize(opts) + validate_strict(opts) + end + end # Setup validation of allowed read preference options. # @@ -263,6 +277,7 @@ def logout # @since 1.0.0 def initialize(seeds, options = {}) validate_strict(options) + WriteOption.new(options[:write]) @options = options @cluster = Cluster.new(seeds, options) end diff --git a/lib/moped/write_concern/propagate.rb b/lib/moped/write_concern/propagate.rb index 6d181d1..e76ea2a 100644 --- a/lib/moped/write_concern/propagate.rb +++ b/lib/moped/write_concern/propagate.rb @@ -29,7 +29,7 @@ def initialize(options) def normalize(options) opts = {} options.each do |key, value| - opts[key] = value.is_a?(Symbol) ? value.to_s : value + opts[key.to_sym] = value.is_a?(Symbol) ? value.to_s : value end opts end diff --git a/spec/moped/session_spec.rb b/spec/moped/session_spec.rb index 6d4005f..8094f35 100644 --- a/spec/moped/session_spec.rb +++ b/spec/moped/session_spec.rb @@ -315,6 +315,29 @@ expect(unverified.write_concern).to be_a(Moped::WriteConcern::Unverified) end end + + context "when the value is an integer" do + + let(:verified) do + described_class.new([ "127.0.0.1:27017" ], write: { w: 1 }) + end + + it "returns the corresponding write concern" do + expect(verified.write_concern).to be_a(Moped::WriteConcern::Propagate) + end + end + + context "when the value is an string" do + + let(:verified) do + described_class.new([ "127.0.0.1:27017" ], write: { w: "majority" }) + end + + it "returns the corresponding write concern" do + expect(verified.write_concern).to be_a(Moped::WriteConcern::Propagate) + expect(verified.write_concern.operation[:w]).to eq('majority') + end + end end context "when no write option is provided" do @@ -327,6 +350,87 @@ expect(propagate.write_concern).to be_a(Moped::WriteConcern::Propagate) end end + + context "when a timeout option is provided" do + + context "when the option is a symbol" do + + let(:verified) do + described_class.new([ "127.0.0.1:27017" ], write: { wtimeout: 10000 }) + end + + it "returns the corresponding write concern" do + expect(verified.write_concern).to be_a(Moped::WriteConcern::Propagate) + expect(verified.write_concern.operation[:wtimeout]).to eq(10000) + end + end + + context "when the option is a string" do + + let(:verified) do + described_class.new([ "127.0.0.1:27017" ], write: { 'wtimeout' => 10000 }) + end + + it "returns the corresponding write concern" do + expect(verified.write_concern).to be_a(Moped::WriteConcern::Propagate) + expect(verified.write_concern.operation[:wtimeout]).to eq(10000) + end + end + end + + context "when a journal option is provided" do + + context "when the option is a symbol" do + + let(:verified) do + described_class.new([ "127.0.0.1:27017" ], write: { j: true }) + end + + it "returns the corresponding write concern" do + expect(verified.write_concern).to be_a(Moped::WriteConcern::Propagate) + expect(verified.write_concern.operation[:j]).to eq(true) + end + end + + context "when the option is a string" do + + let(:verified) do + described_class.new([ "127.0.0.1:27017" ], write: { 'j' => true }) + end + + it "returns the corresponding write concern" do + expect(verified.write_concern).to be_a(Moped::WriteConcern::Propagate) + expect(verified.write_concern.operation[:j]).to eq(true) + end + end + end + + context "when an fsync option is provided" do + + context "when the option is a symbol" do + + let(:verified) do + described_class.new([ "127.0.0.1:27017" ], write: { fsync: true }) + end + + it "returns the corresponding write concern" do + expect(verified.write_concern).to be_a(Moped::WriteConcern::Propagate) + expect(verified.write_concern.operation[:fsync]).to eq(true) + end + end + + context "when the option is a string" do + + let(:verified) do + described_class.new([ "127.0.0.1:27017" ], write: { 'fsync' => true }) + end + + it "returns the corresponding write concern" do + expect(verified.write_concern).to be_a(Moped::WriteConcern::Propagate) + expect(verified.write_concern.operation[:fsync]).to eq(true) + end + end + end end context "when attempting to connect to a node that does not exist" do