adapter needs to be fixed. brb

pull/363/head
Connor Rigby 2017-10-19 10:29:15 -07:00
parent b3ca09a796
commit 0c07507094
20 changed files with 43 additions and 429 deletions

View File

@ -38,19 +38,6 @@ config :farmbot, :behaviour,
authorization: Farmbot.Bootstrap.Authorization,
firmware_handler: Farmbot.Firmware.StubHandler
repos = [Farmbot.Repo.A, Farmbot.Repo.B, Farmbot.System.ConfigStorage]
config :farmbot, ecto_repos: repos
for repo <- [Farmbot.Repo.A, Farmbot.Repo.B] do
config :farmbot, repo,
adapter: Sqlite.Ecto2,
database: "#{repo}-#{env}.sqlite3"
end
config :farmbot, Farmbot.System.ConfigStorage,
adapter: Sqlite.Ecto2,
database: "config-#{env}.sqlite3"
case target do
"host" ->
import_config("host/#{env}.exs")

View File

@ -21,6 +21,15 @@ config :farmbot, :transport, [
Farmbot.BotState.Transport.GenMQTT
]
repos = [Farmbot.Repo.A, Farmbot.Repo.B, Farmbot.System.ConfigStorage]
config :farmbot, ecto_repos: repos
for repo <- [Farmbot.Repo.A, Farmbot.Repo.B] do
config :farmbot, repo,
adapter: Sqlite.Ecto2,
database: "#{repo}_dev.sqlite3"
end
# Configure Farmbot Behaviours.
# Default Authorization behaviour.
# SystemTasks for host mode.

View File

@ -21,15 +21,29 @@ config :farmbot, :behaviour,
system_tasks: Farmbot.Test.SystemTasks
config :farmbot, Farmbot.Repo.A,
adapter: Sqlite.Ecto2,
database: "test_A.sqlite3",
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "farmbot_repo_a_test",
hostname: "localhost",
priv: "priv/repo",
pool: Ecto.Adapters.SQL.Sandbox
config :farmbot, Farmbot.Repo.B,
adapter: Sqlite.Ecto2,
database: "test_B.sqlite3",
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "farmbot_repo_b_test",
hostname: "localhost",
priv: "priv/repo",
pool: Ecto.Adapters.SQL.Sandbox
config :farmbot, Farmbot.System.ConfigStorage,
adapter: Ecto.Adapters.Postgres,
database: "farmbot_config_storage_test",
username: "postgres",
password: "postgres",
hostname: "localhost",
pool: Ecto.Adapters.SQL.Sandbox
config :farmbot, ecto_repos: [Farmbot.Repo.A, Farmbot.Repo.B, Farmbot.System.ConfigStorage]

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
farmbot_repo_a_test 100644

Binary file not shown.

BIN
farmbot_repo_b_test 100644

Binary file not shown.

View File

@ -87,7 +87,7 @@ defmodule Farmbot.Mixfile do
{:cowboy, "~> 1.0.0"},
{:plug, "~> 1.0"},
{:ecto, "~> 2.2.2"},
{:sqlite_ecto2, "~> 2.2.1"}
{:sqlite_ecto2, "~> 2.2.1", only: [:dev, :prod]}
]
end
@ -96,7 +96,8 @@ defmodule Farmbot.Mixfile do
{:credo, "~> 0.8", only: [:dev, :test], runtime: false},
{:ex_doc, "~> 0.14", only: :dev},
{:excoveralls, "~> 0.6", only: :test},
{:mock, "~> 0.2.0", only: :test}
{:mock, "~> 0.2.0", only: :test},
{:postgrex, "~> 0.13.3", only: :test}
]
end

View File

@ -39,6 +39,7 @@
"plug": {:hex, :plug, "1.4.3", "236d77ce7bf3e3a2668dc0d32a9b6f1f9b1f05361019946aae49874904be4aed", [], [{:cowboy, "~> 1.0.1 or ~> 1.1", [hex: :cowboy, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}], "hexpm"},
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [], [], "hexpm"},
"poolboy": {:hex, :poolboy, "1.5.1", "6b46163901cfd0a1b43d692657ed9d7e599853b3b21b95ae5ae0a777cf9b6ca8", [], [], "hexpm"},
"postgrex": {:hex, :postgrex, "0.13.3", "c277cfb2a9c5034d445a722494c13359e361d344ef6f25d604c2353185682bfc", [], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 1.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm"},
"ranch": {:hex, :ranch, "1.4.0", "10272f95da79340fa7e8774ba7930b901713d272905d0012b06ca6d994f8826b", [], [], "hexpm"},
"rsa": {:hex, :rsa, "0.0.1", "a63069f88ce342ffdf8448b7cdef4b39ba7dee3c1510644a39385c7e63ba246f", [], [], "hexpm"},
"sbroker": {:hex, :sbroker, "1.0.0", "28ff1b5e58887c5098539f236307b36fe1d3edaa2acff9d6a3d17c2dcafebbd0", [], [], "hexpm"},

View File

@ -1,40 +0,0 @@
defmodule Farmbot.BotState.InformationalSettingsTest do
@moduledoc "Tests info settings."
alias Farmbot.BotState.InformationalSettings, as: Settings
use ExUnit.Case
@version Mix.Project.config()[:version]
setup do
{:ok, bot_state_tracker} = Farmbot.BotState.start_link()
{:ok, part} = Settings.start_link(bot_state_tracker, [])
[info_settings: part]
end
test "checks default values" do
info = %Settings{}
assert info.controller_version == @version
assert info.sync_status == :sync_now
end
test "checks sync_status enum" do
import Settings.SyncStatus
for sts <- [:locked, :maintenance, :sync_error, :sync_now, :synced, :syncing, :unknown] do
assert status(sts)
end
assert_raise RuntimeError, "unknown sync status: out_of_syc", fn ->
status(:out_of_syc)
end
end
test "sets busy", ctx do
Settings.set_busy(ctx.info_settings, true)
assert :sys.get_state(ctx.info_settings).public.busy == true
Settings.set_busy(ctx.info_settings, false)
assert :sys.get_state(ctx.info_settings).public.busy == false
end
end

View File

@ -1,75 +0,0 @@
defmodule Farmbot.BotState.Lib.PartitionTest do
@moduledoc "Tests common functionality for bot state partitions."
use ExUnit.Case
alias Farmbot.BotState.Lib.Partition
defstruct hello: 1, world: 1
test "dispatches info properly." do
public = %__MODULE__{}
private = %Partition.PrivateState{bot_state_tracker: self(), public: public}
r = Partition.dispatch(private)
assert match?({:noreply, ^private}, r)
assert_received {:"$gen_cast", {:update, __MODULE__, ^public}}
r = Partition.dispatch(:some_cool_result, private)
assert match?({:reply, :some_cool_result, ^private}, r)
assert_received {:"$gen_cast", {:update, __MODULE__, ^public}}
end
defmodule TestPartA do
@moduledoc false
defstruct some_state_data: :default
use Farmbot.BotState.Lib.Partition
def force_push(ser), do: GenServer.call(ser, :force)
def partition_call(:force, _, pub) do
{:reply, pub, pub}
end
end
test "uses partion." do
assert function_exported?(TestPartA, :partition_init, 1)
assert function_exported?(TestPartA, :partition_call, 3)
assert function_exported?(TestPartA, :partition_cast, 2)
assert function_exported?(TestPartA, :partition_info, 2)
end
defmodule BotStateStub do
use GenServer
def start_link() do
GenServer.start_link(__MODULE__, nil, [])
end
def put_test_fn(ser, fun), do: GenServer.call(ser, {:put_test_fn, fun})
def get_res(ser), do: GenServer.call(ser, :get_res)
def handle_call({:put_test_fn, fun}, _, _), do: {:reply, :ok, fun}
def handle_call(:get_res, _, res), do: {:reply, res, nil}
def handle_cast(cast, fun) do
{:noreply, fun.(cast)}
end
end
test "ensures default behaviour." do
{:ok, bs} = BotStateStub.start_link()
{:ok, pid} = TestPartA.start_link(bs, [])
s = :sys.get_state(pid)
pub = %TestPartA{}
assert match?(^pub, s.public)
assert s.bot_state_tracker == bs
fun = fn cast -> cast end
:ok = BotStateStub.put_test_fn(bs, fun)
TestPartA.force_push(pid)
res = BotStateStub.get_res(bs)
assert match?({:update, TestPartA, ^pub}, res)
end
end

View File

@ -1,58 +0,0 @@
defmodule Farmbot.BotState.LocationDataTest do
@moduledoc "Tests location data."
use ExUnit.Case
alias Farmbot.BotState
alias Farmbot.BotState.LocationData
alias LocationData.Vec3
setup do
{:ok, bot_state_tracker} = BotState.start_link()
{:ok, location_data} = LocationData.start_link(bot_state_tracker, [])
[location_data: location_data, bot_state_tracker: bot_state_tracker]
end
test "builds new vec3" do
vec3 = %Vec3{x: -1, y: -2, z: 123}
assert match?(^vec3, Vec3.new(-1, -2, 123))
end
test "updates position", ctx do
LocationData.report_current_position(ctx.location_data, 1, 2, 3)
assert :sys.get_state(ctx.location_data).public.position == %Vec3{x: 1, y: 2, z: 3}
assert :sys.get_state(ctx.bot_state_tracker).bot_state.location_data.position == %Vec3{
x: 1,
y: 2,
z: 3
}
end
test "updates scaled_encoders", ctx do
LocationData.report_encoder_position_scaled(ctx.location_data, 1, 2, 3)
assert :sys.get_state(ctx.location_data).public.scaled_encoders == %Vec3{x: 1, y: 2, z: 3}
assert :sys.get_state(ctx.bot_state_tracker).bot_state.location_data.scaled_encoders == %Vec3{
x: 1,
y: 2,
z: 3
}
end
test "updates raw_encoders", ctx do
LocationData.report_encoder_position_raw(ctx.location_data, 1, 2, 3)
assert :sys.get_state(ctx.location_data).public.raw_encoders == %Vec3{x: 1, y: 2, z: 3}
assert :sys.get_state(ctx.bot_state_tracker).bot_state.location_data.raw_encoders == %Vec3{
x: 1,
y: 2,
z: 3
}
end
test "updates end_stops", ctx do
LocationData.report_end_stops(ctx.location_data, 1, 1, 0, 0, 1, 0)
assert :sys.get_state(ctx.location_data).public.end_stops == "110010"
assert :sys.get_state(ctx.bot_state_tracker).bot_state.location_data.end_stops == "110010"
end
end

View File

@ -1,40 +0,0 @@
defmodule Farmbot.BotState.Transport.SupervisorTest do
@moduledoc "Tests the transport supervisor."
alias Farmbot.BotState.Transport.Supervisor, as: TPSup
use ExUnit.Case, async: false
setup_all do
{:ok, bot_state_tracker} = Farmbot.BotState.start_link()
token = "blah blah blah"
[bot_state_tracker: bot_state_tracker, token: token]
end
test "starts a transport supervisor with no children", ctx do
Application.put_env(:farmbot, :transport, [])
{:ok, transport_sup} = TPSup.start_link(ctx.token, ctx.bot_state_tracker, [])
assert Supervisor.which_children(transport_sup) == []
end
defmodule TestTransport do
@moduledoc false
use GenServer
def start_link(token, bot_state_tracker) do
GenServer.start_link(__MODULE__, [token, bot_state_tracker], name: __MODULE__)
end
def log(_), do: :ok
def emit(_), do: :ok
end
test "starts a transport supervisor with some children", ctx do
child = TestTransport
Application.put_env(:farmbot, :transport, [child])
{:ok, _tp_sup} = TPSup.start_link(ctx.token, ctx.bot_state_tracker, [])
assert is_pid(Process.whereis(TestTransport))
assert :sys.get_state(TestTransport) == [ctx.token, ctx.bot_state_tracker]
end
end

View File

@ -1,50 +0,0 @@
defmodule Farmbot.BotState.TransportTest do
@moduledoc "Tests BotState Transport"
alias Farmbot.BotState.Transport
alias Farmbot.CeleryScript.Ast
alias Farmbot.Log
use ExUnit.Case
defmodule StubTransport do
use GenServer
def start_link do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end
def emit(_msg) do
:ok
end
def log(_msg) do
:ok
end
end
setup_all do
{:ok, _tp} = StubTransport.start_link()
old = Application.get_env(:farmbot, :transport)
new = old ++ [StubTransport]
Application.put_env(:farmbot, :transport, new)
end
test "emits an ast" do
msg = %Ast{kind: "hello", args: %{}, body: []}
resp = Transport.emit(msg)
Enum.map(resp, fn res -> assert res == :ok end)
end
test "logs a message" do
log = %Log{
meta: [],
message: "hello",
created_at: DateTime.utc_now(),
channels: []
}
resp = Transport.log(log)
Enum.map(resp, fn res -> assert res == :ok end)
end
end

View File

@ -1,68 +0,0 @@
defmodule Farmbot.BotStateTest do
@moduledoc "Tests the bot state struct and server."
use ExUnit.Case
alias Farmbot.BotState
doctest BotState
test "subscribes to events" do
{:ok, pid} = BotState.start_link([])
:ok = BotState.subscribe(pid)
assert_receive {:bot_state, _state}
end
test "cant subscribe twice" do
{:ok, pid} = BotState.start_link([])
first = BotState.subscribe(pid)
second = BotState.subscribe(pid)
assert match?(:ok, first)
assert match?({:error, :already_subscribed}, second)
task =
Task.async(fn ->
third = BotState.subscribe(pid)
assert match?(:ok, third)
end)
Task.await(task)
end
test "unsubscribes from events" do
{:ok, pid} = BotState.start_link([])
:ok = BotState.subscribe(pid)
unsub = BotState.unsubscribe(pid)
assert unsub == true
end
test "updates parts" do
alias BotState.{InformationalSettings, Configuration, LocationData, ProcessInfo}
{:ok, pid} = BotState.start_link([])
:ok = GenServer.cast(pid, {:update, InformationalSettings, :info_state})
:ok = GenServer.cast(pid, {:update, Configuration, :config_state})
:ok = GenServer.cast(pid, {:update, LocationData, :location_data_state})
:ok = GenServer.cast(pid, {:update, ProcessInfo, :proc_info_state})
%{bot_state: state} = :sys.get_state(pid)
assert state.informational_settings == :info_state
assert state.configuration == :config_state
assert state.location_data == :location_data_state
assert state.process_info == :proc_info_state
end
test "forces a dispatch." do
{:ok, pid} = BotState.start_link([])
:ok = BotState.subscribe(pid)
# receive the first on_subscribe message.
receive do
{:bot_state, _} -> :ok
end
# make sure there is no more messages in the queue
refute_received {:bot_state, _}
BotState.force_dispatch(pid)
assert_received {:bot_state, _state}
end
end

View File

@ -47,8 +47,8 @@ defmodule Farmbot.Firmware.Gcode.ParserTest do
end
test "parses report position" do
a = Farmbot.Firmware.Gcode.Parser.parse_code("R82 X1 Y2 Z3 Q10")
assert a == {"10", {:report_current_position, 1, 2, 3}}
a = Farmbot.Firmware.Gcode.Parser.parse_code("R82 X1.0 Y2.0 Z3.0 Q10")
assert a == {"10", {:report_current_position, 1.0, 2.0, 3.0}}
end
test "parses report calibration" do
@ -75,8 +75,8 @@ defmodule Farmbot.Firmware.Gcode.ParserTest do
end
test "parses report encoder position scaled" do
a = Farmbot.Firmware.Gcode.Parser.parse_code("R84 X1 Y2 Z3 Q123")
assert a == {"123", {:report_encoder_position_scaled, 1, 2, 3}}
a = Farmbot.Firmware.Gcode.Parser.parse_code("R84 X1.0 Y2.0 Z3.0 Q123")
assert a == {"123", {:report_encoder_position_scaled, 1.0, 2.0, 3.0}}
end
test "parses report encoder position raw" do

View File

@ -1,74 +0,0 @@
defmodule Farmbot.FirmwareTest do
@moduledoc "Tests the Firmware transport."
use ExUnit.Case
alias Farmbot.Firmware
defmodule FirmwareHandler do
use GenServer
def start_link(firmware, opts) do
GenServer.start_link(__MODULE__, firmware, opts)
end
end
setup do
bss = Farmbot.BotStateSupport.start_bot_state_stack()
mod = FirmwareHandler
{:ok, firmware} =
Firmware.start_link(
bss.bot_state,
bss.informational_settings,
bss.configuration,
bss.location_data,
bss.mcu_params,
mod,
[]
)
{:ok, Map.put(bss, :firmware, firmware)}
end
test "gives an error on unknown/unimmplemented gcode", ctx do
r = Firmware.handle_gcode(ctx.firmware, {:not_real, 1})
assert r == {:error, :unhandled}
end
test "sets busy to false on idle", ctx do
Firmware.handle_gcode(ctx.firmware, :idle)
assert :sys.get_state(ctx.bot_state).bot_state.informational_settings.busy == false
end
test "reports position", ctx do
Firmware.handle_gcode(ctx.firmware, {:report_current_position, 123, 1234, -123})
assert match?(
%{x: 123, y: 1234, z: -123},
:sys.get_state(ctx.bot_state).bot_state.location_data.position
)
end
test "reports scaled encoders", ctx do
Firmware.handle_gcode(ctx.firmware, {:report_encoder_position_scaled, 123, 1234, -123})
assert match?(
%{x: 123, y: 1234, z: -123},
:sys.get_state(ctx.bot_state).bot_state.location_data.scaled_encoders
)
end
test "reports raw encoders", ctx do
Firmware.handle_gcode(ctx.firmware, {:report_encoder_position_raw, 123, 1234, -123})
assert match?(
%{x: 123, y: 1234, z: -123},
:sys.get_state(ctx.bot_state).bot_state.location_data.raw_encoders
)
end
test "reports end stops", ctx do
Firmware.handle_gcode(ctx.firmware, {:report_end_stops, 1, 1, 0, 0, 1, 1})
assert :sys.get_state(ctx.bot_state).bot_state.location_data.end_stops == "110011"
end
end

View File

@ -2,6 +2,10 @@ defmodule Farmbot.SystemTest do
@moduledoc "Tests system functionaity."
use ExUnit.Case
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!")
last = Farmbot.Test.SystemTasks.fetch_last()

View File

@ -4,6 +4,9 @@ unless '--exclude' in :init.get_plain_arguments() do
end
# Start ExUnit.
ExUnit.start(capture_log: true)
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)
require IEx; IEx.pry
ExUnit.start()