Packages
commanded
0.18.1
1.4.10
1.4.9
1.4.8
1.4.7
1.4.6
1.4.3
1.4.2
1.4.1
1.4.0
1.4.0-rc.0
1.3.1
1.3.0
1.2.0
1.1.1
1.1.0
1.0.1
1.0.0
1.0.0-rc.1
1.0.0-rc.0
0.19.1
0.19.0
0.18.1
0.18.0
0.17.5
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.0
0.16.0-rc.1
0.16.0-rc.0
0.15.1
0.15.0
0.14.0
0.14.0-rc.0
0.13.0
0.12.0
0.11.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.1
0.8.0
0.7.1
0.6.2
0.6.1
0.6.0
0.4.0
0.3.1
0.3.0
0.2.1
0.2.0
0.1.0
Use Commanded to build your own Elixir applications following the CQRS/ES pattern.
Current section
Files
Jump to
Current section
Files
test/event_store/support/append_events_test_case.ex
defmodule Commanded.EventStore.AppendEventsTestCase do
import Commanded.SharedTestCase
define_tests do
import Commanded.Enumerable, only: [pluck: 2]
alias Commanded.EventStore
alias Commanded.EventStore.EventData
defmodule BankAccountOpened do
@derive Jason.Encoder
defstruct [:account_number, :initial_balance]
end
describe "append events to a stream" do
test "should append events" do
assert :ok == EventStore.append_to_stream("stream", 0, build_events(1))
assert :ok == EventStore.append_to_stream("stream", 1, build_events(2))
assert :ok == EventStore.append_to_stream("stream", 3, build_events(3))
end
test "should append events with `:any_version` without checking expected version" do
assert :ok == EventStore.append_to_stream("stream", :any_version, build_events(3))
assert :ok == EventStore.append_to_stream("stream", :any_version, build_events(2))
assert :ok == EventStore.append_to_stream("stream", :any_version, build_events(1))
end
test "should append events with `:no_stream` parameter" do
assert :ok == EventStore.append_to_stream("stream", :no_stream, build_events(2))
end
test "should fail when stream aleady exists with `:no_stream` parameter" do
assert :ok == EventStore.append_to_stream("stream", :no_stream, build_events(2))
assert {:error, :stream_exists} ==
EventStore.append_to_stream("stream", :no_stream, build_events(1))
end
test "should append events with `:stream_exists` parameter" do
assert :ok == EventStore.append_to_stream("stream", :no_stream, build_events(2))
assert :ok == EventStore.append_to_stream("stream", :stream_exists, build_events(1))
end
test "should fail with `:stream_exists` parameter when stream does not exist" do
assert {:error, :stream_does_not_exist} ==
EventStore.append_to_stream("stream", :stream_exists, build_events(1))
end
test "should fail to append to a stream because of wrong expected version when no stream" do
assert {:error, :wrong_expected_version} ==
EventStore.append_to_stream("stream", 1, build_events(1))
end
test "should fail to append to a stream because of wrong expected version" do
assert :ok == EventStore.append_to_stream("stream", 0, build_events(3))
assert {:error, :wrong_expected_version} ==
EventStore.append_to_stream("stream", 0, build_events(1))
assert {:error, :wrong_expected_version} ==
EventStore.append_to_stream("stream", 1, build_events(1))
assert {:error, :wrong_expected_version} ==
EventStore.append_to_stream("stream", 2, build_events(1))
assert :ok == EventStore.append_to_stream("stream", 3, build_events(1))
end
end
describe "stream events from an unknown stream" do
test "should return stream not found error" do
assert {:error, :stream_not_found} == EventStore.stream_forward("unknownstream")
end
end
describe "stream events from an existing stream" do
test "should read events" do
correlation_id = UUID.uuid4()
causation_id = UUID.uuid4()
events = build_events(4, correlation_id, causation_id)
assert :ok == EventStore.append_to_stream("stream", 0, events)
read_events = EventStore.stream_forward("stream") |> Enum.to_list()
assert length(read_events) == 4
assert coerce(events) == coerce(read_events)
assert pluck(read_events, :stream_version) == [1, 2, 3, 4]
Enum.each(read_events, fn event ->
assert_is_uuid(event.event_id)
assert event.stream_id == "stream"
assert event.correlation_id == correlation_id
assert event.causation_id == causation_id
assert event.metadata == %{"metadata" => "value"}
end)
read_events = EventStore.stream_forward("stream", 3) |> Enum.to_list()
assert coerce(Enum.slice(events, 2, 2)) == coerce(read_events)
assert pluck(read_events, :stream_version) == [3, 4]
end
test "should read from single stream" do
events1 = build_events(2)
events2 = build_events(4)
assert :ok == EventStore.append_to_stream("stream", 0, events1)
assert :ok == EventStore.append_to_stream("secondstream", 0, events2)
read_events = EventStore.stream_forward("stream", 0) |> Enum.to_list()
assert 2 == length(read_events)
assert coerce(events1) == coerce(read_events)
read_events = EventStore.stream_forward("secondstream", 0) |> Enum.to_list()
assert 4 == length(read_events)
assert coerce(events2) == coerce(read_events)
end
test "should read events in batches" do
events = build_events(10)
assert :ok == EventStore.append_to_stream("stream", 0, events)
read_events = EventStore.stream_forward("stream", 0, 2) |> Enum.to_list()
assert length(read_events) == 10
assert coerce(events) == coerce(read_events)
assert pluck(read_events, :stream_version) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
end
end
defp build_event(account_number, correlation_id, causation_id) do
%EventData{
correlation_id: correlation_id,
causation_id: causation_id,
event_type: "#{__MODULE__}.BankAccountOpened",
data: %BankAccountOpened{account_number: account_number, initial_balance: 1_000},
metadata: %{"metadata" => "value"}
}
end
defp build_events(count, correlation_id \\ UUID.uuid4(), causation_id \\ UUID.uuid4())
defp build_events(count, correlation_id, causation_id) do
for account_number <- 1..count,
do: build_event(account_number, correlation_id, causation_id)
end
defp assert_is_uuid(uuid) do
assert uuid |> UUID.string_to_binary!() |> is_binary()
end
defp coerce(events) do
Enum.map(
events,
&%{
causation_id: &1.causation_id,
correlation_id: &1.correlation_id,
data: &1.data,
metadata: &1.metadata
}
)
end
end
end