Skip to content

Commit 2986d34

Browse files
authored
Merge pull request #126 from orbit-apps/feature/add-testdata-support
add test data support
2 parents ae0767b + 76226bc commit 2986d34

4 files changed

Lines changed: 83 additions & 13 deletions

File tree

lib/ex_launch_darkly/app.ex

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
defmodule ExLaunchDarkly.App do
2+
@default_instance_name :default
3+
@test_options %{
4+
datasource: :testdata,
5+
send_events: false,
6+
feature_store: :ldclient_storage_map
7+
}
8+
29
@spec start(String.t(), atom(), map()) :: :ok | {:error, any(), any()}
3-
def start(key, tag \\ :default, options \\ %{})
4-
when is_binary(key) and is_atom(tag) and is_map(options) do
10+
@spec start(String.t(), map()) :: :ok | {:error, any(), any()}
11+
@spec start(String.t(), atom()) :: :ok | {:error, any(), any()}
12+
def start(key, tag, %{} = options) when is_binary(key) and is_atom(tag) do
513
options =
6-
Map.merge(
7-
%{
8-
:http_options => %{
9-
:tls_options => :ldclient_config.tls_basic_options()
10-
}
11-
},
12-
options
13-
)
14+
Map.merge(%{http_options: %{tls_options: :ldclient_config.tls_basic_options()}}, options)
1415

1516
key |> String.to_charlist() |> :ldclient.start_instance(tag, options)
1617
end
1718

19+
def start(key, options \\ %{})
20+
21+
def start(key, %{} = options) when is_binary(key),
22+
do: start(key, @default_instance_name, options)
23+
24+
def start(key, tag) when is_binary(key) and is_atom(tag), do: start(key, tag, %{})
25+
1826
@spec stop_all() :: :ok
1927
def stop_all, do: :ldclient.stop_all_instances()
28+
29+
def test_options, do: @test_options
2030
end

lib/ex_launch_darkly/test_data.ex

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
defmodule ExLaunchDarkly.TestData do
2+
@moduledoc """
3+
This provides some wrappers around the LaunchDarkly Erlang :ldclient_flagbuilder and :ldclient_testdata modules.
4+
5+
## To Use
6+
7+
Add the following to your exunit test helper:
8+
```elixir
9+
ExLaunchDarkly.App.start("test", :sometag, ExLaunchDarkly.App.test_options())
10+
or
11+
ExLaunchDarkly.App.start("test", ExLaunchDarkly.App.test_options())
12+
```
13+
14+
Add some flag set calls to your test file.
15+
16+
For an individual user:
17+
```elixir
18+
ExLaunchDarkly.TestData.set("feature-foo-bar", true, "someUser")
19+
```
20+
21+
For all users:
22+
```elixir
23+
ExLaunchDarkly.TestData.set("feature-fizz-buzz", true, "someUser")
24+
```
25+
"""
26+
import ExLaunchDarkly.Variation, only: [is_variation_value: 1]
27+
alias ExLaunchDarkly.Variation
28+
29+
@spec set(String.t(), Variation.value(), String.t()) :: :ok
30+
def set(flag, value, user)
31+
when is_binary(flag) and is_binary(user) and is_variation_value(value) do
32+
{:ok, ld_flag} = :ldclient_testdata.flag(flag)
33+
user_variation = :ldclient_flagbuilder.variation_for_context(value, "user", user, ld_flag)
34+
ld_flag = :ldclient_flagbuilder.fallthrough_variation(false, user_variation)
35+
:ldclient_testdata.update(ld_flag)
36+
end
37+
38+
@spec set_all(String.t(), Variation.value()) :: :ok
39+
def set_all(flag, value) when is_binary(flag) and is_variation_value(value) do
40+
{:ok, ld_flag} = :ldclient_testdata.flag(flag)
41+
42+
value
43+
|> :ldclient_flagbuilder.variation_for_all(ld_flag)
44+
|> :ldclient_testdata.update()
45+
end
46+
end

lib/ex_launch_darkly/user.ex

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule ExLaunchDarkly.User do
44
55
Usage
66
7-
```
7+
```elixir
88
user = User.new("a-slug")
99
# => %{key: "a-slug"}
1010
@@ -13,10 +13,13 @@ defmodule ExLaunchDarkly.User do
1313
```
1414
"""
1515
@type t() :: :ldclient_user.user()
16+
@type key() :: :ldclient_user.key()
1617
@type attribute() :: :ldclient_user.attribute()
1718

18-
@spec new(String.t()) :: t()
19-
def new(key) when is_binary(key), do: :ldclient_user.new(key)
19+
defguard is_user_key(key) when is_binary(key) or key == :null
20+
21+
@spec new(key()) :: t()
22+
def new(key) when is_user_key(key), do: :ldclient_user.new(key)
2023

2124
@spec new_from_map(map()) :: t()
2225
def new_from_map(%{} = user_attributes), do: :ldclient_user.new_from_map(user_attributes)

lib/ex_launch_darkly/variation.ex

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
defmodule ExLaunchDarkly.Variation do
2+
@type value() :: :ldclient_flag.variation_value()
3+
@type key() :: :ldclient_flag.key()
4+
5+
defguard is_variation_key(key) when is_binary(key)
6+
7+
defguard is_variation_value(value)
8+
when is_boolean(value) or is_integer(value) or
9+
is_float(value) or is_binary(value) or
10+
is_list(value) or is_map(value)
11+
end

0 commit comments

Comments
 (0)