Packages

This repo contains a testing library used in Carla end-to-end tests. It provides a library to describe and test conversations.

Current section

Files

Jump to
carla_test_helper lib carla_test_helper.ex
Raw

lib/carla_test_helper.ex

defmodule CarlaTestHelper do
@moduledoc """
Carla test helper
"""
alias CarlaTestHelper.V1.Connector, as: BotV1Connector
alias CarlaTestHelper.V2.Connector, as: BotV2Connector
defmacro __using__(_) do
end
@doc """
Macro to help easily define a set a list of assertions to test against the bot.
`carlatest` will accept a name and a list of assertion functions and return a new function
with the given name that accepts 2 parameters.
The resulting function's parameters:
* uuid - The uuid of the user to use when testing the list of assertions.
* state (defaults to `%{}`) - The state to setup for the user before running tests.
The macro's `assertions` argument is a expected to be a list of assertion functions.
This macro will take the list of functions and iterate through each one and call them.
This allows the user of the macro to define a conversation test case like so:
```
carlatest "request details optima", do: [
message("optima", "request.details.optima"),
quick_reply("Build & Price", "build:_optima"),
]
```
"""
defmacro carlatest(name, do: assertions) do
assertion_functions =
case assertions do
nil ->
[]
block ->
Enum.map(block, fn {assert_fn, _, args} ->
{assert_fn, args}
end)
end
quote do
test unquote(name), context do
adapter = get_adapter()
Enum.reduce(unquote(assertion_functions), %{}, fn {func, args}, response ->
fn_args = Enum.concat([response, context[:conversation_id]], args)
apply(adapter, func, fn_args)
end)
end
end
end
@doc """
Get the adapter specified in the config file
Returns a Response object.
"""
@spec get_bot_version() :: atom()
def get_bot_version() do
case Application.get_env(:carla_test_helper, :bot_version, "v1") do
nil -> "v1"
bot_version -> String.downcase(bot_version)
end
end
@doc """
Get the adapter based on the bot version
"""
@spec get_adapter() :: module()
def get_adapter() do
case get_bot_version() do
"v1" ->
BotV1Connector
"v2" ->
BotV2Connector
end
end
end