Finish testing AutoSyncAssetHandler

pull/1184/head
Rick Carlino 2020-03-31 14:45:14 -05:00
parent 1aac649e9b
commit 53f28daefe
3 changed files with 41 additions and 15 deletions

View File

@ -1,6 +1,7 @@
use Mix.Config
if Mix.env() == :test do
config :ex_unit, capture_logs: true
mapper = fn mod -> config :farmbot_ext, mod, children: [] end
list = [

View File

@ -1,4 +1,5 @@
defmodule AutoSyncAssetHandlerTest do
require Helpers
use ExUnit.Case, async: false
use Mimic
@ -8,6 +9,8 @@ defmodule AutoSyncAssetHandlerTest do
alias FarmbotExt.AMQP.AutoSyncAssetHandler
alias FarmbotCore.{Asset, BotState, Leds}
import ExUnit.CaptureLog
def auto_sync_off, do: expect(Asset.Query, :auto_sync?, fn -> false end)
def auto_sync_on, do: expect(Asset.Query, :auto_sync?, fn -> true end)
@ -27,9 +30,12 @@ defmodule AutoSyncAssetHandlerTest do
test "Handles @no_cache_kinds" do
id = 64
params = %{}
kind = ~w(Device FbosConfig FirmwareConfig FarmwareEnv FarmwareInstallation)
|> Enum.shuffle
|> Enum.at(0)
kind =
~w(Device FbosConfig FirmwareConfig FarmwareEnv FarmwareInstallation)
|> Enum.shuffle()
|> Enum.at(0)
expect(Asset.Command, :update, 1, fn ^kind, ^id, ^params -> :ok end)
assert :ok = AutoSyncAssetHandler.cache_sync(kind, id, params)
end
@ -42,4 +48,19 @@ defmodule AutoSyncAssetHandlerTest do
expect_green_leds(:solid)
AutoSyncAssetHandler.handle_asset("Point", 32, nil)
end
test "cache sync" do
id = 64
params = %{}
kind = "Point"
# Helpers.expect_log("Autocaching sync #{kind} #{id} #{inspect(params)}")
changeset = %{ab: :cd}
changesetfaker = fn ^kind, ^id, ^params -> changeset end
expect(FarmbotCore.Asset.Command, :new_changeset, 1, changesetfaker)
expect(FarmbotExt.API.EagerLoader, :cache, 1, fn ^changeset -> :ok end)
expect_sync_status_to_be("sync_now")
expect_green_leds(:slow_blink)
do_it = fn -> AutoSyncAssetHandler.cache_sync(kind, id, params) end
assert capture_log(do_it) =~ "Autocaching sync Point 64 %{}"
end
end

View File

@ -1,17 +1,21 @@
Application.ensure_all_started(:farmbot)
Mimic.copy(AMQP.Channel)
Mimic.copy(FarmbotCeleryScript.SysCalls.Stubs)
Mimic.copy(FarmbotCore.Asset.Command)
Mimic.copy(FarmbotCore.Asset.Query)
Mimic.copy(FarmbotCore.BotState)
Mimic.copy(FarmbotCore.Leds)
Mimic.copy(FarmbotCore.LogExecutor)
Mimic.copy(FarmbotExt.AMQP.ConnectionWorker)
Mimic.copy(FarmbotExt.API.EagerLoader.Supervisor)
Mimic.copy(FarmbotExt.API.Preloader)
Mimic.copy(FarmbotExt.API)
Mimic.copy(FarmbotExt.AMQP.AutoSyncAssetHandler)
[
AMQP.Channel,
FarmbotCeleryScript.SysCalls.Stubs,
FarmbotCore.Asset.Command,
FarmbotCore.Asset.Query,
FarmbotCore.BotState,
FarmbotCore.Leds,
FarmbotCore.LogExecutor,
FarmbotExt.AMQP.AutoSyncAssetHandler,
FarmbotExt.AMQP.ConnectionWorker,
FarmbotExt.API,
FarmbotExt.API.EagerLoader,
FarmbotExt.API.EagerLoader.Supervisor,
FarmbotExt.API.Preloader,
]
|> Enum.map(&Mimic.copy/1)
timeout = System.get_env("EXUNIT_TIMEOUT") || "5000"
System.put_env("LOG_SILENCE", "true")