Skip to content

Commit 57dbdb4

Browse files
committed
test: add tests corresponding to #561
unable to reproduce
1 parent 7f91cc8 commit 57dbdb4

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

test/atomics_test.exs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,27 @@ defmodule AshPostgres.AtomicsTest do
4040
|> Ash.update!()
4141
end
4242

43+
test "an atomic update on decimals works" do
44+
post =
45+
Post
46+
|> Ash.Changeset.for_create(:create, %{title: "foo", decimal: Decimal.new("1")})
47+
|> Ash.create!()
48+
49+
assert %{decimal: result} =
50+
post
51+
|> Ash.Changeset.for_update(:subtract_integer_from_decimal, %{amount: 2})
52+
|> Ash.update!()
53+
54+
assert Decimal.eq?(result, Decimal.new("-1"))
55+
56+
assert %{decimal: result} =
57+
post
58+
|> Ash.Changeset.for_update(:subtract_from_decimal, %{amount: Decimal.new("2")})
59+
|> Ash.update!()
60+
61+
assert Decimal.eq?(result, Decimal.new("-3"))
62+
end
63+
4364
test "an atomic works on a constrained integer" do
4465
post =
4566
Post

test/support/resources/post.ex

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ defmodule AshPostgres.Test.Post do
184184
end)
185185
end
186186

187+
update :subtract_integer_from_decimal do
188+
argument(:amount, :integer, allow_nil?: false)
189+
change(atomic_update(:decimal, expr(decimal + -(^arg(:amount)))))
190+
end
191+
192+
update :subtract_from_decimal do
193+
argument(:amount, :decimal, allow_nil?: false)
194+
change(atomic_update(:decimal, expr(decimal + -(^arg(:amount)))))
195+
end
196+
187197
update :add_to_limited_score do
188198
argument(:amount, :integer, allow_nil?: false)
189199
change(atomic_update(:limited_score, expr((limited_score || 0) + ^arg(:amount))))

0 commit comments

Comments
 (0)