ash/test/reactor/get_test.exs
James Harton 86d74ed789
feat(Ash.Reactor): Add a Reactor extension that makes working with resources easy. (#683)
* feat: Add `Ash.Reactor` with create support.

* improvement: Add `Ash.Reactor` update support.

* improvement: Add `Ash.Reactor` destroy support.

* improvement(Ash.Reactor): Support for transactional handling of notifications.

* improvement(Ash.Reactor): Add `read` and `get` steps.

* docs: Add the beginnings of a Reactor topic.

* improvement(Ash.Reactor): add support for generic actions.

* improvement: Add `undo` capability to `create` step.

* improvement: transaction and undo working.

* docs: Start documenting undo behaviour.

* chore: Update to Reactor 0.6.

* improvement: Automatically thread Ash tracers through Reactor.

* improvement(Ash.Reactor): Add undo to generic actions.

* docs: Improve reactor documentation.

* fix: Mimic copying `Ash.Notifier` seems to break the compiler for some reason.
2024-03-02 10:26:25 +13:00

84 lines
1.9 KiB
Elixir

defmodule Ash.Test.ReactorGetTest do
@moduledoc false
use ExUnit.Case, async: true
defmodule Post do
@moduledoc false
use Ash.Resource, data_layer: Ash.DataLayer.Ets
ets do
private? true
end
attributes do
uuid_primary_key :id
attribute :title, :string, allow_nil?: false
end
actions do
defaults [:create, :read]
end
code_interface do
define_for Ash.Test.ReactorGetTest.Api
define :create
end
end
defmodule Api do
@moduledoc false
use Ash.Api
resources do
resource Ash.Test.ReactorGetTest.Post
end
end
defmodule SimpleGetReactor do
@moduledoc false
use Reactor, extensions: [Ash.Reactor]
ash do
default_api Api
end
get(:get_post, Ash.Test.ReactorGetTest.Post, :read)
end
test "when more than one record is returned it returns an error" do
~w[Marty Doc Einstein]
|> Enum.each(&Post.create!(%{title: &1}))
assert {:error, [error]} = Reactor.run(SimpleGetReactor, %{}, %{}, async?: false)
assert Exception.message(error) =~ "expected at most one result"
end
test "when no records are returned it returns nil" do
assert {:ok, nil} = Reactor.run(SimpleGetReactor, %{}, %{}, async?: false)
end
test "when no records are returned it can return an error" do
defmodule NotFoundReactor do
@moduledoc false
use Reactor, extensions: [Ash.Reactor]
ash do
default_api Api
end
get :get_post, Ash.Test.ReactorGetTest.Post, :read do
fail_on_not_found?(true)
end
end
assert {:error, [error]} = Reactor.run(NotFoundReactor, %{}, %{}, async?: false)
assert Exception.message(error) =~ "not found"
end
test "when exactly one record is returned it returns it" do
expected = Post.create!(%{title: "Marty"})
assert {:ok, actual} = Reactor.run(SimpleGetReactor, %{}, %{}, async?: false)
assert expected.id == actual.id
end
end