forked from ankane/searchkick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuggest_test.rb
75 lines (60 loc) · 1.96 KB
/
suggest_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
require_relative "test_helper"
class TestSuggest < Minitest::Test
def test_basic
store_names ["Great White Shark", "Hammerhead Shark", "Tiger Shark"]
assert_suggest "How Big is a Tigre Shar", "how big is a tiger shark", fields: [:name]
end
def test_perfect
store_names ["Tiger Shark", "Great White Shark"]
assert_suggest "Tiger Shark", nil, fields: [:name] # no correction
end
def test_phrase
store_names ["Big Tiger Shark", "Tiger Sharp Teeth", "Tiger Sharp Mind"]
assert_suggest "How to catch a big tiger shar", "how to catch a big tiger shark", fields: [:name]
end
def test_without_option
assert_raises(RuntimeError){ Product.search("hi").suggestions }
end
def test_multiple_fields
store [
{name: "Shark", color: "Sharp"}
]
assert_suggest_all "shar", ["shark", "sharp"]
end
def test_multiple_fields_highest_score_first
store [
{name: "Tiger Shark", color: "Sharp"}
]
assert_suggest "tiger shar", "tiger shark"
end
def test_multiple_fields_same_value
store [
{name: "Shark", color: "Shark"}
]
assert_suggest_all "shar", ["shark"]
end
def test_fields_option
store [
{name: "Shark", color: "Sharp"}
]
assert_suggest_all "shar", ["shark"], fields: [:name]
end
def test_fields_option_multiple
store [
{name: "Shark"}
]
assert_suggest "shar", "shark", fields: [:name, :unknown]
end
def test_fields_word_start
store_names ["Great White Shark", "Hammerhead Shark", "Tiger Shark"]
assert_suggest "How Big is a Tigre Shar", "how big is a tiger shark", fields: [{name: :word_start}]
end
protected
def assert_suggest(term, expected, options = {})
assert_equal expected, Product.search(term, options.merge(suggest: true)).suggestions.first
end
# any order
def assert_suggest_all(term, expected, options = {})
assert_equal expected.sort, Product.search(term, options.merge(suggest: true)).suggestions.sort
end
end