spec/riak/map_reduce_spec.rb in riak-client-2.2.0.pre1 vs spec/riak/map_reduce_spec.rb in riak-client-2.2.0
- old
+ new
@@ -1,9 +1,9 @@
require 'spec_helper'
describe Riak::MapReduce do
-
+
let(:backend){ double 'Backend' }
let(:client) do
Riak::Client.new.tap do |c|
allow(c).to receive(:backend).and_yield(backend)
end
@@ -40,24 +40,24 @@
it "returns self for chaining" do
expect(mr.add("foo", "bar")).to eq(mr)
end
it "adds bucket/key pairs to the inputs" do
- mr.add("foo","bar")
- expect(mr.inputs).to eq([["foo","bar"]])
+ mr.add("foo", "bar")
+ expect(mr.inputs).to eq([%w(foo bar)])
end
it "adds an array containing a bucket/key pair to the inputs" do
- mr.add(["foo","bar"])
- expect(mr.inputs).to eq([["foo","bar"]])
+ mr.add(%w(foo bar))
+ expect(mr.inputs).to eq([%w(foo bar)])
end
it "adds an object to the inputs by its bucket and key" do
bucket = Riak::Bucket.new(client, "foo")
obj = Riak::RObject.new(bucket, "bar")
mr.add(obj)
- expect(mr.inputs).to eq([["foo", "bar"]])
+ expect(mr.inputs).to eq([%w(foo bar)])
end
it 'adds a bucket-typed object to the inputs' do
mr.add typed_object
expect(mr.inputs).to eq [[typed_bucket.name,
@@ -66,12 +66,12 @@
typed_bucket.type.name
]]
end
it "adds an array containing a bucket/key/key-data triple to the inputs" do
- mr.add(["foo","bar",1000])
- expect(mr.inputs).to eq([["foo","bar",1000]])
+ mr.add(["foo", "bar", 1000])
+ expect(mr.inputs).to eq([["foo", "bar", 1000]])
end
it "uses a bucket name as the single input" do
mr.add(Riak::Bucket.new(client, "foo"))
expect(mr.inputs).to eq("foo")
@@ -88,12 +88,18 @@
mr.add default_bucket
expect(mr.inputs).to eq default_bucket.name
end
it "accepts a list of key-filters along with a bucket" do
- mr.add("foo", [[:tokenize, "-", 3], [:string_to_int], [:between, 2009, 2010]])
- expect(mr.inputs).to eq({:bucket => "foo", :key_filters => [[:tokenize, "-", 3], [:string_to_int], [:between, 2009, 2010]]})
+ mr.add("foo",
+ [[:tokenize, "-", 3],
+ [:string_to_int],
+ [:between, 2009, 2010]])
+ expect(mr.inputs).to eq({bucket: "foo",
+ key_filters: [[:tokenize, "-", 3],
+ [:string_to_int],
+ [:between, 2009, 2010]]})
end
it 'accepts a list of key-filters along with a bucket-typed bucket' do
filters = [
[:tokenize, '-', 3],
@@ -102,11 +108,11 @@
]
mr.add(typed_bucket, filters)
expect(mr.inputs).to eq(
- bucket: [typed_bucket.type.name,
+ bucket: [typed_bucket.type.name,
typed_bucket.name],
key_filters: filters
)
end
@@ -114,27 +120,39 @@
mr.filter("foo") do
tokenize "-", 3
string_to_int
between 2009, 2010
end
- expect(mr.inputs).to eq({:bucket => "foo", :key_filters => [[:tokenize, "-", 3], [:string_to_int], [:between, 2009, 2010]]})
+ expect(mr.inputs).to eq(bucket: "foo",
+ key_filters: [[:tokenize, "-", 3],
+ [:string_to_int],
+ [:between, 2009, 2010]])
end
context "using secondary indexes as inputs" do
it "sets the inputs for equality" do
expect(mr.index("foo", "email_bin", "sean@basho.com")).to eq(mr)
- expect(mr.inputs).to eq({:bucket => "foo", :index => "email_bin", :key => "sean@basho.com"})
+ expect(mr.inputs).to eq(bucket: "foo",
+ index: "email_bin",
+ key: "sean@basho.com")
end
it "sets the inputs for a range" do
expect(mr.index("foo", "rank_int", 10..20)).to eq(mr)
- expect(mr.inputs).to eq({:bucket => "foo", :index => "rank_int", :start => 10, :end => 20})
+ expect(mr.inputs).to eq(bucket: "foo",
+ index: "rank_int",
+ start: 10,
+ end: 20)
end
it "raises an error when given an invalid query" do
- expect { mr.index("foo", "rank_int", 1.0348) }.to raise_error(ArgumentError)
- expect { mr.index("foo", "rank_int", Range.new(1.03, 1.05)) }.to raise_error(ArgumentError)
+ expect do
+ mr.index("foo", "rank_int", 1.0348)
+ end.to raise_error(ArgumentError)
+ expect do
+ mr.index("foo", "rank_int", Range.new(1.03, 1.05))
+ end.to raise_error(ArgumentError)
end
end
describe "escaping" do
before { @oldesc, Riak.escaper = Riak.escaper, CGI }
@@ -143,29 +161,29 @@
context "when url_decoding is false" do
before { @urldecode, Riak.url_decoding = Riak.url_decoding, false }
after { Riak.url_decoding = @urldecode }
it "adds bucket/key pairs to the inputs with bucket and key escaped" do
- mr.add("[foo]","(bar)")
- expect(mr.inputs).to eq([["%5Bfoo%5D","%28bar%29"]])
+ mr.add("[foo]", "(bar)")
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29"]])
end
it "adds an escaped array containing a bucket/key pair to the inputs" do
- mr.add(["[foo]","(bar)"])
- expect(mr.inputs).to eq([["%5Bfoo%5D","%28bar%29"]])
+ mr.add(["[foo]", "(bar)"])
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29"]])
end
it "adds an object to the inputs by its escaped bucket and key" do
bucket = Riak::Bucket.new(client, "[foo]")
obj = Riak::RObject.new(bucket, "(bar)")
mr.add(obj)
expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29"]])
end
it "adds an escaped array containing a bucket/key/key-data triple to the inputs" do
- mr.add(["[foo]","(bar)","[]()"])
- expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29","[]()"]])
+ mr.add(["[foo]", "(bar)", "[]()"])
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29", "[]()"]])
end
it "uses an escaped bucket name as the single input" do
mr.add(Riak::Bucket.new(client, "[foo]"))
expect(mr.inputs).to eq("%5Bfoo%5D")
@@ -177,29 +195,29 @@
context "when url_decoding is true" do
before { @urldecode, Riak.url_decoding = Riak.url_decoding, true }
after { Riak.url_decoding = @urldecode }
it "adds bucket/key pairs to the inputs with bucket and key unescaped" do
- mr.add("[foo]","(bar)")
- expect(mr.inputs).to eq([["[foo]","(bar)"]])
+ mr.add("[foo]", "(bar)")
+ expect(mr.inputs).to eq([["[foo]", "(bar)"]])
end
it "adds an unescaped array containing a bucket/key pair to the inputs" do
- mr.add(["[foo]","(bar)"])
- expect(mr.inputs).to eq([["[foo]","(bar)"]])
+ mr.add(["[foo]", "(bar)"])
+ expect(mr.inputs).to eq([["[foo]", "(bar)"]])
end
it "adds an object to the inputs by its unescaped bucket and key" do
bucket = Riak::Bucket.new(client, "[foo]")
obj = Riak::RObject.new(bucket, "(bar)")
mr.add(obj)
- expect(mr.inputs).to eq([["[foo]","(bar)"]])
+ expect(mr.inputs).to eq([["[foo]", "(bar)"]])
end
it "adds an unescaped array containing a bucket/key/key-data triple to the inputs" do
- mr.add(["[foo]","(bar)","[]()"])
- expect(mr.inputs).to eq([["[foo]","(bar)","[]()"]])
+ mr.add(["[foo]", "(bar)", "[]()"])
+ expect(mr.inputs).to eq([["[foo]", "(bar)", "[]()"]])
end
it "uses an unescaped bucket name as the single input" do
mr.add(Riak::Bucket.new(client, "[foo]"))
expect(mr.inputs).to eq("[foo]")
@@ -212,29 +230,29 @@
context "escaping" do
before { @oldesc, Riak.escaper = Riak.escaper, CGI }
after { Riak.escaper = @oldesc }
it "adds bucket/key pairs to the inputs with bucket and key escaped" do
- mr.add("[foo]","(bar)")
- expect(mr.inputs).to eq([["%5Bfoo%5D","%28bar%29"]])
+ mr.add("[foo]", "(bar)")
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29"]])
end
it "adds an escaped array containing a bucket/key pair to the inputs" do
- mr.add(["[foo]","(bar)"])
- expect(mr.inputs).to eq([["%5Bfoo%5D","%28bar%29"]])
+ mr.add(["[foo]", "(bar)"])
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29"]])
end
it "adds an object to the inputs by its escaped bucket and key" do
bucket = Riak::Bucket.new(client, "[foo]")
obj = Riak::RObject.new(bucket, "(bar)")
mr.add(obj)
expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29"]])
end
it "adds an escaped array containing a bucket/key/key-data triple to the inputs" do
- mr.add(["[foo]","(bar)","[]()"])
- expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29","[]()"]])
+ mr.add(["[foo]", "(bar)", "[]()"])
+ expect(mr.inputs).to eq([["%5Bfoo%5D", "%28bar%29", "[]()"]])
end
it "uses an escaped bucket name as the single input" do
mr.add(Riak::Bucket.new(client, "[foo]"))
expect(mr.inputs).to eq("%5Bfoo%5D")
@@ -282,23 +300,23 @@
expect(phase.type).to eq(type)
expect(phase.keep).to be_truthy
end
it "accepts a module/function pair" do
- mr.send(type, ["riak","mapsomething"])
+ mr.send(type, %w(riak mapsomething))
expect(mr.query.size).to eq(1)
phase = mr.query.first
- expect(phase.function).to eq(["riak", "mapsomething"])
+ expect(phase.function).to eq(%w(riak mapsomething))
expect(phase.type).to eq(type)
expect(phase.language).to eq("erlang")
end
it "accepts a module/function pair with extra options" do
- mr.send(type, ["riak", "mapsomething"], :arg => [1000])
+ mr.send(type, %w(riak mapsomething), :arg => [1000])
expect(mr.query.size).to eq(1)
phase = mr.query.first
- expect(phase.function).to eq(["riak", "mapsomething"])
+ expect(phase.function).to eq(%w(riak mapsomething))
expect(phase.type).to eq(type)
expect(phase.language).to eq("erlang")
expect(phase.arg).to eq([1000])
end
end
@@ -354,11 +372,11 @@
mr.add("foo")
expect(mr.to_json).to include('"inputs":"foo"')
end
it "emits an array of inputs when there are multiple inputs" do
- mr.add("foo","bar",1000).add("foo","baz")
+ mr.add("foo", "bar", 1000).add("foo", "baz")
expect(mr.to_json).to include('"inputs":[["foo","bar",1000],["foo","baz"]]')
end
it "adds the timeout value when set" do
mr.timeout(50000)
@@ -370,11 +388,11 @@
expect(mr.timeout(5000)).to eq(mr)
end
describe "executing the map reduce job" do
before :each do
- mr.map("Riak.mapValues",:keep => true)
+ mr.map("Riak.mapValues", :keep => true)
end
it "submits the query to the backend" do
expect(backend).to receive(:mapred).with(mr).and_return([])
expect(mr.run).to eq([])
@@ -382,10 +400,10 @@
it "passes the given block to the backend for streaming" do
arr = []
expect(backend).to receive(:mapred).with(mr).and_yield("foo").and_yield("bar")
mr.run {|v| arr << v }
- expect(arr).to eq(["foo", "bar"])
+ expect(arr).to eq(%w(foo bar))
end
it "interprets failed requests with JSON content-types as map reduce errors" do
allow(backend).to receive(:mapred).
and_raise(Riak::ProtobuffsFailedRequest.new(:server_error, '{"error":"syntax error"}'))