Skip to content

Commit

Permalink
Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gordalina committed Nov 17, 2022
1 parent fa6e374 commit 9559902
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 101 deletions.
2 changes: 2 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ import Config
config :ex_tier,
adapter: Tesla.Mock,
url: "http://localhost:8080"

config :logger, backends: []
10 changes: 0 additions & 10 deletions lib/models/phase.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ defmodule ExTier.Phase do
@moduledoc false
defstruct [:effective, :features]

alias ExTier.Utils

@type plan_name :: String.t()
@type versioned_feature_name :: String.t()
@type features :: plan_name() | versioned_feature_name()
Expand All @@ -12,12 +10,4 @@ defmodule ExTier.Phase do
effective: DateTime.t(),
features: [features()]
}

@spec new(map()) :: t
def new(params) do
%__MODULE__{
effective: Utils.to_datetime!(params["effective"]),
features: params["features"]
}
end
end
105 changes: 61 additions & 44 deletions test/api/limits_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,72 @@ defmodule ExTier.Api.LimitsTest do

alias ExTier.{Limits, Usage}

setup do
Tesla.Mock.mock(fn
%{method: :get} ->
body = %{
"org" => "org:o",
"usage" => [
%{
"feature" => "feature:f1",
"limit" => 9_223_372_036_854_775_807,
"used" => 0
},
%{
"feature" => "feature:f2",
"limit" => 9_223_372_036_854_775_807,
"used" => 0
},
%{
"feature" => "feature:f3",
"limit" => 9_223_372_036_854_775_807,
"used" => 0
}
]
}

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

:ok
end
describe "ok" do
setup do
Tesla.Mock.mock(fn
%{method: :get} ->
body = %{
"org" => "org:o",
"usage" => [
%{
"feature" => "feature:f1",
"limit" => 9_223_372_036_854_775_807,
"used" => 0
},
%{
"feature" => "feature:f2",
"limit" => 9_223_372_036_854_775_807,
"used" => 0
},
%{
"feature" => "feature:f3",
"limit" => 9_223_372_036_854_775_807,
"used" => 0
}
]
}

test "limits/1" do
assert {:ok, %Limits{} = limits} = ExTier.limits(%{org: "org:o"})
assert "org:o" == limits.org
assert 3 == length(limits.usage)
end
%Tesla.Env{status: 200, body: body}
end)

:ok
end

test "limits/1" do
assert {:ok, %Limits{} = limits} = ExTier.limits(%{org: "org:o"})
assert "org:o" == limits.org
assert 3 == length(limits.usage)
end

test "limit/1" do
assert {:ok, %Usage{} = usage} = ExTier.limit(%{org: "org:o", feature: "feature:f3"})
test "limit/1" do
assert {:ok, %Usage{} = usage} = ExTier.limit(%{org: "org:o", feature: "feature:f3"})

assert "feature:f3" == usage.feature
assert 9_223_372_036_854_775_807 == usage.limit
assert 0 == usage.used
assert "feature:f3" == usage.feature
assert 9_223_372_036_854_775_807 == usage.limit
assert 0 == usage.used
end

test "limit/1 with unknown feature" do
assert {:ok, %Usage{} = usage} = ExTier.limit(%{org: "org:o", feature: "feature:unk"})

assert "feature:unk" == usage.feature
assert 0 == usage.limit
assert 0 == usage.used
end
end

test "limit/1 with unknown feature" do
assert {:ok, %Usage{} = usage} = ExTier.limit(%{org: "org:o", feature: "feature:unk"})
describe "error" do
setup do
Tesla.Mock.mock(fn
%{method: :get} ->
%Tesla.Env{status: 400, body: %{"code" => "invalid"}}
end)

:ok
end

assert "feature:unk" == usage.feature
assert 0 == usage.limit
assert 0 == usage.used
test "limit/1" do
assert {:error, "invalid"} == ExTier.limit(%{org: "org:o", feature: "feature:f3"})
end
end
end
134 changes: 87 additions & 47 deletions test/api/pull_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,95 @@ defmodule ExTier.Api.PullTest do

alias ExTier.{Model, Plan, Feature, FeatureTier}

setup do
Tesla.Mock.mock(fn
%{method: :get} ->
body = %{
"plans" => %{
"plan:basic@0" => %{
"features" => %{
"feature:IncomingMessage" => %{"tiers" => [%{"price" => 8}]},
"feature:OutgoingMessage" => %{"tiers" => [%{"price" => 8}]},
"feature:PhoneNumber" => %{"tiers" => [%{"price" => 300}]}
},
"title" => "Basic"
},
"plan:basic@1" => %{
"features" => %{
"feature:IncomingMessage" => %{"tiers" => [%{"price" => 81}]},
"feature:OutgoingMessage" => %{"tiers" => [%{"price" => 82}]},
"feature:PhoneNumber" => %{"tiers" => [%{"price" => 3000}]}
},
"title" => "Basic"
},
"plan:basic@2" => %{
"features" => %{
"feature:IncomingMessage" => %{"tiers" => [%{"price" => 7}]},
"feature:OutgoingMessage" => %{"tiers" => [%{"price" => 7}]},
"feature:PhoneNumber" => %{"tiers" => [%{"price" => 30_000}]}
},
"title" => "Basic"
}
}
}

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

:ok
end
@body %{
"plans" => %{
"plan:basic@0" => %{
"features" => %{
"feature:IncomingMessage" => %{"tiers" => [%{"price" => 8}]},
"feature:OutgoingMessage" => %{"tiers" => [%{"price" => 8}]},
"feature:PhoneNumber" => %{"tiers" => [%{"price" => 300}]}
},
"title" => "Basic"
},
"plan:basic@32" => %{
"features" => %{
"feature:IncomingMessage" => %{"tiers" => [%{"price" => 7}]},
"feature:OutgoingMessage" => %{"tiers" => [%{"price" => 7}]},
"feature:PhoneNumber" => %{"tiers" => [%{"price" => 30_000}]}
},
"title" => "Basic"
},
"plan:basic@31" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@30" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@29" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@28" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@27" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@26" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@25" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@24" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@23" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@22" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@21" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@20" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@19" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@18" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@17" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@16" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@15" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@14" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@13" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@12" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@11" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@10" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@8" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@9" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@7" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@6" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@5" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@4" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@3" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@2" => %{"features" => %{}, "title" => "Basic"},
"plan:basic@1" => %{"features" => %{}, "title" => "Basic"}
}
}

describe "ok" do
setup do
Tesla.Mock.mock(fn
%{method: :get} ->
%Tesla.Env{status: 200, body: @body}
end)

:ok
end

test "pull/1" do
assert {:ok, %Model{} = model} = ExTier.pull()
assert 3 == model.plans |> Map.keys() |> length()
assert %Plan{features: features, title: "Basic"} = model.plans["plan:basic@0"]
assert %Feature{tiers: [%FeatureTier{price: 8}]} = features["feature:IncomingMessage"]
test "pull/1" do
assert {:ok, %Model{} = model} = ExTier.pull()
assert 33 == model.plans |> Map.keys() |> length()
assert %Plan{features: features, title: "Basic"} = model.plans["plan:basic@0"]
assert %Feature{tiers: [%FeatureTier{price: 8}]} = features["feature:IncomingMessage"]
end

test "pull_latest/1" do
assert {:ok, %Model{} = model} = ExTier.pull_latest()
assert 1 == model.plans |> Map.keys() |> length()
assert %Plan{features: features, title: "Basic"} = model.plans["plan:basic"]
assert %Feature{tiers: [%FeatureTier{price: 7}]} = features["feature:IncomingMessage"]
end
end

test "pull_latest/1" do
assert {:ok, %Model{} = model} = ExTier.pull_latest()
assert 1 == model.plans |> Map.keys() |> length()
assert %Plan{features: features, title: "Basic"} = model.plans["plan:basic"]
assert %Feature{tiers: [%FeatureTier{price: 7}]} = features["feature:IncomingMessage"]
describe "error" do
setup do
Tesla.Mock.mock(fn
%{method: :get} ->
%Tesla.Env{status: 400, body: %{"code" => "invalid"}}
end)

:ok
end

test "pull_latest/0" do
assert {:error, "invalid"} == ExTier.pull_latest()
end
end
end
13 changes: 13 additions & 0 deletions test/utils_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule ExTier.UtilsTest do
use ExUnit.Case

alias ExTier.Utils

test "cast error" do
assert :error == Utils.cast(:error, :unused)
end

test "cast exception" do
assert match?({:error, _}, Utils.cast({:ok, %{"error" => "yes"}}, :invalid))
end
end

0 comments on commit 9559902

Please sign in to comment.