require 'spec_helper'

describe 'JamTrack Search Model' do

  let(:artist_filter) {       
    filter = JamTrackSearch.json_schema.clone
    filter[JamTrackSearch::KEY_RESULT_TYPES] = [JamTrackSearch::KEY_ARTISTS]
    filter
  }
  let(:song_filter) {       
    filter = JamTrackSearch.json_schema.clone
    filter[JamTrackSearch::KEY_RESULT_TYPES] = [JamTrackSearch::KEY_SONGS]
    filter
  }
  let(:freebird) {
    FactoryGirl.create(:jam_track_with_tracks, original_artist: 'jim bob', name: 'freebird')
  }
  let(:stairway) {
    FactoryGirl.create(:jam_track_with_tracks, original_artist: 'jim bob', name: 'stairway to heaven')
  }

  before :each do 
    JamTrack.delete_all
    JamTrackTrack.delete_all
    freebird
    stairway
  end

  describe "Search filter" do

    it "finds by artist" do 
      pending
      filter = artist_filter.clone
      filter[JamTrackSearch::KEY_SEARCH_STR] = freebird.original_artist
      filter = JamTrackSearch.new.search_results_page(filter['query'])
      expect(filter[JamTrackSearch::KEY_RESULTS][JamTrackSearch::KEY_ARTISTS].count).to be(1)
    end

    it "paginates by artist" do 
      pending
      JamTrackSearch::PER_PAGE.times do |nn|
        FactoryGirl.create(:jam_track_with_tracks, 
                           original_artist: freebird.original_artist + nn.to_s, 
                           name: 'abc'+nn.to_s)
      end
      filter = artist_filter.clone
      filter[JamTrackSearch::KEY_SEARCH_STR] = freebird.original_artist
      out_filter = JamTrackSearch.new.search_results_page(filter.clone['query'])
      expect(out_filter[JamTrackSearch::KEY_RESULTS][JamTrackSearch::KEY_ARTISTS].count).to be([JamTrackSearch::PER_PAGE, JamTrack.count].min)
      num_page = (JamTrack.count / JamTrackSearch::PER_PAGE) + 1
      expect(out_filter[JamTrackSearch::KEY_ARTISTS]['page_count']).to be(num_page)

      filter[JamTrackSearch::KEY_ARTISTS]['page_num'] = 2
      out_filter = JamTrackSearch.new.search_results_page(filter.clone['query'])
      expect(out_filter[JamTrackSearch::KEY_RESULTS][JamTrackSearch::KEY_ARTISTS].count).to be(1)
    end

    it "finds by song" do 
      pending
      filter = song_filter.clone
      filter[JamTrackSearch::KEY_SEARCH_STR] = freebird.name
      filter = JamTrackSearch.new.search_results_page(filter.clone['query'])
      expect(filter[JamTrackSearch::KEY_RESULTS][JamTrackSearch::KEY_SONGS].count).to be(1)
    end

    it "paginates by song" do 
      pending
      (JamTrackSearch::PER_PAGE + 2).times do |nn|
        FactoryGirl.create(:jam_track_with_tracks, 
                           original_artist: freebird.original_artist, 
                           name: 'abc'+nn.to_s)
      end
      filter = song_filter.clone
      filter[JamTrackSearch::KEY_SEARCH_STR] = 'abc'
      out_filter = JamTrackSearch.new.search_results_page(filter.clone['query'])
      expect(out_filter[JamTrackSearch::KEY_RESULTS][JamTrackSearch::KEY_SONGS].count).to be([JamTrackSearch::PER_PAGE, JamTrack.count].min)

      total_count = JamTrack.where("name LIKE 'abc%'").count
      num_page = (total_count / JamTrackSearch::PER_PAGE) + (0==(total_count % JamTrackSearch::PER_PAGE) ? 0 : 1)
      expect(out_filter[JamTrackSearch::KEY_SONGS]['page_count']).to be(num_page)

      filter[JamTrackSearch::KEY_SONGS]['page_num'] = 2
      out_filter = JamTrackSearch.new.search_results_page(filter.clone['query'])
      expect(out_filter[JamTrackSearch::KEY_RESULTS][JamTrackSearch::KEY_SONGS].count).to be(2)
    end

  end

end
