diff --git a/test/ash_postgres_test.exs b/test/ash_postgres_test.exs index 346037ad..353d3cdb 100644 --- a/test/ash_postgres_test.exs +++ b/test/ash_postgres_test.exs @@ -1,5 +1,6 @@ defmodule AshPostgresTest do use AshPostgres.RepoCase, async: false + import ExUnit.CaptureLog test "transaction metadata is given to on_transaction_begin" do AshPostgres.Test.Post @@ -40,4 +41,30 @@ defmodule AshPostgresTest do |> Map.get(:title) end end + + test "it does not run queries for exists/2 expressions that can be determined from loaded data" do + author = + AshPostgres.Test.Author + |> Ash.Changeset.for_create(:create, %{}, authorize?: false) + |> Ash.create!() + + post = + AshPostgres.Test.Post + |> Ash.Changeset.for_create(:create, %{title: "good", author_id: author.id}) + |> Ash.create!() + |> Ash.load!(:author) + + log = + capture_log(fn -> + post + |> Ash.Changeset.for_update(:update_if_author, %{title: "bad"}, + authorize?: true, + actor: nil, + actor: author + ) + |> then(&AshPostgres.Test.Post.can_update_if_author?(author, &1)) + end) + + assert log == "" + end end diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index 92b6e4ab..a3fe41b3 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -80,6 +80,10 @@ defmodule AshPostgres.Test.Post do authorize_if(PassIfOriginalDataPresent) end + bypass action(:update_if_author) do + authorize_if relates_to_actor_via(:author) + end + policy action_type(:update) do authorize_if(action(:requires_initial_data)) authorize_if(relates_to_actor_via([:author, :authors_with_same_first_name])) @@ -231,6 +235,10 @@ defmodule AshPostgres.Test.Post do require_atomic?(false) end + update :update_if_author do + require_atomic?(false) + end + update(:dont_validate) update :change_title_to_foo_unless_its_already_foo do @@ -423,6 +431,7 @@ defmodule AshPostgres.Test.Post do define(:get_by_id, action: :read, get_by: [:id]) define(:increment_score, args: [{:optional, :amount}]) define(:destroy) + define(:update_if_author) define(:update_constrained_int, args: [:amount]) define_calculation(:upper_title, args: [:title])