Skip to content

Commit eb8d5b6

Browse files
authored
fix: Spear.append/3 raw?: true raw return (#97)
Issue #96 Passing a `raw?: true` option to `Spear.append/3` or `Spear.Client/append` continued to return `:ok` instead of `{:ok, AppendResp.t()}`. This contradicted the docs and the `Spear.append_batch/5` behaviour. Solution: This commit adds the conditional case in `Spear.append/3` to return `{:ok, AppendResp.t()}` when `raw?` is `true`. `Spear.Client.append` will also be fixed as a result. Additional Notes: * Added to `SpearTest`: to test `append/3` for both raw true and false * Added to `SpearTest`: to test `append_batch/5` for both raw true and false. * Updated the `Spear.append/3` type spec to add the missing `{:ok, AppendResp.t()}` return type. * Updated the `Spear.Client.append/3` and `Spear.Client.append/4` callback specs to add the missing `{:ok, AppendResp.t()}` return type.
1 parent a08dfa9 commit eb8d5b6

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

lib/spear.ex

+6-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ defmodule Spear do
390390
connection :: Spear.Connection.t(),
391391
stream_name :: String.t(),
392392
opts :: Keyword.t()
393-
) :: :ok | {:error, reason :: Spear.ExpectationViolation.t() | any()}
393+
) ::
394+
:ok | {:ok, AppendResp.t()} | {:error, reason :: Spear.ExpectationViolation.t() | any()}
394395
def append(event_stream, conn, stream_name, opts \\ []) when is_binary(stream_name) do
395396
default_write_opts = [
396397
expect: :any,
@@ -400,6 +401,7 @@ defmodule Spear do
400401

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

404406
messages =
405407
[Spear.Writing.build_append_request(params)]
@@ -413,6 +415,9 @@ defmodule Spear do
413415
messages,
414416
Keyword.take(opts, [:credentials, :timeout])
415417
) do
418+
{:ok, response} when raw? == true ->
419+
{:ok, response}
420+
416421
{:ok, Streams.append_resp(result: {:success, _})} ->
417422
:ok
418423

lib/spear/client.ex

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ defmodule Spear.Client do
6767
"""
6868
@doc since: "0.1.0"
6969
@callback append(event_stream :: Enumerable.t(), stream_name :: String.t()) ::
70-
:ok | {:error, any()}
70+
:ok | {:ok, AppendResp.t()} | {:error, any()}
7171

7272
@doc """
7373
A wrapper around `Spear.append/4`
7474
"""
7575
@doc since: "0.1.0"
7676
@callback append(event_stream :: Enumerable.t(), stream_name :: String.t(), opts :: Keyword.t()) ::
77-
:ok | {:error, any()}
77+
:ok | {:ok, AppendResp.t()} | {:error, any()}
7878

7979
@doc """
8080
A wrapper around `Spear.append_batch/4`

test/spear_test.exs

+69
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ defmodule SpearTest do
77
import Spear.Uuid, only: [uuid_v4: 0]
88
import VersionHelper
99

10+
require Spear.Records.Streams, as: Streams
11+
1012
@max_append_bytes 1_048_576
1113
@checkpoint_after 32 * 32 * 32
1214

@@ -923,6 +925,31 @@ defmodule SpearTest do
923925
assert Spear.cancel_subscription(c.conn, subscription) == :ok
924926
end
925927

928+
test "the append/3 with `raw?: false` returns :ok", c do
929+
assert :ok =
930+
random_events()
931+
|> Stream.take(7)
932+
|> Spear.append(c.conn, c.stream_name, expect: :empty, raw?: false)
933+
end
934+
935+
test "the append/3 with `raw?: true` returns the raw result", c do
936+
result =
937+
random_events()
938+
|> Stream.take(7)
939+
|> Spear.append(c.conn, c.stream_name, expect: :empty, raw?: true)
940+
941+
assert {:ok, Streams.append_resp(result: {:success, _})} = result
942+
end
943+
944+
test "the append/3 with `raw?: true` returns expectation error as raw", c do
945+
result =
946+
random_events()
947+
|> Stream.take(1)
948+
|> Spear.append(c.conn, c.stream_name, expect: 9999, raw?: true)
949+
950+
assert {:ok, Streams.append_resp(result: {:wrong_expected_version, _})} = result
951+
end
952+
926953
@tag compatible(">= 21.6.0")
927954
test "append_batch/5 appends a batch of events", c do
928955
assert {:ok, batch_id, request_id} =
@@ -978,6 +1005,48 @@ defmodule SpearTest do
9781005
assert Spear.stream!(c.conn, c.stream_name) |> Enum.map(& &1.body) == Enum.to_list(0..19)
9791006
end
9801007

1008+
@tag compatible(">= 21.6.0")
1009+
test "the append_batch/5 with `raw?: false` returns :ok", c do
1010+
assert {:ok, batch_id, request_id} =
1011+
random_events()
1012+
|> Stream.take(5)
1013+
|> Spear.append_batch(c.conn, :new, c.stream_name, expect: :empty, raw?: false)
1014+
1015+
assert_receive %Spear.BatchAppendResult{
1016+
result: result,
1017+
batch_id: ^batch_id,
1018+
request_id: ^request_id,
1019+
revision: 4
1020+
}
1021+
1022+
assert :ok = result
1023+
1024+
assert Spear.cancel_subscription(c.conn, request_id) == :ok
1025+
1026+
assert Spear.stream!(c.conn, c.stream_name) |> Enum.map(& &1.body) == Enum.to_list(0..4)
1027+
end
1028+
1029+
@tag compatible(">= 21.6.0")
1030+
test "the append_batch/5 with `raw?: true` returns the raw result", c do
1031+
assert {:ok, batch_id, request_id} =
1032+
random_events()
1033+
|> Stream.take(5)
1034+
|> Spear.append_batch(c.conn, :new, c.stream_name, expect: :empty, raw?: true)
1035+
1036+
assert_receive %Spear.BatchAppendResult{
1037+
result: result,
1038+
batch_id: ^batch_id,
1039+
request_id: ^request_id,
1040+
revision: 4
1041+
}
1042+
1043+
assert {:success, Streams.batch_append_resp_success()} = result
1044+
1045+
assert Spear.cancel_subscription(c.conn, request_id) == :ok
1046+
1047+
assert Spear.stream!(c.conn, c.stream_name) |> Enum.map(& &1.body) == Enum.to_list(0..4)
1048+
end
1049+
9811050
@tag compatible(">= 21.6.0")
9821051
test "append_batch/5 can fragment with the :done? flag and :batch_id", c do
9831052
assert {:ok, batch_id, request_id} =

0 commit comments

Comments
 (0)