Skip to content

Commit

Permalink
Update FarmbotCeleryScriptTest to use Mimic
Browse files Browse the repository at this point in the history
  • Loading branch information
RickCarlino committed Jan 22, 2020
1 parent f3f8316 commit 2f7685c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 328 deletions.
9 changes: 2 additions & 7 deletions farmbot_celery_script/config/config.exs
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions farmbot_celery_script/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions farmbot_celery_script/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
46 changes: 14 additions & 32 deletions farmbot_celery_script/test/farmbot_celery_script_test.exs
Original file line number Diff line number Diff line change
@@ -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 =
Expand All @@ -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}
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -78,16 +68,11 @@ defmodule FarmbotCeleryScriptTest do
}
|> AST.decode()

:ok =
TestSysCalls.handle(TestSysCalls, fn
:read_pin, _ -> {:error, "failed to read pin!"}
end)

assert {:error, "failed to read pin!"} =
FarmbotCeleryScript.execute(execute_ast, execute_ast)
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_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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions farmbot_celery_script/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Application.ensure_all_started(:mimic)
Mimic.copy(FarmbotCeleryScript.SysCalls.Stubs)
ExUnit.start()
9 changes: 5 additions & 4 deletions farmbot_ext/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -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)
Loading

0 comments on commit 2f7685c

Please sign in to comment.