diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index ed67862..73c9d81 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -575,4 +575,16 @@ class ApplicationControllerTest < ActionController::TestCase end end + should 'allow plugin to propose search terms suggestions' do + class SuggestionsPlugin < Noosfero::Plugin + def find_suggestions(query, context, asset, options={:limit => 5}) + ['a', 'b', 'c'] + end + end + + controller = ApplicationController.new + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([SuggestionsPlugin.new]) + + assert_equal ['a', 'b', 'c'], controller.send(:find_suggestions, 'random', Environment.default, 'random') + end end diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 7d00890..0cf7c9b 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -662,6 +662,14 @@ class SearchControllerTest < ActionController::TestCase assert_no_tag :tag => 'li', :attributes => { :class => 'search-product-item highlighted' }, :content => /Holier Than Thou/ end + should 'get search suggestions on json' do + st1 = 'universe A' + st2 = 'universe B' + @controller.stubs(:find_suggestions).with('universe', Environment.default, 'communities').returns([st1, st2]) + get :suggestions, :term => 'universe', :asset => 'communities' + assert_equal [st1,st2].to_json, response.body + end + protected def create_event(profile, options) diff --git a/test/unit/plugin_test.rb b/test/unit/plugin_test.rb index 1db0350..d58b21d 100644 --- a/test/unit/plugin_test.rb +++ b/test/unit/plugin_test.rb @@ -508,11 +508,56 @@ class PluginTest < ActiveSupport::TestCase assert_equal [], plugin.check_comment_actions(nil) end - should 'check_comment_actions be an empty array by default' do class SomePlugin < Noosfero::Plugin; end plugin = SomePlugin.new assert_equal [], plugin.check_comment_actions(Comment.new) end + should 'have default search suggestions based on search terms' do + context = Environment.default + st1 = SearchTerm.new(:term => 'universe', :asset => 'science', :context => context) + st1.score = 3 + st1.save! + st2 = SearchTerm.new(:term => 'unisound', :asset => 'science', :context => context) + st2.score = 5 + st2.save! + st3 = SearchTerm.new(:term => 'unicrowd', :asset => 'science', :context => context) + st3.score = 2 + st3.save! + st4 = SearchTerm.new(:term => 'univault', :asset => 'science', :context => context) + st4.score = 8 + st4.save! + # Query not match + st5 = SearchTerm.create!(:term => 'destroyer', :asset => 'science', :context => context) + st5.score = 8 + st5.save! + # Different asset + st6 = SearchTerm.create!(:term => 'unilight', :asset => 'pseudo-science', :context => context) + st6.score = 8 + st6.save! + # Score not bigger than zero + st7 = SearchTerm.create!(:term => 'unicloud', :asset => 'science', :context => context) + st7.score = 0 + st7.save! + + class SomePlugin < Noosfero::Plugin; end + plugin = SomePlugin.new + + suggestions = plugin.find_suggestions('uni', context, 'science') + + assert_includes suggestions, st1.term + assert_includes suggestions, st2.term + assert_includes suggestions, st3.term + assert_includes suggestions, st4.term + assert_not_includes suggestions, st5.term + assert_not_includes suggestions, st6.term + assert_not_includes suggestions, st7.term + + assert_order [st4.term, st2.term, st1.term, st3.term], suggestions + + limited_suggestions = plugin.find_suggestions('uni', context, 'science', {:limit => 2}) + assert_equivalent [st4.term, st2.term], limited_suggestions + end + end diff --git a/test/unit/search_term_test.rb b/test/unit/search_term_test.rb index 9cba835..2fb6284 100644 --- a/test/unit/search_term_test.rb +++ b/test/unit/search_term_test.rb @@ -49,13 +49,22 @@ class SearchTermTest < ActiveSupport::TestCase should 'calculate score' do search_term = SearchTerm.find_or_create('universe', Environment.default) SearchTermOccurrence.create!(:search_term => search_term, :total => 10, :indexed => 3) - # Search term must happens at least two times to be considered - SearchTermOccurrence.create!(:search_term => search_term, :total => 10, :indexed => 3) SearchTerm.calculate_scores search_term.reload assert search_term.score > 0, "Score was not calculated." end + should 'have different scores for the same term with different assets' do + st1 = SearchTerm.find_or_create('universe', Environment.default, 'a') + st2 = SearchTerm.find_or_create('universe', Environment.default, 'b') + SearchTermOccurrence.create!(:search_term => st1, :total => 10, :indexed => 3) + SearchTermOccurrence.create!(:search_term => st2, :total => 10, :indexed => 8) + SearchTerm.calculate_scores + st1.reload + st2.reload + assert st1.score != st2.score, "Same term with different assets can have different scores." + end + should 'not consider expired occurrences to calculate the score' do search_term = SearchTerm.find_or_create('universe', Environment.default) occurrence = SearchTermOccurrence.create!(:search_term => search_term, :total => 10, :indexed => 3, :created_at => DateTime.now - (SearchTermOccurrence::EXPIRATION_TIME + 1.day)) @@ -82,7 +91,7 @@ class SearchTermTest < ActiveSupport::TestCase assert st2.score > 0, "Did not calculate st2 score." end - should 'the older the occurrence the less it should influence the score' do + should 'consider the older the occurrence less it should influence the score' do st1 = SearchTerm.find_or_create('st1', Environment.default) SearchTermOccurrence.create!(:search_term => st1, :total => 10, :indexed => 3, :created_at => 1.month.ago) SearchTermOccurrence.create!(:search_term => st1, :total => 20, :indexed => 8, :created_at => 1.month.ago) @@ -97,4 +106,16 @@ class SearchTermTest < ActiveSupport::TestCase assert st1.score > st2.score, "Older occurrences are not influencing score less than newer ones." end + should 'consider higher relevance if the ratio results:total is smaller' do + st1 = SearchTerm.find_or_create('st1', Environment.default) + SearchTermOccurrence.create!(:search_term => st1, :total => 10, :indexed => 4) + st2 = SearchTerm.find_or_create('st2', Environment.default) + SearchTermOccurrence.create!(:search_term => st2, :total => 10, :indexed => 5) + + SearchTerm.calculate_scores + st1.reload + st2.reload + + assert st1.score > st2.score, "Less ratio results:total are not getting higher scores." + end end -- libgit2 0.21.2