Skip to content

Commit

Permalink
Fix flaky test (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
PragTob authored Nov 13, 2024
1 parent 57cd0ac commit b1852b6
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions test/mindwendel/accounts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ defmodule Mindwendel.AccountsTest do
Factory.insert!(:idea, brainstorming: old_brainstorming, user: old_user)

# we want to make sure that the database is not handling this with a foreign key restraint, but rather that it's handled in the app:
{_result, log} =
with_log(fn ->
Accounts.delete_inactive_users()
end)
log = capture_log(fn -> Accounts.delete_inactive_users() end)

assert Repo.exists?(from u in User, where: u.id == ^old_user.id)
assert log == ""

# The code only logs normal messages on `info` level which tests don't print (only warning and above)
# since we're run async we can't assert an empty log (picks up log messages from other tests as well),
# but we can assert we're not hitting the messages we know we'll throw.
refute log =~ ~r/error.*delete.*inactive.*user.*#{old_user.id}/i
end

test "does not delete the user a user is a creating user of a brainstorming", %{
Expand All @@ -107,13 +108,10 @@ defmodule Mindwendel.AccountsTest do
)

# we want to make sure that the database is not handling this with a foreign key restraint, but rather that it's handled in the app:
{_result, log} =
with_log(fn ->
Accounts.delete_inactive_users()
end)
log = capture_log(fn -> Accounts.delete_inactive_users() end)

assert Repo.exists?(from u in User, where: u.id == ^old_user.id)
assert log == ""
refute log =~ ~r/error.*delete.*inactive.*user.*#{old_user.id}/i
end

test "does not delete the user a user is a moderating user of a brainstorming", %{
Expand All @@ -125,16 +123,13 @@ defmodule Mindwendel.AccountsTest do
)

# we want to make sure that the database is not handling this with a foreign key restraint, but rather that it's handled in the app:
log =
capture_log(fn ->
Accounts.delete_inactive_users()
end)
log = capture_log(fn -> Accounts.delete_inactive_users() end)

assert Repo.exists?(from u in User, where: u.id == ^old_user.id)
# we don't expect actual logs but due to async running other tests may emit logs at
# the same time and so we want to make sure here that no delete logs around the user
# are emitted
refute log =~ ~r/delet.*#{old_user.id}/i
refute log =~ ~r/error.*delete.*inactive.*user.*#{old_user.id}/i
end
end
end

0 comments on commit b1852b6

Please sign in to comment.