From 03bab53b2f0b15130cbf1289d7a29c295dc2ed21 Mon Sep 17 00:00:00 2001 From: Steven Miers Date: Wed, 29 Apr 2015 19:06:33 -0500 Subject: [PATCH] VRFS-3007 : Remove redundant "model" suffix from spec name as folder is already named "model". --- .../models/musician_search_model_spec.rb | 310 --------- .../jam_ruby/models/musician_search_spec.rb | 641 +++++++----------- 2 files changed, 261 insertions(+), 690 deletions(-) delete mode 100644 ruby/spec/jam_ruby/models/musician_search_model_spec.rb diff --git a/ruby/spec/jam_ruby/models/musician_search_model_spec.rb b/ruby/spec/jam_ruby/models/musician_search_model_spec.rb deleted file mode 100644 index 499a0b14f..000000000 --- a/ruby/spec/jam_ruby/models/musician_search_model_spec.rb +++ /dev/null @@ -1,310 +0,0 @@ -require 'spec_helper' -require 'time_difference' - - -describe 'Musician Search Model' do - - let!(:searcher) { FactoryGirl.create(:austin_user) } - let!(:search) { MusicianSearch.create_search(searcher) } - - let!(:austin_user) { FactoryGirl.create(:austin_user) } - let!(:dallas_user) { FactoryGirl.create(:dallas_user) } - let!(:miami_user) { FactoryGirl.create(:miami_user) } - let!(:seattle_user) { FactoryGirl.create(:seattle_user) } - - let!(:user_types) { [:austin_user, :dallas_user, :miami_user, :seattle_user] } - - describe "creates search obj" do - it "associates to user" do - expect(search.user).to eq(searcher) - searcher.reload - expect(searcher.musician_search).to eq(search) - end - - it "sets json" do - search.update_json_value(MusicianSearch::KEY_GIGS, MusicianSearch::GIG_COUNTS[1]) - expect(search.json[MusicianSearch::KEY_GIGS]).to eq(MusicianSearch::GIG_COUNTS[1]) - end - end - - describe "filtering criteria" do - - # it "filters musicians" do - # expect(search.do_search(per_page: User.musicians.count).count).to eq(User.musicians.count) - # end - - describe "filters by age" do - before(:all) do - User.delete_all - @users = [] - today = Date.today - MusicianSearch::AGE_COUNTS.each_with_index do |age, idx| - dd = today - age.years - 1.day - @users << FactoryGirl.create(:austin_user, :birth_date => dd) - @users << FactoryGirl.create(:dallas_user, :birth_date => dd) - @users << FactoryGirl.create(:miami_user, :birth_date => dd) - @users << FactoryGirl.create(:seattle_user, :birth_date => dd) - end - end - - it "filters by one age" do - age = MusicianSearch::AGE_COUNTS[0] - search.update_json_value(MusicianSearch::KEY_AGES, [age]) - today = Date.today.to_time - search.do_search.all.each do |uu| - diff = TimeDifference.between(uu.birth_date.to_time, today).in_years - expect(diff).to be >= age - expect(diff).to be < MusicianSearch::AGE_COUNTS[1] - end - end - - it "filters by multiple ages" do - ages = MusicianSearch::AGE_COUNTS[0..2] - search.update_json_value(MusicianSearch::KEY_AGES, ages[0..1]) - today = Date.today.to_time - search.do_search.all.each do |uu| - diff = TimeDifference.between(uu.birth_date.to_time, today).in_years - expect(diff).to be >= ages.first - expect(diff).to be < ages.last - end - end - - it "skips filtering by ages" do - search.update_json_value(MusicianSearch::KEY_AGES, [MusicianSearch::ANY_VAL_INT]) - search.do_search.to_sql =~ /(birth_date)/ - expect($1).to eq(nil) - end - end - - describe "filtering by gig" do - before(:all) do - User.delete_all - user_types.each do |utype| - FactoryGirl.create(utype, :concert_count => MusicianSearch::GIG_COUNTS[1]) - end - end - - it "ignores gig count if any selected" do - search.update_json_value(MusicianSearch::KEY_GIGS, MusicianSearch::GIG_COUNTS[0]) - expect(search.do_search.count).to eq(User.musicians.count - 1) # searcher is musician - end - - it "filters by gig count" do - search.update_json_value(MusicianSearch::KEY_GIGS, MusicianSearch::GIG_COUNTS[1]) - expect(search.do_search.count).to eq(user_types.count) - end - end - - describe "filtering by studio" do - before(:all) do - User.delete_all - user_types.each do |utype| - FactoryGirl.create(utype, :studio_session_count => MusicianSearch::STUDIO_COUNTS[1]) - end - end - - it "ignores studio count if any selected" do - search.update_json_value(MusicianSearch::KEY_STUDIOS, MusicianSearch::STUDIO_COUNTS[0]) - expect(search.do_search.count).to eq(User.musicians.count - 1) - end - - it "filters by studio count" do - search.update_json_value(MusicianSearch::KEY_STUDIOS, MusicianSearch::STUDIO_COUNTS[1]) - expect(search.do_search.count).to eq(user_types.count) - end - end - - describe "filters skills" do - before(:all) do - User.delete_all - MusicianSearch::SKILL_VALS.each do |val| - user_types.each { |utype| FactoryGirl.create(utype, :skill_level => val) } - end - end - - it "get expected number per skill" do - search.update_json_value(MusicianSearch::KEY_SKILL, MusicianSearch::SKILL_VALS[1]) - expect(search.do_search.count).to eq(user_types.count) - end - end - - describe "filters interests" do - before(:all) do - User.delete_all - MusicianSearch::INTEREST_VALS[1..-1].each do |val| - user_types.each { |utype| FactoryGirl.create(utype, val => true) } - end - end - - it "get expected number per interest" do - search.update_json_value(MusicianSearch::KEY_INTERESTS, MusicianSearch::INTEREST_VALS[1]) - expect(search.do_search.count).to eq(user_types.count) - end - end - - describe "filters genres" do - before(:all) do - user_types.each do |utype| - uu = FactoryGirl.create(utype) - uu.update_genres([Genre.first.id, Genre.last.id], GenrePlayer::PROFILE) - end - end - - it "gets expected number of users" do - search.update_json_value(MusicianSearch::KEY_GENRES, [Genre.first.id, Genre.last.id]) - expect(search.do_search.count).to eq(user_types.count) - end - end - - describe "filters instruments" do - before(:all) do - instruments = Instrument.first(user_types.count).collect do |inst| - { instrument_id: inst.id, proficiency_level: 2, priority: 1 } - end - user_types[0..2].each do |utype| - uu = FactoryGirl.create(utype) - uu.update_instruments(instruments) - end - end - - it "gets expected number of users" do - instjson = [{ instrument_id: Instrument.first.id, proficiency_level: 2 }, - { instrument_id: Instrument.first(2)[1].id, proficiency_level: 2 } - ] - search.update_json_value(MusicianSearch::KEY_INSTRUMENTS, instjson) - expect(search.do_search.count).to eq(3) - end - end - end - - describe "sort order by distance" do - before(:all) do - User.delete_all - end - - before(:each) do - create_phony_database - end - - it "sorts by distance" do - musician_search = MusicianSearch.create_search(searcher) - musician_search.update_json_value(MusicianSearch::KEY_SORT_ORDER, MusicianSearch::SORT_VALS[1]) - results = musician_search.do_search - expect(results[0].id).to eq(austin_user.id) - expect(results[1].id).to eq(dallas_user.id) - expect(results[2].id).to eq(miami_user.id) - expect(results[3].id).to eq(seattle_user.id) - end - end - - describe "sort order by latency" do - before(:each) do - User.delete_all - t = Time.now - 10.minute - - @user1 = FactoryGirl.create(:user, created_at: t+1.minute, last_jam_locidispid: 1) - @user2 = FactoryGirl.create(:user, created_at: t+2.minute, last_jam_locidispid: 2) - @user3 = FactoryGirl.create(:user, created_at: t+3.minute, last_jam_locidispid: 3) - @user4 = FactoryGirl.create(:user, created_at: t+4.minute, last_jam_locidispid: 4) - @user5 = FactoryGirl.create(:user, created_at: t+5.minute, last_jam_locidispid: 5) - @user6 = FactoryGirl.create(:user, created_at: t+6.minute) # not geocoded - @user7 = FactoryGirl.create(:user, created_at: t+7.minute, musician: false) # not musician - - @musicians = [] - @musicians << @user1 - @musicians << @user2 - @musicians << @user3 - @musicians << @user4 - @musicians << @user5 - @musicians << @user6 - - # from these scores: - # user1 has scores other users in user1 location, and with user2, user3, user4 - # user2 has scores with users in user3 and user4 location - # Score.delete_all - # Score.connection.execute('delete from current_network_scores').check - Score.createx(1, 'a', 1, 1, 'a', 1, 10) - Score.createx(1, 'a', 1, 2, 'b', 2, 20) - Score.createx(1, 'a', 1, 3, 'c', 3, 30) - Score.createx(1, 'a', 1, 4, 'd', 4, 40) - Score.createx(2, 'b', 2, 3, 'c', 3, 15) - Score.createx(2, 'b', 2, 4, 'd', 4, 70) - end - - it "sorts by latency" do - search.update_json_value(MusicianSearch::KEY_SORT_ORDER, MusicianSearch::SORT_VALS[0]) - results = search.do_search - expect(results[0].id).to eq(@user1.id) - expect(results[1].id).to eq(@user2.id) - expect(results[2].id).to eq(@user3.id) - expect(results[3].id).to eq(@user4.id) - end - - end - - describe "produces accurate query description" do - - it 'has default description' do - expect(search.description).to match(/^Click search button to look for musicians/) - end - - it 'has correct sort order description' do - search.update_json_value(MusicianSearch::KEY_SORT_ORDER, MusicianSearch::SORT_VALS[1]) - str = MusicianSearch::SORT_ORDERS[search.json_value(MusicianSearch::KEY_SORT_ORDER)] - expect(search.description).to match(/^Current Search: Sort = #{str}$/) - end - - it 'has correct description for single-valued selections' do - selections = [{ - key: MusicianSearch::KEY_INTERESTS, - value: MusicianSearch::INTEREST_VALS[1], - lookup: MusicianSearch::INTERESTS, - description: 'Interest' - }, - { - key: MusicianSearch::KEY_SKILL, - value: MusicianSearch::SKILL_VALS[1], - lookup: MusicianSearch::SKILL_LEVELS, - description: 'Skill' - }, - { - key: MusicianSearch::KEY_STUDIOS, - value: MusicianSearch::STUDIO_COUNTS[1], - lookup: MusicianSearch::STUDIOS_LABELS, - description: 'Studio Sessions' - }, - { - key: MusicianSearch::KEY_GIGS, - value: MusicianSearch::GIG_COUNTS[1], - lookup: MusicianSearch::GIG_LABELS, - description: 'Concert Gigs' - }] - selections.each do |hash| - search.update_json_value(hash[:key], hash[:value]) - json_val = search.json_value(hash[:key]) - expect(search.description).to match(/; #{hash[:description]} = #{hash[:lookup][json_val]}/) - end - end - - it 'has correct description for genres' do - search.update_json_value(MusicianSearch::KEY_GENRES, [Genre.first.id, Genre.last.id]) - expect(search.description).to match(/; Genres = #{Genre.first.description}, #{Genre.last.description}/) - end - - it 'has correct description for ages' do - search.update_json_value(MusicianSearch::KEY_AGES, [MusicianSearch::AGE_COUNTS[0],MusicianSearch::AGE_COUNTS[1]]) - expect(search.description).to match(/; Ages = #{MusicianSearch::AGES[MusicianSearch::AGE_COUNTS[0]]}, #{MusicianSearch::AGES[MusicianSearch::AGE_COUNTS[1]]}/) - end - - it 'has correct description for instruments' do - instrs = Instrument.limit(2).order(:description) - instjson = [{ instrument_id: instrs[0].id, proficiency_level: 2 }, - { instrument_id: instrs[1].id, proficiency_level: 1 } - ] - search.update_json_value(MusicianSearch::KEY_INSTRUMENTS, instjson) - instr_descrip = "#{instrs[0].description} (#{MusicianSearch::INSTRUMENT_PROFICIENCY[2]}), #{instrs[1].description} (#{MusicianSearch::INSTRUMENT_PROFICIENCY[1]})" - expect(search.description).to match(/; Instruments = #{Regexp.escape(instr_descrip)}/) - end - - end -end diff --git a/ruby/spec/jam_ruby/models/musician_search_spec.rb b/ruby/spec/jam_ruby/models/musician_search_spec.rb index 117222d82..499a0b14f 100644 --- a/ruby/spec/jam_ruby/models/musician_search_spec.rb +++ b/ruby/spec/jam_ruby/models/musician_search_spec.rb @@ -1,79 +1,205 @@ require 'spec_helper' +require 'time_difference' -describe 'Musician search' do - before(:all) do - User.delete_all +describe 'Musician Search Model' do + + let!(:searcher) { FactoryGirl.create(:austin_user) } + let!(:search) { MusicianSearch.create_search(searcher) } + + let!(:austin_user) { FactoryGirl.create(:austin_user) } + let!(:dallas_user) { FactoryGirl.create(:dallas_user) } + let!(:miami_user) { FactoryGirl.create(:miami_user) } + let!(:seattle_user) { FactoryGirl.create(:seattle_user) } + + let!(:user_types) { [:austin_user, :dallas_user, :miami_user, :seattle_user] } + + describe "creates search obj" do + it "associates to user" do + expect(search.user).to eq(searcher) + searcher.reload + expect(searcher.musician_search).to eq(search) + end + + it "sets json" do + search.update_json_value(MusicianSearch::KEY_GIGS, MusicianSearch::GIG_COUNTS[1]) + expect(search.json[MusicianSearch::KEY_GIGS]).to eq(MusicianSearch::GIG_COUNTS[1]) + end + end + + describe "filtering criteria" do + + # it "filters musicians" do + # expect(search.do_search(per_page: User.musicians.count).count).to eq(User.musicians.count) + # end + + describe "filters by age" do + before(:all) do + User.delete_all + @users = [] + today = Date.today + MusicianSearch::AGE_COUNTS.each_with_index do |age, idx| + dd = today - age.years - 1.day + @users << FactoryGirl.create(:austin_user, :birth_date => dd) + @users << FactoryGirl.create(:dallas_user, :birth_date => dd) + @users << FactoryGirl.create(:miami_user, :birth_date => dd) + @users << FactoryGirl.create(:seattle_user, :birth_date => dd) + end + end + + it "filters by one age" do + age = MusicianSearch::AGE_COUNTS[0] + search.update_json_value(MusicianSearch::KEY_AGES, [age]) + today = Date.today.to_time + search.do_search.all.each do |uu| + diff = TimeDifference.between(uu.birth_date.to_time, today).in_years + expect(diff).to be >= age + expect(diff).to be < MusicianSearch::AGE_COUNTS[1] + end + end + + it "filters by multiple ages" do + ages = MusicianSearch::AGE_COUNTS[0..2] + search.update_json_value(MusicianSearch::KEY_AGES, ages[0..1]) + today = Date.today.to_time + search.do_search.all.each do |uu| + diff = TimeDifference.between(uu.birth_date.to_time, today).in_years + expect(diff).to be >= ages.first + expect(diff).to be < ages.last + end + end + + it "skips filtering by ages" do + search.update_json_value(MusicianSearch::KEY_AGES, [MusicianSearch::ANY_VAL_INT]) + search.do_search.to_sql =~ /(birth_date)/ + expect($1).to eq(nil) + end + end + + describe "filtering by gig" do + before(:all) do + User.delete_all + user_types.each do |utype| + FactoryGirl.create(utype, :concert_count => MusicianSearch::GIG_COUNTS[1]) + end + end + + it "ignores gig count if any selected" do + search.update_json_value(MusicianSearch::KEY_GIGS, MusicianSearch::GIG_COUNTS[0]) + expect(search.do_search.count).to eq(User.musicians.count - 1) # searcher is musician + end + + it "filters by gig count" do + search.update_json_value(MusicianSearch::KEY_GIGS, MusicianSearch::GIG_COUNTS[1]) + expect(search.do_search.count).to eq(user_types.count) + end + end + + describe "filtering by studio" do + before(:all) do + User.delete_all + user_types.each do |utype| + FactoryGirl.create(utype, :studio_session_count => MusicianSearch::STUDIO_COUNTS[1]) + end + end + + it "ignores studio count if any selected" do + search.update_json_value(MusicianSearch::KEY_STUDIOS, MusicianSearch::STUDIO_COUNTS[0]) + expect(search.do_search.count).to eq(User.musicians.count - 1) + end + + it "filters by studio count" do + search.update_json_value(MusicianSearch::KEY_STUDIOS, MusicianSearch::STUDIO_COUNTS[1]) + expect(search.do_search.count).to eq(user_types.count) + end + end + + describe "filters skills" do + before(:all) do + User.delete_all + MusicianSearch::SKILL_VALS.each do |val| + user_types.each { |utype| FactoryGirl.create(utype, :skill_level => val) } + end + end + + it "get expected number per skill" do + search.update_json_value(MusicianSearch::KEY_SKILL, MusicianSearch::SKILL_VALS[1]) + expect(search.do_search.count).to eq(user_types.count) + end + end + + describe "filters interests" do + before(:all) do + User.delete_all + MusicianSearch::INTEREST_VALS[1..-1].each do |val| + user_types.each { |utype| FactoryGirl.create(utype, val => true) } + end + end + + it "get expected number per interest" do + search.update_json_value(MusicianSearch::KEY_INTERESTS, MusicianSearch::INTEREST_VALS[1]) + expect(search.do_search.count).to eq(user_types.count) + end + end + + describe "filters genres" do + before(:all) do + user_types.each do |utype| + uu = FactoryGirl.create(utype) + uu.update_genres([Genre.first.id, Genre.last.id], GenrePlayer::PROFILE) + end + end + + it "gets expected number of users" do + search.update_json_value(MusicianSearch::KEY_GENRES, [Genre.first.id, Genre.last.id]) + expect(search.do_search.count).to eq(user_types.count) + end + end + + describe "filters instruments" do + before(:all) do + instruments = Instrument.first(user_types.count).collect do |inst| + { instrument_id: inst.id, proficiency_level: 2, priority: 1 } + end + user_types[0..2].each do |utype| + uu = FactoryGirl.create(utype) + uu.update_instruments(instruments) + end + end + + it "gets expected number of users" do + instjson = [{ instrument_id: Instrument.first.id, proficiency_level: 2 }, + { instrument_id: Instrument.first(2)[1].id, proficiency_level: 2 } + ] + search.update_json_value(MusicianSearch::KEY_INSTRUMENTS, instjson) + expect(search.do_search.count).to eq(3) + end + end end - # need a data set with actual distances - describe "test set A" do + describe "sort order by distance" do + before(:all) do + User.delete_all + end - before(:each) do + before(:each) do create_phony_database end - let!(:austin_user) { FactoryGirl.create(:austin_user) } - let!(:dallas_user) { FactoryGirl.create(:dallas_user) } - let!(:miami_user) { FactoryGirl.create(:miami_user) } - let!(:seattle_user) { FactoryGirl.create(:seattle_user) } - - describe "search on distance" do - - it "finds self when very local search" do - Search.musician_filter({distance: 1, orderby: 'distance'}, austin_user).results.should == [austin_user] # just to see that distance is 0 to self - Search.musician_filter({distance: 1, orderby: 'distance'}, dallas_user).results.should == [dallas_user] # just to see that distance is 0 to self - Search.musician_filter({distance: 1, orderby: 'distance'}, miami_user).results.should == [miami_user] # just to see that distance is 0 to self - Search.musician_filter({distance: 1, orderby: 'distance'}, seattle_user).results.should == [seattle_user] # just to see that distance is 0 to self - end - - it "finds dallas when in range of austin" do - expected_results = [austin_user, dallas_user] - - Search.musician_filter({distance: 500, orderby: 'distance'}, austin_user).results.should == expected_results - Search.musician_filter({distance: 100, orderby: 'distance'}, austin_user).results.should == [austin_user] - end - - it "finds miami when in range of austin" do - expected_results = [austin_user, dallas_user, miami_user] - - Search.musician_filter({distance: 1500, orderby: 'distance'}, austin_user).results.should == expected_results - Search.musician_filter({distance: 300, orderby: 'distance'}, austin_user).results.should == [austin_user, dallas_user] - Search.musician_filter({distance: 100, orderby: 'distance'}, austin_user).results.should == [austin_user] - end - - it "finds seattle when in range of austin" do - expected_results = [austin_user, dallas_user, miami_user, seattle_user] - - Search.musician_filter({distance: 2000, orderby: 'distance'}, austin_user).results.should == expected_results - Search.musician_filter({distance: 1500, orderby: 'distance'}, austin_user).results.should == [austin_user, dallas_user, miami_user] - Search.musician_filter({distance: 300, orderby: 'distance'}, austin_user).results.should == [austin_user, dallas_user] - Search.musician_filter({distance: 100, orderby: 'distance'}, austin_user).results.should == [austin_user] - end - - it "finds austin & dallas by user-specified location when in range" do - Search.musician_filter({distance: 500, orderby: 'distance', city: 'Austin', region: 'TX', country: 'US'}, austin_user).results.should == [austin_user, dallas_user] - end - - it "finds dallas & austin by user-specified location when in range" do - Search.musician_filter({distance: 500, orderby: 'distance', city: 'Dallas', region: 'TX', country: 'US'}, austin_user).results.should == [dallas_user, austin_user] - end - - it "finds miami user-specified location when in range" do - Search.musician_filter({distance: 300, orderby: 'distance', city: 'Tampa', region: 'FL', country: 'US'}, austin_user).results.should == [miami_user] - end - - it "finds all users with user-specified location when in range" do - Search.musician_filter({distance: 2500, orderby: 'distance', city: 'Tampa', region: 'FL', country: 'US'}, austin_user).results.should == [miami_user, dallas_user, austin_user, seattle_user] - end + it "sorts by distance" do + musician_search = MusicianSearch.create_search(searcher) + musician_search.update_json_value(MusicianSearch::KEY_SORT_ORDER, MusicianSearch::SORT_VALS[1]) + results = musician_search.do_search + expect(results[0].id).to eq(austin_user.id) + expect(results[1].id).to eq(dallas_user.id) + expect(results[2].id).to eq(miami_user.id) + expect(results[3].id).to eq(seattle_user.id) end end - describe "test set B" do - + describe "sort order by latency" do before(:each) do - # @geocode1 = FactoryGirl.create(:geocoder) - # @geocode2 = FactoryGirl.create(:geocoder) + User.delete_all t = Time.now - 10.minute @user1 = FactoryGirl.create(:user, created_at: t+1.minute, last_jam_locidispid: 1) @@ -92,13 +218,6 @@ describe 'Musician search' do @musicians << @user5 @musicians << @user6 - @geomusicians = [] - @geomusicians << @user1 - @geomusicians << @user2 - @geomusicians << @user3 - @geomusicians << @user4 - @geomusicians << @user5 - # from these scores: # user1 has scores other users in user1 location, and with user2, user3, user4 # user2 has scores with users in user3 and user4 location @@ -112,318 +231,80 @@ describe 'Musician search' do Score.createx(2, 'b', 2, 4, 'd', 4, 70) end - - context 'default filter settings' do - - it "finds all musicians" do - # expects all the musicians (geocoded) - results = Search.musician_filter({score_limit: Search::TEST_SCORE}) - results.search_type.should == :musicians_filter - results.results.count.should == @musicians.length - results.results.should eq @musicians.reverse - end - - it "finds all musicians page 1" do - # expects all the musicians - results = Search.musician_filter({page: 1, score_limit: Search::TEST_SCORE}) - results.search_type.should == :musicians_filter - results.results.count.should == @musicians.length - results.results.should eq @musicians.reverse - end - - it "finds all musicians page 2" do - # expects no musicians (all fit on page 1) - results = Search.musician_filter({page: 2, score_limit: Search::TEST_SCORE}) - results.search_type.should == :musicians_filter - results.results.count.should == 0 - end - - it "finds all musicians page 1 per_page 3" do - # expects three of the musicians - results = Search.musician_filter({per_page: 3, score_limit: Search::TEST_SCORE}) - results.search_type.should == :musicians_filter - results.results.count.should == 3 - results.results.should eq @musicians.reverse.slice(0, 3) - end - - it "finds all musicians page 2 per_page 3" do - # expects two of the musicians - results = Search.musician_filter({page: 2, per_page: 3, score_limit: Search::TEST_SCORE}) - results.search_type.should == :musicians_filter - results.results.count.should == 3 - results.results.should eq @musicians.reverse.slice(3, 3) - end - - it "finds all musicians page 3 per_page 3" do - # expects two of the musicians - results = Search.musician_filter({page: 3, per_page: 3, score_limit: Search::TEST_SCORE}) - results.search_type.should == :musicians_filter - results.results.count.should == 0 - end - - it "sorts musicians by followers" do - # establish sorting order - - # @user4 - f1 = Follow.new - f1.user = @user2 - f1.followable = @user4 - f1.save - - f2 = Follow.new - f2.user = @user3 - f2.followable = @user4 - f2.save - - f3 = Follow.new - f3.user = @user4 - f3.followable = @user4 - f3.save - - # @user3 - f4 = Follow.new - f4.user = @user3 - f4.followable = @user3 - f4.save - - f5 = Follow.new - f5.user = @user4 - f5.followable = @user3 - f5.save - - # @user2 - f6 = Follow.new - f6.user = @user1 - f6.followable = @user2 - f6.save - - # @user4.followers.concat([@user2, @user3, @user4]) - # @user3.followers.concat([@user3, @user4]) - # @user2.followers.concat([@user1]) - expect(@user4.followers.count).to be 3 - expect(Follow.count).to be 6 - - # refresh the order to ensure it works right - f1 = Follow.new - f1.user = @user3 - f1.followable = @user2 - f1.save - - f2 = Follow.new - f2.user = @user4 - f2.followable = @user2 - f2.save - - f3 = Follow.new - f3.user = @user2 - f3.followable = @user2 - f3.save - - # @user2.followers.concat([@user3, @user4, @user2]) - results = Search.musician_filter({:per_page => @musicians.size, score_limit: Search::TEST_SCORE, orderby: 'followed'}, @user3) - expect(results.results[0].id).to eq(@user2.id) - - # check the follower count for given entry - expect(results.results[0].search_follow_count.to_i).not_to eq(0) - # check the follow relationship between current_user and result - expect(results.is_follower?(@user2)).to be true - end - - it 'paginates properly' do - # make sure pagination works right - params = {:per_page => 2, :page => 1, score_limit: Search::TEST_SCORE} - results = Search.musician_filter(params) - expect(results.results.count).to be 2 - end - + it "sorts by latency" do + search.update_json_value(MusicianSearch::KEY_SORT_ORDER, MusicianSearch::SORT_VALS[0]) + results = search.do_search + expect(results[0].id).to eq(@user1.id) + expect(results[1].id).to eq(@user2.id) + expect(results[2].id).to eq(@user3.id) + expect(results[3].id).to eq(@user4.id) end - def make_recording(usr) - connection = FactoryGirl.create(:connection, :user => usr, locidispid: usr.last_jam_locidispid) - instrument = FactoryGirl.create(:instrument, :description => 'a great instrument') - track = FactoryGirl.create(:track, :connection => connection, :instrument => instrument) - music_session = FactoryGirl.create(:active_music_session, :creator => usr, :musician_access => true) - # music_session.connections << connection - # music_session.save - connection.join_the_session(music_session, true, nil, usr, 10) - recording = Recording.start(music_session, usr) - recording.stop - recording.reload - genre = FactoryGirl.create(:genre) - recording.claim(usr, "name", "description", genre, true) - recording.reload - recording - end - - def make_session(usr) - connection = FactoryGirl.create(:connection, :user => usr, locidispid: usr.last_jam_locidispid) - music_session = FactoryGirl.create(:active_music_session, :creator => usr, :musician_access => true) - # music_session.connections << connection - # music_session.save - connection.join_the_session(music_session, true, nil, usr, 10) - end - - context 'musician stat counters' do - - it "displays musicians top followings" do - f1 = Follow.new - f1.user = @user4 - f1.followable = @user4 - f1.save - - f2 = Follow.new - f2.user = @user4 - f2.followable = @user3 - f2.save - - f3 = Follow.new - f3.user = @user4 - f3.followable = @user2 - f3.save - - # @user4.followers.concat([@user4]) - # @user3.followers.concat([@user4]) - # @user2.followers.concat([@user4]) - expect(@user4.top_followings.count).to eq 3 - expect(@user4.top_followings.map(&:id)).to match_array((@musicians - [@user1, @user5, @user6]).map(&:id)) - end - - it "friends stat shows friend count" do - # create friendship record - Friendship.save(@user1.id, @user2.id) - # search on user2 - results = Search.musician_filter({score_limit: Search::TEST_SCORE}, @user2) - friend = results.results.detect { |mm| mm.id == @user1.id } - expect(friend).to_not be_nil - expect(results.friend_count(friend)).to be 1 - @user1.reload - expect(friend.friends?(@user2)).to be true - expect(results.is_friend?(@user1)).to be true - end - - it "recording stat shows recording count" do - Recording.delete_all - - recording = make_recording(@user1) - expect(recording.users.length).to be 1 - expect(recording.users.first).to eq(@user1) - @user1.reload - expect(@user1.recordings.length).to be 1 - expect(@user1.recordings.first).to eq(recording) - expect(recording.claimed_recordings.length).to be 1 - expect(@user1.recordings.detect { |rr| rr == recording }).to_not be_nil - - results = Search.musician_filter({score_limit: Search::TEST_SCORE}, @user1) - # puts "====================== results #{results.inspect}" - uu = results.results.detect { |mm| mm.id == @user1.id } - expect(uu).to_not be_nil - - expect(results.record_count(uu)).to be 1 - expect(results.session_count(uu)).to be 1 - end - - end - - context 'musician sorting' do - - it "by plays" do - Recording.delete_all - - make_recording(@user1) - # order results by num recordings - results = Search.musician_filter({orderby: 'plays', score_limit: Search::TEST_SCORE}, @user2) - # puts "========= results #{results.inspect}" - expect(results.results.length).to eq(2) - expect(results.results[0].id).to eq(@user1.id) - expect(results.results[1].id).to eq(@user3.id) - - # add more data and make sure order still correct - make_recording(@user3) - make_recording(@user3) - results = Search.musician_filter({:orderby => 'plays', score_limit: Search::TEST_SCORE}, @user2) - expect(results.results.length).to eq(2) - expect(results.results[0].id).to eq(@user3.id) - expect(results.results[1].id).to eq(@user1.id) - end - - it "by now playing" do - pending "these tests worked, so leaving them in, but we don't currently have 'Now Playing' in the find musicians screen" - # should get 1 result with 1 active session - make_session(@user1) - results = Search.musician_filter({:orderby => 'playing', score_limit: Search::TEST_SCORE}, @user2) - expect(results.results.count).to be 1 - expect(results.results.first.id).to eq(@user1.id) - - # should get 2 results with 2 active sessions - # sort order should be created_at DESC - make_session(@user3) - results = Search.musician_filter({:orderby => 'playing', score_limit: Search::TEST_SCORE}, @user2) - expect(results.results.count).to be 2 - expect(results.results[0].id).to eq(@user3.id) - expect(results.results[1].id).to eq(@user1.id) - end - - end - - context 'filter settings' do - it "searches musicians for an instrument" do - minst = FactoryGirl.create(:musician_instrument, { - :user => @user1, - :instrument => Instrument.find('tuba')}) - @user1.musician_instruments << minst - @user1.reload - ii = @user1.instruments.detect { |inst| inst.id == 'tuba' } - expect(ii).to_not be_nil - results = Search.musician_filter({:instrument => ii.id, score_limit: Search::TEST_SCORE}, @user2) - results.results.each do |rr| - expect(rr.instruments.detect { |inst| inst.id=='tuba' }.id).to eq(ii.id) - end - expect(results.results.count).to be 1 - end - end - - context 'new users' do - - it "find three for user1" do - # user2..4 are scored against user1 - ms = Search.new_musicians(@user1, Time.now - 1.week) - ms.should_not be_nil - ms.length.should == 3 - ms.should eq [@user2, @user3, @user4] - end - - it "find two for user2" do - # user1,3,4 are scored against user1, but user4 is bad - ms = Search.new_musicians(@user2, Time.now - 1.week) - ms.should_not be_nil - ms.length.should == 2 - ms.should eq [@user3, @user1] - end - - it "find two for user3" do - # user1..2 are scored against user3 - ms = Search.new_musicians(@user3, Time.now - 1.week) - ms.should_not be_nil - ms.length.should == 2 - ms.should eq [@user2, @user1] - end - - it "find one for user4" do - # user1..2 are scored against user4, but user2 is bad - ms = Search.new_musicians(@user4, Time.now - 1.week) - ms.should_not be_nil - ms.length.should == 1 - ms.should eq [@user1] - end - - it "find none for user5" do - # user1..4 are not scored against user5 - ms = Search.new_musicians(@user5, Time.now - 1.week) - ms.should_not be_nil - ms.length.should == 0 - end - - end end + + describe "produces accurate query description" do + it 'has default description' do + expect(search.description).to match(/^Click search button to look for musicians/) + end + + it 'has correct sort order description' do + search.update_json_value(MusicianSearch::KEY_SORT_ORDER, MusicianSearch::SORT_VALS[1]) + str = MusicianSearch::SORT_ORDERS[search.json_value(MusicianSearch::KEY_SORT_ORDER)] + expect(search.description).to match(/^Current Search: Sort = #{str}$/) + end + + it 'has correct description for single-valued selections' do + selections = [{ + key: MusicianSearch::KEY_INTERESTS, + value: MusicianSearch::INTEREST_VALS[1], + lookup: MusicianSearch::INTERESTS, + description: 'Interest' + }, + { + key: MusicianSearch::KEY_SKILL, + value: MusicianSearch::SKILL_VALS[1], + lookup: MusicianSearch::SKILL_LEVELS, + description: 'Skill' + }, + { + key: MusicianSearch::KEY_STUDIOS, + value: MusicianSearch::STUDIO_COUNTS[1], + lookup: MusicianSearch::STUDIOS_LABELS, + description: 'Studio Sessions' + }, + { + key: MusicianSearch::KEY_GIGS, + value: MusicianSearch::GIG_COUNTS[1], + lookup: MusicianSearch::GIG_LABELS, + description: 'Concert Gigs' + }] + selections.each do |hash| + search.update_json_value(hash[:key], hash[:value]) + json_val = search.json_value(hash[:key]) + expect(search.description).to match(/; #{hash[:description]} = #{hash[:lookup][json_val]}/) + end + end + + it 'has correct description for genres' do + search.update_json_value(MusicianSearch::KEY_GENRES, [Genre.first.id, Genre.last.id]) + expect(search.description).to match(/; Genres = #{Genre.first.description}, #{Genre.last.description}/) + end + + it 'has correct description for ages' do + search.update_json_value(MusicianSearch::KEY_AGES, [MusicianSearch::AGE_COUNTS[0],MusicianSearch::AGE_COUNTS[1]]) + expect(search.description).to match(/; Ages = #{MusicianSearch::AGES[MusicianSearch::AGE_COUNTS[0]]}, #{MusicianSearch::AGES[MusicianSearch::AGE_COUNTS[1]]}/) + end + + it 'has correct description for instruments' do + instrs = Instrument.limit(2).order(:description) + instjson = [{ instrument_id: instrs[0].id, proficiency_level: 2 }, + { instrument_id: instrs[1].id, proficiency_level: 1 } + ] + search.update_json_value(MusicianSearch::KEY_INSTRUMENTS, instjson) + instr_descrip = "#{instrs[0].description} (#{MusicianSearch::INSTRUMENT_PROFICIENCY[2]}), #{instrs[1].description} (#{MusicianSearch::INSTRUMENT_PROFICIENCY[1]})" + expect(search.description).to match(/; Instruments = #{Regexp.escape(instr_descrip)}/) + end + + end end