diff --git a/plugins/elasticsearch/Gemfile b/plugins/elasticsearch/Gemfile index fb6db74..6b6aa79 100644 --- a/plugins/elasticsearch/Gemfile +++ b/plugins/elasticsearch/Gemfile @@ -2,4 +2,3 @@ source 'https://rubygems.org' gem 'elasticsearch-model' gem 'elasticsearch-rails' -gem 'elasticsearch-extensions' diff --git a/plugins/elasticsearch/Rakefile b/plugins/elasticsearch/Rakefile index 71c57e1..2c6eedd 100644 --- a/plugins/elasticsearch/Rakefile +++ b/plugins/elasticsearch/Rakefile @@ -24,6 +24,7 @@ task :start do end sh 'sudo systemctl start elasticsearch' sh 'sudo systemctl enable elasticsearch' + sleep 100 # this sleep gives time to the service to be ready. end desc "stop elasticsearch" diff --git a/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb b/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb index 06c412e..e2f4151 100644 --- a/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb +++ b/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb @@ -7,7 +7,8 @@ module ElasticsearchIndexedModel settings index: { number_of_shards: 1 } do mappings dynamic: 'false' do base.indexable_fields.each do |field, value| - indexes field + value = {} if value.nil? + indexes field, type: value[:type].presence print '.' end end @@ -26,7 +27,7 @@ module ElasticsearchIndexedModel module ClassMethods def indexable_fields - self::SEARCHABLE_FIELDS.keys + self.control_fields + self::SEARCHABLE_FIELDS.update self.control_fields end end diff --git a/plugins/elasticsearch/lib/ext/community.rb b/plugins/elasticsearch/lib/ext/community.rb index 6dd34b6..8dd9d86 100644 --- a/plugins/elasticsearch/lib/ext/community.rb +++ b/plugins/elasticsearch/lib/ext/community.rb @@ -3,7 +3,7 @@ require_relative '../elasticsearch_indexed_model' class Community def self.control_fields - [] + {} end include ElasticsearchIndexedModel end diff --git a/plugins/elasticsearch/lib/ext/event.rb b/plugins/elasticsearch/lib/ext/event.rb index 6866649..d0a4b15 100644 --- a/plugins/elasticsearch/lib/ext/event.rb +++ b/plugins/elasticsearch/lib/ext/event.rb @@ -3,10 +3,10 @@ require_relative '../elasticsearch_indexed_model' class Event def self.control_fields - [ - :advertise, - :published, - ] + { + :advertise => {}, + :published => {}, + } end include ElasticsearchIndexedModel end diff --git a/plugins/elasticsearch/lib/ext/person.rb b/plugins/elasticsearch/lib/ext/person.rb index 46cefef..c99e97e 100644 --- a/plugins/elasticsearch/lib/ext/person.rb +++ b/plugins/elasticsearch/lib/ext/person.rb @@ -3,10 +3,10 @@ require_relative '../elasticsearch_indexed_model' class Person def self.control_fields - [ - :visible, - :public_profile, - ] + { + :visible => {type: 'boolean'}, + :public_profile => {type: 'boolean'}, + } end include ElasticsearchIndexedModel end diff --git a/plugins/elasticsearch/lib/ext/text_article.rb b/plugins/elasticsearch/lib/ext/text_article.rb index 7f81991..8cc4f3a 100644 --- a/plugins/elasticsearch/lib/ext/text_article.rb +++ b/plugins/elasticsearch/lib/ext/text_article.rb @@ -3,10 +3,10 @@ require_relative '../elasticsearch_indexed_model' class TextArticle def self.control_fields - [ - :advertise, - :published, - ] + { + :advertise => nil, + :published => nil, + } end include ElasticsearchIndexedModel end diff --git a/plugins/elasticsearch/lib/ext/uploaded_file.rb b/plugins/elasticsearch/lib/ext/uploaded_file.rb index aa7f4b5..b8425ba 100644 --- a/plugins/elasticsearch/lib/ext/uploaded_file.rb +++ b/plugins/elasticsearch/lib/ext/uploaded_file.rb @@ -3,10 +3,10 @@ require_relative '../elasticsearch_indexed_model' class UploadedFile def self.control_fields - [ - :advertise, - :published, - ] + { + :advertise => nil, + :published => nil, + } end include ElasticsearchIndexedModel end diff --git a/plugins/elasticsearch/test/test_helper.rb b/plugins/elasticsearch/test/test_helper.rb index 1d8d247..c4d2c7e 100644 --- a/plugins/elasticsearch/test/test_helper.rb +++ b/plugins/elasticsearch/test/test_helper.rb @@ -1 +1,37 @@ require 'test_helper' + +class ElasticsearchTestHelper < ActionController::TestCase + + def setup + setup_environment + import_instancies + end + + def teardown + indexed_models.each {|model| + model.__elasticsearch__.client.indices.delete index: model.index_name + } + end + + def import_instancies + indexed_models.each {|model| + model.__elasticsearch__.create_index! + model.import + } + sleep 2 + end + + def setup_environment + @environment = Environment.default + @environment.enable_plugin(ElasticsearchPlugin) + end + + def indexed_models + [] + end + + def indexed_fields model + model.mappings.to_hash[model.name.downcase.to_sym][:properties] + end + +end diff --git a/plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb b/plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb index a5dd047..30ab344 100644 --- a/plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb +++ b/plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb @@ -1,25 +1,35 @@ require "#{File.dirname(__FILE__)}/../../test_helper" -class ElasticsearchPluginControllerTest < ActionController::TestCase +class ElasticsearchPluginControllerTest < ElasticsearchTestHelper + + def indexed_models + [Community, Person] + end def setup - @environment = Environment.default - @environment.enable_plugin(ElasticsearchPlugin) - create_user('John Silva').person - create_user('John Silvio').person - community = fast_create(Community) - community.name = "I like organic" - community.created_at = Time.now - community.save - Community.import - sleep 1 + create_instances + super + end - #TODO: fix this, when update or create a new person - # the Elasticsearch::Model can update the - # indexes models - Person.import - sleep 1 + def teardown + super + end + + def create_instances + create_people + create_communities + end + + def create_people + 5.times do | index | + create_user "person_#{index}" + end + end + def create_communities + 10.times do | index | + fast_create Community, name: "community_#{index}", created_at: Date.new + end end should 'work and uses control filter variables' do @@ -31,31 +41,82 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase assert_not_nil assigns(:selected_filter_field) end - should 'return all results if selected_type is nil' do - get :index, {'selected_type' => :person, :query => 'John'} + should 'return 10 results if selected_type is nil and query is nil' do + get :index + assert_response :success + assert_select ".search-item" , 10 + end + + should 'render pagination if results has more than 10' do + get :index assert_response :success - assert_select ".search-item" , 2 + assert_select ".pagination", 1 end - should 'render index' do - get :index, {'selected_type' => :person, :query => 'Silva'} + should 'return results filtered by selected_type' do + get :index, { 'selected_type' => :person} + assert_response :success + assert_select ".search-item", 5 + assert_template partial: '_person_display' + end + + should 'return results filtered by query' do + get :index, { 'query' => "person_"} + assert_response :success + assert_select ".search-item", 5 + assert_template partial: '_person_display' + end + + should 'return results filtered by query with uppercase' do + get :index, {'query' => "PERSON_1"} + assert_response :success + assert_select ".search-item", 1 + assert_template partial: '_person_display' + end + + should 'return results filtered by query with downcase' do + get :index, {'query' => "person_1"} + assert_response :success + assert_select ".search-item", 1 + end + + should 'return new person indexed' do + get :index, { "selected_type" => :person} + assert_response :success + assert_select ".search-item", 5 + + object = create_user "New Person" + Person.import + sleep 1 + + get :index, { "selected_type" => :person} + assert_response :success + assert_select ".search-item", 6 + end + + should 'not return person deleted' do + get :index, { "selected_type" => :person} + assert_response :success + assert_select ".search-item", 5 + + Person.first.delete + Person.import + sleep 1 + + get :index, { "selected_type" => :person} assert_response :success - assert_select ".search-item" , 1 + assert_select ".search-item", 4 end should 'redirect to elasticsearch plugin when request are send to core' do - oldcontroller = @controller @controller = SearchController.new get 'index' - params = {} - params[:action] = 'index' - params[:controller] = 'search' + params = {:action => 'index', :controller => 'search'} assert_redirected_to controller: 'elasticsearch_plugin', action: 'search', params: params - @controller = oldcontroller end should 'pass params to elastic search controller' do - get 'index', { query: 'like' } + get 'index', { query: 'community_' } assert_not_nil assigns(:results) assert_template partial: '_community_display' end diff --git a/plugins/elasticsearch/test/unit/elasticsearch_test.rb b/plugins/elasticsearch/test/unit/elasticsearch_test.rb index 8482093..f10e336 100644 --- a/plugins/elasticsearch/test/unit/elasticsearch_test.rb +++ b/plugins/elasticsearch/test/unit/elasticsearch_test.rb @@ -1,12 +1,6 @@ require "#{File.dirname(__FILE__)}/../test_helper" -class ElasticsearchTest < ActiveSupport::TestCase - def setup - @environment = Environment.default - @environment.enable_plugin(ElasticsearchPlugin) - @profile = create_user('testing').person - end - +class ElasticsearchTest < ElasticsearchTestHelper should 'be return yellow for health status' do cluster = Elasticsearch::Model.client.cluster diff --git a/plugins/elasticsearch/test/unit/models/community_test.rb b/plugins/elasticsearch/test/unit/models/community_test.rb index c959832..4ee9208 100644 --- a/plugins/elasticsearch/test/unit/models/community_test.rb +++ b/plugins/elasticsearch/test/unit/models/community_test.rb @@ -1,17 +1,26 @@ require "#{File.dirname(__FILE__)}/../../test_helper" -class CommunityTest < ActiveSupport::TestCase +class CommunityTest < ElasticsearchTestHelper + + def indexed_models + [Community] + end + def setup - @environment = Environment.default - @environment.enable_plugin(ElasticsearchPlugin) - @profile = create_user('testing').person + super end - should 'index custom fields for Event model' do - community_cluster = Community.__elasticsearch__.client.cluster + should 'index searchable fields for Community model' do + Community::SEARCHABLE_FIELDS.each do |key, value| + assert_includes indexed_fields(Community), key + end + end - assert_not_nil Community.mappings.to_hash[:community][:properties][:name] - assert_not_nil Community.mappings.to_hash[:community][:properties][:identifier] - assert_not_nil Community.mappings.to_hash[:community][:properties][:nickname] + should 'index control fields for Community model' do + Community::control_fields.each do |key, value| + assert_includes indexed_fields(Community), key + assert_includes indexed_fields(Community)[key][:type], value[:type] || 'string' + end end + end diff --git a/plugins/elasticsearch/test/unit/models/event_test.rb b/plugins/elasticsearch/test/unit/models/event_test.rb index 7b095c3..4b0da33 100644 --- a/plugins/elasticsearch/test/unit/models/event_test.rb +++ b/plugins/elasticsearch/test/unit/models/event_test.rb @@ -1,16 +1,26 @@ require "#{File.dirname(__FILE__)}/../../test_helper" -class EventTest < ActiveSupport::TestCase +class EventTest < ElasticsearchTestHelper + + def indexed_models + [Event] + end + def setup - @environment = Environment.default - @environment.enable_plugin(ElasticsearchPlugin) - @profile = create_user('testing').person + super end - should 'index custom fields for Event model' do - event_cluster = Event.__elasticsearch__.client.cluster + should 'index searchable fields for Event model' do + Event::SEARCHABLE_FIELDS.each do |key, value| + assert_includes indexed_fields(Event), key + end + end - assert_not_nil Event.mappings.to_hash[:event][:properties][:advertise] - assert_not_nil Event.mappings.to_hash[:event][:properties][:published] + should 'index control fields for Event model' do + Event::control_fields.each do |key, value| + assert_includes indexed_fields(Event), key + assert_includes indexed_fields(Event)[key][:type], value[:type] || 'string' + end end + end diff --git a/plugins/elasticsearch/test/unit/models/person_test.rb b/plugins/elasticsearch/test/unit/models/person_test.rb index e716032..0be2861 100644 --- a/plugins/elasticsearch/test/unit/models/person_test.rb +++ b/plugins/elasticsearch/test/unit/models/person_test.rb @@ -1,19 +1,26 @@ require "#{File.dirname(__FILE__)}/../../test_helper" -class PersonTest < ActiveSupport::TestCase +class PersonTest < ElasticsearchTestHelper + + def indexed_models + [Person] + end + def setup - @environment = Environment.default - @environment.enable_plugin(ElasticsearchPlugin) - @profile = create_user('testing').person + super end - should 'index custom fields for Event model' do - person_cluster = Person.__elasticsearch__.client.cluster + should 'index searchable fields for Person model' do + Person::SEARCHABLE_FIELDS.each do |key, value| + assert_includes indexed_fields(Person), key + end + end - assert_not_nil Person.mappings.to_hash[:person][:properties][:name] - assert_not_nil Person.mappings.to_hash[:person][:properties][:identifier] - assert_not_nil Person.mappings.to_hash[:person][:properties][:nickname] - assert_not_nil Person.mappings.to_hash[:person][:properties][:visible] + should 'index control fields for Person model' do + Person::control_fields.each do |key, value| + assert_includes indexed_fields(Person), key + assert_includes indexed_fields(Person)[key][:type], value[:type] || 'string' + end end end -- libgit2 0.21.2