Commit 7f03323ca2542a2c4826dfc54eaf91fd6d904281
1 parent
d5444619
Exists in
elasticsearch_sort
Adding ElasticsearchTestHelper
Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
2 changed files
with
40 additions
and
29 deletions
Show diff stats
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 | +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 | 4 | |
| 5 | - def setup | |
| 6 | - @environment = Environment.default | |
| 7 | - @environment.enable_plugin(ElasticsearchPlugin) | |
| 5 | + def indexed_models | |
| 6 | + [Community, Person] | |
| 7 | + end | |
| 8 | 8 | |
| 9 | - community = fast_create(Community) | |
| 10 | - community.name = "I like organic" | |
| 11 | - community.created_at = Time.now | |
| 12 | - community.save | |
| 9 | + def setup | |
| 13 | 10 | create_instances |
| 14 | - import_instancies | |
| 11 | + super | |
| 15 | 12 | end |
| 16 | 13 | |
| 17 | 14 | def teardown |
| 18 | - Person.__elasticsearch__.client.indices.delete index: Person.index_name | |
| 19 | - Community.__elasticsearch__.client.indices.delete index: Community.index_name | |
| 20 | - end | |
| 21 | - | |
| 22 | - def import_instancies | |
| 23 | - #TODO: fix this, when update or create a new person | |
| 24 | - # the Elasticsearch::Model can update the | |
| 25 | - # indexes models | |
| 26 | - Person.__elasticsearch__.create_index! | |
| 27 | - Community.__elasticsearch__.create_index! | |
| 28 | - | |
| 29 | - Person.import | |
| 30 | - Community.import | |
| 31 | - sleep 2 | |
| 15 | + super | |
| 32 | 16 | end |
| 33 | 17 | |
| 34 | 18 | def create_instances |
| ... | ... | @@ -73,18 +57,21 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase |
| 73 | 57 | get :index, { 'selected_type' => :person} |
| 74 | 58 | assert_response :success |
| 75 | 59 | assert_select ".search-item", 5 |
| 60 | + assert_template partial: '_person_display' | |
| 76 | 61 | end |
| 77 | 62 | |
| 78 | 63 | should 'return results filtered by query' do |
| 79 | 64 | get :index, { 'query' => "person_"} |
| 80 | 65 | assert_response :success |
| 81 | 66 | assert_select ".search-item", 5 |
| 67 | + assert_template partial: '_person_display' | |
| 82 | 68 | end |
| 83 | 69 | |
| 84 | 70 | should 'return results filtered by query with uppercase' do |
| 85 | 71 | get :index, {'query' => "PERSON_1"} |
| 86 | 72 | assert_response :success |
| 87 | 73 | assert_select ".search-item", 1 |
| 74 | + assert_template partial: '_person_display' | |
| 88 | 75 | end |
| 89 | 76 | |
| 90 | 77 | should 'return results filtered by query with downcase' do |
| ... | ... | @@ -122,18 +109,14 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase |
| 122 | 109 | end |
| 123 | 110 | |
| 124 | 111 | should 'redirect to elasticsearch plugin when request are send to core' do |
| 125 | - oldcontroller = @controller | |
| 126 | 112 | @controller = SearchController.new |
| 127 | 113 | get 'index' |
| 128 | - params = {} | |
| 129 | - params[:action] = 'index' | |
| 130 | - params[:controller] = 'search' | |
| 114 | + params = {:action => 'index', :controller => 'search'} | |
| 131 | 115 | assert_redirected_to controller: 'elasticsearch_plugin', action: 'search', params: params |
| 132 | - @controller = oldcontroller | |
| 133 | 116 | end |
| 134 | 117 | |
| 135 | 118 | should 'pass params to elastic search controller' do |
| 136 | - get 'index', { query: 'like' } | |
| 119 | + get 'index', { query: 'community_' } | |
| 137 | 120 | assert_not_nil assigns(:results) |
| 138 | 121 | assert_template partial: '_community_display' |
| 139 | 122 | end | ... | ... |