littlebitatests
parent
0d38ac54b1
commit
7e87d34ba1
|
@ -26,6 +26,7 @@ defmodule Farmbot.Sync do
|
|||
syncable Device, [:id, :planting_area_id, :name, :webcam_url]
|
||||
syncable Peripheral,
|
||||
[:id, :device_id, :pin, :mode, :label, :created_at, :updated_at]
|
||||
syncable Plant, [:id, :device_id]
|
||||
syncable Regimen, [:id, :color, :name, :device_id]
|
||||
syncable RegimenItem, [ :id, :time_offset, :regimen_id, :sequence_id]
|
||||
syncable Sequence, [:args, :body, :color, :device_id, :id, :kind, :name]
|
||||
|
@ -48,7 +49,7 @@ defmodule Farmbot.Sync do
|
|||
def get_user(id), do: Helpers.get_user(id)
|
||||
|
||||
def device_name, do: Helpers.get_device_name
|
||||
|
||||
|
||||
@doc """
|
||||
Downloads the sync object form the API.
|
||||
"""
|
||||
|
|
|
@ -79,9 +79,15 @@ defmodule Farmbot.Sync.SyncObject do
|
|||
end
|
||||
|
||||
def validate_list(module, list) do
|
||||
f = Enum.map(list, fn(thing) ->
|
||||
module.validate!(thing)
|
||||
Enum.partition(list, fn(thing) ->
|
||||
case module.validate(thing) do
|
||||
{:ok, thing} -> thing
|
||||
error -> false
|
||||
end
|
||||
end)
|
||||
{:ok, f}
|
||||
|> validate_partition(module)
|
||||
end
|
||||
|
||||
def validate_partition({win, []}, _module), do: {:ok, win}
|
||||
def validate_partition({_, failed}, module), do: {:error, module, failed}
|
||||
end
|
||||
|
|
|
@ -131,21 +131,5 @@ defmodule Syncable do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Builds a function for getting a module by id
|
||||
"""
|
||||
defmacro get_by_id(name) do
|
||||
module_name = Module.concat(Farmbot.Sync.Database, Macro.camelize(name))
|
||||
function_name = String.to_atom("get_" <> name)
|
||||
quote do
|
||||
def unquote(function_name)(find_id) do
|
||||
Amnesia.transaction do
|
||||
m = unquote(module_name)
|
||||
m.read(find_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
|
@ -55,9 +55,7 @@ defmodule Farmbot.Logger do
|
|||
end
|
||||
|
||||
def handle_event(:flush, state) do
|
||||
IO.puts "YOU FORGOT TO FINISH THIS!!!@"
|
||||
# flush(state)
|
||||
{:ok, state}
|
||||
{:ok, build_state}
|
||||
end
|
||||
|
||||
# If the post succeeded, we clear the messages
|
||||
|
|
6
mix.exs
6
mix.exs
|
@ -77,7 +77,8 @@ defmodule Farmbot.Mixfile do
|
|||
apps ++ [
|
||||
:plug,
|
||||
:cors_plug,
|
||||
:cowboy
|
||||
:cowboy,
|
||||
:faker
|
||||
]
|
||||
end
|
||||
|
||||
|
@ -114,7 +115,8 @@ defmodule Farmbot.Mixfile do
|
|||
[ {:plug, "~> 1.0"},
|
||||
{:cors_plug, "~> 1.1"},
|
||||
{:cowboy, "~> 1.0.0"},
|
||||
{:excoveralls, "~> 0.5"} ]
|
||||
{:excoveralls, "~> 0.5"},
|
||||
{:faker, "~> 0.7"} ]
|
||||
end
|
||||
|
||||
def deps(:dev) do
|
||||
|
|
1
mix.lock
1
mix.lock
|
@ -18,6 +18,7 @@
|
|||
"exquisite": {:hex, :exquisite, "0.1.7", "4106503e976f409246731b168cd76eb54262bd04f4facc5cba82c2f53982aaf0", [:mix], []},
|
||||
"exrm": {:hex, :exrm, "1.0.8", "5aa8990cdfe300282828b02cefdc339e235f7916388ce99f9a1f926a9271a45d", [:mix], [{:relx, "~> 3.5", [hex: :relx, optional: false]}]},
|
||||
"fake_nerves": {:git, "https://github.com/ConnorRigby/fake_nerves.git", "fad032a71e624ebeb06ecb5089c61583f8e111e0", []},
|
||||
"faker": {:hex, :faker, "0.7.0", "2c42deeac7be717173c78c77fb3edc749fb5d5e460e33d01fe592ae99acc2f0d", [:mix], []},
|
||||
"farmbot_auth": {:git, "https://github.com/Farmbot/farmbot_auth.git", "909ff852ff0d40122e3e02c4b8a35769598f151b", []},
|
||||
"farmbot_configurator": {:git, "https://github.com/Farmbot/farmbot_configurator.git", "e3b2ddb498123029ad14306e9d9e525200ab7c2f", []},
|
||||
"gen_mqtt": {:hex, :gen_mqtt, "0.3.1", "6ce6af7c2bcb125d5b4125c67c5ab1f29bcec2638236509bcc6abf510a6661ed", [:mix], [{:vmq_commons, "1.0.0", [hex: :vmq_commons, optional: false]}]},
|
||||
|
|
|
@ -3,5 +3,6 @@ defmodule Farmbot.SyncTest do
|
|||
Tests for handling the sync object
|
||||
"""
|
||||
use ExUnit.Case, async: true
|
||||
use Amnesia
|
||||
|
||||
end
|
||||
|
|
|
@ -0,0 +1,203 @@
|
|||
defmodule Farmbot.Sync.SyncObjectTest do
|
||||
@moduledoc false
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
test "validates and creates a SyncObject" do
|
||||
{:ok, blah } = Farmbot.Sync.SyncObject.validate(json_resp)
|
||||
assert blah.__struct__ == Farmbot.Sync.SyncObject
|
||||
end
|
||||
|
||||
test "validates and does not raise an exception" do
|
||||
blah = Farmbot.Sync.SyncObject.validate!({:ok, json_resp})
|
||||
assert blah.__struct__ == Farmbot.Sync.SyncObject
|
||||
end
|
||||
|
||||
test "does not create a sync object and raises a runtime exception" do
|
||||
assert_raise RuntimeError, fn() ->
|
||||
Farmbot.Sync.SyncObject.validate!("nope")
|
||||
end
|
||||
end
|
||||
|
||||
test "does not create a SyncObject" do
|
||||
{error, _reason} = Farmbot.Sync.SyncObject.validate(%{})
|
||||
assert error == :error
|
||||
end
|
||||
|
||||
test "gives meaningful messages when something is wrong" do
|
||||
json =
|
||||
%{"device" => %{"id" => 1},
|
||||
"peripherals" => [],
|
||||
"plants" => [],
|
||||
"regimen_items" => [],
|
||||
"regimens" => [],
|
||||
"sequences" => [],
|
||||
"tool_bays" => [],
|
||||
"tool_slots" => [],
|
||||
"tools" => [],
|
||||
"users" => []}
|
||||
{error, module, reason} = Farmbot.Sync.SyncObject.validate(json)
|
||||
assert error == :error
|
||||
assert module == Farmbot.Sync.Database.Device
|
||||
assert reason == {:missing_keys, ["planting_area_id", "name", "webcam_url"]}
|
||||
end
|
||||
|
||||
test "makes sure all keys are validated" do
|
||||
json1 = json_resp |> break("device")
|
||||
{:error, Farmbot.Sync.Database.Device, reason1} = Farmbot.Sync.SyncObject.validate(json1)
|
||||
assert reason1 == :bad_map
|
||||
|
||||
json2 = json_resp |> break("peripherals")
|
||||
{:error, Farmbot.Sync.Database.Peripheral, reason2} = Farmbot.Sync.SyncObject.validate(json2)
|
||||
assert reason2 == ["failure"]
|
||||
|
||||
json3 = json_resp |> break("plants")
|
||||
{:error, Farmbot.Sync.Database.Plant, reason3} = Farmbot.Sync.SyncObject.validate(json3)
|
||||
assert reason3 == ["failure"]
|
||||
|
||||
json4 = json_resp |> break("regimen_items")
|
||||
{:error, Farmbot.Sync.Database.RegimenItem, reason4} = Farmbot.Sync.SyncObject.validate(json4)
|
||||
assert reason4 == ["failure"]
|
||||
|
||||
json5 = json_resp |> break("regimens")
|
||||
{:error, Farmbot.Sync.Database.Regimen, reason5} = Farmbot.Sync.SyncObject.validate(json5)
|
||||
assert reason5 == ["failure"]
|
||||
|
||||
json6 = json_resp |> break("sequences")
|
||||
{:error, Farmbot.Sync.Database.Sequence, reason6} = Farmbot.Sync.SyncObject.validate(json6)
|
||||
assert reason6 == ["failure"]
|
||||
|
||||
json7 = json_resp |> break("tool_bays")
|
||||
{:error, Farmbot.Sync.Database.ToolBay, reason7} = Farmbot.Sync.SyncObject.validate(json7)
|
||||
assert reason7 == ["failure"]
|
||||
|
||||
json8 = json_resp |> break("tool_slots")
|
||||
{:error, Farmbot.Sync.Database.ToolSlot, reason8} = Farmbot.Sync.SyncObject.validate(json8)
|
||||
assert reason8 == ["failure"]
|
||||
|
||||
json9 = json_resp |> break("tools")
|
||||
{:error, Farmbot.Sync.Database.Tool, reason9} = Farmbot.Sync.SyncObject.validate(json9)
|
||||
assert reason9 == ["failure"]
|
||||
|
||||
|
||||
json10 = json_resp |> break("users")
|
||||
{:error, Farmbot.Sync.Database.User, reason10} = Farmbot.Sync.SyncObject.validate(json10)
|
||||
assert reason10 == ["failure"]
|
||||
|
||||
end
|
||||
|
||||
def break(map, key) do
|
||||
Map.put(map, key, ["failure"])
|
||||
end
|
||||
|
||||
def json_resp do
|
||||
%{"device" => json_device,
|
||||
"peripherals" => json_peripherals,
|
||||
"plants" => [],
|
||||
"regimen_items" => json_regimen_items,
|
||||
"regimens" => json_regimens,
|
||||
"sequences" => json_sequences,
|
||||
"tool_bays" => json_tool_bays,
|
||||
"tool_slots" => json_tool_slots,
|
||||
"tools" => json_tools,
|
||||
"users" => json_users}
|
||||
end
|
||||
|
||||
def json_device do
|
||||
%{
|
||||
"id" => 1,
|
||||
"planting_area_id" => 1,
|
||||
"name" => Faker.Lorem.word,
|
||||
"webcam_url" => Faker.Internet.url
|
||||
}
|
||||
end
|
||||
|
||||
def json_peripherals, do: [json_peripheral]
|
||||
def json_peripheral do
|
||||
%{
|
||||
"id" => 1,
|
||||
"device_id" => 1,
|
||||
"pin" => 13,
|
||||
"mode" => 0,
|
||||
"label" => Faker.Lorem.Shakespeare.hamlet,
|
||||
"created_at" => 12345,
|
||||
"updated_at" => 12345
|
||||
}
|
||||
end
|
||||
|
||||
def json_regimens, do: [json_regimen]
|
||||
def json_regimen do
|
||||
%{
|
||||
"id" => 1,
|
||||
"color" => "green",
|
||||
"name" => Faker.Company.name,
|
||||
"device_id" => 1
|
||||
}
|
||||
end
|
||||
|
||||
def json_regimen_items, do: [json_regimen_item]
|
||||
def json_regimen_item do
|
||||
%{
|
||||
"id" => 1,
|
||||
"time_offset" => 123,
|
||||
"regimen_id" => 1,
|
||||
"sequence_id" => 1
|
||||
}
|
||||
end
|
||||
|
||||
def json_sequences, do: [json_sequence]
|
||||
def json_sequence do
|
||||
%{
|
||||
"kind" => "sequence",
|
||||
"args" => %{},
|
||||
"body" => [],
|
||||
"color" => "red",
|
||||
"device_id" => 1,
|
||||
"id" => 1,
|
||||
"name" => Faker.Company.name
|
||||
}
|
||||
end
|
||||
|
||||
def json_tool_bays, do: [json_tool_bay]
|
||||
def json_tool_bay do
|
||||
%{
|
||||
"id" => 1,
|
||||
"device_id" => 1,
|
||||
"name" => Faker.Company.name
|
||||
}
|
||||
end
|
||||
|
||||
def json_tool_slots, do: [json_tool_slot]
|
||||
def json_tool_slot do
|
||||
%{
|
||||
"id" => 1,
|
||||
"tool_bay_id" => 1,
|
||||
"name" => Faker.Company.name,
|
||||
"x" => 1,
|
||||
"y" => 2,
|
||||
"z" => -1
|
||||
}
|
||||
end
|
||||
|
||||
def json_tools, do: [json_tool]
|
||||
def json_tool do
|
||||
%{
|
||||
"id" => 1,
|
||||
"slot_id" => 1,
|
||||
"name" => Faker.Company.name
|
||||
}
|
||||
end
|
||||
|
||||
def json_users, do: [json_user]
|
||||
def json_user do
|
||||
%{
|
||||
"id" => 1,
|
||||
"device_id" => 1,
|
||||
"name" => Faker.Company.name,
|
||||
"email" => Faker.Internet.email,
|
||||
"created_at" => 1234,
|
||||
"updated_at" => 1234
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
end
|
|
@ -0,0 +1,22 @@
|
|||
defmodule SyncableTest do
|
||||
@moduledoc false
|
||||
use ExUnit.Case, async: true
|
||||
use Amnesia
|
||||
|
||||
import Syncable
|
||||
defdatabase TestDB do
|
||||
use Amnesia
|
||||
syncable Person, [:legs, :arms]
|
||||
end
|
||||
|
||||
|
||||
setup_all do
|
||||
Amnesia.start
|
||||
TestDB.create! Keyword.put([], :memory, [node])
|
||||
end
|
||||
|
||||
test "gets keys for a syncable" do
|
||||
assert TestDB.Person.required_keys == [:legs, :arms]
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,45 @@
|
|||
defmodule TokenTest do
|
||||
@moduledoc false
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
test "creates a token" do
|
||||
url = Faker.Internet.url
|
||||
bot_name = Faker.Name.title
|
||||
date_thing = 123456
|
||||
email = Faker.Internet.email
|
||||
decoded_json =
|
||||
%{"encoded" => "asdfasdfasdfasdfasdfasdflalalalalas",
|
||||
"unencoded" =>
|
||||
%{"bot" => bot_name,
|
||||
"exp" => date_thing,
|
||||
"fw_update_server" => url,
|
||||
"os_update_server" => url,
|
||||
"iat" => date_thing,
|
||||
"iss" => url,
|
||||
"jti" => "123456",
|
||||
"mqtt" => url,
|
||||
"sub" => email}}
|
||||
|
||||
{:ok, f} = Token.create(decoded_json)
|
||||
# should be aloud to give {:ok, map} to the Token.create function.
|
||||
{:ok, ^f} = Token.create({:ok, decoded_json})
|
||||
une = f.unencoded
|
||||
assert(f.encoded == "asdfasdfasdfasdfasdfasdflalalalalas")
|
||||
assert(une.bot == bot_name)
|
||||
assert une.exp == date_thing
|
||||
assert une.iat == date_thing
|
||||
assert une.fw_update_server == url
|
||||
assert une.os_update_server == url
|
||||
assert une.sub == email
|
||||
end
|
||||
|
||||
test "raises an execption on a bad token" do
|
||||
bad_json =
|
||||
%{"encoded" => "abc",
|
||||
"unencoded" => "arbitrary code injection would be cool."}
|
||||
|
||||
assert_raise RuntimeError, fn ->
|
||||
Token.create!(bad_json)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,33 +1,4 @@
|
|||
defmodule Farmbot.LoggerTest do
|
||||
use ExUnit.Case, async: true
|
||||
setup_all do
|
||||
{:ok, pid} = Farmbot.# Log somethingstart_link []
|
||||
{:ok, %{pid: pid}}
|
||||
end
|
||||
|
||||
test "logs some stuff" do
|
||||
# Log something here("hey world, sup?", [], [])
|
||||
msgs = Farmbot.# Log somethingget_all
|
||||
assert Enum.count(msgs) > 0
|
||||
end
|
||||
|
||||
test "gets exactly 1 message", context do
|
||||
# Log something here("hello", [], [])
|
||||
# Log something here("wonderful", [], [])
|
||||
# Log something here("world", [], [])
|
||||
pid = context[:pid]
|
||||
msgs = GenServer.call(pid, {:get, 1})
|
||||
assert Enum.count(msgs) == 1
|
||||
end
|
||||
|
||||
test "clears the logs" do
|
||||
# Log something here("goodbye", [], [])
|
||||
# Log something here("cruel", [], [])
|
||||
# Log something here("world", [], [])
|
||||
msgs = Farmbot.# Log somethingget_all
|
||||
assert Enum.count(msgs) >= 3
|
||||
Farmbot.# Log somethingclear
|
||||
no_msgs = Farmbot.# Log somethingget_all
|
||||
assert no_msgs == []
|
||||
end
|
||||
end
|
||||
|
|
|
@ -16,16 +16,4 @@ defmodule Farmbot.RPC.HandlerTest do
|
|||
assert(Map.get(decoded, "error") == %{"name" => "error name", "message" => "error message"})
|
||||
assert(Map.get(decoded, "result") == nil)
|
||||
end
|
||||
|
||||
test("it creates a JsonRpc compliant farmbot log message") do
|
||||
msg = Farmbot.RPC.Handler.log_msg("super important log message",
|
||||
[:error_toast, :error_ticker],
|
||||
["SERIAL"])
|
||||
{:ok, decoded} = Poison.decode(msg)
|
||||
params = Map.get(decoded, "params")
|
||||
assert(Map.get(decoded, "id") == nil)
|
||||
assert(Map.get(decoded, "method") == "log_message")
|
||||
assert(is_list(params))
|
||||
assert(Map.get(List.first(params), "message") == "super important log message")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
defmodule Sequence.InstructionSet_0Test do
|
||||
alias Farmbot.Scheduler.Sequence.InstructionSet_0, as: SIS
|
||||
defmodule Farmbot.Scheduler.Sequence.InstructionSet_0Test do
|
||||
use ExUnit.Case, async: true
|
||||
defmodule Parent do
|
||||
use GenServer
|
||||
|
@ -14,7 +15,7 @@ defmodule Sequence.InstructionSet_0Test do
|
|||
|
||||
setup_all do
|
||||
{:ok, par} = Parent.start_link
|
||||
{:ok, sis} = Sequence.InstructionSet_0.start_link(Parent)
|
||||
{:ok, sis} = SIS.start_link(Parent)
|
||||
{:ok, %{parent: par, sis: sis}}
|
||||
end
|
||||
|
||||
|
@ -22,6 +23,6 @@ defmodule Sequence.InstructionSet_0Test do
|
|||
par = context[:parent]
|
||||
sis = context[:sis]
|
||||
assert(is_pid(par) == true)
|
||||
assert(is_pid(sis) == true)
|
||||
assert(is_pid(sis) == true)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue