cirandas.net

ref: master

plugins/elasticsearch/test/unit/elasticsearch_helper_test.rb


 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
require_relative '../test_helper'
require_relative '../../helpers/elasticsearch_helper.rb'

class ElasticsearchHelperTest < ActiveSupport::TestCase

  include ElasticsearchTestHelper
  include ElasticsearchHelper

  attr_accessor :params

  def indexed_models
    [Person,TextArticle,UploadedFile,Community,Event]
  end

  def create_instances
    create_user "Jose Abreu"
    create_user "Joana Abreu"
    create_user "Joao Abreu"
    create_user "Ana Abreu"
  end

  should 'return default_per_page when nil is passed' do
    assert_not_nil default_per_page nil
    assert_equal 10, default_per_page(nil)
  end

  should 'return default_per_page when per_page is passed' do
    assert_equal 15, default_per_page(15)
  end

  should 'have indexed_models in searchable_models' do
    assert_equivalent indexed_models, searchable_models
  end

  should 'return fields from models using weight' do
    class StubClass
      SEARCHABLE_FIELDS = {:name  => {:weight => 10},
                           :login =>  {:weight => 20},
                           :description => {:weight => 2}}
    end

    expected = ["name^10", "login^20", "description^2"]
    assert_equivalent expected, fields_from_models([StubClass])
  end

  should 'search from model Person sorted by Alphabetic' do
    self.params= {:selected_type => 'person',
                  :filter => 'lexical',
                  :query => "Abreu",
                  :per_page => 4}

    result = process_results
    assert_equal ["Ana Abreu","Joana Abreu","Joao Abreu","Jose Abreu"], result.map(&:name)
  end

  should 'search from model Person sorted by More Recent' do
    self.params= {:selected_type => 'person',
                  :filter => 'more_recent',
                  :query => 'ABREU',
                  :per_page => 4}

    result = process_results
    assert_equal ["Ana Abreu","Joao Abreu","Joana Abreu","Jose Abreu"], result.map(&:name)
  end

  should 'search from model Person sorted by Relevance' do
    self.params= {:selected_type => 'person',
                  :query => 'JOA BREU',
                  :per_page => 4}

    result = process_results
    assert_equal ["Joana Abreu", "Joao Abreu"], result.map(&:name)
  end

  should 'have sort in get_query return if has the option sort_by ' do
    self.params= {}
    assert get_query("", sort_by: :more_popular).keys.include?(:sort)
  end

end