Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Spear.append/3 raw?: true raw return #97

Merged
merged 7 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/spear.ex
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ defmodule Spear do
connection :: Spear.Connection.t(),
stream_name :: String.t(),
opts :: Keyword.t()
) :: :ok | {:error, reason :: Spear.ExpectationViolation.t() | any()}
) ::
:ok | {:ok, AppendResp.t()} | {:error, reason :: Spear.ExpectationViolation.t() | any()}
def append(event_stream, conn, stream_name, opts \\ []) when is_binary(stream_name) do
default_write_opts = [
expect: :any,
Expand All @@ -400,6 +401,7 @@ defmodule Spear do

opts = default_write_opts |> Keyword.merge(opts)
params = Enum.into(opts, %{})
raw? = Keyword.get(opts, :raw?, false)

messages =
[Spear.Writing.build_append_request(params)]
Expand All @@ -413,6 +415,9 @@ defmodule Spear do
messages,
Keyword.take(opts, [:credentials, :timeout])
) do
{:ok, response} when raw? == true ->
{:ok, response}

{:ok, Streams.append_resp(result: {:success, _})} ->
:ok

Expand Down
4 changes: 2 additions & 2 deletions lib/spear/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ defmodule Spear.Client do
"""
@doc since: "0.1.0"
@callback append(event_stream :: Enumerable.t(), stream_name :: String.t()) ::
:ok | {:error, any()}
:ok | {:ok, AppendResp.t()} | {:error, any()}

@doc """
A wrapper around `Spear.append/4`
"""
@doc since: "0.1.0"
@callback append(event_stream :: Enumerable.t(), stream_name :: String.t(), opts :: Keyword.t()) ::
:ok | {:error, any()}
:ok | {:ok, AppendResp.t()} | {:error, any()}

@doc """
A wrapper around `Spear.append_batch/4`
Expand Down
69 changes: 69 additions & 0 deletions test/spear_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import Spear.Uuid, only: [uuid_v4: 0]
import VersionHelper

require Spear.Records.Streams, as: Streams

@max_append_bytes 1_048_576
@checkpoint_after 32 * 32 * 32

Expand Down Expand Up @@ -923,6 +925,31 @@
assert Spear.cancel_subscription(c.conn, subscription) == :ok
end

test "the append/3 with `raw?: false` returns :ok", c do
assert :ok =
random_events()
|> Stream.take(7)
|> Spear.append(c.conn, c.stream_name, expect: :empty, raw?: false)
end

test "the append/3 with `raw?: true` returns the raw result", c do
result =
random_events()
|> Stream.take(7)
|> Spear.append(c.conn, c.stream_name, expect: :empty, raw?: true)

assert {:ok, Streams.append_resp(result: {:success, _})} = result
end

test "the append/3 with `raw?: true` returns expectation error as raw", c do
result =
random_events()
|> Stream.take(1)
|> Spear.append(c.conn, c.stream_name, expect: 9999, raw?: true)

assert {:ok, Streams.append_resp(result: {:wrong_expected_version, _})} = result
end

@tag compatible(">= 21.6.0")
test "append_batch/5 appends a batch of events", c do
assert {:ok, batch_id, request_id} =
Expand Down Expand Up @@ -978,6 +1005,48 @@
assert Spear.stream!(c.conn, c.stream_name) |> Enum.map(& &1.body) == Enum.to_list(0..19)
end

@tag compatible(">= 21.6.0")
test "the append_batch/5 with `raw?: false` returns :ok", c do
assert {:ok, batch_id, request_id} =
random_events()
|> Stream.take(5)
|> Spear.append_batch(c.conn, :new, c.stream_name, expect: :empty, raw?: false)

assert_receive %Spear.BatchAppendResult{
result: result,
batch_id: ^batch_id,
request_id: ^request_id,
revision: 4
}

assert :ok = result

assert Spear.cancel_subscription(c.conn, request_id) == :ok

assert Spear.stream!(c.conn, c.stream_name) |> Enum.map(& &1.body) == Enum.to_list(0..4)
end

@tag compatible(">= 21.6.0")
test "the append_batch/5 with `raw?: true` returns the raw result", c do

Check failure on line 1030 in test/spear_test.exs

View workflow job for this annotation

GitHub Actions / Bless (1.7.4, 21.3, 22.6.0)

test given no prior state the append_batch/5 with `raw?: true` returns the raw result (SpearTest)
assert {:ok, batch_id, request_id} =
random_events()
|> Stream.take(5)
|> Spear.append_batch(c.conn, :new, c.stream_name, expect: :empty, raw?: true)

assert_receive %Spear.BatchAppendResult{
result: result,
batch_id: ^batch_id,
request_id: ^request_id,
revision: 4
}

assert {:success, Streams.batch_append_resp_success()} = result
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird that this behaves differently on Elixir 1.7. I want to bump the versions anyway though so I will merge this as-is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience in the collaboration. It was also a learning experience for me, e.g. grpc in the erlang ecosystem, to gain more knowledge about event store db and this spear library (which will be a key part in my new project).

I'm definitely open to contribute again if/when things pop up as I use spear. 👍


assert Spear.cancel_subscription(c.conn, request_id) == :ok

assert Spear.stream!(c.conn, c.stream_name) |> Enum.map(& &1.body) == Enum.to_list(0..4)
end

@tag compatible(">= 21.6.0")
test "append_batch/5 can fragment with the :done? flag and :batch_id", c do
assert {:ok, batch_id, request_id} =
Expand Down
Loading