Commit a37f5e01e4b611dc0b44a43599aa75c730e97711
1 parent
20ed507f
Exists in
master
and in
22 other branches
[search-improvements] Improve test coverage
Showing
4 changed files
with
90 additions
and
4 deletions
Show diff stats
test/functional/application_controller_test.rb
@@ -575,4 +575,16 @@ class ApplicationControllerTest < ActionController::TestCase | @@ -575,4 +575,16 @@ class ApplicationControllerTest < ActionController::TestCase | ||
575 | end | 575 | end |
576 | end | 576 | end |
577 | 577 | ||
578 | + should 'allow plugin to propose search terms suggestions' do | ||
579 | + class SuggestionsPlugin < Noosfero::Plugin | ||
580 | + def find_suggestions(query, context, asset, options={:limit => 5}) | ||
581 | + ['a', 'b', 'c'] | ||
582 | + end | ||
583 | + end | ||
584 | + | ||
585 | + controller = ApplicationController.new | ||
586 | + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([SuggestionsPlugin.new]) | ||
587 | + | ||
588 | + assert_equal ['a', 'b', 'c'], controller.send(:find_suggestions, 'random', Environment.default, 'random') | ||
589 | + end | ||
578 | end | 590 | end |
test/functional/search_controller_test.rb
@@ -662,6 +662,14 @@ class SearchControllerTest < ActionController::TestCase | @@ -662,6 +662,14 @@ class SearchControllerTest < ActionController::TestCase | ||
662 | assert_no_tag :tag => 'li', :attributes => { :class => 'search-product-item highlighted' }, :content => /Holier Than Thou/ | 662 | assert_no_tag :tag => 'li', :attributes => { :class => 'search-product-item highlighted' }, :content => /Holier Than Thou/ |
663 | end | 663 | end |
664 | 664 | ||
665 | + should 'get search suggestions on json' do | ||
666 | + st1 = 'universe A' | ||
667 | + st2 = 'universe B' | ||
668 | + @controller.stubs(:find_suggestions).with('universe', Environment.default, 'communities').returns([st1, st2]) | ||
669 | + get :suggestions, :term => 'universe', :asset => 'communities' | ||
670 | + assert_equal [st1,st2].to_json, response.body | ||
671 | + end | ||
672 | + | ||
665 | protected | 673 | protected |
666 | 674 | ||
667 | def create_event(profile, options) | 675 | def create_event(profile, options) |
test/unit/plugin_test.rb
@@ -508,11 +508,56 @@ class PluginTest < ActiveSupport::TestCase | @@ -508,11 +508,56 @@ class PluginTest < ActiveSupport::TestCase | ||
508 | assert_equal [], plugin.check_comment_actions(nil) | 508 | assert_equal [], plugin.check_comment_actions(nil) |
509 | end | 509 | end |
510 | 510 | ||
511 | - | ||
512 | should 'check_comment_actions be an empty array by default' do | 511 | should 'check_comment_actions be an empty array by default' do |
513 | class SomePlugin < Noosfero::Plugin; end | 512 | class SomePlugin < Noosfero::Plugin; end |
514 | plugin = SomePlugin.new | 513 | plugin = SomePlugin.new |
515 | assert_equal [], plugin.check_comment_actions(Comment.new) | 514 | assert_equal [], plugin.check_comment_actions(Comment.new) |
516 | end | 515 | end |
517 | 516 | ||
517 | + should 'have default search suggestions based on search terms' do | ||
518 | + context = Environment.default | ||
519 | + st1 = SearchTerm.new(:term => 'universe', :asset => 'science', :context => context) | ||
520 | + st1.score = 3 | ||
521 | + st1.save! | ||
522 | + st2 = SearchTerm.new(:term => 'unisound', :asset => 'science', :context => context) | ||
523 | + st2.score = 5 | ||
524 | + st2.save! | ||
525 | + st3 = SearchTerm.new(:term => 'unicrowd', :asset => 'science', :context => context) | ||
526 | + st3.score = 2 | ||
527 | + st3.save! | ||
528 | + st4 = SearchTerm.new(:term => 'univault', :asset => 'science', :context => context) | ||
529 | + st4.score = 8 | ||
530 | + st4.save! | ||
531 | + # Query not match | ||
532 | + st5 = SearchTerm.create!(:term => 'destroyer', :asset => 'science', :context => context) | ||
533 | + st5.score = 8 | ||
534 | + st5.save! | ||
535 | + # Different asset | ||
536 | + st6 = SearchTerm.create!(:term => 'unilight', :asset => 'pseudo-science', :context => context) | ||
537 | + st6.score = 8 | ||
538 | + st6.save! | ||
539 | + # Score not bigger than zero | ||
540 | + st7 = SearchTerm.create!(:term => 'unicloud', :asset => 'science', :context => context) | ||
541 | + st7.score = 0 | ||
542 | + st7.save! | ||
543 | + | ||
544 | + class SomePlugin < Noosfero::Plugin; end | ||
545 | + plugin = SomePlugin.new | ||
546 | + | ||
547 | + suggestions = plugin.find_suggestions('uni', context, 'science') | ||
548 | + | ||
549 | + assert_includes suggestions, st1.term | ||
550 | + assert_includes suggestions, st2.term | ||
551 | + assert_includes suggestions, st3.term | ||
552 | + assert_includes suggestions, st4.term | ||
553 | + assert_not_includes suggestions, st5.term | ||
554 | + assert_not_includes suggestions, st6.term | ||
555 | + assert_not_includes suggestions, st7.term | ||
556 | + | ||
557 | + assert_order [st4.term, st2.term, st1.term, st3.term], suggestions | ||
558 | + | ||
559 | + limited_suggestions = plugin.find_suggestions('uni', context, 'science', {:limit => 2}) | ||
560 | + assert_equivalent [st4.term, st2.term], limited_suggestions | ||
561 | + end | ||
562 | + | ||
518 | end | 563 | end |
test/unit/search_term_test.rb
@@ -49,13 +49,22 @@ class SearchTermTest < ActiveSupport::TestCase | @@ -49,13 +49,22 @@ class SearchTermTest < ActiveSupport::TestCase | ||
49 | should 'calculate score' do | 49 | should 'calculate score' do |
50 | search_term = SearchTerm.find_or_create('universe', Environment.default) | 50 | search_term = SearchTerm.find_or_create('universe', Environment.default) |
51 | SearchTermOccurrence.create!(:search_term => search_term, :total => 10, :indexed => 3) | 51 | SearchTermOccurrence.create!(:search_term => search_term, :total => 10, :indexed => 3) |
52 | - # Search term must happens at least two times to be considered | ||
53 | - SearchTermOccurrence.create!(:search_term => search_term, :total => 10, :indexed => 3) | ||
54 | SearchTerm.calculate_scores | 52 | SearchTerm.calculate_scores |
55 | search_term.reload | 53 | search_term.reload |
56 | assert search_term.score > 0, "Score was not calculated." | 54 | assert search_term.score > 0, "Score was not calculated." |
57 | end | 55 | end |
58 | 56 | ||
57 | + should 'have different scores for the same term with different assets' do | ||
58 | + st1 = SearchTerm.find_or_create('universe', Environment.default, 'a') | ||
59 | + st2 = SearchTerm.find_or_create('universe', Environment.default, 'b') | ||
60 | + SearchTermOccurrence.create!(:search_term => st1, :total => 10, :indexed => 3) | ||
61 | + SearchTermOccurrence.create!(:search_term => st2, :total => 10, :indexed => 8) | ||
62 | + SearchTerm.calculate_scores | ||
63 | + st1.reload | ||
64 | + st2.reload | ||
65 | + assert st1.score != st2.score, "Same term with different assets can have different scores." | ||
66 | + end | ||
67 | + | ||
59 | should 'not consider expired occurrences to calculate the score' do | 68 | should 'not consider expired occurrences to calculate the score' do |
60 | search_term = SearchTerm.find_or_create('universe', Environment.default) | 69 | search_term = SearchTerm.find_or_create('universe', Environment.default) |
61 | occurrence = SearchTermOccurrence.create!(:search_term => search_term, :total => 10, :indexed => 3, :created_at => DateTime.now - (SearchTermOccurrence::EXPIRATION_TIME + 1.day)) | 70 | 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 | @@ -82,7 +91,7 @@ class SearchTermTest < ActiveSupport::TestCase | ||
82 | assert st2.score > 0, "Did not calculate st2 score." | 91 | assert st2.score > 0, "Did not calculate st2 score." |
83 | end | 92 | end |
84 | 93 | ||
85 | - should 'the older the occurrence the less it should influence the score' do | 94 | + should 'consider the older the occurrence less it should influence the score' do |
86 | st1 = SearchTerm.find_or_create('st1', Environment.default) | 95 | st1 = SearchTerm.find_or_create('st1', Environment.default) |
87 | SearchTermOccurrence.create!(:search_term => st1, :total => 10, :indexed => 3, :created_at => 1.month.ago) | 96 | SearchTermOccurrence.create!(:search_term => st1, :total => 10, :indexed => 3, :created_at => 1.month.ago) |
88 | SearchTermOccurrence.create!(:search_term => st1, :total => 20, :indexed => 8, :created_at => 1.month.ago) | 97 | SearchTermOccurrence.create!(:search_term => st1, :total => 20, :indexed => 8, :created_at => 1.month.ago) |
@@ -97,4 +106,16 @@ class SearchTermTest < ActiveSupport::TestCase | @@ -97,4 +106,16 @@ class SearchTermTest < ActiveSupport::TestCase | ||
97 | assert st1.score > st2.score, "Older occurrences are not influencing score less than newer ones." | 106 | assert st1.score > st2.score, "Older occurrences are not influencing score less than newer ones." |
98 | end | 107 | end |
99 | 108 | ||
109 | + should 'consider higher relevance if the ratio results:total is smaller' do | ||
110 | + st1 = SearchTerm.find_or_create('st1', Environment.default) | ||
111 | + SearchTermOccurrence.create!(:search_term => st1, :total => 10, :indexed => 4) | ||
112 | + st2 = SearchTerm.find_or_create('st2', Environment.default) | ||
113 | + SearchTermOccurrence.create!(:search_term => st2, :total => 10, :indexed => 5) | ||
114 | + | ||
115 | + SearchTerm.calculate_scores | ||
116 | + st1.reload | ||
117 | + st2.reload | ||
118 | + | ||
119 | + assert st1.score > st2.score, "Less ratio results:total are not getting higher scores." | ||
120 | + end | ||
100 | end | 121 | end |