write some tests

pull/363/head
connor rigby 2017-11-10 09:51:56 -08:00
parent f5d96d39c9
commit 3db2a97271
9 changed files with 181 additions and 10 deletions

View File

@ -38,7 +38,7 @@ config :farmbot, Farmbot.Repo.B,
config :farmbot, Farmbot.System.ConfigStorage,
adapter: Sqlite.Ecto2,
database: "test_tmp/farmbot_config_storage_test",
pool: Ecto.Adapters.SQL.Sandbox
database: "test_tmp/farmbot_config_storage_test"
# pool: Ecto.Adapters.SQL.Sandbox
config :farmbot, ecto_repos: [Farmbot.Repo.A, Farmbot.Repo.B, Farmbot.System.ConfigStorage]

View File

@ -257,7 +257,7 @@ defmodule Farmbot.BotState do
end
def handle_call({:unregister_farmware, fw}, _, state) do
new_pi = Map.delete(state.process_info.farmware, fw.name)
new_pi = Map.delete(state.process_info.farmwares, fw.name)
new_state = %{state | process_info: %{farmwares: new_pi}}
{:reply, :ok, [new_state], new_state}
end

View File

@ -106,6 +106,7 @@ defmodule Farmbot.Mixfile do
{:inch_ex, ">= 0.0.0", only: :dev},
{:excoveralls, "~> 0.6", only: :test},
{:mock, "~> 0.2.0", only: :test},
{:faker, "~> 0.9", only: :test }
]
end

View File

@ -18,6 +18,7 @@
"ex_json_schema": {:hex, :ex_json_schema, "0.5.5", "d8d4c3f47b86c9e634e124d518b290dda82a8b94dcc314e45af10042fc369361", [], [], "hexpm"},
"excoveralls": {:hex, :excoveralls, "0.7.2", "f69ede8c122ccd3b60afc775348a53fc8c39fe4278aee2f538f0d81cc5e7ff3a", [], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
"exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm"},
"faker": {:hex, :faker, "0.9.0", "b22c55967fbd3413b9e5c121e59e75a553065587cf31e5aa2271b6fae2775cde", [], [], "hexpm"},
"fs": {:hex, :fs, "3.4.0", "6d18575c250b415b3cad559e6f97a4c822516c7bc2c10bfbb2493a8f230f5132", [], [], "hexpm"},
"gen_mqtt": {:hex, :gen_mqtt, "0.3.1", "6ce6af7c2bcb125d5b4125c67c5ab1f29bcec2638236509bcc6abf510a6661ed", [], [{:vmq_commons, "1.0.0", [hex: :vmq_commons, repo: "hexpm", optional: false]}], "hexpm"},
"gen_stage": {:hex, :gen_stage, "0.12.2", "e0e347cbb1ceb5f4e68a526aec4d64b54ad721f0a8b30aa9d28e0ad749419cbb", [], [], "hexpm"},

View File

@ -0,0 +1,37 @@
defmodule Farmbot.Bootstrap.AuthTaskTest do
@moduledoc "Tests the timed token refresher"
alias Farmbot.Bootstrap.AuthTask
alias Farmbot.System.ConfigStorage
use ExUnit.Case, async: true
@moduletag :farmbot_api
setup do
# This is usally a timed task. Cancel the timer, so we can
# simulate the timer firing.
timer = :sys.get_state(AuthTask)
if Process.read_timer(timer) do
Process.cancel_timer(timer)
end
:ok
end
test "refreshes token and causes side effects" do
old_tps = Process.whereis Farmbot.BotState.Transport.Supervisor
old_token = ConfigStorage.get_config_value(:string, "authorization", "token")
send AuthTask, :refresh
# I'm sorry about this.
Process.sleep(1000)
new_tps = Process.whereis Farmbot.BotState.Transport.Supervisor
new_token = ConfigStorage.get_config_value(:string, "authorization", "token")
assert old_tps != new_tps
assert new_token != old_token
# check that the timer refreshed itself.
assert :sys.get_state(AuthTask)
end
end

View File

@ -0,0 +1,101 @@
defmodule Farmbot.BotStateTest do
@moduledoc "Various functions to modify the bot's state"
use ExUnit.Case, async: true
alias Farmbot.BotState
setup_all do
old_state = get_state()
{:ok, %{old_state: old_state}}
end
test "gets a pin value" do
pin = BotState.get_pin_value(-1000)
assert pin == {:error, :unknown_pin}
end
test "gets current position", %{old_state: _old_state} do
pos = BotState.get_current_pos()
assert match?(%{x: _, y: _, z: _}, pos)
end
test "Forces a push of the current state" do
state = BotState.force_state_push
assert state == get_state()
end
test "sets busy", %{old_state: _old_state} do
:ok = BotState.set_busy(true)
assert match?(true, get_state(:informational_settings).busy)
:ok = BotState.set_busy(false)
assert match?(false, get_state(:informational_settings).busy)
end
test "sets sync status :locked" do
:ok = Farmbot.BotState.set_sync_status(:locked)
assert match?(:locked, get_state(:informational_settings).sync_status)
end
test "sets sync status :maintenance" do
:ok = Farmbot.BotState.set_sync_status(:maintenance)
assert match?(:maintenance, get_state(:informational_settings).sync_status)
end
test "sets sync status :sync_error" do
:ok = Farmbot.BotState.set_sync_status(:sync_error)
assert match?(:sync_error, get_state(:informational_settings).sync_status)
end
test "sets sync status :sync_now" do
:ok = Farmbot.BotState.set_sync_status(:sync_now)
assert match?(:sync_now, get_state(:informational_settings).sync_status)
end
test "sets sync status :synced" do
:ok = Farmbot.BotState.set_sync_status(:synced)
assert match?(:synced, get_state(:informational_settings).sync_status)
end
test "sets sync status :syncing" do
:ok = Farmbot.BotState.set_sync_status(:syncing)
assert match?(:syncing, get_state(:informational_settings).sync_status)
end
test "sets sync status :unknown" do
:ok = Farmbot.BotState.set_sync_status(:unknown)
assert match?(:unknown, get_state(:informational_settings).sync_status)
end
test "sets user environment" do
val = "hey! this should be in the bot's state!"
key = inspect self()
:ok = BotState.set_user_env(key, val)
res = BotState.get_user_env
assert match?(%{^key => ^val}, res)
end
test "registers and unregisters farmware" do
name = "Not real farmware"
fw = Farmbot.TestSupport.FarmwareFactory.generate(name: name)
:ok = BotState.register_farmware(fw)
assert match?(%{^name => %{meta: %{author: _, description: _, language: _, min_os_version_major: _, version: _},
args: _,
executable: _,
path: _,
url: _}},
get_state(:process_info).farmwares)
:ok = BotState.unregister_farmware(fw)
refute Map.has_key?(get_state(:process_info).farmwares, name)
end
defp get_state(key \\ nil) do
state = :sys.get_state(Farmbot.BotState).state
if key do
Map.get(state, key)
else
state
end
end
end

View File

@ -2,12 +2,12 @@ defmodule Farmbot.SystemTest do
@moduledoc "Tests system functionaity."
use ExUnit.Case
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Farmbot.System.ConfigStorage)
end
# setup do
# :ok = Ecto.Adapters.SQL.Sandbox.checkout(Farmbot.System.ConfigStorage)
# end
test "does factory reset" do
Farmbot.System.factory_reset("hey something bad happened!")
Farmbot.System.factory_reset({:error, "hey something bad happened!"})
last = Farmbot.Test.SystemTasks.fetch_last()
assert match?({:factory_reset, _}, last)
{_, msg} = last
@ -15,7 +15,7 @@ defmodule Farmbot.SystemTest do
end
test "does reboot" do
Farmbot.System.reboot("goodbye cruel world!")
Farmbot.System.reboot({:error, "goodbye cruel world!"})
last = Farmbot.Test.SystemTasks.fetch_last()
assert match?({:reboot, _}, last)
{_, msg} = last
@ -23,7 +23,7 @@ defmodule Farmbot.SystemTest do
end
test "does shutdown" do
Farmbot.System.shutdown("see you soon!")
Farmbot.System.shutdown({:error, "see you soon!"})
last = Farmbot.Test.SystemTasks.fetch_last()
assert match?({:shutdown, _}, last)
{_, msg} = last

View File

@ -0,0 +1,31 @@
defmodule Farmbot.TestSupport.FarmwareFactory do
def generate(opts) do
meta = struct(Farmbot.Farmware.Meta,
[author: Faker.App.author(), language: "python", description: ""])
fw = struct(Farmbot.Farmware,
[name: Faker.App.name(),
version: Version.parse!(Faker.App.semver),
min_os_version_major: 6,
url: Faker.Internet.url(),
zip: Faker.Internet.url(),
executable: Faker.File.file_name(),
args: [],
config: [],
meta: meta
])
do_update(fw, opts)
end
defp do_update(fw, opts)
defp do_update(fw, [{key, val} | rest]) do
if key in Map.keys(fw) do
do_update(Map.put(fw, key, val), rest)
else
do_update(fw, rest)
end
end
defp do_update(fw, []), do: fw
end

View File

@ -8,4 +8,4 @@ ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(Farmbot.Repo.A, :manual)
Ecto.Adapters.SQL.Sandbox.mode(Farmbot.Repo.B, :manual)
Ecto.Adapters.SQL.Sandbox.mode(Farmbot.System.ConfigStorage, :manual)
# Ecto.Adapters.SQL.Sandbox.mode(Farmbot.System.ConfigStorage, :manual)