From 901c2e16dc2d58777c6bed4512ea5c68d75022df Mon Sep 17 00:00:00 2001 From: Barnabas Jovanovics Date: Wed, 15 May 2024 11:01:17 +0200 Subject: [PATCH] add test for rels in atomic updates --- test/atomics_test.exs | 20 ++++++++++++++++++++ test/support/resources/post.ex | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/test/atomics_test.exs b/test/atomics_test.exs index 62828fa..405049a 100644 --- a/test/atomics_test.exs +++ b/test/atomics_test.exs @@ -1,4 +1,5 @@ defmodule AshPostgres.AtomicsTest do + alias AshPostgres.Test.Author use AshPostgres.RepoCase, async: false alias AshPostgres.Test.Post @@ -101,4 +102,23 @@ defmodule AshPostgres.AtomicsTest do assert Post.increment_score!(post, 2).score == 4 end + + test "use rel in atomic update" do + author = + Author + |> Ash.Changeset.for_create(:create, %{first_name: "John", last_name: "Doe"}) + |> Ash.create!() + + post = + Post + |> Ash.Changeset.for_create(:create, %{price: 1, author_id: author.id}) + |> Ash.create!() + + post = + post + |> Ash.Changeset.for_update(:set_title_from_author, %{}) + |> Ash.update!() + + assert post.title == "John" + end end diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index 5a963f0..fb36b41 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -150,6 +150,10 @@ defmodule AshPostgres.Test.Post do ) end + update :set_title_from_author do + change(atomic_update(:title, expr(author.first_name))) + end + update :increment_score do argument(:amount, :integer, default: 1) change(atomic_update(:score, expr((score || 0) + ^arg(:amount))))