Skip to content

Commit 2c74df3

Browse files
committed
test: capture logs in tests, test various atomic behaviors
1 parent 120d204 commit 2c74df3

File tree

5 files changed

+125
-3
lines changed

5 files changed

+125
-3
lines changed

test/ash_postgres_test.exs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ defmodule AshPostgresTest do
55
test "transaction metadata is given to on_transaction_begin" do
66
AshPostgres.Test.Post
77
|> Ash.Changeset.for_create(:create, %{title: "title"})
8+
|> Ash.Changeset.after_action(fn _, result ->
9+
{:ok, result}
10+
end)
811
|> Ash.create!()
912

1013
assert_receive %{
@@ -62,7 +65,7 @@ defmodule AshPostgresTest do
6265
actor: nil,
6366
actor: author
6467
)
65-
|> then(&AshPostgres.Test.Post.can_update_if_author?(author, &1))
68+
|> then(&AshPostgres.Test.Post.can_update_if_author?(author, &1, reuse_values?: true))
6669
end)
6770

6871
assert log == ""

test/support/resources/post.ex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,28 @@ defmodule AshPostgres.Test.Post do
159159
change(atomic_update(:limited_score, expr((limited_score || 0) + ^arg(:amount))))
160160
end
161161

162+
update :change_nothing do
163+
accept([])
164+
require_atomic?(false)
165+
change(fn changeset, _ -> changeset end)
166+
end
167+
168+
update :change_nothing_atomic do
169+
accept([])
170+
require_atomic?(true)
171+
end
172+
173+
update :change_title do
174+
accept([:title])
175+
require_atomic?(false)
176+
change(fn changeset, _ -> changeset end)
177+
end
178+
179+
update :change_title_atomic do
180+
accept([:title])
181+
require_atomic?(true)
182+
end
183+
162184
destroy :destroy_only_freds do
163185
change(filter(expr(title == "fred")))
164186
end

test/support/test_repo.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule AshPostgres.TestRepo do
77
send(self(), data)
88
end
99

10-
def prefer_transaction?, do: true
10+
def prefer_transaction?, do: false
1111

1212
def prefer_transaction_for_atomic_updates?, do: false
1313

test/test_helper.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
ExUnit.start()
1+
ExUnit.start(capture_log: true)
2+
3+
Logger.configure(level: :debug)
24

35
exclude_tags =
46
case System.get_env("PG_VERSION") do

test/update_test.exs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ defmodule AshPostgres.UpdateTest do
22
use AshPostgres.RepoCase, async: false
33
alias AshPostgres.Test.Post
44
require Ash.Query
5+
import ExUnit.CaptureLog
6+
import Ash.Expr
57

68
test "can update with nested maps" do
79
Post
@@ -60,6 +62,99 @@ defmodule AshPostgres.UpdateTest do
6062
|> Ash.update!()
6163
end
6264

65+
test "timestamps arent updated if there are no changes non-atomically" do
66+
post =
67+
AshPostgres.Test.Post
68+
|> Ash.Changeset.for_create(:create, %{title: "match"})
69+
|> Ash.create!()
70+
71+
post2 =
72+
post
73+
|> Ash.update!(action: :change_nothing)
74+
75+
assert post.updated_at == post2.updated_at
76+
end
77+
78+
test "no queries are run if there are no changes non-atomically" do
79+
post =
80+
AshPostgres.Test.Post
81+
|> Ash.Changeset.for_create(:create, %{title: "match"})
82+
|> Ash.create!()
83+
84+
assert "" =
85+
capture_log(fn ->
86+
post
87+
|> Ash.update!(action: :change_nothing)
88+
end)
89+
end
90+
91+
test "queries are run if there are no changes but there are filters non-atomically" do
92+
post =
93+
AshPostgres.Test.Post
94+
|> Ash.Changeset.for_create(:create, %{title: "match"})
95+
|> Ash.create!()
96+
97+
assert_raise Ash.Error.Invalid, ~r/stale/, fn ->
98+
post
99+
|> Ash.Changeset.for_update(:change_nothing, %{})
100+
|> Ash.Changeset.filter(expr(title != "match"))
101+
|> Ash.update!(action: :change_nothing)
102+
end
103+
end
104+
105+
test "timestamps arent updated if there are no changes atomically" do
106+
post =
107+
AshPostgres.Test.Post
108+
|> Ash.Changeset.for_create(:create, %{title: "match"})
109+
|> Ash.create!()
110+
111+
post2 =
112+
post
113+
|> Ash.update!(action: :change_nothing_atomic)
114+
115+
assert post.updated_at == post2.updated_at
116+
end
117+
118+
test "timestamps arent updated if nothing changes non-atomically" do
119+
post =
120+
AshPostgres.Test.Post
121+
|> Ash.Changeset.for_create(:create, %{title: "match"})
122+
|> Ash.create!()
123+
124+
post2 =
125+
post
126+
|> Ash.update!(%{title: "match"}, action: :change_title)
127+
128+
assert post.updated_at == post2.updated_at
129+
end
130+
131+
test "timestamps arent updated if nothing changes atomically" do
132+
post =
133+
AshPostgres.Test.Post
134+
|> Ash.Changeset.for_create(:create, %{title: "match"})
135+
|> Ash.create!()
136+
137+
post2 =
138+
post
139+
|> Ash.update!(%{title: "match"}, action: :change_title_atomic)
140+
141+
assert post.updated_at == post2.updated_at
142+
end
143+
144+
test "queries are run if there are no changes atomically" do
145+
post =
146+
AshPostgres.Test.Post
147+
|> Ash.Changeset.for_create(:create, %{title: "match"})
148+
|> Ash.create!()
149+
150+
assert_raise Ash.Error.Invalid, ~r/stale/, fn ->
151+
post
152+
|> Ash.Changeset.for_update(:change_nothing_atomic, %{})
153+
|> Ash.Changeset.filter(expr(title != "match"))
154+
|> Ash.update!(action: :change_nothing)
155+
end
156+
end
157+
63158
test "can unrelate belongs_to" do
64159
author =
65160
AshPostgres.Test.Author

0 commit comments

Comments
 (0)