139 lines
4.0 KiB
Ruby
139 lines
4.0 KiB
Ruby
require 'spec_helper'
|
|
|
|
# these tests help verify tire integration
|
|
describe "tire search" do
|
|
|
|
before(:each) do
|
|
Band.delete_search_index
|
|
Band.create_search_index
|
|
User.delete_search_index
|
|
User.create_search_index
|
|
end
|
|
|
|
|
|
it "full search for empty indexes" do
|
|
s = Tire.search ['test-jamruby-users', 'test-jamruby-bands'], :load => true do
|
|
query { string '*' }
|
|
end
|
|
|
|
s.results.length.should == 0
|
|
end
|
|
|
|
it "full search for single user" do
|
|
@user = FactoryGirl.create(:user, name: "User One", email: "user@example.com", musician: true)
|
|
|
|
User.search_index.refresh
|
|
|
|
s = Tire.search ['test-jamruby-users', 'test-jamruby-bands'], :load => true do
|
|
query { string 'user' }
|
|
end
|
|
|
|
s.results.length.should == 1
|
|
result = s.results[0]
|
|
result.should be_a_kind_of User
|
|
result.id.should == @user.id
|
|
end
|
|
|
|
it "full search for single band" do
|
|
@band = FactoryGirl.create(:band, name: "Example Band", website: "www.bands.com", biography: "zomg we rock")
|
|
Band.search_index.refresh
|
|
|
|
s = Tire.search ['test-jamruby-users', 'test-jamruby-bands'], :load => true do
|
|
query { string 'example' }
|
|
end
|
|
|
|
s.results.length.should == 1
|
|
result = s.results[0]
|
|
result.should be_a_kind_of Band
|
|
result.id.should == @band.id
|
|
end
|
|
|
|
it "full search for a band & user" do
|
|
@user = FactoryGirl.create(:user, name: "Peach", email: "user@example.com", musician: true)
|
|
@band = FactoryGirl.create(:band, name: "Peach pit", website: "www.bands.com", biography: "zomg we rock")
|
|
User.search_index.refresh
|
|
Band.search_index.refresh
|
|
|
|
s = Tire.search ['test-jamruby-users', 'test-jamruby-bands'], :load => true do
|
|
query { string 'peach' }
|
|
sort { by [:_score] }
|
|
end
|
|
|
|
s.results.length.should == 2
|
|
result = s.results[0]
|
|
result.should be_a_kind_of User
|
|
result.id.should == @user.id
|
|
result = s.results[1]
|
|
result.should be_a_kind_of Band
|
|
result.id.should == @band.id
|
|
end
|
|
|
|
|
|
it "pagination" do
|
|
@user = FactoryGirl.create(:user, name: "Peach", email: "user@example.com", musician: true)
|
|
@band = FactoryGirl.create(:band, name: "Peach pit", website: "www.bands.com", biography: "zomg we rock")
|
|
User.search_index.refresh
|
|
Band.search_index.refresh
|
|
|
|
s = Tire.search ['test-jamruby-users', 'test-jamruby-bands'], :load => true do
|
|
query { string 'peach' }
|
|
sort { by [:_score] }
|
|
from 0
|
|
size 1
|
|
end
|
|
|
|
s.results.length.should == 1
|
|
result = s.results[0]
|
|
result.should be_a_kind_of User
|
|
result.id.should == @user.id
|
|
|
|
s = Tire.search ['test-jamruby-users', 'test-jamruby-bands'], :load => true do
|
|
query { string 'peach' }
|
|
sort { by [:_score] }
|
|
from 1
|
|
size 2
|
|
end
|
|
|
|
s.results.length.should == 1
|
|
result = s.results[0]
|
|
result.should be_a_kind_of Band
|
|
result.id.should == @band.id
|
|
end
|
|
|
|
it "should fail to search deleted index" do
|
|
pending "until figure out how to stop tire from writing to stdout when a search of deleted index occurs"
|
|
|
|
sleep 1 # https://jamkazam.atlassian.net/browse/VRFS-69
|
|
s = Tire.search ['test-jamruby-users'], :search_type => 'count', :query => {"match_all" => {}} do
|
|
|
|
end
|
|
|
|
s.results.total.should == 0
|
|
|
|
|
|
@user = FactoryGirl.create(:user, name: "Peach", email: "user@example.com", musician: true)
|
|
User.search_index.refresh
|
|
sleep 1 # https://jamkazam.atlassian.net/browse/VRFS-69
|
|
|
|
s = Tire.search ['test-jamruby-users'], :search_type => 'count', :query => {"match_all" => {}} do
|
|
|
|
end
|
|
|
|
s.results.total.should == 1
|
|
|
|
User.delete_search_index
|
|
|
|
s = Tire.search ['test-jamruby-users'], :search_type => 'count', :query => {"match_all" => {}} do
|
|
|
|
end
|
|
|
|
expect {s.results.total}.to raise_error(Tire::Search::SearchRequestFailed)
|
|
begin
|
|
s.results.total
|
|
false.should be_true # should not get here
|
|
rescue Tire::Search::SearchRequestFailed => srf
|
|
srf.to_s.include?("IndexMissingException").should be_true
|
|
end
|
|
end
|
|
end
|