Skip to content

Commit

Permalink
test: capture logs in tests, test various atomic behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Feb 19, 2025
1 parent 120d204 commit 2c74df3
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
5 changes: 4 additions & 1 deletion test/ash_postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ defmodule AshPostgresTest do
test "transaction metadata is given to on_transaction_begin" do
AshPostgres.Test.Post
|> Ash.Changeset.for_create(:create, %{title: "title"})
|> Ash.Changeset.after_action(fn _, result ->
{:ok, result}
end)
|> Ash.create!()

assert_receive %{
Expand Down Expand Up @@ -62,7 +65,7 @@ defmodule AshPostgresTest do
actor: nil,
actor: author
)
|> then(&AshPostgres.Test.Post.can_update_if_author?(author, &1))
|> then(&AshPostgres.Test.Post.can_update_if_author?(author, &1, reuse_values?: true))
end)

assert log == ""
Expand Down
22 changes: 22 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,28 @@ defmodule AshPostgres.Test.Post do
change(atomic_update(:limited_score, expr((limited_score || 0) + ^arg(:amount))))
end

update :change_nothing do
accept([])
require_atomic?(false)
change(fn changeset, _ -> changeset end)
end

update :change_nothing_atomic do
accept([])
require_atomic?(true)
end

update :change_title do
accept([:title])
require_atomic?(false)
change(fn changeset, _ -> changeset end)
end

update :change_title_atomic do
accept([:title])
require_atomic?(true)
end

destroy :destroy_only_freds do
change(filter(expr(title == "fred")))
end
Expand Down
2 changes: 1 addition & 1 deletion test/support/test_repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule AshPostgres.TestRepo do
send(self(), data)
end

def prefer_transaction?, do: true
def prefer_transaction?, do: false

def prefer_transaction_for_atomic_updates?, do: false

Expand Down
4 changes: 3 additions & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
ExUnit.start()
ExUnit.start(capture_log: true)

Logger.configure(level: :debug)

exclude_tags =
case System.get_env("PG_VERSION") do
Expand Down
95 changes: 95 additions & 0 deletions test/update_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule AshPostgres.UpdateTest do
use AshPostgres.RepoCase, async: false
alias AshPostgres.Test.Post
require Ash.Query
import ExUnit.CaptureLog
import Ash.Expr

test "can update with nested maps" do
Post
Expand Down Expand Up @@ -60,6 +62,99 @@ defmodule AshPostgres.UpdateTest do
|> Ash.update!()
end

test "timestamps arent updated if there are no changes non-atomically" do
post =
AshPostgres.Test.Post
|> Ash.Changeset.for_create(:create, %{title: "match"})
|> Ash.create!()

post2 =
post
|> Ash.update!(action: :change_nothing)

assert post.updated_at == post2.updated_at
end

test "no queries are run if there are no changes non-atomically" do

Check failure on line 78 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test no queries are run if there are no changes non-atomically (AshPostgres.UpdateTest)

Check failure on line 78 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test no queries are run if there are no changes non-atomically (AshPostgres.UpdateTest)
post =
AshPostgres.Test.Post
|> Ash.Changeset.for_create(:create, %{title: "match"})
|> Ash.create!()

assert "" =
capture_log(fn ->
post
|> Ash.update!(action: :change_nothing)
end)
end

test "queries are run if there are no changes but there are filters non-atomically" do

Check failure on line 91 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test queries are run if there are no changes but there are filters non-atomically (AshPostgres.UpdateTest)

Check failure on line 91 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test queries are run if there are no changes but there are filters non-atomically (AshPostgres.UpdateTest)

Check failure on line 91 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test queries are run if there are no changes but there are filters non-atomically (AshPostgres.UpdateTest)
post =
AshPostgres.Test.Post
|> Ash.Changeset.for_create(:create, %{title: "match"})
|> Ash.create!()

assert_raise Ash.Error.Invalid, ~r/stale/, fn ->
post
|> Ash.Changeset.for_update(:change_nothing, %{})
|> Ash.Changeset.filter(expr(title != "match"))
|> Ash.update!(action: :change_nothing)
end
end

test "timestamps arent updated if there are no changes atomically" do

Check failure on line 105 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test timestamps arent updated if there are no changes atomically (AshPostgres.UpdateTest)

Check failure on line 105 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test timestamps arent updated if there are no changes atomically (AshPostgres.UpdateTest)

Check failure on line 105 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test timestamps arent updated if there are no changes atomically (AshPostgres.UpdateTest)
post =
AshPostgres.Test.Post
|> Ash.Changeset.for_create(:create, %{title: "match"})
|> Ash.create!()

post2 =
post
|> Ash.update!(action: :change_nothing_atomic)

assert post.updated_at == post2.updated_at
end

test "timestamps arent updated if nothing changes non-atomically" do
post =
AshPostgres.Test.Post
|> Ash.Changeset.for_create(:create, %{title: "match"})
|> Ash.create!()

post2 =
post
|> Ash.update!(%{title: "match"}, action: :change_title)

assert post.updated_at == post2.updated_at
end

test "timestamps arent updated if nothing changes atomically" do

Check failure on line 131 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test timestamps arent updated if nothing changes atomically (AshPostgres.UpdateTest)

Check failure on line 131 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test timestamps arent updated if nothing changes atomically (AshPostgres.UpdateTest)

Check failure on line 131 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test timestamps arent updated if nothing changes atomically (AshPostgres.UpdateTest)
post =
AshPostgres.Test.Post
|> Ash.Changeset.for_create(:create, %{title: "match"})
|> Ash.create!()

post2 =
post
|> Ash.update!(%{title: "match"}, action: :change_title_atomic)

assert post.updated_at == post2.updated_at
end

test "queries are run if there are no changes atomically" do

Check failure on line 144 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test queries are run if there are no changes atomically (AshPostgres.UpdateTest)

Check failure on line 144 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test queries are run if there are no changes atomically (AshPostgres.UpdateTest)

Check failure on line 144 in test/update_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test queries are run if there are no changes atomically (AshPostgres.UpdateTest)
post =
AshPostgres.Test.Post
|> Ash.Changeset.for_create(:create, %{title: "match"})
|> Ash.create!()

assert_raise Ash.Error.Invalid, ~r/stale/, fn ->
post
|> Ash.Changeset.for_update(:change_nothing_atomic, %{})
|> Ash.Changeset.filter(expr(title != "match"))
|> Ash.update!(action: :change_nothing)
end
end

test "can unrelate belongs_to" do
author =
AshPostgres.Test.Author
Expand Down

0 comments on commit 2c74df3

Please sign in to comment.