jam-cloud/spec/jam_ruby/models/band_search_spec.rb

81 lines
2.3 KiB
Ruby

require 'spec_helper'
describe User do
before(:each) do
Band.delete_search_index
Band.create_search_index
@band = Band.save(name: "Example Band", website: "www.bands.com", biography: "zomg we rock")
# you have to poke elasticsearch because it will batch requests internally for a second
Band.search_index.refresh
end
it "should allow search of one band" do
ws = Band.search("Example Band")
ws.results.length.should == 1
band_result = ws.results[0]
band_result._type.should == "jam_ruby/band"
band_result.name.should == @band.name
band_result.id.should == @band.id
band_result.location.should == @band.location
band_result.logo_url.should_not be_nil
band_result.photo_url.should_not be_nil
end
it "should delete band" do
ws = Band.search("Example Band")
ws.results.length.should == 1
band_result = ws.results[0]
band_result.id.should == @band.id
@band.destroy # delete doesn't work; you have to use destroy.
Band.search_index.refresh
ws = Band.search("Example Band")
ws.results.length.should == 0
end
it "should update band" do
ws = Band.search("Example Band")
ws.results.length.should == 1
band_result = ws.results[0]
band_result.id.should == @band.id
@band.name = "bonus-stuff"
@band.save
Band.search_index.refresh
ws = Band.search("Example Band")
ws.results.length.should == 0
ws = Band.search("Bonus")
ws.results.length.should == 1
band_result = ws.results[0]
band_result.id.should == @band.id
band_result.name.should == "bonus-stuff"
end
it "should tokenize correctly" do
@band2 = Band.save(name: "Peach pit", website: "www.bands.com", biography: "zomg we rock")
Band.search_index.refresh
ws = Band.search("pea")
ws.results.length.should == 1
user_result = ws.results[0]
user_result.id.should == @band2.id
end
it "should not return anything with a 1 character search" do
@band2 = Band.save(name: "Peach pit", website: "www.bands.com", biography: "zomg we rock")
Band.search_index.refresh
ws = Band.search("pe")
ws.results.length.should == 1
user_result = ws.results[0]
user_result.id.should == @band2.id
ws = Band.search("p")
ws.results.length.should == 0
end
end