add tests for new transaction behavior

This commit is contained in:
Zach Daniel 2023-02-10 15:41:41 -05:00
parent 4f59b15f17
commit 8eabbde953
3 changed files with 117 additions and 6 deletions

21
mix.exs
View file

@ -161,7 +161,8 @@ defmodule AshPostgres.MixProject do
{:ecto, "~> 3.9"},
{:jason, "~> 1.0"},
{:postgrex, ">= 0.0.0"},
{:ash, ash_version("~> 2.6 and >= 2.6.1")},
{:ash,
ash_version(github: "ash-project/ash", ref: "85a66b1d8556dd48eeae2138a94e9dabb1cc3824")},
{:git_ops, "~> 2.5", only: [:dev, :test]},
{:nimble_options, "~> 0.5"},
{:ex_doc, "~> 0.22", only: [:dev, :test], runtime: false},
@ -175,10 +176,20 @@ defmodule AshPostgres.MixProject do
defp ash_version(default_version) do
case System.get_env("ASH_VERSION") do
nil -> default_version
"local" -> [path: "../ash"]
"main" -> [git: "https://github.com/ash-project/ash.git"]
version -> "~> #{version}"
nil ->
default_version
"local" ->
[path: "../ash"]
"main" ->
[git: "https://github.com/ash-project/ash.git"]
version when is_binary(version) ->
"~> #{version}"
version ->
version
end
end

View file

@ -1,5 +1,5 @@
%{
"ash": {:hex, :ash, "2.6.1", "a9fab10e4039a34a917454a0cdd0bf971d784031f13f98a23bfb709336ad7106", [:mix], [{:comparable, "~> 1.0", [hex: :comparable, repo: "hexpm", optional: false]}, {:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: true]}, {:ecto, "~> 3.7", [hex: :ecto, repo: "hexpm", optional: false]}, {:ets, "~> 0.8.0", [hex: :ets, repo: "hexpm", optional: false]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: false]}, {:picosat_elixir, "~> 0.2", [hex: :picosat_elixir, repo: "hexpm", optional: false]}, {:plug, ">= 0.0.0", [hex: :plug, repo: "hexpm", optional: true]}, {:spark, ">= 0.3.12 and < 1.0.0-0", [hex: :spark, repo: "hexpm", optional: false]}, {:stream_data, "~> 0.5.0", [hex: :stream_data, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.1", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6b8eb5ffa30fb8e048efaac46947485d53fe4259ddb5077f309165f6476ca284"},
"ash": {:git, "https://github.com/ash-project/ash.git", "85a66b1d8556dd48eeae2138a94e9dabb1cc3824", [ref: "85a66b1d8556dd48eeae2138a94e9dabb1cc3824"]},
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
"comparable": {:hex, :comparable, "1.0.0", "bb669e91cedd14ae9937053e5bcbc3c52bb2f22422611f43b6e38367d94a495f", [:mix], [{:typable, "~> 0.1", [hex: :typable, repo: "hexpm", optional: false]}], "hexpm", "277c11eeb1cd726e7cd41c6c199e7e52fa16ee6830b45ad4cdc62e51f62eb60c"},

100
test/transaction_test.exs Normal file
View file

@ -0,0 +1,100 @@
defmodule AshPostgres.Test.TransactionTest do
use AshPostgres.RepoCase, async: false
alias AshPostgres.Test.{Api, Post}
require Ash.Query
test "after_transaction hooks are invoked on failure" do
assert_raise RuntimeError, ~r/something bad happened/, fn ->
Post
|> Ash.Changeset.for_create(:create)
|> Ash.Changeset.after_action(fn _changeset, _result ->
raise "something bad happened"
end)
|> send_after_transaction_result()
|> Api.create()
end
assert_receive {:error,
%RuntimeError{
message: "something bad happened"
}}
end
test "after_transaction hooks are invoked on failure, even in a nested context" do
assert_raise RuntimeError, ~r/something bad happened inside/, fn ->
Post
|> Ash.Changeset.for_create(:create)
|> Ash.Changeset.after_action(fn _changeset, result ->
Post
|> Ash.Changeset.for_create(:create)
|> Ash.Changeset.after_action(fn _changeset, _result ->
raise "something bad happened inside"
end)
|> send_after_transaction_result()
|> Api.create!()
{:ok, result}
end)
|> send_after_transaction_result()
|> Api.create()
end
assert_receive {:error,
%RuntimeError{
message: "something bad happened inside"
}}
assert_receive {:error,
%RuntimeError{
message: "something bad happened inside"
}}
end
test "after_transaction hooks are invoked on success" do
Post
|> Ash.Changeset.for_create(:create)
|> send_after_transaction_result()
|> Api.create()
assert_receive {:ok, %Post{}}
end
test "after_transaction hooks are invoked on success and can reverse a failure" do
assert {:ok, %Post{}} =
Post
|> Ash.Changeset.for_create(:create)
|> Ash.Changeset.after_action(fn _changeset, result ->
Post
|> Ash.Changeset.for_create(:create)
|> Ash.Changeset.after_action(fn _changeset, _result ->
raise "something bad happened inside"
end)
|> send_after_transaction_result()
|> Api.create!()
{:ok, result}
end)
|> Ash.Changeset.after_transaction(fn _changeset, {:error, _} ->
Post
|> Ash.Changeset.for_create(:create)
|> Api.create()
end)
|> send_after_transaction_result()
|> Api.create()
assert_receive {:error,
%RuntimeError{
message: "something bad happened inside"
}}
assert_receive {:ok, %Post{}}
end
defp send_after_transaction_result(changeset) do
Ash.Changeset.after_transaction(changeset, fn _changeset, result ->
send(self(), result)
result
end)
end
end