[#477] User search tests. Normalized search rank in User.search.

This commit is contained in:
Ivan Tashkinov 2019-01-15 13:05:25 +03:00
parent fc965f982c
commit 5b8f9ff8c1
2 changed files with 58 additions and 7 deletions

View File

@ -704,7 +704,8 @@ def search(query, resolve \\ false, for_user \\ nil) do
ts_rank_cd( ts_rank_cd(
setweight(to_tsvector('simple', regexp_replace(nickname, '\\W', ' ', 'g')), 'A') || setweight(to_tsvector('simple', regexp_replace(nickname, '\\W', ' ', 'g')), 'A') ||
setweight(to_tsvector('simple', regexp_replace(coalesce(name, ''), '\\W', ' ', 'g')), 'B'), setweight(to_tsvector('simple', regexp_replace(coalesce(name, ''), '\\W', ' ', 'g')), 'B'),
to_tsquery('simple', ?) to_tsquery('simple', ?),
32
) )
""", """,
^processed_query ^processed_query

View File

@ -775,13 +775,55 @@ test "User.delete() plugs any possible zombie objects" do
end end
describe "User.search" do describe "User.search" do
test "finds a user, ranking by similarity" do test "finds a user by full or partial nickname" do
_user = insert(:user, %{name: "lain"}) user = insert(:user, %{nickname: "john"})
_user_two = insert(:user, %{name: "ean"})
_user_three = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social"})
user_four = insert(:user, %{nickname: "lain@pleroma.soykaf.com"})
assert user_four == User.search("lain@ple") |> List.first() |> Map.put(:search_rank, nil) Enum.each(["john", "jo", "j"], fn query ->
assert user == User.search(query) |> List.first() |> Map.put(:search_rank, nil)
end)
end
test "finds a user by full or partial name" do
user = insert(:user, %{name: "John Doe"})
Enum.each(["John Doe", "JOHN", "doe", "j d", "j", "d"], fn query ->
assert user == User.search(query) |> List.first() |> Map.put(:search_rank, nil)
end)
end
test "finds users, preferring nickname matches over name matches" do
u1 = insert(:user, %{name: "lain", nickname: "nick1"})
u2 = insert(:user, %{nickname: "lain", name: "nick1"})
assert [u2.id, u1.id] == Enum.map(User.search("lain"), & &1.id)
end
test "finds users, considering density of matched tokens" do
u1 = insert(:user, %{name: "Bar Bar plus Word Word"})
u2 = insert(:user, %{name: "Word Word Bar Bar Bar"})
assert [u2.id, u1.id] == Enum.map(User.search("bar word"), & &1.id)
end
test "finds users, ranking by similarity" do
u1 = insert(:user, %{name: "lain"})
_u2 = insert(:user, %{name: "ean"})
u3 = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social"})
u4 = insert(:user, %{nickname: "lain@pleroma.soykaf.com"})
assert [u4.id, u3.id, u1.id] == Enum.map(User.search("lain@ple"), & &1.id)
end
test "finds users, boosting ranks of friends and followers" do
u1 = insert(:user)
u2 = insert(:user, %{name: "Doe"})
follower = insert(:user, %{name: "Doe"})
friend = insert(:user, %{name: "Doe"})
{:ok, follower} = User.follow(follower, u1)
{:ok, u1} = User.follow(u1, friend)
assert [friend.id, follower.id, u2.id] == Enum.map(User.search("doe", false, u1), & &1.id)
end end
test "finds a user whose name is nil" do test "finds a user whose name is nil" do
@ -793,6 +835,14 @@ test "finds a user whose name is nil" do
|> List.first() |> List.first()
|> Map.put(:search_rank, nil) |> Map.put(:search_rank, nil)
end end
test "does not yield false-positive matches" do
insert(:user, %{name: "John Doe"})
Enum.each(["mary", "a", ""], fn query ->
assert [] == User.search(query)
end)
end
end end
test "auth_active?/1 works correctly" do test "auth_active?/1 works correctly" do