mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-19 11:45:01 +00:00
[#477] User search tests. Normalized search rank in User.search.
This commit is contained in:
parent
fc965f982c
commit
5b8f9ff8c1
2 changed files with 58 additions and 7 deletions
|
@ -704,7 +704,8 @@ defmodule Pleroma.User 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
|
||||||
|
|
|
@ -775,13 +775,55 @@ defmodule Pleroma.UserTest 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 @@ defmodule Pleroma.UserTest 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
|
||||||
|
|
Loading…
Reference in a new issue