Commit f160647b03f7bcf10835f7e139574fb9f2c7b466
1 parent
e1b76e5a
Exists in
elasticsearch_api
Adding tests to elasticsearch controller
Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
2 changed files
with
89 additions
and
12 deletions
Show diff stats
plugins/elasticsearch/Gemfile
plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb
... | ... | @@ -5,21 +5,47 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase |
5 | 5 | def setup |
6 | 6 | @environment = Environment.default |
7 | 7 | @environment.enable_plugin(ElasticsearchPlugin) |
8 | - create_user('John Silva').person | |
9 | - create_user('John Silvio').person | |
8 | + | |
10 | 9 | community = fast_create(Community) |
11 | 10 | community.name = "I like organic" |
12 | 11 | community.created_at = Time.now |
13 | 12 | community.save |
14 | - Community.import | |
15 | - sleep 1 | |
13 | + create_instances | |
14 | + import_instancies | |
15 | + end | |
16 | + | |
17 | + def teardown | |
18 | + Person.__elasticsearch__.client.indices.delete index: Person.index_name | |
19 | + Community.__elasticsearch__.client.indices.delete index: Community.index_name | |
20 | + end | |
16 | 21 | |
22 | + def import_instancies | |
17 | 23 | #TODO: fix this, when update or create a new person |
18 | 24 | # the Elasticsearch::Model can update the |
19 | 25 | # indexes models |
26 | + Person.__elasticsearch__.create_index! | |
27 | + Community.__elasticsearch__.create_index! | |
28 | + | |
20 | 29 | Person.import |
21 | - sleep 1 | |
30 | + Community.import | |
31 | + sleep 2 | |
32 | + end | |
33 | + | |
34 | + def create_instances | |
35 | + create_people | |
36 | + create_communities | |
37 | + end | |
38 | + | |
39 | + def create_people | |
40 | + 5.times do | index | | |
41 | + create_user "person_#{index}" | |
42 | + end | |
43 | + end | |
22 | 44 | |
45 | + def create_communities | |
46 | + 10.times do | index | | |
47 | + fast_create Community, name: "community_#{index}", created_at: Date.new | |
48 | + end | |
23 | 49 | end |
24 | 50 | |
25 | 51 | should 'work and uses control filter variables' do |
... | ... | @@ -31,16 +57,68 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase |
31 | 57 | assert_not_nil assigns(:selected_filter_field) |
32 | 58 | end |
33 | 59 | |
34 | - should 'return all results if selected_type is nil' do | |
35 | - get :index, {'selected_type' => :person, :query => 'John'} | |
60 | + should 'return 10 results if selected_type is nil and query is nil' do | |
61 | + get :index | |
62 | + assert_response :success | |
63 | + assert_select ".search-item" , 10 | |
64 | + end | |
65 | + | |
66 | + should 'render pagination if results has more than 10' do | |
67 | + get :index | |
68 | + assert_response :success | |
69 | + assert_select ".pagination", 1 | |
70 | + end | |
71 | + | |
72 | + should 'return results filtered by selected_type' do | |
73 | + get :index, { 'selected_type' => :person} | |
36 | 74 | assert_response :success |
37 | - assert_select ".search-item" , 2 | |
75 | + assert_select ".search-item", 5 | |
38 | 76 | end |
39 | 77 | |
40 | - should 'render index' do | |
41 | - get :index, {'selected_type' => :person, :query => 'Silva'} | |
78 | + should 'return results filtered by query' do | |
79 | + get :index, { 'query' => "person_"} | |
80 | + assert_response :success | |
81 | + assert_select ".search-item", 5 | |
82 | + end | |
83 | + | |
84 | + should 'return results filtered by query with uppercase' do | |
85 | + get :index, {'query' => "PERSON_1"} | |
86 | + assert_response :success | |
87 | + assert_select ".search-item", 1 | |
88 | + end | |
89 | + | |
90 | + should 'return results filtered by query with downcase' do | |
91 | + get :index, {'query' => "person_1"} | |
92 | + assert_response :success | |
93 | + assert_select ".search-item", 1 | |
94 | + end | |
95 | + | |
96 | + should 'return new person indexed' do | |
97 | + get :index, { "selected_type" => :person} | |
98 | + assert_response :success | |
99 | + assert_select ".search-item", 5 | |
100 | + | |
101 | + object = create_user "New Person" | |
102 | + Person.import | |
103 | + sleep 1 | |
104 | + | |
105 | + get :index, { "selected_type" => :person} | |
106 | + assert_response :success | |
107 | + assert_select ".search-item", 6 | |
108 | + end | |
109 | + | |
110 | + should 'not return person deleted' do | |
111 | + get :index, { "selected_type" => :person} | |
112 | + assert_response :success | |
113 | + assert_select ".search-item", 5 | |
114 | + | |
115 | + Person.first.delete | |
116 | + Person.import | |
117 | + sleep 1 | |
118 | + | |
119 | + get :index, { "selected_type" => :person} | |
42 | 120 | assert_response :success |
43 | - assert_select ".search-item" , 1 | |
121 | + assert_select ".search-item", 4 | |
44 | 122 | end |
45 | 123 | |
46 | 124 | should 'redirect to elasticsearch plugin when request are send to core' do | ... | ... |