Skip to content

Commit

Permalink
fix append raw error (again)
Browse files Browse the repository at this point in the history
1. fixes the unintentionally added extra nesting of `{:ok, {:ok, resp}}`
  Instead, when success and raw flag set, returns `{:ok, raw_response_pb}`.
2. Adds the extra raw response check so that `append/3` returns
    `{:error, raw_unexpected_version_response_pb}` if there is the wrong
    expected version response AND raw flag set.

If I just did

```
  {:ok, response} when raw? == true ->
        {:ok, response}
```

An `{:ok, raw_unexpected_version_response_pb}` could have been returned
on unexpected version error response.
  • Loading branch information
byu committed May 6, 2024
1 parent a1038cc commit f2c5fb4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lib/spear.ex
Original file line number Diff line number Diff line change
Expand Up @@ -415,12 +415,16 @@ defmodule Spear do
messages,
Keyword.take(opts, [:credentials, :timeout])
) do
{:ok, Streams.append_resp(result: {:success, _})} = response when raw? == true ->
{:ok, Streams.append_resp(result: {:success, _}) = 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
23 changes: 16 additions & 7 deletions test/spear_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -931,17 +931,26 @@ defmodule SpearTest do
end

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

assert {:ok,
{:"event_store.client.streams.AppendResp",
{:success,
{:"event_store.client.streams.AppendResp.Success", {:current_revision, 6},
{:position, {:"event_store.client.streams.AppendResp.Position", _p1, _p2}}}}}} =
result
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
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)

assert {:"event_store.client.streams.AppendResp",
{:wrong_expected_version, _wrong_expected_version_pb_tuple}} = response
end

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

0 comments on commit f2c5fb4

Please sign in to comment.