Commit c3406cebf6614a4aaeb921c60409c6603d550d9f

Authored by Macartur Sousa
1 parent 13196ae5

Refactored structure and tests

Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing 33 changed files with 697 additions and 699 deletions   Show diff stats
plugins/elasticsearch/helpers/nested_helper/environment.rb
... ... @@ -1,25 +0,0 @@
1   -module NestedEnvironment
2   -
3   - def self.environment_hash
4   - {
5   - :id => { type: :integer },
6   - :is_default => {type: :boolean }
7   - }
8   - end
9   -
10   - def self.environment_filter environment=1
11   - {
12   - query: {
13   - nested: {
14   - path: "environment",
15   - query: {
16   - bool: {
17   - must: { term: { "environment.id" => environment } },
18   - }
19   - }
20   - }
21   - }
22   - }
23   - end
24   -
25   -end
plugins/elasticsearch/helpers/nested_helper/profile.rb
... ... @@ -1,29 +0,0 @@
1   -module NestedProfile
2   -
3   - def self.hash
4   - {
5   - :id => { type: :integer },
6   - :visible => { type: :boolean },
7   - :public_profile => { type: :boolean }
8   - }
9   - end
10   -
11   - def self.filter
12   - {
13   - query: {
14   - nested: {
15   - path: "profile",
16   - query: {
17   - bool: {
18   - must:[
19   - { term: { "profile.visible" => true } },
20   - { term: { "profile.public_profile" => true } }
21   - ],
22   - }
23   - }
24   - }
25   - }
26   - }
27   - end
28   -
29   -end
plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb
... ... @@ -1,100 +0,0 @@
1   -require_relative '../nested_helper/environment'
2   -
3   -module ElasticsearchIndexedModel
4   -
5   - def self.included base
6   - base.send :include, Elasticsearch::Model
7   - base.send :include, Elasticsearch::Model::Callbacks
8   -
9   - base.send :index_name, "#{Rails.env}_#{base.index_name}"
10   -
11   - base.extend ClassMethods
12   - base.send :include, InstanceMethods
13   -
14   - base.class_eval do
15   - settings index: { number_of_shards: 1 } do
16   - mappings dynamic: 'false' do
17   - base.indexed_fields.each do |field, value|
18   - type = value[:type].presence
19   -
20   - if type == :nested
21   - indexes(field, type: type) do
22   - value[:hash].each do |hash_field, hash_value|
23   - indexes(hash_field, base.indexes_as_hash(hash_field,hash_value[:type].presence))
24   - end
25   - end
26   - else
27   - indexes(field, base.indexes_as_hash(field,type))
28   - end
29   - print '.'
30   - end
31   - end
32   -
33   - base.__elasticsearch__.client.indices.delete \
34   - index: base.index_name rescue nil
35   - base.__elasticsearch__.client.indices.create \
36   - index: base.index_name,
37   - body: {
38   - settings: base.settings.to_hash,
39   - mappings: base.mappings.to_hash
40   - }
41   - end
42   - end
43   - base.send :import
44   - end
45   -
46   - module ClassMethods
47   -
48   - def indexes_as_hash(name, type)
49   - hash = {}
50   - if type.nil?
51   - hash[:fields] = raw_field(name, type)
52   - else
53   - hash[:type] = type if not type.nil?
54   - end
55   - hash
56   - end
57   -
58   - def raw_field name, type
59   - {
60   - raw: {
61   - type: "string",
62   - index: "not_analyzed"
63   - }
64   - }
65   - end
66   -
67   - def indexed_fields
68   - fields = {
69   - :id => {type: :integer },
70   - :environment => {type: :nested, hash: NestedEnvironment::environment_hash },
71   - :category_ids => {type: :integer },
72   - :created_at => {type: :date }
73   - }
74   - fields.update(self::SEARCHABLE_FIELDS)
75   - fields.update(self.control_fields)
76   - fields
77   - end
78   -
79   - end
80   -
81   - module InstanceMethods
82   - def as_indexed_json options={}
83   - attrs = {}
84   -
85   - self.class.indexed_fields.each do |field, value|
86   - type = value[:type].presence
87   - if type == :nested
88   - attrs[field] = {}
89   - value[:hash].each do |hash_field, hash_value|
90   - attrs[field][hash_field] = self.send(field).send(hash_field)
91   - end
92   - else
93   - attrs[field] = self.send(field)
94   - end
95   - end
96   - attrs.as_json
97   - end
98   - end
99   -
100   -end
plugins/elasticsearch/helpers/searchable_model/filter.rb
... ... @@ -1,43 +0,0 @@
1   -require_relative '../nested_helper/environment'
2   -
3   -module Filter
4   -
5   - def self.included base
6   - base.extend ClassMethods
7   - base.send :include, InstanceMethods
8   - end
9   -
10   - module ClassMethods
11   -
12   - def filter options={}
13   - environment = options[:environment].presence
14   -
15   - result_filter = {}
16   - result_filter[:indices] = {:index => self.index_name, :no_match_filter => "none" }
17   - result_filter[:indices][:filter] = { :bool => self.filter_bool(environment) }
18   -
19   - result_filter
20   - end
21   -
22   - def filter_bool environment
23   - result_filter = {}
24   -
25   - result_filter[:must] = [ NestedEnvironment::environment_filter(environment) ]
26   -
27   - self.nested_filter.each {|filter| result_filter[:must].append(filter)} if self.respond_to? :nested_filter
28   - self.must.each {|filter| result_filter[:must].append(filter) } if self.respond_to? :must
29   -
30   - result_filter[:should] = self.should if self.respond_to? :should
31   - result_filter[:must_not] = self.must_not if self.respond_to? :must_not
32   -
33   - result_filter
34   - end
35   -
36   -
37   - end
38   -
39   - module InstanceMethods
40   -
41   - end
42   -
43   -end
plugins/elasticsearch/helpers/searchable_model_helper.rb
... ... @@ -1,9 +0,0 @@
1   -require_relative './searchable_model/elasticsearch_indexed_model'
2   -require_relative './searchable_model/filter'
3   -
4   -module SearchableModelHelper
5   - def self.included base
6   - base.send :include, ElasticsearchIndexedModel
7   - base.send :include, Filter
8   - end
9   -end
plugins/elasticsearch/lib/ext/community.rb
1 1 require_dependency 'community'
2   -require_relative '../../helpers/searchable_model_helper'
  2 +require_relative '../searchable_model_helper'
3 3  
4 4 class Community
5 5  
... ...
plugins/elasticsearch/lib/ext/event.rb
1 1 require_dependency 'event'
2 2  
3   -require_relative '../../helpers/searchable_model_helper'
4   -require_relative '../../helpers/nested_helper/profile'
  3 +require_relative '../searchable_model_helper'
  4 +require_relative '../nested_helper/profile'
5 5  
6 6 class Event
7 7 #TODO: o filtro é feito de forma diferente do artigo
... ...
plugins/elasticsearch/lib/ext/person.rb
1 1 require_dependency 'person'
2 2  
3   -require_relative '../../helpers/searchable_model_helper'
  3 +require_relative '../searchable_model_helper'
4 4  
5 5 class Person
6 6  
... ...
plugins/elasticsearch/lib/ext/text_article.rb
... ... @@ -3,8 +3,8 @@ require_dependency &#39;raw_html_article&#39;
3 3 require_dependency 'tiny_mce_article'
4 4 require_dependency 'text_article'
5 5  
6   -require_relative '../../helpers/searchable_model_helper'
7   -require_relative '../../helpers/nested_helper/profile'
  6 +require_relative '../searchable_model_helper'
  7 +require_relative '../nested_helper/profile'
8 8  
9 9 class TextArticle
10 10  
... ...
plugins/elasticsearch/lib/ext/uploaded_file.rb
1 1 require_dependency 'uploaded_file'
2 2  
3   -require_relative '../../helpers/searchable_model_helper'
4   -require_relative '../../helpers/nested_helper/profile'
  3 +require_relative '../searchable_model_helper'
  4 +require_relative '../nested_helper/profile'
5 5  
6 6 class UploadedFile
7 7 def self.control_fields
... ...
plugins/elasticsearch/lib/nested_helper/environment.rb 0 → 100644
... ... @@ -0,0 +1,25 @@
  1 +module NestedEnvironment
  2 +
  3 + def self.hash
  4 + {
  5 + :id => { type: :integer },
  6 + :is_default => {type: :boolean }
  7 + }
  8 + end
  9 +
  10 + def self.filter environment=1
  11 + {
  12 + query: {
  13 + nested: {
  14 + path: "environment",
  15 + query: {
  16 + bool: {
  17 + must: { term: { "environment.id" => environment } },
  18 + }
  19 + }
  20 + }
  21 + }
  22 + }
  23 + end
  24 +
  25 +end
... ...
plugins/elasticsearch/lib/nested_helper/profile.rb 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +module NestedProfile
  2 +
  3 + def self.hash
  4 + {
  5 + :id => { type: :integer },
  6 + :visible => { type: :boolean },
  7 + :public_profile => { type: :boolean }
  8 + }
  9 + end
  10 +
  11 + def self.filter
  12 + {
  13 + query: {
  14 + nested: {
  15 + path: "profile",
  16 + query: {
  17 + bool: {
  18 + must:[
  19 + { term: { "profile.visible" => true } },
  20 + { term: { "profile.public_profile" => true } }
  21 + ],
  22 + }
  23 + }
  24 + }
  25 + }
  26 + }
  27 + end
  28 +
  29 +end
... ...
plugins/elasticsearch/lib/searchable_model/elasticsearch_indexed_model.rb 0 → 100644
... ... @@ -0,0 +1,99 @@
  1 +require_relative '../nested_helper/environment'
  2 +
  3 +module ElasticsearchIndexedModel
  4 +
  5 + def self.included base
  6 + base.send :include, Elasticsearch::Model
  7 + base.send :include, Elasticsearch::Model::Callbacks
  8 +
  9 + base.send :index_name, "#{Rails.env}_#{base.index_name}"
  10 +
  11 + base.extend ClassMethods
  12 + base.send :include, InstanceMethods
  13 +
  14 + base.class_eval do
  15 + settings index: { number_of_shards: 1 } do
  16 + mappings dynamic: 'false' do
  17 + base.indexed_fields.each do |field, value|
  18 + type = value[:type].presence
  19 +
  20 + if type == :nested
  21 + indexes(field, type: type) do
  22 + value[:hash].each do |hash_field, hash_value|
  23 + indexes(hash_field, base.indexes_as_hash(hash_field,hash_value[:type].presence))
  24 + end
  25 + end
  26 + else
  27 + indexes(field, base.indexes_as_hash(field,type))
  28 + end
  29 + print '.'
  30 + end
  31 + end
  32 +
  33 + base.__elasticsearch__.client.indices.delete \
  34 + index: base.index_name rescue nil
  35 + base.__elasticsearch__.client.indices.create \
  36 + index: base.index_name,
  37 + body: {
  38 + settings: base.settings.to_hash,
  39 + mappings: base.mappings.to_hash
  40 + }
  41 + end
  42 + end
  43 + base.send :import
  44 + end
  45 +
  46 + module ClassMethods
  47 +
  48 + def indexes_as_hash(name, type)
  49 + hash = {}
  50 + if type.nil?
  51 + hash[:fields] = raw_field(name, type)
  52 + else
  53 + hash[:type] = type if not type.nil?
  54 + end
  55 + hash
  56 + end
  57 +
  58 + def raw_field name, type
  59 + {
  60 + raw: {
  61 + type: "string",
  62 + index: "not_analyzed"
  63 + }
  64 + }
  65 + end
  66 +
  67 + def indexed_fields
  68 + fields = {
  69 + :environment => {type: :nested, hash: NestedEnvironment::hash },
  70 + :category_ids => {type: :integer },
  71 + :created_at => {type: :date }
  72 + }
  73 + fields.update(self::SEARCHABLE_FIELDS)
  74 + fields.update(self.control_fields)
  75 + fields
  76 + end
  77 +
  78 + end
  79 +
  80 + module InstanceMethods
  81 + def as_indexed_json options={}
  82 + attrs = {}
  83 +
  84 + self.class.indexed_fields.each do |field, value|
  85 + type = value[:type].presence
  86 + if type == :nested
  87 + attrs[field] = {}
  88 + value[:hash].each do |hash_field, hash_value|
  89 + attrs[field][hash_field] = self.send(field).send(hash_field)
  90 + end
  91 + else
  92 + attrs[field] = self.send(field)
  93 + end
  94 + end
  95 + attrs.as_json
  96 + end
  97 + end
  98 +
  99 +end
... ...
plugins/elasticsearch/lib/searchable_model/filter.rb 0 → 100644
... ... @@ -0,0 +1,42 @@
  1 +require_relative '../nested_helper/environment'
  2 +
  3 +module Filter
  4 +
  5 + def self.included base
  6 + base.extend ClassMethods
  7 + base.send :include, InstanceMethods
  8 + end
  9 +
  10 + module ClassMethods
  11 +
  12 + def filter options={}
  13 + environment = options[:environment].presence
  14 +
  15 + result_filter = {}
  16 + result_filter[:indices] = {:index => self.index_name, :no_match_filter => "none" }
  17 + result_filter[:indices][:filter] = { :bool => self.filter_bool(environment) }
  18 +
  19 + result_filter
  20 + end
  21 +
  22 + def filter_bool environment
  23 + result_filter = {}
  24 +
  25 + result_filter[:must] = [ NestedEnvironment::filter(environment) ]
  26 +
  27 + self.nested_filter.each {|filter| result_filter[:must].append(filter)} if self.respond_to? :nested_filter
  28 + self.must.each {|filter| result_filter[:must].append(filter) } if self.respond_to? :must
  29 +
  30 + result_filter[:should] = self.should if self.respond_to? :should
  31 + result_filter[:must_not] = self.must_not if self.respond_to? :must_not
  32 +
  33 + result_filter
  34 + end
  35 +
  36 + end
  37 +
  38 + module InstanceMethods
  39 +
  40 + end
  41 +
  42 +end
... ...
plugins/elasticsearch/lib/searchable_model_helper.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +require_relative './searchable_model/elasticsearch_indexed_model'
  2 +require_relative './searchable_model/filter'
  3 +
  4 +module SearchableModelHelper
  5 + def self.included base
  6 + base.send :include, ElasticsearchIndexedModel
  7 + base.send :include, Filter
  8 + end
  9 +end
... ...
plugins/elasticsearch/test/api/elasticsearch_plugin_api_test.rb 0 → 100644
... ... @@ -0,0 +1,52 @@
  1 +require "#{File.dirname(__FILE__)}/../test_helper"
  2 +require_relative '../../helpers/elasticsearch_helper'
  3 +
  4 +class ElasticsearchPluginApiTest < ActiveSupport::TestCase
  5 +
  6 + include ElasticsearchTestHelper
  7 + include ElasticsearchHelper
  8 +
  9 + def indexed_models
  10 + [Community, Person]
  11 + end
  12 +
  13 + def create_instances
  14 + 7.times.each {|index| create_user "person #{index}"}
  15 + 4.times.each {|index| fast_create Community, name: "community #{index}" }
  16 + end
  17 +
  18 + should 'show all types avaliable in /search/types endpoint' do
  19 + get "/api/v1/search/types"
  20 + json = JSON.parse(last_response.body)
  21 + assert_equal 200, last_response.status
  22 + assert_equal searchable_types.stringify_keys.keys, json["types"]
  23 + end
  24 +
  25 + should 'respond with endpoint /search with more than 10 results' do
  26 + get "/api/v1/search"
  27 + json = JSON.parse(last_response.body)
  28 + assert_equal 200, last_response.status
  29 + assert_equal 10, json["results"].count
  30 + end
  31 +
  32 + should 'respond with query in downcase' do
  33 + get "/api/v1/search?query=person"
  34 + json = JSON.parse(last_response.body)
  35 + assert_equal 200, last_response.status
  36 + assert_equal 7, json["results"].count
  37 + end
  38 +
  39 + should 'respond with query in uppercase' do
  40 + get "/api/v1/search?query=PERSON"
  41 + json = JSON.parse(last_response.body)
  42 + assert_equal 200, last_response.status
  43 + assert_equal 7, json["results"].count
  44 + end
  45 +
  46 + should 'respond with selected_type' do
  47 + get "/api/v1/search?selected_type=community"
  48 + json = JSON.parse(last_response.body)
  49 + assert_equal 200, last_response.status
  50 + assert_equal 4, json["results"].count
  51 + end
  52 +end
... ...
plugins/elasticsearch/test/api/elasticsearch_plugin_entities_test.rb 0 → 100644
... ... @@ -0,0 +1,124 @@
  1 +require "#{File.dirname(__FILE__)}/../test_helper"
  2 +
  3 +class ElasticsearchPluginEntitiesTest < ActiveSupport::TestCase
  4 +
  5 + include ElasticsearchTestHelper
  6 +
  7 + def indexed_models
  8 + [Person,TextArticle,UploadedFile,Community,Event]
  9 + end
  10 +
  11 + def create_instances
  12 + user = create_user "sample person", environment_id: 1
  13 +
  14 + fast_create Community, name: "sample community", created_at: 10.days.ago,updated_at: 5.days.ago, environment_id: 1
  15 +
  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 + end
  21 +
  22 + should 'show attributes from person' do
  23 + params = {:selected_type => "person" }
  24 + get "/api/v1/search?#{params.to_query}"
  25 + json= JSON.parse(last_response.body)
  26 +
  27 + expected_person = Person.find_by name: "sample person"
  28 +
  29 + assert_equal 200, last_response.status
  30 + assert_equal expected_person.id, json['results'][0]['id']
  31 + assert_equal expected_person.name, json['results'][0]['name']
  32 + assert_equal expected_person.type, json['results'][0]['type']
  33 + assert_equal "", json['results'][0]['description']
  34 + assert_equal expected_person.created_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][0]['created_at']
  35 + assert_equal expected_person.updated_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][0]['updated_at']
  36 + end
  37 +
  38 +
  39 + should 'show attributes from community' do
  40 + params = {:selected_type => "community" }
  41 + get "/api/v1/search?#{params.to_query}"
  42 + json= JSON.parse(last_response.body)
  43 +
  44 + expected_community = Community.find_by name: "sample community"
  45 +
  46 + assert_equal 200, last_response.status
  47 +
  48 + assert_equal expected_community.id, json['results'][0]['id']
  49 + assert_equal expected_community.name, json['results'][0]['name']
  50 + assert_equal expected_community.type, json['results'][0]['type']
  51 + assert_equal "", json['results'][0]['description']
  52 + assert_equal expected_community.created_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][0]['created_at']
  53 + assert_equal expected_community.updated_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][0]['updated_at']
  54 + end
  55 +
  56 + should 'show attributes from text_article' do
  57 + params = {:selected_type => "text_article" }
  58 + get "/api/v1/search?#{params.to_query}"
  59 +
  60 + json= JSON.parse(last_response.body)
  61 +
  62 + assert_equal 200, last_response.status
  63 +
  64 + expected_text_articles = TextArticle.all
  65 +
  66 + expected_text_articles.each_with_index {|object,index|
  67 + assert_equal object.id, json['results'][index]['id']
  68 + assert_equal object.name, json['results'][index]['name']
  69 + assert_equal "TextArticle", json['results'][index]['type']
  70 +
  71 + expected_author = (object.author.nil?) ? "" : object.author.name
  72 +
  73 + assert_equal expected_author, json['results'][index]['author']
  74 + assert_equal object.created_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][index]['created_at']
  75 + assert_equal object.updated_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][index]['updated_at']
  76 + }
  77 + end
  78 +
  79 + should 'show attributes from uploaded_file' do
  80 + params = {:selected_type => "uploaded_file"}
  81 + get "/api/v1/search?#{params.to_query}"
  82 +
  83 + json= JSON.parse(last_response.body)
  84 +
  85 + assert_equal 200, last_response.status
  86 +
  87 + expected_uploaded_files = UploadedFile.all
  88 + expected_uploaded_files.each_with_index {|object,index|
  89 + assert_equal object.id, json['results'][index]['id']
  90 + assert_equal object.name, json['results'][index]['name']
  91 + assert_equal object.abstract, json['results'][index]['abstract']
  92 + assert_equal "UploadedFile", json['results'][index]['type']
  93 +
  94 + expected_author = (object.author.nil?) ? "" : object.author.name
  95 + assert_equal expected_author, json['results'][index]['author']
  96 +
  97 + assert_equal object.created_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][index]['created_at']
  98 + assert_equal object.updated_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][index]['updated_at']
  99 + }
  100 + end
  101 +
  102 + should 'show attributes from event' do
  103 + params = {:selected_type => "event"}
  104 + get "/api/v1/search?#{params.to_query}"
  105 +
  106 + json= JSON.parse(last_response.body)
  107 +
  108 + assert_equal 200, last_response.status
  109 + expected_events = Event.all
  110 + expected_events.each_with_index {|object,index|
  111 + assert_equal object.id, json['results'][index]['id']
  112 + assert_equal object.name, json['results'][index]['name']
  113 + assert_equal object.abstract, json['results'][index]['abstract']
  114 + assert_equal "Event", json['results'][index]['type']
  115 +
  116 + expected_author = (object.author.nil?) ? "" : object.author.name
  117 + assert_equal expected_author, json['results'][index]['author']
  118 +
  119 + assert_equal object.created_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][index]['created_at']
  120 + assert_equal object.updated_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][index]['updated_at']
  121 + }
  122 + end
  123 +
  124 +end
... ...
plugins/elasticsearch/test/functional/elasticsearch_plugin_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,115 @@
  1 +require "#{File.dirname(__FILE__)}/../test_helper"
  2 +
  3 +class ElasticsearchPluginControllerTest < ActionController::TestCase
  4 +
  5 + include ElasticsearchTestHelper
  6 +
  7 + def indexed_models
  8 + [Community, Person]
  9 + end
  10 +
  11 + def create_instances
  12 + create_people
  13 + create_communities
  14 + end
  15 +
  16 + def create_people
  17 + 5.times do | index |
  18 + create_user "person #{index}"
  19 + end
  20 + end
  21 +
  22 + def create_communities
  23 + 6.times do | index |
  24 + fast_create Community, name: "community #{index}", created_at: Date.new
  25 + end
  26 + end
  27 +
  28 + should 'work and uses control filter variables' do
  29 + get :index
  30 + assert_response :success
  31 + assert_not_nil assigns(:searchable_types)
  32 + assert_not_nil assigns(:selected_type)
  33 + assert_not_nil assigns(:sort_types)
  34 + assert_not_nil assigns(:selected_sort)
  35 + end
  36 +
  37 + should 'return 10 results if selected_type is nil and query is nil' do
  38 + get :index
  39 + assert_response :success
  40 + assert_select ".search-item" , 10
  41 + end
  42 +
  43 + should 'render pagination if results has more than 10' do
  44 + get :index
  45 + assert_response :success
  46 + assert_select ".pagination", 1
  47 + end
  48 +
  49 + should 'return results filtered by selected_type' do
  50 + get :index, { 'selected_type' => :community}
  51 + assert_response :success
  52 + assert_select ".search-item", 6
  53 + assert_template partial: '_community_display'
  54 + end
  55 +
  56 + should 'return results filtered by query' do
  57 + get :index, { 'query' => "person"}
  58 + assert_response :success
  59 + assert_select ".search-item", 5
  60 + assert_template partial: '_person_display'
  61 + end
  62 +
  63 + should 'return results filtered by query with uppercase' do
  64 + get :index, {'query' => "PERSON 1"}
  65 + assert_response :success
  66 + assert_template partial: '_person_display'
  67 + assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} )
  68 + end
  69 +
  70 + should 'return results filtered by query with downcase' do
  71 + get :index, {'query' => "person 1"}
  72 + assert_response :success
  73 + assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} )
  74 + end
  75 +
  76 + should 'return new person indexed' do
  77 + get :index, { "selected_type" => :community}
  78 + assert_response :success
  79 + assert_select ".search-item", 6
  80 +
  81 + fast_create Community, name: "community #{7}", created_at: Date.new
  82 + Community.import
  83 + sleep 2
  84 +
  85 + get :index, { "selected_type" => :community}
  86 + assert_response :success
  87 + assert_select ".search-item", 7
  88 + end
  89 +
  90 + should 'not return person deleted' do
  91 + get :index, { "selected_type" => :community}
  92 + assert_response :success
  93 + assert_select ".search-item", 6
  94 +
  95 + Community.first.delete
  96 + Community.import
  97 +
  98 + get :index, { "selected_type" => :community}
  99 + assert_response :success
  100 + assert_select ".search-item", 5
  101 + end
  102 +
  103 + should 'redirect to elasticsearch plugin when request are send to core' do
  104 + @controller = SearchController.new
  105 + get 'index'
  106 + params = {:action => 'index', :controller => 'search'}
  107 + assert_redirected_to controller: 'elasticsearch_plugin', action: 'search', params: params
  108 + end
  109 +
  110 + should 'pass params to elastic search controller' do
  111 + get 'index', { query: 'community' }
  112 + assert_not_nil assigns(:results)
  113 + assert_template partial: '_community_display'
  114 + end
  115 +end
... ...
plugins/elasticsearch/test/unit/api/elasticsearch_plugin_api_test.rb
... ... @@ -1,52 +0,0 @@
1   -require "#{File.dirname(__FILE__)}/../../test_helper"
2   -require_relative '../../../helpers/elasticsearch_helper'
3   -
4   -class ElasticsearchPluginApiTest < ActiveSupport::TestCase
5   -
6   - include ElasticsearchTestHelper
7   - include ElasticsearchHelper
8   -
9   - def indexed_models
10   - [Community, Person]
11   - end
12   -
13   - def create_instances
14   - 7.times.each {|index| create_user "person #{index}"}
15   - 4.times.each {|index| fast_create Community, name: "community #{index}" }
16   - end
17   -
18   - should 'show all types avaliable in /search/types endpoint' do
19   - get "/api/v1/search/types"
20   - json = JSON.parse(last_response.body)
21   - assert_equal 200, last_response.status
22   - assert_equal searchable_types.stringify_keys.keys, json["types"]
23   - end
24   -
25   - should 'respond with endpoint /search with more than 10 results' do
26   - get "/api/v1/search"
27   - json = JSON.parse(last_response.body)
28   - assert_equal 200, last_response.status
29   - assert_equal 10, json["results"].count
30   - end
31   -
32   - should 'respond with query in downcase' do
33   - get "/api/v1/search?query=person"
34   - json = JSON.parse(last_response.body)
35   - assert_equal 200, last_response.status
36   - assert_equal 7, json["results"].count
37   - end
38   -
39   - should 'respond with query in uppercase' do
40   - get "/api/v1/search?query=PERSON"
41   - json = JSON.parse(last_response.body)
42   - assert_equal 200, last_response.status
43   - assert_equal 7, json["results"].count
44   - end
45   -
46   - should 'respond with selected_type' do
47   - get "/api/v1/search?selected_type=community"
48   - json = JSON.parse(last_response.body)
49   - assert_equal 200, last_response.status
50   - assert_equal 4, json["results"].count
51   - end
52   -end
plugins/elasticsearch/test/unit/api/elasticsearch_plugin_entities_test.rb
... ... @@ -1,124 +0,0 @@
1   -require "#{File.dirname(__FILE__)}/../../test_helper"
2   -
3   -class ElasticsearchPluginEntitiesTest < ActiveSupport::TestCase
4   -
5   - include ElasticsearchTestHelper
6   -
7   - def indexed_models
8   - [Person,TextArticle,UploadedFile,Community,Event]
9   - end
10   -
11   - def create_instances
12   - user = create_user "sample person", environment_id: 1
13   -
14   - fast_create Community, name: "sample community", created_at: 10.days.ago,updated_at: 5.days.ago, environment_id: 1
15   -
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   - end
21   -
22   - should 'show attributes from person' do
23   - params = {:selected_type => "person" }
24   - get "/api/v1/search?#{params.to_query}"
25   - json= JSON.parse(last_response.body)
26   -
27   - expected_person = Person.find_by name: "sample person"
28   -
29   - assert_equal 200, last_response.status
30   - assert_equal expected_person.id, json['results'][0]['id']
31   - assert_equal expected_person.name, json['results'][0]['name']
32   - assert_equal expected_person.type, json['results'][0]['type']
33   - assert_equal "", json['results'][0]['description']
34   - assert_equal expected_person.created_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][0]['created_at']
35   - assert_equal expected_person.updated_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][0]['updated_at']
36   - end
37   -
38   -
39   - should 'show attributes from community' do
40   - params = {:selected_type => "community" }
41   - get "/api/v1/search?#{params.to_query}"
42   - json= JSON.parse(last_response.body)
43   -
44   - expected_community = Community.find_by name: "sample community"
45   -
46   - assert_equal 200, last_response.status
47   -
48   - assert_equal expected_community.id, json['results'][0]['id']
49   - assert_equal expected_community.name, json['results'][0]['name']
50   - assert_equal expected_community.type, json['results'][0]['type']
51   - assert_equal "", json['results'][0]['description']
52   - assert_equal expected_community.created_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][0]['created_at']
53   - assert_equal expected_community.updated_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][0]['updated_at']
54   - end
55   -
56   - should 'show attributes from text_article' do
57   - params = {:selected_type => "text_article" }
58   - get "/api/v1/search?#{params.to_query}"
59   -
60   - json= JSON.parse(last_response.body)
61   -
62   - assert_equal 200, last_response.status
63   -
64   - expected_text_articles = TextArticle.all
65   -
66   - expected_text_articles.each_with_index {|object,index|
67   - assert_equal object.id, json['results'][index]['id']
68   - assert_equal object.name, json['results'][index]['name']
69   - assert_equal "TextArticle", json['results'][index]['type']
70   -
71   - expected_author = (object.author.nil?) ? "" : object.author.name
72   -
73   - assert_equal expected_author, json['results'][index]['author']
74   - assert_equal object.created_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][index]['created_at']
75   - assert_equal object.updated_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][index]['updated_at']
76   - }
77   - end
78   -
79   - should 'show attributes from uploaded_file' do
80   - params = {:selected_type => "uploaded_file"}
81   - get "/api/v1/search?#{params.to_query}"
82   -
83   - json= JSON.parse(last_response.body)
84   -
85   - assert_equal 200, last_response.status
86   -
87   - expected_uploaded_files = UploadedFile.all
88   - expected_uploaded_files.each_with_index {|object,index|
89   - assert_equal object.id, json['results'][index]['id']
90   - assert_equal object.name, json['results'][index]['name']
91   - assert_equal object.abstract, json['results'][index]['abstract']
92   - assert_equal "UploadedFile", json['results'][index]['type']
93   -
94   - expected_author = (object.author.nil?) ? "" : object.author.name
95   - assert_equal expected_author, json['results'][index]['author']
96   -
97   - assert_equal object.created_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][index]['created_at']
98   - assert_equal object.updated_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][index]['updated_at']
99   - }
100   - end
101   -
102   - should 'show attributes from event' do
103   - params = {:selected_type => "event"}
104   - get "/api/v1/search?#{params.to_query}"
105   -
106   - json= JSON.parse(last_response.body)
107   -
108   - assert_equal 200, last_response.status
109   - expected_events = Event.all
110   - expected_events.each_with_index {|object,index|
111   - assert_equal object.id, json['results'][index]['id']
112   - assert_equal object.name, json['results'][index]['name']
113   - assert_equal object.abstract, json['results'][index]['abstract']
114   - assert_equal "Event", json['results'][index]['type']
115   -
116   - expected_author = (object.author.nil?) ? "" : object.author.name
117   - assert_equal expected_author, json['results'][index]['author']
118   -
119   - assert_equal object.created_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][index]['created_at']
120   - assert_equal object.updated_at.strftime("%Y/%m/%d %H:%M:%S"), json['results'][index]['updated_at']
121   - }
122   - end
123   -
124   -end
plugins/elasticsearch/test/unit/community_test.rb 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +require "#{File.dirname(__FILE__)}/../test_helper"
  2 +
  3 +class CommunityTest < ActiveSupport::TestCase
  4 +
  5 + include ElasticsearchTestHelper
  6 +
  7 + def indexed_models
  8 + [Community]
  9 + end
  10 +
  11 + should 'index searchable fields for Community model' do
  12 + Community::SEARCHABLE_FIELDS.each do |key, value|
  13 + assert_includes indexed_fields(Community), key
  14 + end
  15 + end
  16 +
  17 + should 'index control fields for Community model' do
  18 + Community::control_fields.each do |key, value|
  19 + assert_includes indexed_fields(Community), key
  20 + assert_equal indexed_fields(Community)[key][:type], value[:type] || 'string'
  21 + end
  22 + end
  23 +
  24 +end
... ...
plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb
... ... @@ -1,115 +0,0 @@
1   -require "#{File.dirname(__FILE__)}/../../test_helper"
2   -
3   -class ElasticsearchPluginControllerTest < ActionController::TestCase
4   -
5   - include ElasticsearchTestHelper
6   -
7   - def indexed_models
8   - [Community, Person]
9   - end
10   -
11   - def create_instances
12   - create_people
13   - create_communities
14   - end
15   -
16   - def create_people
17   - 5.times do | index |
18   - create_user "person #{index}"
19   - end
20   - end
21   -
22   - def create_communities
23   - 6.times do | index |
24   - fast_create Community, name: "community #{index}", created_at: Date.new
25   - end
26   - end
27   -
28   - should 'work and uses control filter variables' do
29   - get :index
30   - assert_response :success
31   - assert_not_nil assigns(:searchable_types)
32   - assert_not_nil assigns(:selected_type)
33   - assert_not_nil assigns(:sort_types)
34   - assert_not_nil assigns(:selected_sort)
35   - end
36   -
37   - should 'return 10 results if selected_type is nil and query is nil' do
38   - get :index
39   - assert_response :success
40   - assert_select ".search-item" , 10
41   - end
42   -
43   - should 'render pagination if results has more than 10' do
44   - get :index
45   - assert_response :success
46   - assert_select ".pagination", 1
47   - end
48   -
49   - should 'return results filtered by selected_type' do
50   - get :index, { 'selected_type' => :community}
51   - assert_response :success
52   - assert_select ".search-item", 6
53   - assert_template partial: '_community_display'
54   - end
55   -
56   - should 'return results filtered by query' do
57   - get :index, { 'query' => "person"}
58   - assert_response :success