Skip to content

Commit

Permalink
feat: initial integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ejscunha committed Jul 28, 2024
1 parent 20ac5a9 commit 03779b9
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/open_feature/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ defmodule OpenFeature.Client do
try do
before_hooks(all_hooks, hook_context, opts)

evaluation_details = evaluation_details(client, type, key, default, hook_context)
evaluation_details = evaluation_details(client, type, key, default, merged_context)

after_hooks(all_hooks, hook_context, evaluation_details, opts)

Expand Down
2 changes: 1 addition & 1 deletion lib/open_feature/provider/in_memory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule OpenFeature.Provider.InMemory do
end

defp check_flag_enabled(%{disabled: false}, _default), do: :ok
defp check_flag_enabled(%{disabled: true}, default), do: {:ok, %{value: default, reason: :disabled}}
defp check_flag_enabled(%{disabled: true}, default), do: {:ok, %ResolutionDetails{value: default, reason: :disabled}}

defp get_variant(%{context_evaluator: context_evaluator}, context) when is_function(context_evaluator, 1),
do: {:ok, context_evaluator.(context), true}
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule OpenFeature.MixProject do
version: "0.1.0",
elixir: "~> 1.14",
start_permanent: Mix.env() == :prod,
test_paths: ["test/unit", "test/integration"],
deps: deps(),

# Docs
Expand Down
1 change: 1 addition & 0 deletions test/integration/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()
84 changes: 84 additions & 0 deletions test/integration/value_resolution_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
defmodule Integration.ValueResolutionTest do
use ExUnit.Case, async: false
alias OpenFeature.Client
alias OpenFeature.EvaluationDetails
alias OpenFeature.Provider.InMemory

setup_all do
OpenFeature.clear_providers()

provider = %InMemory{
flags: %{
"flag_key" => %{
disabled: false,
default_variant: "default",
variants: %{
"default" => "default_value"
}
},
"target_key" => %{
disabled: false,
default_variant: "default",
context_evaluator: fn context -> Map.get(context, :variant, "default") end,
variants: %{
"default" => "default_value",
"variant1" => "variant1_value"
}
},
"disabled_flag" => %{
disabled: true,
default_variant: "default",
variants: %{
"default" => "default_value"
}
}
}
}

OpenFeature.set_provider("domain", provider)
client = OpenFeature.get_client("domain")

{:ok, client: client}
end

describe "Value resolution" do
test "resolves a static value", %{client: client} do
assert %EvaluationDetails{value: "default_value", variant: "default", reason: :static} =
Client.get_string_details(client, "flag_key", "default")
end

test "resolves a targetted value", %{client: client} do
assert %EvaluationDetails{value: "variant1_value", variant: "variant1", reason: :targeting_match} =
Client.get_string_details(client, "target_key", "default", context: %{variant: "variant1"})
end

test "resolves a flag without variant", %{client: client} do
assert %EvaluationDetails{
value: "default",
variant: nil,
reason: :error,
error_code: :general,
error_message: "variant not found"
} =
Client.get_string_details(client, "target_key", "default", context: %{variant: "variant2"})
end

test "resolves a disabled flag", %{client: client} do
assert %EvaluationDetails{
value: "default",
variant: nil,
reason: :disabled
} =
Client.get_string_details(client, "disabled_flag", "default")
end

test "resolves a non existant flag", %{client: client} do
assert %EvaluationDetails{
value: "default",
variant: nil,
reason: :error
} =
Client.get_string_details(client, "inexistent_flag", "default")
end
end
end
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ defmodule OpenFeature.Provider.InMemoryTest do
key = "test_key"
default = true
context = %{}
expected_result = {:ok, %{value: true, reason: :disabled}}
expected_result = {:ok, %ResolutionDetails{value: true, reason: :disabled}}

assert ^expected_result = InMemory.resolve_boolean_value(provider, key, default, context)
end
Expand Down Expand Up @@ -192,7 +192,7 @@ defmodule OpenFeature.Provider.InMemoryTest do
key = "test_key"
default = "default"
context = %{}
expected_result = {:ok, %{value: "default", reason: :disabled}}
expected_result = {:ok, %ResolutionDetails{value: "default", reason: :disabled}}

assert ^expected_result = InMemory.resolve_string_value(provider, key, default, context)
end
Expand Down Expand Up @@ -281,7 +281,7 @@ defmodule OpenFeature.Provider.InMemoryTest do
key = "test_key"
default = 42
context = %{}
expected_result = {:ok, %{value: 42, reason: :disabled}}
expected_result = {:ok, %ResolutionDetails{value: 42, reason: :disabled}}

assert ^expected_result = InMemory.resolve_number_value(provider, key, default, context)
end
Expand Down Expand Up @@ -370,7 +370,7 @@ defmodule OpenFeature.Provider.InMemoryTest do
key = "test_key"
default = %{}
context = %{}
expected_result = {:ok, %{value: %{}, reason: :disabled}}
expected_result = {:ok, %ResolutionDetails{value: %{}, reason: :disabled}}

assert ^expected_result = InMemory.resolve_map_value(provider, key, default, context)
end
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 03779b9

Please sign in to comment.