write some test for repo + farm_events

This commit is contained in:
connor rigby 2017-09-19 12:34:41 -07:00
parent c5e24ae09e
commit a05b6ebd7d
8 changed files with 85 additions and 10 deletions

View file

@ -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]

View file

@ -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

View file

@ -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

View 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

View 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

View 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

View file

@ -1,7 +0,0 @@
defmodule FarmbotTest do
@moduledoc "Tests Farmbot"
use ExUnit.Case
end

View file

@ -5,3 +5,4 @@ end
# Start ExUnit.
ExUnit.start([capture_log: true])
Ecto.Adapters.SQL.Sandbox.mode(Farmbot.Repo, :manual)