mirror of
https://github.com/ash-project/ash_postgres.git
synced 2024-09-20 05:23:18 +12:00
add test for rels in atomic updates (#286)
This commit is contained in:
parent
555a737493
commit
e597c39687
2 changed files with 24 additions and 0 deletions
|
@ -1,4 +1,5 @@
|
||||||
defmodule AshPostgres.AtomicsTest do
|
defmodule AshPostgres.AtomicsTest do
|
||||||
|
alias AshPostgres.Test.Author
|
||||||
use AshPostgres.RepoCase, async: false
|
use AshPostgres.RepoCase, async: false
|
||||||
alias AshPostgres.Test.Post
|
alias AshPostgres.Test.Post
|
||||||
|
|
||||||
|
@ -101,4 +102,23 @@ defmodule AshPostgres.AtomicsTest do
|
||||||
|
|
||||||
assert Post.increment_score!(post, 2).score == 4
|
assert Post.increment_score!(post, 2).score == 4
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -150,6 +150,10 @@ defmodule AshPostgres.Test.Post do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
update :set_title_from_author do
|
||||||
|
change(atomic_update(:title, expr(author.first_name)))
|
||||||
|
end
|
||||||
|
|
||||||
update :increment_score do
|
update :increment_score do
|
||||||
argument(:amount, :integer, default: 1)
|
argument(:amount, :integer, default: 1)
|
||||||
change(atomic_update(:score, expr((score || 0) + ^arg(:amount))))
|
change(atomic_update(:score, expr((score || 0) + ^arg(:amount))))
|
||||||
|
|
Loading…
Reference in a new issue