misspellings_test.rb
3.69 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
require_relative "test_helper"
class MisspellingsTest < Minitest::Test
def test_false
store_names ["abc", "abd", "aee"]
assert_search "abc", ["abc"], misspellings: false
end
def test_distance
store_names ["abbb", "aabb"]
assert_search "aaaa", ["aabb"], misspellings: {distance: 2}
end
def test_prefix_length
store_names ["ap", "api", "apt", "any", "nap", "ah", "ahi"]
if Searchkick.server_below?("8.0.0")
assert_search "ap", ["ap"], misspellings: {prefix_length: 2}
else
assert_search "ap", ["ap", "api", "apt"], misspellings: {prefix_length: 2}
end
assert_search "api", ["ap", "api", "apt"], misspellings: {prefix_length: 2}
end
def test_prefix_length_operator
store_names ["ap", "api", "apt", "any", "nap", "ah", "aha"]
if Searchkick.server_below?("8.0.0")
assert_search "ap ah", ["ap", "ah"], operator: "or", misspellings: {prefix_length: 2}
else
assert_search "ap ah", ["ap", "ah", "api", "apt", "aha"], operator: "or", misspellings: {prefix_length: 2}
end
assert_search "api ahi", ["ap", "api", "apt", "ah", "aha"], operator: "or", misspellings: {prefix_length: 2}
end
def test_fields_operator
store [
{name: "red", color: "red"},
{name: "blue", color: "blue"},
{name: "cyan", color: "blue green"},
{name: "magenta", color: "red blue"},
{name: "green", color: "green"}
]
assert_search "red blue", ["red", "blue", "cyan", "magenta"], operator: "or", fields: ["color"], misspellings: false
end
def test_below_unmet
store_names ["abc", "abd", "aee"]
assert_search "abc", ["abc", "abd"], misspellings: {below: 2}
end
def test_below_unmet_result
store_names ["abc", "abd", "aee"]
assert Product.search("abc", misspellings: {below: 2}).misspellings?
end
def test_below_met
store_names ["abc", "abd", "aee"]
assert_search "abc", ["abc"], misspellings: {below: 1}
end
def test_below_met_result
store_names ["abc", "abd", "aee"]
assert !Product.search("abc", misspellings: {below: 1}).misspellings?
end
def test_field_correct_spelling_still_works
store [{name: "Sriracha", color: "blue"}]
assert_misspellings "Sriracha", ["Sriracha"], {fields: [:name, :color]}
assert_misspellings "blue", ["Sriracha"], {fields: [:name, :color]}
end
def test_field_enabled
store [{name: "Sriracha", color: "blue"}]
assert_misspellings "siracha", ["Sriracha"], {fields: [:name]}
assert_misspellings "clue", ["Sriracha"], {fields: [:color]}
end
def test_field_disabled
store [{name: "Sriracha", color: "blue"}]
assert_misspellings "siracha", [], {fields: [:color]}
assert_misspellings "clue", [], {fields: [:name]}
end
def test_field_with_transpositions
store [{name: "Sriracha", color: "blue"}]
assert_misspellings "lbue", [], {transpositions: false, fields: [:color]}
end
def test_field_with_edit_distance
store [{name: "Sriracha", color: "blue"}]
assert_misspellings "crue", ["Sriracha"], {edit_distance: 2, fields: [:color]}
end
def test_field_multiple
store [
{name: "Greek Yogurt", color: "white"},
{name: "Green Onions", color: "yellow"}
]
assert_misspellings "greed", ["Greek Yogurt", "Green Onions"], {fields: [:name, :color]}
assert_misspellings "mellow", ["Green Onions"], {fields: [:name, :color]}
end
def test_field_requires_explicit_search_fields
store_names ["Sriracha"]
assert_raises(ArgumentError) do
assert_search "siracha", ["Sriracha"], {misspellings: {fields: [:name]}}
end
end
def test_field_word_start
store_names ["Sriracha"]
assert_search "siracha", ["Sriracha"], fields: [{name: :word_middle}], misspellings: {fields: [:name]}
end
end