Skip to content

Commit

Permalink
README revelation: remove redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilMenne committed Jun 25, 2018
1 parent 5fc0ace commit 789f151
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -89,6 +89,6 @@ end

```elixir
iex(1)> {:ok, id} = MyApp.Aggregate.new()
iex(2)> :ok = MyApp.Aggregate.evaluate(id, %Maestro.Types.Command{aggregate_id: id, type: "increment_counter", data: %{}})
iex(2)> :ok = MyApp.Aggregate.evaluate(%Maestro.Types.Command{aggregate_id: id, type: "increment_counter", data: %{}})
iex(3)> {:ok, %{"value" => 1}} = MyApp.Aggregate.get(id)
```
8 changes: 6 additions & 2 deletions lib/maestro/aggregate/root.ex
Expand Up @@ -87,7 +87,11 @@ defmodule Maestro.Aggregate.Root do

def replay(agg_id, seq), do: call(agg_id, {:replay, seq})

def evaluate(agg_id, command), do: call(agg_id, {:eval_command, command})
def evaluate(%Maestro.Types.Command{} = command) do
call(command.aggregate_id, {:eval_command, command})
end

def evaluate(_), do: raise(ArgumentError, "invalid command")

def snapshot(agg_id) do
with {:ok, snap} <- call(agg_id, :get_snapshot) do
Expand Down Expand Up @@ -233,7 +237,7 @@ defmodule Maestro.Aggregate.Root do
@doc """
Evaluate the command within the aggregate's context.
"""
@callback evaluate(id(), command()) :: :ok | {:error, any(), stack()}
@callback evaluate(command()) :: :ok | {:error, any(), stack()}

@doc """
Using the aggregate root's `prepare_snapshot` function, generate and store a
Expand Down
31 changes: 19 additions & 12 deletions test/maestro/aggregate_test.exs
Expand Up @@ -34,7 +34,7 @@ defmodule Maestro.AggregateTest do
check all agg_id <- timestamp(),
coms <- commands(agg_id, max_commands: 200) do
for com <- coms do
:ok = SampleAggregate.evaluate(agg_id, com)
:ok = SampleAggregate.evaluate(com)
end

{:ok, %{"value" => value}} = SampleAggregate.get(agg_id)
Expand All @@ -51,13 +51,13 @@ defmodule Maestro.AggregateTest do
{:ok, %{"value" => value}} = SampleAggregate.get(agg_id)
assert value == 0

SampleAggregate.evaluate(agg_id, %Command{
SampleAggregate.evaluate(%Command{
type: "increment_counter",
aggregate_id: agg_id,
data: %{}
})

SampleAggregate.evaluate(agg_id, %Command{
SampleAggregate.evaluate(%Command{
type: "increment_counter",
aggregate_id: agg_id,
data: %{}
Expand All @@ -69,7 +69,7 @@ defmodule Maestro.AggregateTest do

{:ok, _pid} = SampleAggregate.start_link(agg_id)

SampleAggregate.evaluate(agg_id, %Command{
SampleAggregate.evaluate(%Command{
type: "increment_counter",
aggregate_id: agg_id,
data: %{}
Expand All @@ -91,7 +91,7 @@ defmodule Maestro.AggregateTest do
commands = repeat(base_command, 10)

for com <- commands do
res = SampleAggregate.evaluate(agg_id, com)
res = SampleAggregate.evaluate(com)

case res do
:ok -> :ok
Expand All @@ -116,9 +116,16 @@ defmodule Maestro.AggregateTest do
data: %{}
}

{:error, err, _stack} = SampleAggregate.evaluate(agg_id, com)
{:error, err, _stack} = SampleAggregate.evaluate(com)

assert err == InvalidHandlerError.exception(type: "invalid")

assert_raise(ArgumentError, fn ->
SampleAggregate.evaluate(%{
type: "increment_counter",
data: %{}
})
end)
end

test "handler rejected command" do
Expand All @@ -130,7 +137,7 @@ defmodule Maestro.AggregateTest do
data: %{"do_inc" => false}
}

{:error, err, _stack} = SampleAggregate.evaluate(agg_id, com)
{:error, err, _stack} = SampleAggregate.evaluate(com)

assert err ==
InvalidCommandError.exception(
Expand All @@ -147,7 +154,7 @@ defmodule Maestro.AggregateTest do
data: %{"raise" => true}
}

{:error, err, _stack} = SampleAggregate.evaluate(agg_id, com)
{:error, err, _stack} = SampleAggregate.evaluate(com)

assert err ==
ArgumentError.exception(
Expand All @@ -169,7 +176,7 @@ defmodule Maestro.AggregateTest do
get_snapshot: fn _, _, _ ->
raise(ConnectionError, "some")
end do
{:error, err, _stack} = SampleAggregate.evaluate(agg_id, com)
{:error, err, _stack} = SampleAggregate.evaluate(com)
assert err == ConnectionError.exception("some")
end
end
Expand All @@ -191,9 +198,9 @@ defmodule Maestro.AggregateTest do
data: %{"name" => "sample"}
}

:ok = SampleAggregate.evaluate(agg_id, com)
:ok = SampleAggregate.evaluate(com)

{:error, err, _stack} = SampleAggregate.evaluate(agg_id, com)
{:error, err, _stack} = SampleAggregate.evaluate(com)

assert err ==
InvalidCommandError.exception(
Expand All @@ -204,7 +211,7 @@ defmodule Maestro.AggregateTest do

com_2 = Map.put(com, :aggregate_id, agg_id_2)

{:error, err, _stack} = SampleAggregate.evaluate(agg_id_2, com_2)
{:error, err, _stack} = SampleAggregate.evaluate(com_2)

assert err.__struct__ == Ecto.ConstraintError

Expand Down

0 comments on commit 789f151

Please sign in to comment.