Commit 625d8f639e19de27e51b55a2dccb451f30eb1f7d

Authored by Macartur Sousa
1 parent 2aa4656e

Elasticsearch: Fixed test

Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
plugins/elasticsearch/helpers/elasticsearch_helper.rb
... ... @@ -71,7 +71,6 @@ module ElasticsearchHelper
71 71  
72 72 def query_string expression="", models=[]
73 73 return { match_all: {} } if not expression
74   -
75 74 {
76 75 query_string: {
77 76 query: "*"+expression.downcase.split.join('* *')+"*",
... ...
plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb
... ... @@ -66,6 +66,7 @@ module ElasticsearchIndexedModel
66 66  
67 67 def indexed_fields
68 68 fields = {
  69 + :id => {type: :integer },
69 70 :environment => {type: :nested, hash: NestedEnvironment::environment_hash },
70 71 :category_ids => {type: :integer },
71 72 :created_at => {type: :date }
... ... @@ -83,7 +84,6 @@ module ElasticsearchIndexedModel
83 84  
84 85 self.class.indexed_fields.each do |field, value|
85 86 type = value[:type].presence
86   -
87 87 if type == :nested
88 88 attrs[field] = {}
89 89 value[:hash].each do |hash_field, hash_value|
... ...
plugins/elasticsearch/lib/elasticsearch_plugin/api.rb
... ... @@ -10,7 +10,7 @@ class ElasticsearchPlugin::API &lt; Grape::API
10 10 target = process_results
11 11 present target,
12 12 :with => Elasticsearch::Entities::Result,
13   - :types => ElasticsearchHelper.searchable_types.except(:all).keys.map { |key| key.to_s.classify }
  13 + :types => searchable_types.except(:all).keys.map { |key| key.to_s.classify }
14 14 end
15 15  
16 16 get 'types' do
... ...
plugins/elasticsearch/test/test_helper.rb
... ... @@ -20,7 +20,7 @@ module ElasticsearchTestHelper
20 20 model.__elasticsearch__.create_index! force: true
21 21 model.import
22 22 }
23   - sleep 2
  23 + sleep 3
24 24 end
25 25  
26 26 def setup_environment
... ...
plugins/elasticsearch/test/unit/api/elasticsearch_plugin_api_test.rb
1 1 require "#{File.dirname(__FILE__)}/../../test_helper"
  2 +require_relative '../../../helpers/elasticsearch_helper'
2 3  
3 4 class ElasticsearchPluginApiTest < ActiveSupport::TestCase
4 5  
5 6 include ElasticsearchTestHelper
  7 + include ElasticsearchHelper
6 8  
7 9 def indexed_models
8 10 [Community, Person]
... ... @@ -17,7 +19,7 @@ class ElasticsearchPluginApiTest &lt; ActiveSupport::TestCase
17 19 get "/api/v1/search/types"
18 20 json = JSON.parse(last_response.body)
19 21 assert_equal 200, last_response.status
20   - assert_equal ElasticsearchHelper::searchable_types.stringify_keys.keys, json["types"]
  22 + assert_equal searchable_types.stringify_keys.keys, json["types"]
21 23 end
22 24  
23 25 should 'respond with endpoint /search with more than 10 results' do
... ...
plugins/elasticsearch/test/unit/api/elasticsearch_plugin_entities_test.rb
... ... @@ -9,14 +9,14 @@ class ElasticsearchPluginEntitiesTest &lt; ActiveSupport::TestCase
9 9 end
10 10  
11 11 def create_instances
12   - user = create_user "sample person"
  12 + user = create_user "sample person", environment_id: 1
13 13  
14   - fast_create Community, name: "sample community", created_at: 10.days.ago,updated_at: 5.days.ago
15   - fast_create UploadedFile, name: "sample uploadedfile", created_at: 3.days.ago, updated_at: 1.days.ago, author_id: user.person.id, abstract: "sample abstract"
16   - fast_create Event, name: "sample event", created_at: 20.days.ago, updated_at: 5.days.ago, author_id: user.person.id, abstract: "sample abstract"
  14 + fast_create Community, name: "sample community", created_at: 10.days.ago,updated_at: 5.days.ago, environment_id: 1
17 15  
18   - fast_create RawHTMLArticle, name: "sample raw html article", created_at: 15.days.ago ,updated_at: 5.days.ago, author_id: user.person.id
19   - fast_create TinyMceArticle, name: "sample tiny mce article", created_at: 5.days.ago, updated_at: 5.days.ago, author_id: user.person.id
  16 + fast_create UploadedFile, name: "sample uploadedfile", created_at: 3.days.ago, updated_at: 1.days.ago, author_id: user.person.id, abstract: "sample abstract", profile_id: user.person.id
  17 + fast_create Event, name: "sample event", created_at: 20.days.ago, updated_at: 5.days.ago, author_id: user.person.id, abstract: "sample abstract", profile_id: user.person.id
  18 + fast_create RawHTMLArticle, name: "sample raw html article", created_at: 15.days.ago ,updated_at: 5.days.ago, author_id: user.person.id, profile_id: user.person.id
  19 + fast_create TinyMceArticle, name: "sample tiny mce article", created_at: 5.days.ago, updated_at: 5.days.ago, author_id: user.person.id, profile_id: user.person.id
20 20 end
21 21  
22 22 should 'show attributes from person' do
... ... @@ -35,6 +35,7 @@ class ElasticsearchPluginEntitiesTest &lt; ActiveSupport::TestCase
35 35 assert_equal expected_person.updated_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][0]['updated_at']
36 36 end
37 37  
  38 +
38 39 should 'show attributes from community' do
39 40 params = {:selected_type => "community" }
40 41 get "/api/v1/search?#{params.to_query}"
... ... @@ -43,6 +44,7 @@ class ElasticsearchPluginEntitiesTest &lt; ActiveSupport::TestCase
43 44 expected_community = Community.find_by name: "sample community"
44 45  
45 46 assert_equal 200, last_response.status
  47 +
46 48 assert_equal expected_community.id, json['results'][0]['id']
47 49 assert_equal expected_community.name, json['results'][0]['name']
48 50 assert_equal expected_community.type, json['results'][0]['type']
... ...
plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb
... ... @@ -30,8 +30,8 @@ class ElasticsearchPluginControllerTest &lt; ActionController::TestCase
30 30 assert_response :success
31 31 assert_not_nil assigns(:searchable_types)
32 32 assert_not_nil assigns(:selected_type)
33   - assert_not_nil assigns(:filter_types)
34   - assert_not_nil assigns(:selected_filter)
  33 + assert_not_nil assigns(:sort_types)
  34 + assert_not_nil assigns(:selected_sort)
35 35 end
36 36  
37 37 should 'return 10 results if selected_type is nil and query is nil' do
... ...
plugins/elasticsearch/test/unit/helpers/elasticsearch_helper_test.rb
... ... @@ -32,17 +32,6 @@ class ElasticsearchHelperTest &lt; ActiveSupport::TestCase
32 32 assert_equivalent indexed_models, searchable_models
33 33 end
34 34  
35   - should 'return query_string if expression is valid' do
36   -
37   - query= "my_query"
38   - fields = ['name','login']
39   - result = query_method(query,fields)
40   -
41   - assert_includes result[:query][:query_string][:query], query
42   - assert_equivalent result[:query][:query_string][:fields], fields
43   - end
44   -
45   -
46 35 should 'return fields from models using weight' do
47 36 class StubClass
48 37 SEARCHABLE_FIELDS = {:name => {:weight => 10},
... ...
plugins/elasticsearch/test/unit/models/community_test.rb
... ... @@ -17,7 +17,7 @@ class CommunityTest &lt; ActionController::TestCase
17 17 should 'index control fields for Community model' do
18 18 Community::control_fields.each do |key, value|
19 19 assert_includes indexed_fields(Community), key
20   - assert_includes indexed_fields(Community)[key][:type], value[:type] || 'string'
  20 + assert_equal indexed_fields(Community)[key][:type], value[:type] || 'string'
21 21 end
22 22 end
23 23  
... ...
plugins/elasticsearch/test/unit/models/event_test.rb
... ... @@ -17,7 +17,7 @@ class EventTest &lt; ActionController::TestCase
17 17 should 'index control fields for Event model' do
18 18 Event::control_fields.each do |key, value|
19 19 assert_includes indexed_fields(Event), key
20   - assert_includes indexed_fields(Event)[key][:type], value[:type] || 'string'
  20 + assert_equal indexed_fields(Event)[key][:type], value[:type] || 'string'
21 21 end
22 22 end
23 23  
... ...
plugins/elasticsearch/test/unit/models/person_test.rb
... ... @@ -17,7 +17,7 @@ class PersonTest &lt; ActionController::TestCase
17 17 should 'index control fields for Person model' do
18 18 Person::control_fields.each do |key, value|
19 19 assert_includes indexed_fields(Person), key
20   - assert_includes indexed_fields(Person)[key][:type], value[:type] || 'string'
  20 + assert_equal indexed_fields(Person)[key][:type], value[:type] || 'string'
21 21 end
22 22 end
23 23 end
... ...
plugins/elasticsearch/test/unit/models/text_article_test.rb
... ... @@ -17,7 +17,7 @@ class TextArticleTest &lt; ActionController::TestCase
17 17 should 'index control fields for TextArticle model' do
18 18 TextArticle::control_fields.each do |key, value|
19 19 assert_includes indexed_fields(TextArticle), key
20   - assert_includes indexed_fields(TextArticle)[key][:type], value[:type] || 'string'
  20 + assert_equal indexed_fields(TextArticle)[key][:type], value[:type] || 'string'
21 21 end
22 22 end
23 23  
... ...
plugins/elasticsearch/test/unit/models/uploaded_file_test.rb
... ... @@ -17,7 +17,7 @@ class UploadedFileTest &lt; ActionController::TestCase
17 17 should 'index control fields for UploadedFile model' do
18 18 UploadedFile::control_fields.each do |key, value|
19 19 assert_includes indexed_fields(UploadedFile), key
20   - assert_includes indexed_fields(UploadedFile)[key][:type], value[:type].presence || 'string'
  20 + assert_equal indexed_fields(UploadedFile)[key][:type], value[:type].presence || 'string'
21 21 end
22 22 end
23 23  
... ...
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
... ... @@ -27,7 +27,7 @@
27 27 <ul class="search-options">
28 28 <% for type,value in @searchable_types %>
29 29 <li class="select-search-type <%= "active" if type == @selected_type %>">
30   - <%= link_to value[:label], "?selected_type=#{type}&query=#{@query}&filter=#{@selected_sort}"%>
  30 + <%= link_to value[:label], "?selected_type=#{type}&query=#{@query}"%>
31 31 </li>
32 32 <% end %>
33 33 </ul>
... ...