require 'spec_helper'

describe User do

  let(:user) { FactoryGirl.create(:user) }

  before(:each) do

    @band = Band.save(nil, "Example Band", "www.bands.com", "zomg we rock", "Apex", "NC", "USA", ["hip hop"], user.id, nil, nil)

  end

  it "should allow search of one band with an exact match" do
    ws = Band.search("Example Band")
    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 = Band.search("Ex")
    ws.length.should == 1
    ws[0].id.should == @band.id

    ws = Band.search("Exa")
    ws.length.should == 1
    ws[0].id.should == @band.id

    ws = Band.search("Exam")
    ws.length.should == 1
    ws[0].id.should == @band.id

    ws = Band.search("Examp")
    ws.length.should == 1
    ws[0].id.should == @band.id

    ws = Band.search("Exampl")
    ws.length.should == 1
    ws[0].id.should == @band.id

    ws = Band.search("Example")
    ws.length.should == 1
    ws[0].id.should == @band.id

    ws = Band.search("Ba")
    ws.length.should == 1
    ws[0].id.should == @band.id

    ws = Band.search("Ban")
    ws.length.should == 1
    ws[0].id.should == @band.id
  end

   it "should not match mid-word searchs" do
    ws = Band.search("xa")
    ws.length.should == 0

    ws = Band.search("le")
    ws.length.should == 0
  end

  it "should delete band" do
    ws = Band.search("Example Band")
    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 = Band.search("Example Band")
    ws.length.should == 0
  end

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

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

    ws = Band.search("Example Band")
    ws.length.should == 0

    ws = Band.search("Bonus")
    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 = Band.save(nil, "Peach pit", "www.bands.com", "zomg we rock", "Apex", "NC", "USA", ["hip hop"], user.id, nil, nil)
    ws = Band.search("pea")
    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 = Band.save(nil, "Peach pit", "www.bands.com", "zomg we rock", "Apex", "NC", "USA", ["hip hop"], user.id, nil, nil)
    ws = Band.search("pe")
    ws.length.should == 1
    user_result = ws[0]
    user_result.id.should == @band2.id

    ws = Band.search("p")
    ws.length.should == 0
   end
end