match_test.rb
3.77 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
require_relative "test_helper"
class TestMatch < Minitest::Unit::TestCase
# exact
def test_match
store_names ["Whole Milk", "Fat Free Milk", "Milk"]
assert_search "milk", ["Milk", "Whole Milk", "Fat Free Milk"]
end
def test_case
store_names ["Whole Milk", "Fat Free Milk", "Milk"]
assert_search "MILK", ["Milk", "Whole Milk", "Fat Free Milk"]
end
def test_cheese_space_in_index
store_names ["Pepper Jack Cheese Skewers"]
assert_search "pepperjack cheese skewers", ["Pepper Jack Cheese Skewers"]
end
def test_cheese_space_in_query
store_names ["Pepperjack Cheese Skewers"]
assert_search "pepper jack cheese skewers", ["Pepperjack Cheese Skewers"]
end
def test_middle_token
store_names ["Dish Washer Amazing Organic Soap"]
assert_search "dish soap", ["Dish Washer Amazing Organic Soap"]
end
def test_percent
store_names ["1% Milk", "2% Milk", "Whole Milk"]
assert_search "1%", ["1% Milk"]
end
# ascii
def test_jalapenos
store_names ["Jalapeño"]
assert_search "jalapeno", ["Jalapeño"]
end
# stemming
def test_stemming
store_names ["Whole Milk", "Fat Free Milk", "Milk"]
assert_search "milks", ["Milk", "Whole Milk", "Fat Free Milk"]
end
# fuzzy
def test_misspelling_sriracha
store_names ["Sriracha"]
assert_search "siracha", ["Sriracha"]
end
def test_short_word
store_names ["Finn"]
assert_search "fin", ["Finn"]
end
def test_edit_distance
store_names ["Bingo"]
assert_search "bin", []
assert_search "bing", ["Bingo"]
assert_search "bingoo", ["Bingo"]
assert_search "bingooo", []
assert_search "ringo", ["Bingo"]
assert_search "mango", []
store_names ["thisisareallylongword"]
assert_search "thisisareallylongwor", ["thisisareallylongword"] # missing letter
assert_search "thisisareelylongword", [] # edit distance = 2
end
def test_misspelling_tabasco
store_names ["Tabasco"]
assert_search "tobasco", ["Tabasco"]
end
def test_misspelling_zucchini
store_names ["Zucchini"]
assert_search "zuchini", ["Zucchini"]
end
def test_misspelling_ziploc
store_names ["Ziploc"]
assert_search "zip lock", ["Ziploc"]
end
# spaces
def test_spaces_in_field
store_names ["Red Bull"]
assert_search "redbull", ["Red Bull"]
end
def test_spaces_in_query
store_names ["Dishwasher"]
assert_search "dish washer", ["Dishwasher"]
end
def test_spaces_three_words
store_names ["Dish Washer Soap", "Dish Washer"]
assert_search "dish washer soap", ["Dish Washer Soap"]
end
def test_spaces_stemming
store_names ["Almond Milk"]
assert_search "almondmilks", ["Almond Milk"]
end
# autocomplete
def test_autocomplete
store_names ["Hummus"]
assert_search "hum", ["Hummus"], autocomplete: true
end
def test_autocomplete_two_words
store_names ["Organic Hummus"]
assert_search "hum", [], autocomplete: true
end
def test_autocomplete_fields
store_names ["Hummus"]
assert_search "hum", ["Hummus"], autocomplete: true, fields: [:name]
end
# suggest
def test_suggest
store_names ["Great White Shark", "Hammerhead Shark", "Tiger Shark"]
assert_suggest "How Big is a Tigre Shar?", "how big is a tiger shark?"
end
def test_suggest_perfect
store_names ["Tiger Shark", "Great White Shark"]
assert_suggest "Tiger Shark", nil # no correction
end
def test_suggest_phrase
store_names ["Tiger Shark", "Sharp Teeth", "Sharp Mind"]
assert_suggest "How to catch a tiger shar", "how to catch a tiger shark"
end
def test_suggest_without_option
assert_raises(RuntimeError){ Product.search("hi").suggestion }
end
protected
def assert_suggest(term, expected)
assert_equal expected, Product.search(term, suggest: true).suggestion
end
end