Skip to content
Permalink
Browse files

Tests for needs_reset update usecase

  • Loading branch information
RickCarlino authored and ConnorRigby committed Dec 19, 2019
1 parent 27fb2d2 commit 7664d2a9cc85cf7e8d9272a8048a3cc71331018a
Showing with 31 additions and 4 deletions.
  1. +27 −3 farmbot_core/test/asset_workers/device_worker_test.exs
  2. +4 −1 test/support/asset_fixtures.ex
@@ -5,12 +5,17 @@ defmodule FarmbotCore.DeviceWorkerTest do
alias FarmbotCore.AssetWorker
alias Farmbot.TestSupport.CeleryScript.TestSysCalls

def fresh_device(needs_reset \\ true) do
params = %{needs_reset: needs_reset}
assert %Device{} = dev = AssetFixtures.device_init(params)
dev
end

describe "devices" do
test "updates device triggering " do
test "triggering of factory reset during init" do
{:ok, _} = TestSysCalls.checkout()
test_pid = self()
params = %{needs_reset: true}
assert %Device{} = dev = AssetFixtures.device(params)
dev = fresh_device()

:ok =
TestSysCalls.handle(TestSysCalls, fn
@@ -23,4 +28,23 @@ defmodule FarmbotCore.DeviceWorkerTest do
assert_receive {:factory_reset, ["farmbot_os"]}
end
end

test "triggering of factory reset during update" do
{:ok, _} = TestSysCalls.checkout()
test_pid = self()
dev = fresh_device(false)

:ok =
TestSysCalls.handle(TestSysCalls, fn
kind, args ->
send(test_pid, {kind, args})
:ok
end)

{:ok, pid} = AssetWorker.start_link(dev, [])
refute_receive {:factory_reset, ["farmbot_os"]}

GenServer.cast(pid, {:new_data, %{dev | needs_reset: true}})
assert_receive {:factory_reset, ["farmbot_os"]}
end
end
@@ -101,7 +101,10 @@ defmodule Farmbot.TestSupport.AssetFixtures do
|> Repo.insert!()
end

def device(params \\ %{}) do
@doc """
Instantiates, but does not create, a %Device{}
"""
def device_init(params \\ %{}) do
defaults = %{id: :rand.uniform(1_000_000), monitor: false}
params = Map.merge(defaults, params)

0 comments on commit 7664d2a

Please sign in to comment.
You can’t perform that action at this time.