Skip to content

Commit

Permalink
test: add test showing no query with exists
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Dec 16, 2024
1 parent 6126ef9 commit 25882e9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
27 changes: 27 additions & 0 deletions test/ash_postgres_test.exs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 =

Check warning on line 46 in test/ash_postgres_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix credo --strict

There should be no trailing white-space at the end of a line.

Check warning on line 46 in test/ash_postgres_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix credo --strict

There should be no trailing white-space at the end of a line.

Check warning on line 46 in test/ash_postgres_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix credo --strict

There should be no trailing white-space at the end of a line.
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 ->

Check warning on line 58 in test/ash_postgres_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix credo --strict

There should be no trailing white-space at the end of a line.

Check warning on line 58 in test/ash_postgres_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix credo --strict

There should be no trailing white-space at the end of a line.

Check warning on line 58 in test/ash_postgres_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix credo --strict

There should be no trailing white-space at the end of a line.
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
9 changes: 9 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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])
Expand Down

0 comments on commit 25882e9

Please sign in to comment.