elasticsearch_plugin_controller_test.rb
3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require "#{File.dirname(__FILE__)}/../../test_helper"
class ElasticsearchPluginControllerTest < ActionController::TestCase
def setup
@environment = Environment.default
@environment.enable_plugin(ElasticsearchPlugin)
community = fast_create(Community)
community.name = "I like organic"
community.created_at = Time.now
community.save
create_instances
import_instancies
end
def teardown
Person.__elasticsearch__.client.indices.delete index: Person.index_name
Community.__elasticsearch__.client.indices.delete index: Community.index_name
end
def import_instancies
#TODO: fix this, when update or create a new person
# the Elasticsearch::Model can update the
# indexes models
Person.__elasticsearch__.create_index!
Community.__elasticsearch__.create_index!
Person.import
Community.import
sleep 2
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
get :index
assert_response :success
assert_not_nil assigns(:searchable_types)
assert_not_nil assigns(:selected_type)
assert_not_nil assigns(:search_filter_types)
assert_not_nil assigns(:selected_filter_field)
end
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 ".pagination", 1
end
should 'return results filtered by selected_type' do
get :index, { 'selected_type' => :person}
assert_response :success
assert_select ".search-item", 5
end
should 'return results filtered by query' do
get :index, { 'query' => "person_"}
assert_response :success
assert_select ".search-item", 5
end
should 'return results filtered by query with uppercase' do
get :index, {'query' => "PERSON_1"}
assert_response :success
assert_select ".search-item", 1
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", 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'
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' }
assert_not_nil assigns(:results)
assert_template partial: '_community_display'
end
end