forked from angelf/escargot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_test.rb
73 lines (58 loc) · 2.29 KB
/
search_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
require 'test_helper'
require File.dirname(__FILE__) + '/test_helper.rb'
# tests the behaviour of the index creation tasks that run locally (in the "simple" mode)
# without real time support
class BasicSearchTest < Test::Unit::TestCase
load_schema
class ::User < ActiveRecord::Base
elastic_index :updates => false, :mapping => {
:properties => {
:country_code => {:type => "string", :index => "not_analyzed"}
}
}
end
def setup
User.delete_all
User.delete_index
User.create(:name => 'John the Long', :country_code => 'ca')
User.create(:name => 'Peter the Young', :created_at => Date.today, :country_code => 'uk')
User.create(:name => 'Peter the Old', :created_at => Date.today - 1, :country_code => 'us')
User.create(:name => 'Bob the Skinny', :country_code => 'ca')
User.create(:name => 'John the Skinny Too', :country_code => 'it')
User.create(:name => 'Jamie the Amazing Flying Machine', :country_code => 'es')
Escargot::LocalIndexing.create_index_for_model(User)
end
def test_search_count
results = User.search("peter")
assert_equal results.total_entries, 2
assert_equal User.search_count("peter"), 2
end
def test_facets
assert_equal User.facets(:country_code)[:country_code]["ca"], 2
facets = User.facets([:name, :country_code], :query => "LONG or SKINNY", :size => 100)
assert_equal facets[:name]["john"], 2
assert_equal facets[:country_code]["it"], 1
end
def test_facets_size
assert_equal User.facets(:name)[:name].keys.size, 10
assert_equal User.facets(:name, :size => 1000)[:name].keys.size, 12
end
def test_search
results = User.search("peter")
assert_equal [results.first.name, results.second.name].sort, ['Peter the Old', 'Peter the Young']
end
def test_wildcard
results = User.search("*")
assert_equal results.total_entries, 6
end
def test_paging
results = User.search("*", :per_page => 3, :page => 2)
assert_equal 6, results.total_entries
assert_equal 2, results.current_page
assert_equal 3, results.per_page
end
def test_sort
assert_equal User.search("peter", :sort => 'created_at').first.name, "Peter the Old"
assert_equal User.search("peter", :sort => 'created_at:desc').first.name, "Peter the Young"
end
end