Commit 24f9fb1f9ff4677593e6a66151e19514ab442634
1 parent
c3406ceb
Exists in
elasticsearch_filter
and in
1 other branch
Adding unit tests for sort
Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
12 changed files
with
158 additions
and
20 deletions
Show diff stats
plugins/elasticsearch/helpers/elasticsearch_helper.rb
| @@ -35,15 +35,20 @@ module ElasticsearchHelper | @@ -35,15 +35,20 @@ module ElasticsearchHelper | ||
| 35 | private | 35 | private |
| 36 | 36 | ||
| 37 | def search_from_all_models | 37 | def search_from_all_models |
| 38 | - query = get_query params[:query], sort_by: get_sort_by(params[:filter]) | ||
| 39 | - Elasticsearch::Model.search(query,searchable_models, size: default_per_page(params[:per_page])).page(params[:page]).records | 38 | + begin |
| 39 | + filter = (params[:filter] || "" ).to_sym | ||
| 40 | + query = get_query params[:query], sort_by: get_sort_by(filter) | ||
| 41 | + Elasticsearch::Model.search(query,searchable_models, size: default_per_page(params[:per_page])).page(params[:page]).records | ||
| 42 | + rescue | ||
| 43 | + [] | ||
| 44 | + end | ||
| 40 | end | 45 | end |
| 41 | 46 | ||
| 42 | def search_from_model model | 47 | def search_from_model model |
| 43 | begin | 48 | begin |
| 44 | klass = model.to_s.classify.constantize | 49 | klass = model.to_s.classify.constantize |
| 45 | - | ||
| 46 | - query = get_query params[:query], klass: klass, sort_by: get_sort_by(params[:filter],klass) | 50 | + filter = (params[:filter] || "" ).to_sym |
| 51 | + query = get_query params[:query], klass: klass, sort_by: get_sort_by(filter,klass) | ||
| 47 | klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records | 52 | klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records |
| 48 | rescue | 53 | rescue |
| 49 | [] | 54 | [] |
| @@ -56,9 +61,9 @@ module ElasticsearchHelper | @@ -56,9 +61,9 @@ module ElasticsearchHelper | ||
| 56 | 61 | ||
| 57 | def get_sort_by sort_by, klass=nil | 62 | def get_sort_by sort_by, klass=nil |
| 58 | case sort_by | 63 | case sort_by |
| 59 | - when "lexical" | 64 | + when :lexical |
| 60 | { "name.raw" => {"order" => "asc" }} | 65 | { "name.raw" => {"order" => "asc" }} |
| 61 | - when "more_recent" | 66 | + when :more_recent |
| 62 | { "created_at" => {"order" => "desc"}} | 67 | { "created_at" => {"order" => "desc"}} |
| 63 | else | 68 | else |
| 64 | ( klass and klass.respond_to?(:get_sort_by) ) ? klass.get_sort_by(sort_by) : nil | 69 | ( klass and klass.respond_to?(:get_sort_by) ) ? klass.get_sort_by(sort_by) : nil |
| @@ -105,6 +110,7 @@ module ElasticsearchHelper | @@ -105,6 +110,7 @@ module ElasticsearchHelper | ||
| 105 | 110 | ||
| 106 | query = query_method(text, models) | 111 | query = query_method(text, models) |
| 107 | query[:sort] = sort_by if sort_by | 112 | query[:sort] = sort_by if sort_by |
| 113 | + | ||
| 108 | query | 114 | query |
| 109 | end | 115 | end |
| 110 | 116 |
plugins/elasticsearch/lib/ext/community.rb
| @@ -30,12 +30,12 @@ class Community | @@ -30,12 +30,12 @@ class Community | ||
| 30 | } | 30 | } |
| 31 | end | 31 | end |
| 32 | 32 | ||
| 33 | - def self.get_sort_by sort_by | 33 | + def self.get_sort_by sort_by="" |
| 34 | case sort_by | 34 | case sort_by |
| 35 | - when "more_active" | 35 | + when :more_active |
| 36 | { :activities_count => {order: :desc}} | 36 | { :activities_count => {order: :desc}} |
| 37 | - when "more_popular" | ||
| 38 | - { :members_count => {order: :desc} } | 37 | + when :more_popular |
| 38 | + { :members_count => {order: :desc}} | ||
| 39 | end | 39 | end |
| 40 | end | 40 | end |
| 41 | 41 |
plugins/elasticsearch/lib/ext/person.rb
| @@ -31,11 +31,11 @@ class Person | @@ -31,11 +31,11 @@ class Person | ||
| 31 | } | 31 | } |
| 32 | end | 32 | end |
| 33 | 33 | ||
| 34 | - def self.get_sort_by sort_by | 34 | + def self.get_sort_by sort_by="" |
| 35 | case sort_by | 35 | case sort_by |
| 36 | - when "more_active" | 36 | + when :more_active |
| 37 | { :activities_count => {order: :desc}} | 37 | { :activities_count => {order: :desc}} |
| 38 | - when "more_popular" | 38 | + when :more_popular |
| 39 | { :friends_count => {order: :desc} } | 39 | { :friends_count => {order: :desc} } |
| 40 | end | 40 | end |
| 41 | end | 41 | end |
plugins/elasticsearch/lib/ext/text_article.rb
| @@ -37,15 +37,15 @@ class TextArticle | @@ -37,15 +37,15 @@ class TextArticle | ||
| 37 | def self.especific_sort | 37 | def self.especific_sort |
| 38 | { | 38 | { |
| 39 | :more_popular => { label: _("More Viewed") }, | 39 | :more_popular => { label: _("More Viewed") }, |
| 40 | - :more_comments => { label: _("More Commented") } | 40 | + :more_comments => { label: _("Most Commented") } |
| 41 | } | 41 | } |
| 42 | end | 42 | end |
| 43 | 43 | ||
| 44 | - def self.get_sort_by sort_by | 44 | + def self.get_sort_by sort_by="" |
| 45 | case sort_by | 45 | case sort_by |
| 46 | - when "more_popular" | 46 | + when :more_popular |
| 47 | { :hits => {order: :desc} } | 47 | { :hits => {order: :desc} } |
| 48 | - when "more_comments" | 48 | + when :more_comments |
| 49 | { :comments_count => {order: :desc}} | 49 | { :comments_count => {order: :desc}} |
| 50 | end | 50 | end |
| 51 | end | 51 | end |
plugins/elasticsearch/lib/searchable_model/filter.rb
plugins/elasticsearch/test/functional/elasticsearch_plugin_controller_test.rb
| @@ -9,6 +9,20 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase | @@ -9,6 +9,20 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase | ||
| 9 | end | 9 | end |
| 10 | 10 | ||
| 11 | def create_instances | 11 | def create_instances |
| 12 | + create_instances_environment | ||
| 13 | + create_instances_environment2 | ||
| 14 | + end | ||
| 15 | + | ||
| 16 | + def create_instances_environment2 | ||
| 17 | + 5.times do |index| | ||
| 18 | + create_user "Sample User Environment 2", environment:Environment.second | ||
| 19 | + end | ||
| 20 | + 6.times do |index| | ||
| 21 | + fast_create Community, name:"Sample Community Environment 2", created_at: Date.new, environment_id: Environment.second.id | ||
| 22 | + end | ||
| 23 | + end | ||
| 24 | + | ||
| 25 | + def create_instances_environment | ||
| 12 | create_people | 26 | create_people |
| 13 | create_communities | 27 | create_communities |
| 14 | end | 28 | end |
| @@ -73,7 +87,7 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase | @@ -73,7 +87,7 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase | ||
| 73 | assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} ) | 87 | assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} ) |
| 74 | end | 88 | end |
| 75 | 89 | ||
| 76 | - should 'return new person indexed' do | 90 | + should 'return new community indexed' do |
| 77 | get :index, { "selected_type" => :community} | 91 | get :index, { "selected_type" => :community} |
| 78 | assert_response :success | 92 | assert_response :success |
| 79 | assert_select ".search-item", 6 | 93 | assert_select ".search-item", 6 |
| @@ -87,13 +101,14 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase | @@ -87,13 +101,14 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase | ||
| 87 | assert_select ".search-item", 7 | 101 | assert_select ".search-item", 7 |
| 88 | end | 102 | end |
| 89 | 103 | ||
| 90 | - should 'not return person deleted' do | 104 | + should 'not return community deleted' do |
| 91 | get :index, { "selected_type" => :community} | 105 | get :index, { "selected_type" => :community} |
| 92 | assert_response :success | 106 | assert_response :success |
| 93 | assert_select ".search-item", 6 | 107 | assert_select ".search-item", 6 |
| 94 | 108 | ||
| 95 | Community.first.delete | 109 | Community.first.delete |
| 96 | Community.import | 110 | Community.import |
| 111 | + sleep 2 | ||
| 97 | 112 | ||
| 98 | get :index, { "selected_type" => :community} | 113 | get :index, { "selected_type" => :community} |
| 99 | assert_response :success | 114 | assert_response :success |
| @@ -112,4 +127,17 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase | @@ -112,4 +127,17 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase | ||
| 112 | assert_not_nil assigns(:results) | 127 | assert_not_nil assigns(:results) |
| 113 | assert_template partial: '_community_display' | 128 | assert_template partial: '_community_display' |
| 114 | end | 129 | end |
| 130 | + | ||
| 131 | + should 'filter community by default environment' do | ||
| 132 | + get :index, { "selected_type" => :community} | ||
| 133 | + assert_response :success | ||
| 134 | + assert_select ".search-item", 6 | ||
| 135 | + end | ||
| 136 | + | ||
| 137 | + should 'filter person by default environment' do | ||
| 138 | + get :index, { "selected_type" => :person} | ||
| 139 | + assert_response :success | ||
| 140 | + assert_select ".search-item", 5 | ||
| 141 | + end | ||
| 142 | + | ||
| 115 | end | 143 | end |
plugins/elasticsearch/test/unit/community_test.rb
| @@ -21,4 +21,26 @@ class CommunityTest < ActiveSupport::TestCase | @@ -21,4 +21,26 @@ class CommunityTest < ActiveSupport::TestCase | ||
| 21 | end | 21 | end |
| 22 | end | 22 | end |
| 23 | 23 | ||
| 24 | + should 'respond with should method to return public community' do | ||
| 25 | + assert Community.respond_to? :should | ||
| 26 | + end | ||
| 27 | + | ||
| 28 | + should 'respond with especific sort' do | ||
| 29 | + assert Community.respond_to? :especific_sort | ||
| 30 | + end | ||
| 31 | + | ||
| 32 | + should 'respond with get_sort_by to order especific sort' do | ||
| 33 | + assert Community.respond_to? :get_sort_by | ||
| 34 | + end | ||
| 35 | + | ||
| 36 | + should 'return hash to sort by more_active' do | ||
| 37 | + more_active_hash = {:activities_count => {order: :desc}} | ||
| 38 | + assert_equal more_active_hash, Community.get_sort_by(:more_active) | ||
| 39 | + end | ||
| 40 | + | ||
| 41 | + should 'return hash to sort by more_popular' do | ||
| 42 | + more_popular_hash = {:members_count => {order: :desc}} | ||
| 43 | + assert_equal more_popular_hash, Community.get_sort_by(:more_popular) | ||
| 44 | + end | ||
| 45 | + | ||
| 24 | end | 46 | end |
plugins/elasticsearch/test/unit/elasticsearch_helper_test.rb
| @@ -72,4 +72,9 @@ class ElasticsearchHelperTest < ActiveSupport::TestCase | @@ -72,4 +72,9 @@ class ElasticsearchHelperTest < ActiveSupport::TestCase | ||
| 72 | assert_equal ["Joana Abreu", "Joao Abreu"], result.map(&:name) | 72 | assert_equal ["Joana Abreu", "Joao Abreu"], result.map(&:name) |
| 73 | end | 73 | end |
| 74 | 74 | ||
| 75 | + should 'have sort in get_query return if has the option sort_by ' do | ||
| 76 | + self.params= {} | ||
| 77 | + assert get_query("", sort_by: :more_popular).keys.include?(:sort) | ||
| 78 | + end | ||
| 79 | + | ||
| 75 | end | 80 | end |
plugins/elasticsearch/test/unit/event_test.rb
| 1 | require "#{File.dirname(__FILE__)}/../test_helper" | 1 | require "#{File.dirname(__FILE__)}/../test_helper" |
| 2 | +require_relative '../../lib/nested_helper/profile' | ||
| 2 | 3 | ||
| 3 | class EventTest < ActionController::TestCase | 4 | class EventTest < ActionController::TestCase |
| 4 | 5 | ||
| @@ -21,4 +22,16 @@ class EventTest < ActionController::TestCase | @@ -21,4 +22,16 @@ class EventTest < ActionController::TestCase | ||
| 21 | end | 22 | end |
| 22 | end | 23 | end |
| 23 | 24 | ||
| 25 | + should 'respond with should method to return public event' do | ||
| 26 | + assert Event.respond_to? :should | ||
| 27 | + end | ||
| 28 | + | ||
| 29 | + should 'respond with nested_filter' do | ||
| 30 | + assert Event.respond_to? :nested_filter | ||
| 31 | + end | ||
| 32 | + | ||
| 33 | + should 'have NestedProfile_filter in nested_filter' do | ||
| 34 | + assert Event.nested_filter.include? NestedProfile.filter | ||
| 35 | + end | ||
| 36 | + | ||
| 24 | end | 37 | end |
plugins/elasticsearch/test/unit/person_test.rb
| @@ -20,4 +20,26 @@ class PersonTest < ActionController::TestCase | @@ -20,4 +20,26 @@ class PersonTest < ActionController::TestCase | ||
| 20 | assert_equal indexed_fields(Person)[key][:type], value[:type] || 'string' | 20 | assert_equal indexed_fields(Person)[key][:type], value[:type] || 'string' |
| 21 | end | 21 | end |
| 22 | end | 22 | end |
| 23 | + | ||
| 24 | + should 'respond with should method to return public person' do | ||
| 25 | + assert Person.respond_to? :should | ||
| 26 | + end | ||
| 27 | + | ||
| 28 | + should 'respond with especific sort' do | ||
| 29 | + assert Person.respond_to? :especific_sort | ||
| 30 | + end | ||
| 31 | + | ||
| 32 | + should 'respond with get_sort_by to order especific sort' do | ||
| 33 | + assert Person.respond_to? :get_sort_by | ||
| 34 | + end | ||
| 35 | + | ||
| 36 | + should 'return hash to sort by more_active' do | ||
| 37 | + more_active_hash = {:activities_count => {order: :desc}} | ||
| 38 | + assert_equal more_active_hash, Person.get_sort_by(:more_active) | ||
| 39 | + end | ||
| 40 | + | ||
| 41 | + should 'return hash to sort by more_popular' do | ||
| 42 | + more_popular_hash = {:friends_count => {order: :desc}} | ||
| 43 | + assert_equal more_popular_hash, Person.get_sort_by(:more_popular) | ||
| 44 | + end | ||
| 23 | end | 45 | end |
plugins/elasticsearch/test/unit/text_article_test.rb
| 1 | require "#{File.dirname(__FILE__)}/../test_helper" | 1 | require "#{File.dirname(__FILE__)}/../test_helper" |
| 2 | +require_relative '../../lib/nested_helper/profile' | ||
| 2 | 3 | ||
| 3 | class TextArticleTest < ActionController::TestCase | 4 | class TextArticleTest < ActionController::TestCase |
| 4 | 5 | ||
| @@ -21,4 +22,34 @@ class TextArticleTest < ActionController::TestCase | @@ -21,4 +22,34 @@ class TextArticleTest < ActionController::TestCase | ||
| 21 | end | 22 | end |
| 22 | end | 23 | end |
| 23 | 24 | ||
| 25 | + should 'respond with should method to return public text_article' do | ||
| 26 | + assert TextArticle.respond_to? :should | ||
| 27 | + end | ||
| 28 | + | ||
| 29 | + should 'respond with especific sort' do | ||
| 30 | + assert TextArticle.respond_to? :especific_sort | ||
| 31 | + end | ||
| 32 | + | ||
| 33 | + should 'respond with get_sort_by to order especific sort' do | ||
| 34 | + assert TextArticle.respond_to? :get_sort_by | ||
| 35 | + end | ||
| 36 | + | ||
| 37 | + should 'return hash to sort by most commented' do | ||
| 38 | + more_active_hash = {:comments_count => {order: :desc}} | ||
| 39 | + assert_equal more_active_hash, TextArticle.get_sort_by(:more_comments) | ||
| 40 | + end | ||
| 41 | + | ||
| 42 | + should 'return hash to sort by more popular' do | ||
| 43 | + more_popular_hash = {:hits => {order: :desc}} | ||
| 44 | + assert_equal more_popular_hash, TextArticle.get_sort_by(:more_popular) | ||
| 45 | + end | ||
| 46 | + | ||
| 47 | + should 'respond with nested_filter' do | ||
| 48 | + assert TextArticle.respond_to? :nested_filter | ||
| 49 | + end | ||
| 50 | + | ||
| 51 | + should 'have NestedProfile_filter in nested_filter' do | ||
| 52 | + assert TextArticle.nested_filter.include? NestedProfile.filter | ||
| 53 | + end | ||
| 54 | + | ||
| 24 | end | 55 | end |
plugins/elasticsearch/test/unit/uploaded_file_test.rb
| @@ -21,4 +21,16 @@ class UploadedFileTest < ActionController::TestCase | @@ -21,4 +21,16 @@ class UploadedFileTest < ActionController::TestCase | ||
| 21 | end | 21 | end |
| 22 | end | 22 | end |
| 23 | 23 | ||
| 24 | + should 'respond with should method to return public text_article' do | ||
| 25 | + assert TextArticle.respond_to? :should | ||
| 26 | + end | ||
| 27 | + | ||
| 28 | + should 'respond with nested_filter' do | ||
| 29 | + assert TextArticle.respond_to? :nested_filter | ||
| 30 | + end | ||
| 31 | + | ||
| 32 | + should 'have NestedProfile_filter in nested_filter' do | ||
| 33 | + assert TextArticle.nested_filter.include? NestedProfile.filter | ||
| 34 | + end | ||
| 35 | + | ||
| 24 | end | 36 | end |