Skip to content

Commit

Permalink
fix: append/3 returns {:ok, raw_resp} for all
Browse files Browse the repository at this point in the history
`raw?: true` cases regardless of `:success` or `:wrong_expected_version`

Also, cleans up the test cases to use `Streams.append_resp`
pattern matching.
  • Loading branch information
byu committed May 7, 2024
1 parent f2c5fb4 commit 02e9ba3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
6 changes: 1 addition & 5 deletions lib/spear.ex
Original file line number Diff line number Diff line change
Expand Up @@ -415,16 +415,12 @@ defmodule Spear do
messages,
Keyword.take(opts, [:credentials, :timeout])
) do
{:ok, Streams.append_resp(result: {:success, _}) = response} when raw? == true ->
{:ok, response} when raw? == true ->
{:ok, response}

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

{:ok, Streams.append_resp(result: {:wrong_expected_version, _}) = response}
when raw? == true ->
{:error, response}

{:ok, Streams.append_resp(result: {:wrong_expected_version, expectation_violation})} ->
{:error, Spear.Writing.map_expectation_violation(expectation_violation)}

Expand Down
27 changes: 12 additions & 15 deletions test/spear_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule SpearTest do
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 @@ -931,26 +933,21 @@ defmodule SpearTest do
end

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

assert {:"event_store.client.streams.AppendResp",
{:success,
{:"event_store.client.streams.AppendResp.Success", {:current_revision, 6},
{:position, {:"event_store.client.streams.AppendResp.Position", _p1, _p2}}}}} =
response
assert {:ok, Streams.append_resp(result: {:success, _})} = result
end

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

assert {:"event_store.client.streams.AppendResp",
{:wrong_expected_version, _wrong_expected_version_pb_tuple}} = response
assert {:ok, Streams.append_resp(result: {:wrong_expected_version, _})} = result
end

@tag compatible(">= 21.6.0")
Expand Down

0 comments on commit 02e9ba3

Please sign in to comment.