Skip to content

Commit e597c39

Browse files
authored
add test for rels in atomic updates (#286)
1 parent 555a737 commit e597c39

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

test/atomics_test.exs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule AshPostgres.AtomicsTest do
2+
alias AshPostgres.Test.Author
23
use AshPostgres.RepoCase, async: false
34
alias AshPostgres.Test.Post
45

@@ -101,4 +102,23 @@ defmodule AshPostgres.AtomicsTest do
101102

102103
assert Post.increment_score!(post, 2).score == 4
103104
end
105+
106+
test "use rel in atomic update" do
107+
author =
108+
Author
109+
|> Ash.Changeset.for_create(:create, %{first_name: "John", last_name: "Doe"})
110+
|> Ash.create!()
111+
112+
post =
113+
Post
114+
|> Ash.Changeset.for_create(:create, %{price: 1, author_id: author.id})
115+
|> Ash.create!()
116+
117+
post =
118+
post
119+
|> Ash.Changeset.for_update(:set_title_from_author, %{})
120+
|> Ash.update!()
121+
122+
assert post.title == "John"
123+
end
104124
end

test/support/resources/post.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ defmodule AshPostgres.Test.Post do
150150
)
151151
end
152152

153+
update :set_title_from_author do
154+
change(atomic_update(:title, expr(author.first_name)))
155+
end
156+
153157
update :increment_score do
154158
argument(:amount, :integer, default: 1)
155159
change(atomic_update(:score, expr((score || 0) + ^arg(:amount))))

0 commit comments

Comments
 (0)