Skip to content

Commit

Permalink
Add Push.log/2 and PushResult.log/2
Browse files Browse the repository at this point in the history
  • Loading branch information
gordalina committed Dec 16, 2022
1 parent a109caf commit 2eed56f
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Next

- Add `ExTier.Push.log/2` and `ExTier.PushResult.log/2`

## v0.6.0

- Add `ExTier.lookup/1` endpoint
Expand Down
21 changes: 15 additions & 6 deletions lib/models/push.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ defmodule ExTier.Push do

alias ExTier.PushResult

@type push_result :: %{
feature: String.t(),
status: String.t(),
reason: String.t()
}

@type t :: %__MODULE__{
:results => [PushResult.t()]
}

@type log_opts :: [
already_exists_error?: boolean
]

@spec new(map()) :: t
def new(params) do
%__MODULE__{
results: Enum.map(params["results"], fn result -> PushResult.new(result) end)
}
end

@spec log(t(), log_opts) :: :ok | :error
def log(%__MODULE__{results: results}, opts \\ [already_exists_error?: false]) do
results
|> Enum.map(&PushResult.log(&1, opts))
|> Enum.all?(fn res -> res == :ok end)
|> case do
true -> :ok
false -> :error
end
end
end
26 changes: 26 additions & 0 deletions lib/models/push_result.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ defmodule ExTier.PushResult do
@moduledoc false
defstruct [:feature, :reason, :status]

require Logger

@type t :: %__MODULE__{
:feature => String.t(),
:reason => String.t(),
:status => String.t()
}

@type log_opts :: [
already_exists_error?: boolean
]

@spec new(map()) :: t
def new(params) do
%__MODULE__{
Expand All @@ -16,4 +22,24 @@ defmodule ExTier.PushResult do
status: params["status"]
}
end

@spec log(t(), log_opts) :: :ok | :error
def log(result, opts \\ [already_exists_error?: false])

def log(%__MODULE__{status: "ok"} = result, _) do
Logger.info("✔ #{result.reason}: #{result.feature}")
end

def log(%__MODULE__{} = result, opts) do
if already_exists?(result, opts) do
log(%__MODULE__{result | status: "ok"}, opts)
else
Logger.error("✘ #{result.reason}: #{result.feature} (#{result.status})")
:error
end
end

defp already_exists?(%__MODULE__{reason: reason}, opts) do
Regex.match?(~r/already exists/, reason) && opts[:already_exists_error?] == false
end
end
46 changes: 23 additions & 23 deletions test/api/push_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,43 @@ defmodule ExTier.Api.PushTest do
}
"""

@body_already_exists %{
"results" => [
%{
"feature" => "feature:PhoneNumber@plan:basic@2",
"reason" => "created",
"status" => "ok"
},
%{
"feature" => "feature:IncomingMessage@plan:basic@2",
"reason" => "feature already exists",
"status" => "failed"
},
%{
"feature" => "feature:OutgoingMessage@plan:basic@2",
"reason" => "feature already exists",
"status" => "failed"
}
]
}

setup do
Tesla.Mock.mock(fn
%{method: :post} ->
body = %{
"results" => [
%{
"feature" => "feature:PhoneNumber@plan:basic@2",
"reason" => "feature already exists",
"status" => "ok"
},
%{
"feature" => "feature:IncomingMessage@plan:basic@2",
"reason" => "feature already exists",
"status" => "ok"
},
%{
"feature" => "feature:OutgoingMessage@plan:basic@2",
"reason" => "feature already exists",
"status" => "ok"
}
]
}

%Tesla.Env{status: 200, body: body}
%Tesla.Env{status: 200, body: @body_already_exists}
end)

:ok
end

test "pull/1" do
test "push/1" do
assert {:ok, %Push{} = push} = ExTier.push(@json)
assert 3 == push.results |> length()

[%PushResult{} = result | _] = push.results

assert "feature:PhoneNumber@plan:basic@2" == result.feature
assert "feature already exists" == result.reason
assert "created" == result.reason
assert "ok" == result.status
end
end
44 changes: 44 additions & 0 deletions test/models/push_result_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
defmodule ExTier.Models.PushResultTest do
use ExUnit.Case
import ExUnit.CaptureLog
require Logger

alias ExTier.PushResult

@ok %PushResult{
feature: "feature:PhoneNumber@plan:basic@2",
reason: "created",
status: "ok"
}

@exists %PushResult{
feature: "feature:IncomingMessage@plan:basic@2",
reason: "feature already exists",
status: "failed"
}

@error %PushResult{
feature: "feature:OutgoingMessage@plan:basic@2",
reason: "could not create feature",
status: "failed"
}

test "log/2" do
assert :ok = PushResult.log(@ok)
assert :ok = PushResult.log(@exists, already_exists_error?: false)
assert :error = PushResult.log(@exists, already_exists_error?: true)
assert :error = PushResult.log(@error)
assert :error = PushResult.log(@error, already_exists_error?: false)
assert :error = PushResult.log(@error, already_exists_error?: true)

{_, created} = with_log(fn -> PushResult.log(@ok) end)
{_, failed_ok} = with_log(fn -> PushResult.log(@exists, already_exists_error?: false) end)
{_, failed_error} = with_log(fn -> PushResult.log(@exists, already_exists_error?: true) end)
{_, error} = with_log(fn -> PushResult.log(@error) end)

assert created =~ "✔ created: feature:PhoneNumber@plan:basic"
assert failed_ok =~ "✔ feature already exists: feature:IncomingMessage@plan:basic@2"
assert failed_error =~ "✘ feature already exists: feature:IncomingMessage@plan:basic@2"
assert error =~ "✘ could not create feature: feature:OutgoingMessage@plan:basic@2"
end
end
31 changes: 31 additions & 0 deletions test/models/push_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule ExTier.Models.PushTest do
use ExUnit.Case

alias ExTier.{Push, PushResult}

@push %Push{
results: [
%PushResult{
feature: "feature:PhoneNumber@plan:basic@2",
reason: "created",
status: "ok"
},
%PushResult{
feature: "feature:IncomingMessage@plan:basic@2",
reason: "feature already exists",
status: "failed"
},
%PushResult{
feature: "feature:OutgoingMessage@plan:basic@2",
reason: "feature already exists",
status: "failed"
}
]
}

test "log/2" do
assert :ok = Push.log(@push)
assert :ok = Push.log(@push, already_exists_error?: false)
assert :error = Push.log(@push, already_exists_error?: true)
end
end

0 comments on commit 2eed56f

Please sign in to comment.