Adds fuzzy string matching and prefix matching

This commit is contained in:
Albert Yi
2018-08-24 12:04:49 -07:00
parent 68c30961ac
commit 66e413b540
6 changed files with 245 additions and 37 deletions

View File

@@ -0,0 +1,101 @@
require 'test_helper'
class TagAutocompleteTest < ActiveSupport::TestCase
subject { TagAutocomplete }
context "#search_exact" do
setup do
@tags = [
create(:tag, name: "abcdef", post_count: 1),
create(:tag, name: "abczzz", post_count: 2),
create(:tag, name: "abcyyy", post_count: 0),
create(:tag, name: "bbbbbb")
]
end
should "find the tags" do
expected = [
@tags[1],
@tags[0]
].map(&:name)
assert_equal(expected, subject.search_exact("abc", 3).map(&:name))
end
end
context "#search_fuzzy" do
setup do
@tags = [
create(:tag, name: "abcdef", post_count: 1),
create(:tag, name: "abcdzz", post_count: 2),
# one char mismatch
create(:tag, name: "abcezz", post_count: 2),
# too long
create(:tag, name: "abcdefghijk", post_count: 2),
# wrong prefix
create(:tag, name: "bbcdef", post_count: 2),
# zero post count
create(:tag, name: "abcdyy", post_count: 0),
# completely different
create(:tag, name: "bbbbbb")
]
end
should "find the tags" do
expected = [
@tags[1],
@tags[2],
@tags[0]
].map(&:name)
assert_equal(expected, subject.search_fuzzy("abcd", 3).map(&:name))
end
end
context "#search_prefix" do
setup do
@tags = [
create(:tag, name: "abcdef", post_count: 1),
create(:tag, name: "alpha_beta_cat", post_count: 2),
create(:tag, name: "alpha_beta_dat", post_count: 0),
create(:tag, name: "alpha_beta_(cane)", post_count: 2),
create(:tag, name: "alpha_beta/cane", post_count: 2)
]
end
should "find the tags" do
expected = [
@tags[1],
@tags[3],
@tags[4]
].map(&:name)
assert_equal(expected, subject.search_prefix("abc", 3).map(&:name))
end
end
context "#search_aliases" do
setup do
@user = create(:user)
@tags = [
create(:tag, name: "/abc", post_count: 0),
create(:tag, name: "abcdef", post_count: 1),
create(:tag, name: "zzzzzz", post_count: 1),
]
as_user do
@aliases = [
create(:tag_alias, antecedent_name: "/abc", consequent_name: "abcdef", status: "active", post_count: 1)
]
end
end
should "find the tags" do
results = subject.search_aliases("/abc", 3)
assert_equal(1, results.size)
assert_equal("abcdef", results[0].name)
assert_equal("/abc", results[0].antecedent_name)
end
end
end

View File

@@ -100,11 +100,11 @@ class TagImplicationTest < ActiveSupport::TestCase
end
should "update its descendants on save" do
ti1 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
ti2 = FactoryBot.create(:tag_implication, :antecedent_name => "ccc", :consequent_name => "ddd")
ti1 = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb", :status => "active")
ti2 = FactoryBot.create(:tag_implication, :antecedent_name => "ccc", :consequent_name => "ddd", :status => "active")
ti1.reload
ti2.reload
ti2.update_attributes(
ti2.update(
:antecedent_name => "bbb"
)
ti1.reload