|
| 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 |
0 commit comments