write some test for repo + farm_events
This commit is contained in:
parent
c5e24ae09e
commit
a05b6ebd7d
|
@ -18,3 +18,10 @@ config :farmbot, :behaviour, [
|
|||
authorization: Farmbot.Test.Authorization,
|
||||
system_tasks: Farmbot.Test.SystemTasks
|
||||
]
|
||||
|
||||
config :farmbot, Farmbot.Repo,
|
||||
adapter: Sqlite.Ecto2,
|
||||
database: "test.sqlite3",
|
||||
pool: Ecto.Adapters.SQL.Sandbox
|
||||
|
||||
config :farmbot, ecto_repos: [Farmbot.Repo]
|
||||
|
|
|
@ -44,7 +44,9 @@ defmodule Farmbot do
|
|||
name = Keyword.get(start_opts, :name, __MODULE__)
|
||||
case Supervisor.start_link(__MODULE__, [], [name: name]) do
|
||||
{:ok, pid} -> {:ok, pid}
|
||||
{:error, reason} -> Farmbot.System.factory_reset(reason)
|
||||
{:error, reason} ->
|
||||
Farmbot.System.factory_reset(reason)
|
||||
exit(reason)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -15,19 +15,21 @@ defmodule Farmbot.Repo.FarmEvent.ExecutableType do
|
|||
|
||||
def cast(string), do: {:ok, string}
|
||||
|
||||
# Load from DB
|
||||
Enum.map(@executable_types, fn(exp) ->
|
||||
def load(unquote(exp)) do
|
||||
{:ok, Module.concat([Farmbot, Repo, FarmEvent, unquote(exp)])}
|
||||
{:ok, Module.concat([Farmbot, Repo, unquote(exp)])}
|
||||
end
|
||||
end)
|
||||
|
||||
def load(_), do: :error
|
||||
|
||||
# Dump to DB
|
||||
Enum.map(@executable_types, fn(exp) ->
|
||||
def dump(unquote(exp)) do
|
||||
{:ok, unquote(exp)}
|
||||
end
|
||||
end)
|
||||
|
||||
def dump(_), do: :error
|
||||
def dump(exe_type), do: :error
|
||||
end
|
||||
|
|
43
test/farmbot/repo/farm_event_test.exs
Normal file
43
test/farmbot/repo/farm_event_test.exs
Normal file
|
@ -0,0 +1,43 @@
|
|||
defmodule Farmbot.Repo.FarmEventTest do
|
||||
alias Farmbot.Repo
|
||||
alias Farmbot.Repo.FarmEvent
|
||||
use ExUnit.Case
|
||||
|
||||
setup do
|
||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
|
||||
end
|
||||
|
||||
@farm_event_seq %{"end_time" => "2017-09-20T19:07:00.000000Z", "executable_id" => 132,
|
||||
"executable_type" => "Sequence", "id" => 379,
|
||||
"repeat" => 1, "start_time" => "2017-09-20T19:06:00.000000Z",
|
||||
"time_unit" => "never"}
|
||||
|
||||
test "inserts and transforms valid farm_event" do
|
||||
assert(@farm_event_seq
|
||||
|> Poison.encode!
|
||||
|> Poison.decode!(as: %FarmEvent{})
|
||||
|> FarmEvent.changeset()
|
||||
|> Repo.insert())
|
||||
|
||||
import Ecto.Query
|
||||
id = @farm_event_seq["id"]
|
||||
[fe] = (from fe in FarmEvent, where: fe.id == ^id, select: fe) |> Repo.all()
|
||||
assert fe.executable_type == Repo.Sequence
|
||||
assert fe.start_time.__struct__ == DateTime
|
||||
|
||||
end
|
||||
|
||||
test "raises on unimplemented executable types" do
|
||||
cs = @farm_event_seq
|
||||
|> Map.put("executable_type", "UnknownResource")
|
||||
|> Poison.encode!
|
||||
|> Poison.decode!(as: %FarmEvent{})
|
||||
|> FarmEvent.changeset()
|
||||
msg = ~S(value `"UnknownResource"` for `Farmbot.Repo.FarmEvent.executable_type` in `insert` does not match type Farmbot.Repo.FarmEvent.ExecutableType)
|
||||
assert_raise Ecto.ChangeError, msg, fn() ->
|
||||
Repo.insert(cs)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
14
test/farmbot/repo/syncable_test.exs
Normal file
14
test/farmbot/repo/syncable_test.exs
Normal file
|
@ -0,0 +1,14 @@
|
|||
defmodule Farmbot.Repo.SyncableTest do
|
||||
@moduledoc "Tests Syncable module."
|
||||
|
||||
use ExUnit.Case
|
||||
|
||||
test "ensures time from is8601" do
|
||||
now_dt = DateTime.utc_now()
|
||||
now_iso = now_dt |> DateTime.to_iso8601()
|
||||
obj = %{some_time: now_iso, other_time: now_iso}
|
||||
res = Farmbot.Repo.Syncable.ensure_time(obj, [:some_time, :other_time])
|
||||
assert res.some_time == now_dt
|
||||
assert res.other_time == now_dt
|
||||
end
|
||||
end
|
13
test/farmbot/repo_test.exs
Normal file
13
test/farmbot/repo_test.exs
Normal file
|
@ -0,0 +1,13 @@
|
|||
defmodule Farmbot.RepoTest do
|
||||
@moduledoc "Tests the Farmbot Repo"
|
||||
|
||||
use ExUnit.Case
|
||||
|
||||
test "ensures all syncables implement behaviour" do
|
||||
modules = Farmbot.Repo.syncables()
|
||||
assert(Enum.all? modules, fn(mod) ->
|
||||
assert Code.ensure_loaded(mod)
|
||||
assert function_exported?(mod, :sync!, 1)
|
||||
end)
|
||||
end
|
||||
end
|
|
@ -1,7 +0,0 @@
|
|||
defmodule FarmbotTest do
|
||||
@moduledoc "Tests Farmbot"
|
||||
|
||||
use ExUnit.Case
|
||||
|
||||
end
|
||||
|
|
@ -5,3 +5,4 @@ end
|
|||
|
||||
# Start ExUnit.
|
||||
ExUnit.start([capture_log: true])
|
||||
Ecto.Adapters.SQL.Sandbox.mode(Farmbot.Repo, :manual)
|
||||
|
|
Loading…
Reference in a new issue