require 'spec_helper'

describe "Band Search" do

  let(:user) { FactoryGirl.create(:user) }
  let(:band) { FactoryGirl.create(:band, name: "Example Band") }
  let(:band_params) {
    {
        name: "The Band",
        biography: "Biography",
        city: 'Austin',
        state: 'TX',
        country: 'US',
        genres: ['country']
    }
  }

  before(:all) do
    Recording.delete_all
    Band.delete_all
  end

  before(:each) do
    @user = FactoryGirl.create(:user)
    band.touch
    

  end

  it "should allow search of one band with an exact match" do
    ws = Search.band_search("Example Band").results
    ws.length.should == 1
    band_result = ws[0]
    band_result.name.should == band.name
    band_result.id.should == band.id
    band_result.location.should == band.location
  end

  it "should allow search of one band with partial matches" do
    ws = Search.band_search("Ex").results
    ws.length.should == 1
    ws[0].id.should == band.id

    ws = Search.band_search("Exa").results
    ws.length.should == 1
    ws[0].id.should == band.id

    ws = Search.band_search("Exam").results
    ws.length.should == 1
    ws[0].id.should == band.id

    ws = Search.band_search("Examp").results
    ws.length.should == 1
    ws[0].id.should == band.id

    ws = Search.band_search("Exampl").results
    ws.length.should == 1
    ws[0].id.should == band.id

    ws = Search.band_search("Example").results
    ws.length.should == 1
    ws[0].id.should == band.id

    ws = Search.band_search("Ba").results
    ws.length.should == 1
    ws[0].id.should == band.id

    ws = Search.band_search("Ban").results
    ws.length.should == 1
    ws[0].id.should == band.id
  end

   it "should not match mid-word searchs" do
    ws = Search.band_search("xa").results
    ws.length.should == 0

    ws = Search.band_search("le").results
    ws.length.should == 0
  end

  it "should delete band" do
    ws = Search.band_search("Example Band").results
    ws.length.should == 1
    band_result = ws[0]
    band_result.id.should == band.id

    band.destroy # delete doesn't work; you have to use destroy.

    ws = Search.band_search("Example Band").results
    ws.length.should == 0
  end

   it "should update band" do
    ws = Search.band_search("Example Band").results
    ws.length.should == 1
    band_result = ws[0]
    band_result.id.should == band.id

    band.name = "bonus-stuff"
    band.save

    ws = Search.band_search("Example Band").results
    ws.length.should == 0

    ws = Search.band_search("Bonus").results
    ws.length.should == 1
    band_result = ws[0]
    band_result.id.should == band.id
    band_result.name.should == "bonus-stuff"
   end

  it "should tokenize correctly" do
    band2 = FactoryGirl.create(:band, name: 'Peach pit')
    ws = Search.band_search("pea").results
    ws.length.should == 1
    user_result = ws[0]
    user_result.id.should == band2.id
  end


  it "should not return anything with a 1 character search" do
    band2 = FactoryGirl.create(:band, name: 'Peach pit')
    ws = Search.band_search("pe").results
    ws.length.should == 1
    user_result = ws[0]
    user_result.id.should == band2.id

    ws = Search.band_search("p").results
    ws.length.should == 0
   end
end
