diff options
author | Claire McQuin <claire@getchef.com> | 2014-07-14 17:09:24 -0700 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2014-09-04 15:52:24 -0700 |
commit | 97da8b5a8c5f86151b07c85ecfd7e52c1516c058 (patch) | |
tree | 95eae9dcaeea1e97ca498ca37b456f8193cba73f | |
parent | 79ff958e4d5f48ffe7250ecf12c2976c955e862c (diff) | |
download | chef-97da8b5a8c5f86151b07c85ecfd7e52c1516c058.tar.gz |
Fix typos, add partial_search spec.
-rw-r--r-- | lib/chef/search/query.rb | 20 | ||||
-rw-r--r-- | spec/unit/search/query_spec.rb | 145 |
2 files changed, 106 insertions, 59 deletions
diff --git a/lib/chef/search/query.rb b/lib/chef/search/query.rb index 83d447c4cc..274c5dfd23 100644 --- a/lib/chef/search/query.rb +++ b/lib/chef/search/query.rb @@ -38,10 +38,10 @@ class Chef # This search is only kept for backwards compatibility, since the results of the # new filtered search method will be in a slightly different format def partial_search(type, query='*:*', *args, &block) - # accept both types of args Chef::Log.warn("DEPRECATED: The 'partial_search' api is deprecated, please use the search api with 'filter_result'") + # accept both types of args if args.length == 1 && args[0].is_a?(Hash) - args_hash = args[0] + args_hash = args[0].dup # partial_search implemented in the partial search cookbook uses the # arg hash :keys instead of :filter_result to filter returned data args_hash[:filter_result] = args_hash[:keys] @@ -55,11 +55,11 @@ class Chef unless block.nil? raw_results = search(type,query,args_hash) else - raw_results = search(type,query,args,&block) + raw_results = search(type,query,args_hash,&block) end results = Array.new raw_results[0].each do |r| - results << r + results << r["data"] end return results end @@ -123,21 +123,15 @@ class Chef query_string = create_query_string(type, query, args) response = call_rest_service(query_string, args) - if !args.nil? && args.key?(:filter_result) - response_rows = response['rows'].map { |row| row['data'] } - else - response_rows = response['rows'] - end - unless block.nil? - response_rows.each { |rowset| block.call(rowset) unless rowset.nil?} - unless (response["start"] + response_rows.length) >= response["total"] + response["rows"].each { |rowset| block.call(rowset) unless rowset.nil?} + unless (response["start"] + response["rows"].length) >= response["total"] args[:start] = response["start"] + args[:rows] do_search(type, query, args, &block) end true else - [ response_rows, response["start"], response["total"] ] + [ response["rows"], response["start"], response["total"] ] end end diff --git a/spec/unit/search/query_spec.rb b/spec/unit/search/query_spec.rb index e7068960ab..8a890898d3 100644 --- a/spec/unit/search/query_spec.rb +++ b/spec/unit/search/query_spec.rb @@ -23,6 +23,50 @@ describe Chef::Search::Query do let(:rest) { double("Chef::REST") } let(:query) { Chef::Search::Query.new } + shared_context "filtered search" do + let(:query_string) { "search/node?q=platform:rhel&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=1000" } + let(:server_url) { "https://api.opscode.com/organizations/opscode/nodes" } + let(:args) { { filter_key => filter_hash } } + let(:filter_hash) { + { + 'env' => [ 'chef_environment' ], + 'ruby_plat' => [ 'languages', 'ruby', 'platform' ] + } + } + let(:response) { + { + "rows" => [ + { "url" => "#{server_url}/my-name-is-node", + "data" => { + "env" => "elysium", + "ruby_plat" => "nudibranch" + } + }, + { "url" => "#{server_url}/my-name-is-jonas", + "data" => { + "env" => "hades", + "ruby_plat" => "i386-mingw32" + } + }, + { "url" => "#{server_url}/my-name-is-flipper", + "data" => { + "env" => "elysium", + "ruby_plat" => "centos" + } + }, + { "url" => "#{server_url}/my-name-is-butters", + "data" => { + "env" => "moon", + "ruby_plat" => "solaris2", + } + } + ], + "start" => 0, + "total" => 4 + } + } + end + before(:each) do Chef::REST.stub(:new).and_return(rest) rest.stub(:get_rest).and_return(response) @@ -143,59 +187,68 @@ describe Chef::Search::Query do end context "when :filter_result is provided as a result" do - let(:server_url) { "https://api.opscode.com/organizations/opscode/nodes"} - let(:response) { { - "rows" => [ - { "url" => "#{server_url}/my-name-is-node", - "data" => { - "env" => "elysium", - "ruby_plat" => "nudibranch" - } - }, - { "url" => "#{server_url}/my-name-is-jonas", - "data" => { - "env" => "hades", - "ruby_plat" => "i386-mingw32" - } - }, - { "url" => "#{server_url}/my-name-is-flipper", - "data" => { - "env" => "elysium", - "ruby_plat" => "centos" - } - }, - { "url" => "#{server_url}/my-name-is-butters", - "data" => { - "env" => "moon", - "ruby_plat" => "solaris2", - } - } - ], - "start" => 0, - "total" => 4 - } } + include_context "filtered search" do + let(:filter_key) { :filter_result } - it "should return only the filtered data" do - args = { - :filter_result => { - 'env' => ['chef_environment'], - 'ruby_plat' => ['languages', 'ruby', 'platform'] - } - } + before(:each) do + rest.should_receive(:post_rest).with(query_string, args[filter_key]).and_return(response) + end + + it "should return start" do + start = query.search(:node, "platform:rhel", args)[1] + start.should == response['start'] + end + + it "should return total" do + total = query.search(:node, "platform:rhel", args)[2] + total.should == response['total'] + end + + it "should return rows with the filter applied" do + results = query.search(:node, "platform:rhel", args)[0] + + results.each_with_index do |result, idx| + expected = response["rows"][idx] - rest.should_receive(:post_rest).with("search/node?q=platform:rhel&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=1000", args[:filter_result]).and_return(response) - results, start, total = query.search(:node, "platform:rhel", args) + result.should have_key("url") + result["url"].should == expected["url"] + + result.should have_key("data") + filter_hash.keys.each do |filter_key| + result["data"].should have_key(filter_key) + result["data"][filter_key].should == expected["data"][filter_key] + end + end + end + + end + end + end + + describe "#partial_search" do + include_context "filtered search" do + let(:filter_key) { :keys } + + it "should emit a deprecation warning" do + # partial_search calls search, so we'll stub search to return empty + query.stub(:search).and_return( [ [], 0, 0 ] ) + Chef::Log.should_receive(:warn).with("DEPRECATED: The 'partial_search' api is deprecated, please use the search api with 'filter_result'") + query.partial_search(:node, "platform:rhel", args) + end + + it "should return an array of filtered hashes" do + rest.should_receive(:post_rest).with(query_string, args[filter_key]).and_return(response) + results = query.partial_search(:node, "platform:rhel", args) results.each_with_index do |result, idx| expected = response["rows"][idx] - result.should have_key('env') - result['env'].should == expected['data']['env'] - - result.should have_key('ruby_plat') - result['ruby_plat'].should == expected['data']['ruby_plat'] + filter_hash.keys.each do |filter_key| + result.should have_key(filter_key) + result[filter_key].should == expected["data"][filter_key] + end end end end end -end
\ No newline at end of file +end |