Commit 174fea9ac1bafe1129fe241278b180670d311718

Authored by Macartur Sousa
1 parent 1ff43b06
Exists in fix_sign_up_form

Elasticsearch: Adding tests

* Adding unit tests for sort
* Adding private itens to be filtered into tests
* Adding filter tests to api

Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
plugins/elasticsearch/helpers/elasticsearch_helper.rb
... ... @@ -35,15 +35,20 @@ module ElasticsearchHelper
35 35 private
36 36  
37 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 45 end
41 46  
42 47 def search_from_model model
43 48 begin
44 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 52 klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records
48 53 rescue
49 54 []
... ... @@ -56,9 +61,9 @@ module ElasticsearchHelper
56 61  
57 62 def get_sort_by sort_by, klass=nil
58 63 case sort_by
59   - when "lexical"
  64 + when :lexical
60 65 { "name.raw" => {"order" => "asc" }}
61   - when "more_recent"
  66 + when :more_recent
62 67 { "created_at" => {"order" => "desc"}}
63 68 else
64 69 ( klass and klass.respond_to?(:get_sort_by) ) ? klass.get_sort_by(sort_by) : nil
... ... @@ -105,6 +110,7 @@ module ElasticsearchHelper
105 110  
106 111 query = query_method(text, models)
107 112 query[:sort] = sort_by if sort_by
  113 +
108 114 query
109 115 end
110 116  
... ...
plugins/elasticsearch/lib/ext/community.rb
... ... @@ -30,12 +30,12 @@ class Community
30 30 }
31 31 end
32 32  
33   - def self.get_sort_by sort_by
  33 + def self.get_sort_by sort_by=""
34 34 case sort_by
35   - when "more_active"
  35 + when :more_active
36 36 { :activities_count => {order: :desc}}
37   - when "more_popular"
38   - { :members_count => {order: :desc} }
  37 + when :more_popular
  38 + { :members_count => {order: :desc}}
39 39 end
40 40 end
41 41  
... ...
plugins/elasticsearch/lib/ext/person.rb
... ... @@ -31,11 +31,11 @@ class Person
31 31 }
32 32 end
33 33  
34   - def self.get_sort_by sort_by
  34 + def self.get_sort_by sort_by=""
35 35 case sort_by
36   - when "more_active"
  36 + when :more_active
37 37 { :activities_count => {order: :desc}}
38   - when "more_popular"
  38 + when :more_popular
39 39 { :friends_count => {order: :desc} }
40 40 end
41 41 end
... ...
plugins/elasticsearch/lib/ext/text_article.rb
... ... @@ -37,15 +37,15 @@ class TextArticle
37 37 def self.especific_sort
38 38 {
39 39 :more_popular => { label: _("More Viewed") },
40   - :more_comments => { label: _("More Commented") }
  40 + :more_comments => { label: _("Most Commented") }
41 41 }
42 42 end
43 43  
44   - def self.get_sort_by sort_by
  44 + def self.get_sort_by sort_by=""
45 45 case sort_by
46   - when "more_popular"
  46 + when :more_popular
47 47 { :hits => {order: :desc} }
48   - when "more_comments"
  48 + when :more_comments
49 49 { :comments_count => {order: :desc}}
50 50 end
51 51 end
... ...
plugins/elasticsearch/lib/searchable_model/filter.rb
... ... @@ -32,7 +32,6 @@ module Filter
32 32  
33 33 result_filter
34 34 end
35   -
36 35 end
37 36  
38 37 module InstanceMethods
... ...
plugins/elasticsearch/test/api/elasticsearch_plugin_api_test.rb
... ... @@ -7,7 +7,53 @@ class ElasticsearchPluginApiTest &lt; ActiveSupport::TestCase
7 7 include ElasticsearchHelper
8 8  
9 9 def indexed_models
10   - [Community, Person]
  10 + [Person,TextArticle,UploadedFile,Community,Event]
  11 + end
  12 +
  13 + def create_instances
  14 + create_instances_environment
  15 + create_instances_environment2
  16 + end
  17 +
  18 + def create_instances_environment2
  19 + create_user "Sample User Environment 2", environment:Environment.second
  20 + fast_create Community, name:"Sample Community Environment 2", created_at: Date.new, environment_id: Environment.second.id
  21 + end
  22 +
  23 + def create_instances_environment
  24 + create_visible_models
  25 + create_private_models
  26 + end
  27 +
  28 + def create_visible_models
  29 + 7.times{ | index | create_user "person #{index}" }
  30 + 4.times{ | index | fast_create Community, name: "community #{index}", created_at: Date.new }
  31 + end
  32 +
  33 + def create_private_models
  34 + secret_user = create_user("Secret Person")
  35 + fast_update(secret_user.person, secret: true, visible: true)
  36 +
  37 + invisible_user= create_user("Invisible Person")
  38 + fast_update(invisible_user.person, secret: false, visible: false, public_profile: false)
  39 +
  40 + fast_create(Community, name: "secret community", secret: true, visible: true)
  41 + fast_create(Community, name: "invisible community", secret: false, visible: false)
  42 +
  43 + create_private_article(TextArticle,public_person: User.first.person, private_person: invisible_user.person)
  44 + create_private_article(UploadedFile,public_person: User.first.person, private_person: invisible_user.person)
  45 + create_private_article(Event,public_person: User.first.person, private_person: invisible_user.person)
  46 +
  47 + end
  48 +
  49 + def create_private_article model,options = {}
  50 + public_person = options[:public_person]
  51 + private_person = options[:private_person]
  52 +
  53 + fast_create(model, name: "#{model.to_s.underscore} not advertise", advertise: false, published: true, profile_id: public_person, created_at: Time.now)
  54 + fast_create(model, name: "#{model.to_s.underscore} not published", advertise: true, published: false, profile_id: public_person, created_at: Time.now)
  55 + fast_create(model, name: "#{model.to_s.underscore} with not visible profile", advertise: true, published: true, profile_id: private_person, created_at: Time.now)
  56 + fast_create(model, name: "#{model.to_s.underscore} with not public_profile", advertise: true, published: true, profile_id: private_person, created_at: Time.now)
11 57 end
12 58  
13 59 def create_instances
... ... @@ -49,4 +95,19 @@ class ElasticsearchPluginApiTest &lt; ActiveSupport::TestCase
49 95 assert_equal 200, last_response.status
50 96 assert_equal 4, json["results"].count
51 97 end
  98 +
  99 + should 'filter person by default environment' do
  100 + get "/api/v1/search?selected_type=person"
  101 + json = JSON.parse(last_response.body)
  102 + assert_equal 200, last_response.status
  103 + assert_equal 7, json["results"].count
  104 + end
  105 +
  106 + should 'not show private text_article' do
  107 + get "/api/v1/search?selected_type=text_article"
  108 + json = JSON.parse(last_response.body)
  109 + assert_equal 200, last_response.status
  110 + assert_equal 7, json["results"].count
  111 + end
  112 +
52 113 end
... ...
plugins/elasticsearch/test/functional/elasticsearch_plugin_controller_test.rb
... ... @@ -5,26 +5,60 @@ class ElasticsearchPluginControllerTest &lt; ActionController::TestCase
5 5 include ElasticsearchTestHelper
6 6  
7 7 def indexed_models
8   - [Community, Person]
  8 + [Person,TextArticle,UploadedFile,Community,Event]
9 9 end
10 10  
11 11 def create_instances
12   - create_people
13   - create_communities
  12 + create_instances_environment
  13 + create_instances_environment2
14 14 end
15 15  
16   - def create_people
  16 + def create_instances_environment2
  17 + create_user "Sample User Environment 2", environment:Environment.second
  18 + fast_create Community, name:"Sample Community Environment 2", created_at: Date.new, environment_id: Environment.second.id
  19 + end
  20 +
  21 + def create_instances_environment
  22 + create_visible_models
  23 + create_private_models
  24 + end
  25 +
  26 + def create_visible_models
17 27 5.times do | index |
18 28 create_user "person #{index}"
19 29 end
20   - end
21   -
22   - def create_communities
23 30 6.times do | index |
24 31 fast_create Community, name: "community #{index}", created_at: Date.new
25 32 end
26 33 end
27 34  
  35 + def create_private_models
  36 + secret_user = create_user("Secret Person")
  37 + fast_update(secret_user.person, secret: true, visible: true)
  38 +
  39 + invisible_user= create_user("Invisible Person")
  40 + fast_update(invisible_user.person, secret: false, visible: false, public_profile: false)
  41 +
  42 + fast_create(Community, name: "secret community", secret: true, visible: true)
  43 + fast_create(Community, name: "invisible community", secret: false, visible: false)
  44 +
  45 + create_private_article(TextArticle,public_person: User.first.person, private_person: invisible_user.person)
  46 + create_private_article(UploadedFile,public_person: User.first.person, private_person: invisible_user.person)
  47 + create_private_article(Event,public_person: User.first.person, private_person: invisible_user.person)
  48 +
  49 + end
  50 +
  51 + def create_private_article model,options = {}
  52 + public_person = options[:public_person]
  53 + private_person = options[:private_person]
  54 +
  55 + fast_create(model, name: "#{model.to_s.underscore} not advertise", advertise: false, published: true, profile_id: public_person, created_at: Time.now)
  56 + fast_create(model, name: "#{model.to_s.underscore} not published", advertise: true, published: false, profile_id: public_person, created_at: Time.now)
  57 + fast_create(model, name: "#{model.to_s.underscore} with not visible profile", advertise: true, published: true, profile_id: private_person, created_at: Time.now)
  58 + fast_create(model, name: "#{model.to_s.underscore} with not public_profile", advertise: true, published: true, profile_id: private_person, created_at: Time.now)
  59 + end
  60 +
  61 +
28 62 should 'work and uses control filter variables' do
29 63 get :index
30 64 assert_response :success
... ... @@ -60,20 +94,20 @@ class ElasticsearchPluginControllerTest &lt; ActionController::TestCase
60 94 assert_template partial: '_person_display'
61 95 end
62 96  
63   - should 'return results filtered by query with uppercase' do
64   - get :index, {'query' => "PERSON 1"}
65   - assert_response :success
66   - assert_template partial: '_person_display'
67   - assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} )
68   - end
  97 + should 'return results filtered by query with uppercase' do
  98 + get :index, {'query' => "PERSON 1"}
  99 + assert_response :success
  100 + assert_template partial: '_person_display'
  101 + assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} )
  102 + end
69 103  
70   - should 'return results filtered by query with downcase' do
71   - get :index, {'query' => "person 1"}
72   - assert_response :success
73   - assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} )
74   - end
  104 + should 'return results filtered by query with downcase' do
  105 + get :index, {'query' => "person 1"}
  106 + assert_response :success
  107 + assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} )
  108 + end
75 109  
76   - should 'return new person indexed' do
  110 + should 'return new community indexed' do
77 111 get :index, { "selected_type" => :community}
78 112 assert_response :success
79 113 assert_select ".search-item", 6
... ... @@ -87,13 +121,14 @@ class ElasticsearchPluginControllerTest &lt; ActionController::TestCase
87 121 assert_select ".search-item", 7
88 122 end
89 123  
90   - should 'not return person deleted' do
  124 + should 'not return community deleted' do
91 125 get :index, { "selected_type" => :community}
92 126 assert_response :success
93 127 assert_select ".search-item", 6
94 128  
95 129 Community.first.delete
96 130 Community.import
  131 + sleep 2
97 132  
98 133 get :index, { "selected_type" => :community}
99 134 assert_response :success
... ... @@ -107,9 +142,35 @@ class ElasticsearchPluginControllerTest &lt; ActionController::TestCase
107 142 assert_redirected_to controller: 'elasticsearch_plugin', action: 'search', params: params
108 143 end
109 144  
110   - should 'pass params to elastic search controller' do
111   - get 'index', { query: 'community' }
112   - assert_not_nil assigns(:results)
113   - assert_template partial: '_community_display'
  145 +
  146 + should 'filter community by default environment' do
  147 + get :index, { "selected_type" => :community}
  148 + assert_response :success
  149 + assert_select ".search-item", 6
114 150 end
  151 +
  152 + should 'filter person by default environment' do
  153 + get :index, { "selected_type" => :person}
  154 + assert_response :success
  155 + assert_select ".search-item", 5
  156 + end
  157 +
  158 + should 'not show private text_article' do
  159 + get :index, { :selected_type => "text_article" }
  160 + assert_response :success
  161 + assert_select ".search-item", 6
  162 + end
  163 +
  164 + should 'not show private uploaded_file' do
  165 + get :index, { :selected_type => "uploaded_file" }
  166 + assert_response :success
  167 + assert_select ".search-item", 0
  168 + end
  169 +
  170 + should 'not show private event' do
  171 + get :index, { :selected_type => "event" }
  172 + assert_response :success
  173 + assert_select ".search-item", 0
  174 + end
  175 +
115 176 end
... ...
plugins/elasticsearch/test/unit/community_test.rb
... ... @@ -21,4 +21,26 @@ class CommunityTest &lt; ActiveSupport::TestCase
21 21 end
22 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 46 end
... ...
plugins/elasticsearch/test/unit/elasticsearch_helper_test.rb
... ... @@ -72,4 +72,9 @@ class ElasticsearchHelperTest &lt; ActiveSupport::TestCase
72 72 assert_equal ["Joana Abreu", "Joao Abreu"], result.map(&:name)
73 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 80 end
... ...
plugins/elasticsearch/test/unit/event_test.rb
1 1 require "#{File.dirname(__FILE__)}/../test_helper"
  2 +require_relative '../../lib/nested_helper/profile'
2 3  
3 4 class EventTest < ActionController::TestCase
4 5  
... ... @@ -21,4 +22,16 @@ class EventTest &lt; ActionController::TestCase
21 22 end
22 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 37 end
... ...
plugins/elasticsearch/test/unit/person_test.rb
... ... @@ -20,4 +20,26 @@ class PersonTest &lt; ActionController::TestCase
20 20 assert_equal indexed_fields(Person)[key][:type], value[:type] || 'string'
21 21 end
22 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 45 end
... ...
plugins/elasticsearch/test/unit/text_article_test.rb
1 1 require "#{File.dirname(__FILE__)}/../test_helper"
  2 +require_relative '../../lib/nested_helper/profile'
2 3  
3 4 class TextArticleTest < ActionController::TestCase
4 5  
... ... @@ -21,4 +22,34 @@ class TextArticleTest &lt; ActionController::TestCase
21 22 end
22 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 55 end
... ...
plugins/elasticsearch/test/unit/uploaded_file_test.rb
... ... @@ -21,4 +21,16 @@ class UploadedFileTest &lt; ActionController::TestCase
21 21 end
22 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 36 end
... ...