Update FarmbotCeleryScriptTest to use Mimic

pull/1124/head
Rick Carlino 2020-01-22 17:07:22 -06:00
parent f3f8316cc3
commit 2f7685c077
7 changed files with 25 additions and 328 deletions

View File

@ -1,9 +1,4 @@
use Mix.Config
if Mix.env() == :test do
config :farmbot_celery_script, FarmbotCeleryScript.SysCalls,
sys_calls: Farmbot.TestSupport.CeleryScript.TestSysCalls
else
config :farmbot_celery_script, FarmbotCeleryScript.SysCalls,
sys_calls: FarmbotCeleryScript.SysCalls.Stubs
end
config :farmbot_celery_script, FarmbotCeleryScript.SysCalls,
sys_calls: FarmbotCeleryScript.SysCalls.Stubs

View File

@ -66,8 +66,8 @@ defmodule FarmbotCeleryScript.MixProject do
{:jason, "~> 1.1"},
{:timex, "~> 3.4"},
{:excoveralls, "~> 0.10", only: [:test], targets: [:host]},
{:dialyxir, "~> 1.0.0-rc.3",
only: [:dev], targets: [:host], runtime: false},
{:mimic, "~> 1.1", only: :test},
{:dialyxir, "~> 1.0.0-rc.3", only: [:dev], targets: [:host], runtime: false},
{:ex_doc, "~> 0.21.2", only: [:dev], targets: [:host], runtime: false}
]
end

View File

@ -13,6 +13,7 @@
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm"},
"mimic": {:hex, :mimic, "1.1.3", "3bad83d5271b4faa7bbfef587417a6605cbbc802a353395d446a1e5f46fe7115", [:mix], [], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.5.3", "def21c10a9ed70ce22754fdeea0810dafd53c2db3219a0cd54cf5526377af1c6", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.5", "6eaf7ad16cb568bb01753dbbd7a95ff8b91c7979482b95f38443fe2c8852a79b", [:make, :mix, :rebar3], [], "hexpm"},

View File

@ -1,12 +1,9 @@
defmodule FarmbotCeleryScriptTest do
use ExUnit.Case, async: true
alias FarmbotCeleryScript.AST
alias Farmbot.TestSupport.CeleryScript.TestSysCalls
use Mimic
setup do
{:ok, _shim} = TestSysCalls.checkout()
:ok
end
alias FarmbotCeleryScript.AST
alias FarmbotCeleryScript.SysCalls.Stubs
test "uses default values when no parameter is found" do
sequence_ast =
@ -24,7 +21,7 @@ defmodule FarmbotCeleryScriptTest do
label: "foo",
default_value: %{
kind: :coordinate,
args: %{x: 129, y: 129, z: 129}
args: %{x: 12, y: 11, z: 10}
}
}
}
@ -52,19 +49,12 @@ defmodule FarmbotCeleryScriptTest do
me = self()
:ok =
TestSysCalls.handle(TestSysCalls, fn
:move_absolute, args ->
send(me, {:move_absolute, args})
:ok
expect(Stubs, :coordinate, fn x, y, z ->
%{x: x, y: y, z: z}
end)
:coordinate, [x, y, z] ->
%{x: x, y: y, z: z}
end)
_ = FarmbotCeleryScript.execute(sequence_ast, me)
assert_receive {:step_complete, ^me, :ok}
assert_receive {:move_absolute, [129, 129, 129, 921]}
result = FarmbotCeleryScript.execute(sequence_ast, me)
assert result = :ok
end
test "syscall errors" do
@ -78,16 +68,11 @@ defmodule FarmbotCeleryScriptTest do
}
|> AST.decode()
:ok =
TestSysCalls.handle(TestSysCalls, fn
:read_pin, _ -> {:error, "failed to read pin!"}
end)
expect(Stubs, :read_pin, fn _, _ -> {:error, "failed to read pin!"} end)
result = FarmbotCeleryScript.execute(execute_ast, execute_ast)
assert {:error, "failed to read pin!"} = result
assert {:error, "failed to read pin!"} =
FarmbotCeleryScript.execute(execute_ast, execute_ast)
assert_receive {:step_complete, ^execute_ast,
{:error, "failed to read pin!"}}
assert_receive {:step_complete, ^execute_ast, {:error, "failed to read pin!"}}
end
test "regular exceptions still occur" do
@ -101,10 +86,7 @@ defmodule FarmbotCeleryScriptTest do
}
|> AST.decode()
:ok =
TestSysCalls.handle(TestSysCalls, fn
:read_pin, _ -> raise("big oops")
end)
expect(Stubs, :read_pin, fn _, _ -> raise("big oops") end)
assert {:error, "big oops"} ==
FarmbotCeleryScript.execute(execute_ast, execute_ast)

View File

@ -1,2 +1,3 @@
Application.ensure_all_started(:mimic)
Mimic.copy(FarmbotCeleryScript.SysCalls.Stubs)
ExUnit.start()

View File

@ -1,11 +1,12 @@
timeout = System.get_env("EXUNIT_TIMEOUT")
Mimic.copy(FarmbotCore.Asset.Query)
Mimic.copy(FarmbotExt.API)
Mimic.copy(FarmbotExt.AMQP.ConnectionWorker)
Mimic.copy(FarmbotCeleryScript.SysCalls.Stubs)
if timeout do
ExUnit.start(assert_receive_timeout: String.to_integer(timeout))
else
ExUnit.start()
end
Mimic.copy(FarmbotCore.Asset.Query)
Mimic.copy(FarmbotExt.API)
Mimic.copy(FarmbotExt.AMQP.ConnectionWorker)

View File

@ -1,283 +0,0 @@
defmodule Farmbot.TestSupport.CeleryScript.TestSysCalls do
@moduledoc """
Stub implementation of CeleryScript SysCalls
"""
@behaviour FarmbotCeleryScript.SysCalls
use GenServer
def checkout do
case GenServer.start_link(__MODULE__, [], name: __MODULE__) do
{:error, {:already_started, pid}} ->
:ok = GenServer.call(pid, :checkout)
{:ok, pid}
{:ok, pid} ->
:ok = GenServer.call(pid, :checkout)
{:ok, pid}
end
end
def handle(pid, fun) when is_function(fun, 2) do
GenServer.call(pid, {:handle, fun})
end
@impl true
def init([]) do
{:ok, %{checked_out: nil, handler: nil}}
end
@impl true
def handle_call(:checkout, {pid, _}, state) do
{:reply, :ok, %{state | checked_out: pid}}
end
def handle_call({:handle, fun}, {pid, _}, %{checked_out: pid} = state) do
{:reply, :ok, %{state | handler: fun}}
end
def handle_call({kind, args}, _from, %{handler: handler} = state)
when is_function(handler, 2) do
{:reply, {handler, kind, args}, state}
end
@impl true
def log(_message, _force) do
:ok
end
@impl true
def sequence_init_log(_message) do
:ok
end
@impl true
def sequence_complete_log(_message) do
:ok
end
@impl true
def coordinate(x, y, z) do
%{x: x, y: y, z: z}
end
@impl true
def nothing() do
call({:nothing, []})
end
@impl true
def set_servo_angle(pin_number, angle) do
call({:set_servo_angle, [pin_number, angle]})
end
@impl true
def set_pin_io_mode(pin_number, mode) do
call({:set_pin_io_mode, [pin_number, mode]})
end
@impl true
def install_first_party_farmware() do
call({:install_first_party_farmware, []})
end
@impl true
def point(type, id) do
call({:point, [type, id]})
end
@impl true
def get_point_group(type_or_id) do
call({:get_point_group, [type_or_id]})
end
@impl true
def move_absolute(x, y, z, speed) do
call({:move_absolute, [x, y, z, speed]})
end
@impl true
def get_current_x do
call({:get_current_x, []})
end
@impl true
def get_current_y do
call({:get_current_y, []})
end
@impl true
def get_current_z do
call({:get_current_z, []})
end
@impl true
def get_cached_x do
call({:get_cached_x, []})
end
@impl true
def get_cached_y do
call({:get_cached_y, []})
end
@impl true
def get_cached_z do
call({:get_cached_z, []})
end
@impl true
def write_pin(pin_number, mode, value) do
call({:write_pin, [pin_number, mode, value]})
end
@impl true
def named_pin(type, id) do
call({:named_pin, [type, id]})
end
@impl true
def read_pin(number, mode) do
call({:read_pin, [number, mode]})
end
@impl true
def read_cached_pin(number) do
call({:read_cached_pin, [number]})
end
@impl true
def toggle_pin(number) do
call({:toggle_pin, [number]})
end
@impl true
def wait(millis) do
call({:wait, [millis]})
end
@impl true
def send_message(level, message, channels) do
call({:send_message, [level, message, channels]})
end
@impl true
def find_home(axis) do
call({:find_home, [axis]})
end
@impl true
def get_sequence(id) do
call({:get_sequence, [id]})
end
@impl true
def execute_script(name, args) do
call({:execute_script, [name, args]})
end
@impl true
def update_farmware(name) do
call({:update_farmware, [name]})
end
@impl true
def read_status do
call({:read_status, []})
end
@impl true
def set_user_env(key, val) do
call({:set_user_env, [key, val]})
end
@impl true
def sync do
call({:sync, []})
end
@impl true
def calibrate(axis) do
call({:calibrate, [axis]})
end
@impl true
def flash_firmware(package) do
call({:flash_firmware, [package]})
end
@impl true
def change_ownership(email, secret, server) do
call({:change_ownership, [email, secret, server]})
end
@impl true
def dump_info() do
call({:dump_info, []})
end
@impl true
def factory_reset(package) do
call({:factory_reset, [package]})
end
@impl true
def firmware_reboot do
call({:firmware_reboot, []})
end
@impl true
def power_off do
call({:power_off, []})
end
@impl true
def reboot do
call({:reboot, []})
end
@impl true
def resource_update(kind, id, params) do
call({:resource_update, [kind, id, params]})
end
@impl true
def check_update() do
call({:check_update, []})
end
@impl true
def emergency_lock() do
call({:emergency_lock, []})
end
@impl true
def emergency_unlock() do
call({:emergency_unlock, []})
end
@impl true
def get_toolslot_for_tool(id) do
call({:get_toolslot_for_tool, [id]})
end
@impl true
def home(axis, speed) do
call({:home, [axis, speed]})
end
@impl true
def zero(axis) do
call({:zero, [axis]})
end
@impl true
def eval_assertion(comment, expression) do
call({:eval_assertion, [comment, expression]})
end
defp call(data) do
{handler, kind, args} = GenServer.call(__MODULE__, data, :infinity)
handler.(kind, args)
end
end