summaryrefslogtreecommitdiff
path: root/spec/unit/search_index_spec.rb
blob: ff234c6a4170960f9c8b1ab1ff869239a140786e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#
# Author:: Adam Jacob (<adam@hjksolutions.com>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# 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 File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))

describe Chef::SearchIndex, "initialize method" do
  it "should create a new Chef::SearchIndex object" do
    mf = mock("Ferret::Index::Index", :null_object => true)
    Ferret::Index::Index.stub!(:new).and_return(mf)
    Chef::SearchIndex.new.should be_kind_of(Chef::SearchIndex)
  end
  
  it "should create a Ferret Indexer" do
    mf = mock("Ferret::Index::Index", :null_object => true)
    Ferret::Index::Index.should_receive(:new).and_return(mf)
    Chef::SearchIndex.new
  end
end

describe Chef::SearchIndex, "create_index_object method" do
  before(:each) do
    @mf = mock("Ferret::Index::Index", :null_object => true)
    @fakeobj = mock("ToIndex", :null_object => true)
    @the_pigeon = { :index_name => "bird", :id => "pigeon" }
    @fakeobj.stub!(:respond_to?).with(:to_index).and_return(true)
    @fakeobj.stub!(:to_index).and_return(@the_pigeon)
    Ferret::Index::Index.stub!(:new).and_return(@mf)
  end
  
  def do_create_index_object
    index = Chef::SearchIndex.new
    index.create_index_object(@fakeobj)
  end
  
  it "should call to_index if the passed object responds to it" do
    @fakeobj.should_receive(:respond_to?).with(:to_index).and_return(true)
    @fakeobj.should_receive(:to_index).and_return(@the_pigeon)
    do_create_index_object
  end
  
  it "should use a hash if the passed argument does not have to_index (but is a hash)" do
    @fakeobj.stub!(:respond_to?).with(:to_index).and_return(false)
    @fakeobj.should_receive(:kind_of?).with(Hash).and_return(true)
    do_create_index_object
  end
  
  it "should raise SearchIndex exception if the hash does not contain an :id field" do
    @the_pigeon.delete(:id)
    lambda { do_create_index_object }.should raise_error(Chef::Exception::SearchIndex)
  end
  
  it "should raise SearchIndex exception if the hash does not contain an :index_name field" do
    @the_pigeon.delete(:index_name)
    lambda { do_create_index_object }.should raise_error(Chef::Exception::SearchIndex)
  end
  
  it "should raise SearchIndex exception if the hash new_object cannot be indexed" do
    @fakeobj.stub!(:respond_to?).and_return(false)
    @fakeobj.stub!(:kind_of?).and_return(false)
    lambda { do_create_index_object }.should raise_error(Chef::Exception::SearchIndex)
  end
  
  it "should turn index hash keys in to symbols if it has strings" do
    @the_pigeon["john"] = "and_yoko"
    do_create_index_object.should have_key(:john)
  end
end

describe Chef::SearchIndex, "add method" do
  before(:each) do
    @mf = mock("Ferret::Index::Index", :null_object => true)
    @fakeobj = mock("ToIndex", :null_object => true)
    @the_pigeon = { :index_name => "bird", :id => "pigeon" }
    @fakeobj.stub!(:respond_to?).with(:to_index).and_return(true)
    @fakeobj.stub!(:to_index).and_return(@the_pigeon)
    Ferret::Index::Index.stub!(:new).and_return(@mf)
  end
  
  def do_add
    index = Chef::SearchIndex.new
    index.add(@fakeobj)
  end

  it "should send the resulting hash to the index" do
    @mf.should_receive(:add_document).with(@the_pigeon)
    do_add
  end
end

describe Chef::SearchIndex, "delete method" do
  before(:each) do
    @mf = mock("Ferret::Index::Index", :null_object => true)
    @fakeobj = mock("ToIndex", :null_object => true)
    @the_pigeon = { :index_name => "bird", :id => "pigeon" }
    @fakeobj.stub!(:respond_to?).with(:to_index).and_return(true)
    @fakeobj.stub!(:to_index).and_return(@the_pigeon)
    Ferret::Index::Index.stub!(:new).and_return(@mf)
  end
  
  def do_delete(object)
    index = Chef::SearchIndex.new
    index.delete(object)
  end
  
  it "should delete the resulting hash to the index" do
    @mf.should_receive(:delete).with(@the_pigeon[:id])
    do_delete(@fakeobj)
  end
end

describe Chef::SearchIndex, "commit method" do
  before(:each) do
    @mf = mock("Ferret::Index::Index", :null_object => true)
    Ferret::Index::Index.stub!(:new).and_return(@mf)
  end
  
  it "should commit index to disk" do
    @mf.should_receive(:commit)
    Chef::SearchIndex.new.commit
  end
end