Commit 4ae80bab19a28b6559373ccb1b69fadc288e7e0d

Authored by Macartur Sousa
1 parent 572f99a7

Elasticsearch: Adding tests

Adding tests to elasticsearch controller
Adding ElasticsearchTestHelper
Fixed model tests
Adding tests and refactored control_fields indexed

Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
plugins/elasticsearch/Gemfile
... ... @@ -2,4 +2,3 @@ source &#39;https://rubygems.org&#39;
2 2  
3 3 gem 'elasticsearch-model'
4 4 gem 'elasticsearch-rails'
5   -gem 'elasticsearch-extensions'
... ...
plugins/elasticsearch/Rakefile
... ... @@ -24,6 +24,7 @@ task :start do
24 24 end
25 25 sh 'sudo systemctl start elasticsearch'
26 26 sh 'sudo systemctl enable elasticsearch'
  27 + sleep 100 # this sleep gives time to the service to be ready.
27 28 end
28 29  
29 30 desc "stop elasticsearch"
... ...
plugins/elasticsearch/lib/elasticsearch_indexed_model.rb
... ... @@ -7,7 +7,8 @@ module ElasticsearchIndexedModel
7 7 settings index: { number_of_shards: 1 } do
8 8 mappings dynamic: 'false' do
9 9 base.indexable_fields.each do |field, value|
10   - indexes field
  10 + value = {} if value.nil?
  11 + indexes field, type: value[:type].presence
11 12 print '.'
12 13 end
13 14 end
... ... @@ -26,7 +27,7 @@ module ElasticsearchIndexedModel
26 27  
27 28 module ClassMethods
28 29 def indexable_fields
29   - self::SEARCHABLE_FIELDS.keys + self.control_fields
  30 + self::SEARCHABLE_FIELDS.update self.control_fields
30 31 end
31 32 end
32 33  
... ...
plugins/elasticsearch/lib/ext/community.rb
... ... @@ -3,7 +3,7 @@ require_relative &#39;../elasticsearch_indexed_model&#39;
3 3  
4 4 class Community
5 5 def self.control_fields
6   - []
  6 + {}
7 7 end
8 8 include ElasticsearchIndexedModel
9 9 end
... ...
plugins/elasticsearch/lib/ext/event.rb
... ... @@ -3,10 +3,10 @@ require_relative &#39;../elasticsearch_indexed_model&#39;
3 3  
4 4 class Event
5 5 def self.control_fields
6   - [
7   - :advertise,
8   - :published,
9   - ]
  6 + {
  7 + :advertise => {},
  8 + :published => {},
  9 + }
10 10 end
11 11 include ElasticsearchIndexedModel
12 12 end
... ...
plugins/elasticsearch/lib/ext/person.rb
... ... @@ -3,10 +3,10 @@ require_relative &#39;../elasticsearch_indexed_model&#39;
3 3  
4 4 class Person
5 5 def self.control_fields
6   - [
7   - :visible,
8   - :public_profile,
9   - ]
  6 + {
  7 + :visible => {type: 'boolean'},
  8 + :public_profile => {type: 'boolean'},
  9 + }
10 10 end
11 11 include ElasticsearchIndexedModel
12 12 end
... ...
plugins/elasticsearch/lib/ext/text_article.rb
... ... @@ -3,10 +3,10 @@ require_relative &#39;../elasticsearch_indexed_model&#39;
3 3  
4 4 class TextArticle
5 5 def self.control_fields
6   - [
7   - :advertise,
8   - :published,
9   - ]
  6 + {
  7 + :advertise => nil,
  8 + :published => nil,
  9 + }
10 10 end
11 11 include ElasticsearchIndexedModel
12 12 end
... ...
plugins/elasticsearch/lib/ext/uploaded_file.rb
... ... @@ -3,10 +3,10 @@ require_relative &#39;../elasticsearch_indexed_model&#39;
3 3  
4 4 class UploadedFile
5 5 def self.control_fields
6   - [
7   - :advertise,
8   - :published,
9   - ]
  6 + {
  7 + :advertise => nil,
  8 + :published => nil,
  9 + }
10 10 end
11 11 include ElasticsearchIndexedModel
12 12 end
... ...
plugins/elasticsearch/test/test_helper.rb
1 1 require 'test_helper'
  2 +
  3 +class ElasticsearchTestHelper < ActionController::TestCase
  4 +
  5 + def setup
  6 + setup_environment
  7 + import_instancies
  8 + end
  9 +
  10 + def teardown
  11 + indexed_models.each {|model|
  12 + model.__elasticsearch__.client.indices.delete index: model.index_name
  13 + }
  14 + end
  15 +
  16 + def import_instancies
  17 + indexed_models.each {|model|
  18 + model.__elasticsearch__.create_index!
  19 + model.import
  20 + }
  21 + sleep 2
  22 + end
  23 +
  24 + def setup_environment
  25 + @environment = Environment.default
  26 + @environment.enable_plugin(ElasticsearchPlugin)
  27 + end
  28 +
  29 + def indexed_models
  30 + []
  31 + end
  32 +
  33 + def indexed_fields model
  34 + model.mappings.to_hash[model.name.downcase.to_sym][:properties]
  35 + end
  36 +
  37 +end
... ...
plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb
1 1 require "#{File.dirname(__FILE__)}/../../test_helper"
2 2  
3   -class ElasticsearchPluginControllerTest < ActionController::TestCase
  3 +class ElasticsearchPluginControllerTest < ElasticsearchTestHelper
  4 +
  5 + def indexed_models
  6 + [Community, Person]
  7 + end
4 8  
5 9 def setup
6   - @environment = Environment.default
7   - @environment.enable_plugin(ElasticsearchPlugin)
8   - create_user('John Silva').person
9   - create_user('John Silvio').person
10   - community = fast_create(Community)
11   - community.name = "I like organic"
12   - community.created_at = Time.now
13   - community.save
14   - Community.import
15   - sleep 1
  10 + create_instances
  11 + super
  12 + end
16 13  
17   - #TODO: fix this, when update or create a new person
18   - # the Elasticsearch::Model can update the
19   - # indexes models
20   - Person.import
21   - sleep 1
  14 + def teardown
  15 + super
  16 + end
  17 +
  18 + def create_instances
  19 + create_people
  20 + create_communities
  21 + end
  22 +
  23 + def create_people
  24 + 5.times do | index |
  25 + create_user "person_#{index}"
  26 + end
  27 + end
22 28  
  29 + def create_communities
  30 + 10.times do | index |
  31 + fast_create Community, name: "community_#{index}", created_at: Date.new
  32 + end
23 33 end
24 34  
25 35 should 'work and uses control filter variables' do
... ... @@ -31,31 +41,82 @@ class ElasticsearchPluginControllerTest &lt; ActionController::TestCase
31 41 assert_not_nil assigns(:selected_filter_field)
32 42 end
33 43  
34   - should 'return all results if selected_type is nil' do
35   - get :index, {'selected_type' => :person, :query => 'John'}
  44 + should 'return 10 results if selected_type is nil and query is nil' do
  45 + get :index
  46 + assert_response :success
  47 + assert_select ".search-item" , 10
  48 + end
  49 +
  50 + should 'render pagination if results has more than 10' do
  51 + get :index
36 52 assert_response :success
37   - assert_select ".search-item" , 2
  53 + assert_select ".pagination", 1
38 54 end
39 55  
40   - should 'render index' do
41   - get :index, {'selected_type' => :person, :query => 'Silva'}
  56 + should 'return results filtered by selected_type' do
  57 + get :index, { 'selected_type' => :person}
  58 + assert_response :success
  59 + assert_select ".search-item", 5
  60 + assert_template partial: '_person_display'
  61 + end
  62 +
  63 + should 'return results filtered by query' do
  64 + get :index, { 'query' => "person_"}
  65 + assert_response :success
  66 + assert_select ".search-item", 5
  67 + assert_template partial: '_person_display'
  68 + end
  69 +
  70 + should 'return results filtered by query with uppercase' do
  71 + get :index, {'query' => "PERSON_1"}
  72 + assert_response :success
  73 + assert_select ".search-item", 1
  74 + assert_template partial: '_person_display'
  75 + end
  76 +
  77 + should 'return results filtered by query with downcase' do
  78 + get :index, {'query' => "person_1"}
  79 + assert_response :success
  80 + assert_select ".search-item", 1
  81 + end
  82 +
  83 + should 'return new person indexed' do
  84 + get :index, { "selected_type" => :person}
  85 + assert_response :success
  86 + assert_select ".search-item", 5
  87 +
  88 + object = create_user "New Person"
  89 + Person.import
  90 + sleep 1
  91 +
  92 + get :index, { "selected_type" => :person}
  93 + assert_response :success
  94 + assert_select ".search-item", 6
  95 + end
  96 +
  97 + should 'not return person deleted' do
  98 + get :index, { "selected_type" => :person}
  99 + assert_response :success
  100 + assert_select ".search-item", 5
  101 +
  102 + Person.first.delete
  103 + Person.import
  104 + sleep 1
  105 +
  106 + get :index, { "selected_type" => :person}
42 107 assert_response :success
43   - assert_select ".search-item" , 1
  108 + assert_select ".search-item", 4
44 109 end
45 110  
46 111 should 'redirect to elasticsearch plugin when request are send to core' do
47   - oldcontroller = @controller
48 112 @controller = SearchController.new
49 113 get 'index'
50   - params = {}
51   - params[:action] = 'index'
52   - params[:controller] = 'search'
  114 + params = {:action => 'index', :controller => 'search'}
53 115 assert_redirected_to controller: 'elasticsearch_plugin', action: 'search', params: params
54   - @controller = oldcontroller
55 116 end
56 117  
57 118 should 'pass params to elastic search controller' do
58   - get 'index', { query: 'like' }
  119 + get 'index', { query: 'community_' }
59 120 assert_not_nil assigns(:results)
60 121 assert_template partial: '_community_display'
61 122 end
... ...
plugins/elasticsearch/test/unit/elasticsearch_test.rb
1 1 require "#{File.dirname(__FILE__)}/../test_helper"
2 2  
3   -class ElasticsearchTest < ActiveSupport::TestCase
4   - def setup
5   - @environment = Environment.default
6   - @environment.enable_plugin(ElasticsearchPlugin)
7   - @profile = create_user('testing').person
8   - end
9   -
  3 +class ElasticsearchTest < ElasticsearchTestHelper
10 4  
11 5 should 'be return yellow for health status' do
12 6 cluster = Elasticsearch::Model.client.cluster
... ...
plugins/elasticsearch/test/unit/models/community_test.rb
1 1 require "#{File.dirname(__FILE__)}/../../test_helper"
2 2  
3   -class CommunityTest < ActiveSupport::TestCase
  3 +class CommunityTest < ElasticsearchTestHelper
  4 +
  5 + def indexed_models
  6 + [Community]
  7 + end
  8 +
4 9 def setup
5   - @environment = Environment.default
6   - @environment.enable_plugin(ElasticsearchPlugin)
7   - @profile = create_user('testing').person
  10 + super
8 11 end
9 12  
10   - should 'index custom fields for Event model' do
11   - community_cluster = Community.__elasticsearch__.client.cluster
  13 + should 'index searchable fields for Community model' do
  14 + Community::SEARCHABLE_FIELDS.each do |key, value|
  15 + assert_includes indexed_fields(Community), key
  16 + end
  17 + end
12 18  
13   - assert_not_nil Community.mappings.to_hash[:community][:properties][:name]
14   - assert_not_nil Community.mappings.to_hash[:community][:properties][:identifier]
15   - assert_not_nil Community.mappings.to_hash[:community][:properties][:nickname]
  19 + should 'index control fields for Community model' do
  20 + Community::control_fields.each do |key, value|
  21 + assert_includes indexed_fields(Community), key
  22 + assert_includes indexed_fields(Community)[key][:type], value[:type] || 'string'
  23 + end
16 24 end
  25 +
17 26 end
... ...
plugins/elasticsearch/test/unit/models/event_test.rb
1 1 require "#{File.dirname(__FILE__)}/../../test_helper"
2 2  
3   -class EventTest < ActiveSupport::TestCase
  3 +class EventTest < ElasticsearchTestHelper
  4 +
  5 + def indexed_models
  6 + [Event]
  7 + end
  8 +
4 9 def setup
5   - @environment = Environment.default
6   - @environment.enable_plugin(ElasticsearchPlugin)
7   - @profile = create_user('testing').person
  10 + super
8 11 end
9 12  
10   - should 'index custom fields for Event model' do
11   - event_cluster = Event.__elasticsearch__.client.cluster
  13 + should 'index searchable fields for Event model' do
  14 + Event::SEARCHABLE_FIELDS.each do |key, value|
  15 + assert_includes indexed_fields(Event), key
  16 + end
  17 + end
12 18  
13   - assert_not_nil Event.mappings.to_hash[:event][:properties][:advertise]
14   - assert_not_nil Event.mappings.to_hash[:event][:properties][:published]
  19 + should 'index control fields for Event model' do
  20 + Event::control_fields.each do |key, value|
  21 + assert_includes indexed_fields(Event), key
  22 + assert_includes indexed_fields(Event)[key][:type], value[:type] || 'string'
  23 + end
15 24 end
  25 +
16 26 end
... ...
plugins/elasticsearch/test/unit/models/person_test.rb
1 1 require "#{File.dirname(__FILE__)}/../../test_helper"
2 2  
3   -class PersonTest < ActiveSupport::TestCase
  3 +class PersonTest < ElasticsearchTestHelper
  4 +
  5 + def indexed_models
  6 + [Person]
  7 + end
  8 +
4 9 def setup
5   - @environment = Environment.default
6   - @environment.enable_plugin(ElasticsearchPlugin)
7   - @profile = create_user('testing').person
  10 + super
8 11 end
9 12  
10   - should 'index custom fields for Event model' do
11   - person_cluster = Person.__elasticsearch__.client.cluster
  13 + should 'index searchable fields for Person model' do
  14 + Person::SEARCHABLE_FIELDS.each do |key, value|
  15 + assert_includes indexed_fields(Person), key
  16 + end
  17 + end
12 18  
13   - assert_not_nil Person.mappings.to_hash[:person][:properties][:name]
14   - assert_not_nil Person.mappings.to_hash[:person][:properties][:identifier]
15   - assert_not_nil Person.mappings.to_hash[:person][:properties][:nickname]
16   - assert_not_nil Person.mappings.to_hash[:person][:properties][:visible]
  19 + should 'index control fields for Person model' do
  20 + Person::control_fields.each do |key, value|
  21 + assert_includes indexed_fields(Person), key
  22 + assert_includes indexed_fields(Person)[key][:type], value[:type] || 'string'
  23 + end
17 24 end
18 25  
19 26 end
... ...