Skip to content

Commit

Permalink
clean up the test generation macro a bunch
Browse files Browse the repository at this point in the history
  • Loading branch information
peburrows committed Jul 11, 2024
1 parent 25f6373 commit aa5ac10
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 67 deletions.
3 changes: 2 additions & 1 deletion test/consistency_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ defmodule StatsigEx.ConsistencyTest do
|> File.read!()
|> Jason.decode!()
|> Map.get("data")
|> generate_tests()
|> generate_all_tests()

@tag :skip
test "one test" do
result =
StatsigEx.Evaluator.eval(
Expand Down
24 changes: 21 additions & 3 deletions test/exposure_logging_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule StatsigEx.ExposureLoggingTest do
if all_conditions_supported?(name, :config) do
[prim | exp] = Evaluator.eval(user, name, :config).exposures
assert Enum.sort(exp) == Enum.sort(sec)
compare_primary_exposure(user, name, :config)
end
end)
end)
Expand Down Expand Up @@ -67,9 +68,26 @@ defmodule StatsigEx.ExposureLoggingTest do
compare_logs(user, name, :gate)
end

defp compare_with_expected(user, id, type) do
defp compare_primary_exposure(user, id, type) do
flush()
result = Evaluator.eval(user, id, type)
check(user, id, type)
[{_, erl_logs} | _rest] = GenServer.call(:statsig_server, {:state})
%{events: ex_logs} = StatsigEx.state()

prune = fn logs ->
logs
|> Enum.map(fn log ->
log
|> Map.delete("time")
|> Map.delete("secondaryExposures")
end)
end

erl = prune.(erl_logs)
ex = prune.(ex_logs)

# , "test: #{inspect(user)} :: #{id} :: #{type}"
assert erl == ex
end

defp compare_logs(user, id, type) do
Expand All @@ -82,7 +100,7 @@ defmodule StatsigEx.ExposureLoggingTest do
erl = Enum.map(erl_logs, &Map.delete(&1, "time"))
ex = Enum.map(ex_logs, &Map.delete(&1, "time"))

assert ex == erl
assert erl == ex
end

defp check(user, id, :gate) do
Expand Down
90 changes: 27 additions & 63 deletions test/support/test_generator.ex
Original file line number Diff line number Diff line change
@@ -1,79 +1,43 @@
defmodule StatsigEx.TestGenerator do
defmacro generate_tests(configs) do
defmacro generate_all_tests(configs) do
quote do
Enum.flat_map(unquote(configs), fn %{
"user" => user,
"feature_gates_v2" => gates,
"dynamic_configs" => d_configs
} ->
Enum.map(gates, fn {name, %{"value" => expected, "secondary_exposures" => sec}} ->
# if(!String.contains?(name, "id_list")) do
quote do
test(
unquote(
"gate #{name} for #{Map.get(user, "userID")} | #{
:crypto.strong_rand_bytes(8) |> Base.encode64()
}"
Enum.map(gates, fn {name, spec} -> {:gate, user, name, spec} end) ++
Enum.map(d_configs, fn {name, spec} -> {:config, user, name, spec} end)
end)
|> Enum.map(fn {type, user, name,
%{"value" => expected, "secondary_exposures" => secondary}} ->
quote do
test(
unquote(
"#{type} #{name} for #{Map.get(user, "userID")} | #{
:crypto.strong_rand_bytes(8) |> Base.encode64()
}"
)
) do
result =
StatsigEx.Evaluator.eval(
unquote(Macro.escape(user)),
unquote(name),
unquote(type)
)
) do
result =
StatsigEx.Evaluator.eval(
unquote(Macro.escape(user)),
unquote(Macro.escape(name)),
:gate
)

[_ | cal_sec] = result.exposures
assert unquote(Macro.escape(expected)) == result.result
# assert unquote(Macro.escape(expected)) ==
# StatsigEx.Evaluator.eval(
# unquote(Macro.escape(user)),
# unquote(Macro.escape(name)),
# :gate
# ).result

assert Enum.sort(unquote(Macro.escape(sec))) == Enum.sort(cal_sec)
end
end

# end
end) ++
Enum.map(d_configs, fn {name, %{"value" => expected, "secondary_exposures" => sec}} ->
# don't do id_lists
# if(!String.contains?(name, "id_list")) do
quote do
# ideally I'd skip some of these dynamically based on what checks we support
# @tag skip: ...
test(
unquote(
"dynamic config #{name} for #{Map.get(user, "userID")} | #{
:crypto.strong_rand_bytes(8) |> Base.encode64()
}"
)
) do
result =
StatsigEx.Evaluator.eval(
unquote(Macro.escape(user)),
unquote(Macro.escape(name)),
:config
)

[_ | cal_sec] = result.exposures
case unquote(type) do
:gate ->
assert unquote(Macro.escape(expected)) == result.result

_ ->
assert unquote(Macro.escape(expected)) == result.value
# assert unquote(Macro.escape(expected)) ==
# StatsigEx.Evaluator.eval(
# unquote(Macro.escape(user)),
# unquote(Macro.escape(name)),
# :config
# ).value

assert Enum.sort(unquote(Macro.escape(sec))) == Enum.sort(cal_sec)
end
end

# end
end)
[_ | cal_sec] = result.exposures
assert Enum.sort(unquote(Macro.escape(secondary))) == Enum.sort(cal_sec)
end
end
end)
|> Enum.filter(fn a -> a end)
|> Enum.each(fn test_case ->
Expand Down

0 comments on commit aa5ac10

Please sign in to comment.