Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Merge 06113d8 into 7ef2b2c
Browse files Browse the repository at this point in the history
  • Loading branch information
leemhenson committed Feb 27, 2015
2 parents 7ef2b2c + 06113d8 commit 08ba0fd
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 7 deletions.
27 changes: 21 additions & 6 deletions lib/moped/session.rb
Expand Up @@ -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.
#
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/moped/write_concern/propagate.rb
Expand Up @@ -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
Expand Down
104 changes: 104 additions & 0 deletions spec/moped/session_spec.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 08ba0fd

Please sign in to comment.