File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -40,6 +40,27 @@ defmodule AshPostgres.AtomicsTest do
40
40
|> Ash . update! ( )
41
41
end
42
42
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
+
43
64
test "an atomic works on a constrained integer" do
44
65
post =
45
66
Post
Original file line number Diff line number Diff line change @@ -184,6 +184,16 @@ defmodule AshPostgres.Test.Post do
184
184
end )
185
185
end
186
186
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
+
187
197
update :add_to_limited_score do
188
198
argument ( :amount , :integer , allow_nil?: false )
189
199
change ( atomic_update ( :limited_score , expr ( ( limited_score || 0 ) + ^ arg ( :amount ) ) ) )
You can’t perform that action at this time.
0 commit comments