summaryrefslogtreecommitdiff
path: root/spec/unit/search
diff options
context:
space:
mode:
authorSeth Chisamore <schisamo@opscode.com>2012-10-30 10:39:35 -0400
committerSeth Chisamore <schisamo@opscode.com>2012-10-30 10:39:35 -0400
commit24dc69a9a97e82a6e4207de68d6dcc664178249b (patch)
tree19bb289c9f88b4bbab066bc56b95d6d222fd5c35 /spec/unit/search
parent9348c1c9c80ee757354d624b7dc1b78ebc7605c4 (diff)
downloadchef-24dc69a9a97e82a6e4207de68d6dcc664178249b.tar.gz
[OC-3564] move core Chef to the repo root \o/ \m/
The opscode/chef repository now only contains the core Chef library code used by chef-client, knife and chef-solo!
Diffstat (limited to 'spec/unit/search')
-rw-r--r--spec/unit/search/query_spec.rb99
1 files changed, 99 insertions, 0 deletions
diff --git a/spec/unit/search/query_spec.rb b/spec/unit/search/query_spec.rb
new file mode 100644
index 0000000000..e32bed697c
--- /dev/null
+++ b/spec/unit/search/query_spec.rb
@@ -0,0 +1,99 @@
+#
+# Author:: Adam Jacob (<adam@opscode.com>)
+# Copyright:: Copyright (c) 2009,2010 Opscode, Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'spec_helper'
+require 'chef/search/query'
+
+describe Chef::Search::Query do
+ before(:each) do
+ @rest = mock("Chef::REST")
+ Chef::REST.stub!(:new).and_return(@rest)
+ @query = Chef::Search::Query.new
+ end
+
+ describe "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" },
+ ],
+ "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("foo") }.should_not raise_error(ArgumentError)
+ lambda { @query.search(:foo) }.should_not raise_error(ArgumentError)
+ 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)
+ @query = Chef::Search::Query.new
+ @query.search(:foo)
+ 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)
+ @query = Chef::Search::Query.new
+ @query.search(:foo, "gorilla:dundee")
+ 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)
+ @query = Chef::Search::Query.new
+ @query.search(:foo, "gorilla:dundee", "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)
+ @query = Chef::Search::Query.new
+ @query.search(:foo, "gorilla:dundee", "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)
+ @query = Chef::Search::Query.new
+ @query.search(:foo, "gorilla:dundee", "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.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 = mock("blocky")
+ @response["rows"].each { |r| @call_me.should_receive(:do).with(r) }
+ @query.search(:foo) { |r| @call_me.do(r) }
+ end
+
+ it "should page through the responses" do
+ @call_me = mock("blocky")
+ @response["rows"].each { |r| @call_me.should_receive(:do).with(r) }
+ @query.search(:foo, "*:*", nil, 0, 1) { |r| @call_me.do(r) }
+ end
+ end
+end