diff options
author | Scott Hain <shain@getchef.com> | 2014-06-25 13:40:08 -0700 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2014-09-04 15:52:22 -0700 |
commit | a1cf6416ee25b9d477029e6b44333cf41e4929be (patch) | |
tree | c480cf7b9e7ededeee56bef6f12beb6fd629ae7e /spec/unit/search | |
parent | 7e93d3f41d516ec9d2f1f84d33efa1ba1a9b6164 (diff) | |
download | chef-a1cf6416ee25b9d477029e6b44333cf41e4929be.tar.gz |
Finished up search with filtering
Diffstat (limited to 'spec/unit/search')
-rw-r--r-- | spec/unit/search/query_spec.rb | 269 |
1 files changed, 249 insertions, 20 deletions
diff --git a/spec/unit/search/query_spec.rb b/spec/unit/search/query_spec.rb index 7463e3bb3c..714806ce29 100644 --- a/spec/unit/search/query_spec.rb +++ b/spec/unit/search/query_spec.rb @@ -26,14 +26,62 @@ describe Chef::Search::Query do @query = Chef::Search::Query.new end - describe "search" do + describe "legacy search" do before(:each) do @response = { "rows" => [ - { "id" => "for you" }, - { "id" => "hip hop" }, - { "id" => "thought was down by law for you" }, - { "id" => "kept it hard core for you" }, + { "name" => "my-name-is-node", + "chef_environment" => "elysium", + "platform" => "rhel", + "automatic" => { + "languages" => { + "ruby" => { + "platform" => "nudibranch", + "version" => "1.9.3", + "target" => "ming-the-merciless" + } + } + } + }, + { "name" => "my-name-is-jonas", + "chef_environment" => "hades", + "platform" => "rhel", + "automatic" => { + "languages" => { + "ruby" => { + "platform" => "i386-mingw32", + "version" => "1.9.3", + "target" => "bilbo" + } + } + } + }, + { "name" => "my-name-is-flipper", + "chef_environment" => "elysium", + "platform" => "rhel", + "automatic" => { + "languages" => { + "ruby" => { + "platform" => "centos", + "version" => "2.0.0", + "target" => "uno" + } + } + } + }, + { "name" => "my-name-is-butters", + "chef_environment" => "moon", + "platform" => "rhel", + "automatic" => { + "languages" => { + "ruby" => { + "platform" => "solaris2", + "version" => "2.1.2", + "target" => "random" + } + } + } + }, ], "start" => 0, "total" => 4 @@ -42,43 +90,43 @@ describe Chef::Search::Query do end it "should accept a type as the first argument" do - lambda { @query.search("foo") }.should_not raise_error - lambda { @query.search(:foo) }.should_not raise_error + lambda { @query.search("node") }.should_not raise_error + lambda { @query.search(:node) }.should_not raise_error lambda { @query.search(Hash.new) }.should raise_error(ArgumentError) end it "should query for every object of a type by default" do - @rest.should_receive(:get_rest).with("search/foo?q=*:*&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=1000").and_return(@response) + @rest.should_receive(:get_rest).with("search/node?q=*:*&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=1000").and_return(@response) @query = Chef::Search::Query.new - @query.search(:foo) + @query.search(:node) end it "should allow a custom query" do - @rest.should_receive(:get_rest).with("search/foo?q=gorilla:dundee&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=1000").and_return(@response) + @rest.should_receive(:get_rest).with("search/node?q=platform:rhel&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=1000").and_return(@response) @query = Chef::Search::Query.new - @query.search(:foo, "gorilla:dundee") + @query.search(:node, "platform:rhel") end it "should let you set a sort order" do - @rest.should_receive(:get_rest).with("search/foo?q=gorilla:dundee&sort=id%20desc&start=0&rows=1000").and_return(@response) + @rest.should_receive(:get_rest).with("search/node?q=platform:rhel&sort=id%20desc&start=0&rows=1000").and_return(@response) @query = Chef::Search::Query.new - @query.search(:foo, "gorilla:dundee", "id desc") + @query.search(:node, "platform:rhel", "id desc") end it "should let you set a starting object" do - @rest.should_receive(:get_rest).with("search/foo?q=gorilla:dundee&sort=id%20desc&start=2&rows=1000").and_return(@response) + @rest.should_receive(:get_rest).with("search/node?q=platform:rhel&sort=id%20desc&start=2&rows=1000").and_return(@response) @query = Chef::Search::Query.new - @query.search(:foo, "gorilla:dundee", "id desc", 2) + @query.search(:node, "platform:rhel", "id desc", 2) end it "should let you set how many rows to return" do - @rest.should_receive(:get_rest).with("search/foo?q=gorilla:dundee&sort=id%20desc&start=2&rows=40").and_return(@response) + @rest.should_receive(:get_rest).with("search/node?q=platform:rhel&sort=id%20desc&start=2&rows=40").and_return(@response) @query = Chef::Search::Query.new - @query.search(:foo, "gorilla:dundee", "id desc", 2, 40) + @query.search(:node, "platform:rhel", "id desc", 2, 40) end it "should return the raw rows, start, and total if no block is passed" do - rows, start, total = @query.search(:foo) + rows, start, total = @query.search(:node) rows.should equal(@response["rows"]) start.should equal(@response["start"]) total.should equal(@response["total"]) @@ -87,13 +135,194 @@ describe Chef::Search::Query do it "should call a block for each object in the response" do @call_me = double("blocky") @response["rows"].each { |r| @call_me.should_receive(:do).with(r) } - @query.search(:foo) { |r| @call_me.do(r) } + @query.search(:node) { |r| @call_me.do(r) } end it "should page through the responses" do @call_me = double("blocky") @response["rows"].each { |r| @call_me.should_receive(:do).with(r) } - @query.search(:foo, "*:*", nil, 0, 1) { |r| @call_me.do(r) } + @query.search(:node, "*:*", nil, 0, 1) { |r| @call_me.do(r) } + end + end + + # copypasta existing functionality for new searhc, because new search should at the very least do the same stuff! + describe "new search" do + before(:each) do + @response = { + "rows" => [ + { "name" => "my-name-is-node", + "chef_environment" => "elysium", + "platform" => "rhel", + "automatic" => { + "languages" => { + "ruby" => { + "platform" => "nudibranch", + "version" => "1.9.3", + "target" => "ming-the-merciless" + } + } + } + }, + { "name" => "my-name-is-jonas", + "chef_environment" => "hades", + "platform" => "rhel", + "automatic" => { + "languages" => { + "ruby" => { + "platform" => "i386-mingw32", + "version" => "1.9.3", + "target" => "bilbo" + } + } + } + }, + { "name" => "my-name-is-flipper", + "chef_environment" => "elysium", + "platform" => "rhel", + "automatic" => { + "languages" => { + "ruby" => { + "platform" => "centos", + "version" => "2.0.0", + "target" => "uno" + } + } + } + }, + { "name" => "my-name-is-butters", + "chef_environment" => "moon", + "platform" => "rhel", + "automatic" => { + "languages" => { + "ruby" => { + "platform" => "solaris2", + "version" => "2.1.2", + "target" => "random" + } + } + } + }, + ], + "start" => 0, + "total" => 4 + } + @rest.stub(:get_rest).and_return(@response) + end + + it "should accept a type as the first argument" do + lambda { @query.search("node") }.should_not raise_error + lambda { @query.search(:node) }.should_not raise_error + lambda { @query.search(Hash.new) }.should raise_error(ArgumentError) + end + + it "should query for every object of a type by default" do + @rest.should_receive(:get_rest).with("search/node?q=*:*&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=1000").and_return(@response) + @query = Chef::Search::Query.new + @query.search(:node) + end + + it "should allow a custom query" do + @rest.should_receive(:get_rest).with("search/node?q=platform:rhel&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=1000").and_return(@response) + @query = Chef::Search::Query.new + @query.search(:node, "platform:rhel") + end + + it "should let you set a sort order" do + @rest.should_receive(:get_rest).with("search/node?q=platform:rhel&sort=id%20desc&start=0&rows=1000").and_return(@response) + @query = Chef::Search::Query.new + args = Hash.new + args[:sort] = "id desc" + @query.search(:node, "platform:rhel", args) + end + + it "should let you set a starting object" do + @rest.should_receive(:get_rest).with("search/node?q=platform:rhel&sort=id%20desc&start=2&rows=1000").and_return(@response) + @query = Chef::Search::Query.new + args = Hash.new + args[:sort] = "id desc" + args[:start] = 2 + @query.search(:node, "platform:rhel", args) + end + + it "should let you set how many rows to return" do + @rest.should_receive(:get_rest).with("search/node?q=platform:rhel&sort=id%20desc&start=2&rows=40").and_return(@response) + @query = Chef::Search::Query.new + args = Hash.new + args[:sort] = "id desc" + args[:start] = 2 + args[:rows] = 40 + @query.search(:node, "platform:rhel", args) + end + + it "should return the raw rows, start, and total if no block is passed" do + rows, start, total = @query.search(:node) + rows.should equal(@response["rows"]) + start.should equal(@response["start"]) + total.should equal(@response["total"]) + end + + it "should call a block for each object in the response" do + @call_me = double("blocky") + @response["rows"].each { |r| @call_me.should_receive(:do).with(r) } + @query.search(:node) { |r| @call_me.do(r) } + end + + it "should page through the responses" do + @call_me = double("blocky") + @response["rows"].each { |r| @call_me.should_receive(:do).with(r) } + args = Hash.new + args[:sort] = nil + args[:start] = 0 + args[:rows] = 1 + @query.search(:node, "*:*", args) { |r| @call_me.do(r) } + end + end + + # filtered search results should only return the things asked for + describe "new search" do + before(:each) do + @response = { + "rows" => [ + { "url" => "my-url-is-node", + "data" => { + "env" => "elysium", + "ruby_plat" => "i386-mingw32" + } + }, + { "url" => "my-url-is-jonas", + "data" => { + "env" => "hades", + "ruby_plat" => "i386-mingw32" + } + }, + { "url" => "my-url-is-flipper", + "data" => { + "env" => "elysium", + "ruby_plat" => "centos" + } + }, + { "url" => "my-url-is-butters", + "data" => { + "env" => "moon", + "ruby_plat" => "solaris2" + } + }, + ], + "start" => 0, + "total" => 4 + } + @rest.stub(:post_rest).and_return(@response) + end + + it "should allow you to filter search results" do + filter_args = Hash.new + filter_args[:env] = ["chef_environment"] + filter_args[:ruby_plat] = ["languages", "ruby", "platform"] + @rest.should_receive(:post_rest).with("search/node?q=platform:rhel&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=1000", filter_args).and_return(@response) + @query = Chef::Search::Query.new + args = Hash.new + args[:filter_result] = filter_args + @query.search(:node, "platform:rhel", args) end end end |