Better `mode` validation errors + tests
parent
1635e146a6
commit
fa3bd1e22f
|
@ -1,3 +1,8 @@
|
||||||
class Sensor < ApplicationRecord
|
class Sensor < ApplicationRecord
|
||||||
belongs_to :device
|
belongs_to :device
|
||||||
|
validates :device, presence: true
|
||||||
|
validates :pin, presence: true
|
||||||
|
validates :pin, uniqueness: { scope: :device }
|
||||||
|
validates :pin, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||||
|
validates :label, presence: true
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,10 +1,20 @@
|
||||||
module Sensors
|
module Sensors
|
||||||
class Create < Mutations::Command
|
class Create < Mutations::Command
|
||||||
|
MODES = CeleryScriptSettingsBag::ALLOWED_PIN_MODES
|
||||||
|
BAD_MODE = CeleryScriptSettingsBag::BAD_ALLOWED_PIN_MODES
|
||||||
|
|
||||||
required do
|
required do
|
||||||
model :device, class: Device
|
model :device, class: Device
|
||||||
integer :pin
|
integer :pin
|
||||||
string :label
|
string :label
|
||||||
integer :mode, in: CeleryScriptSettingsBag::ALLOWED_PIN_MODES
|
integer :mode
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate
|
||||||
|
if !MODES.include?(mode)
|
||||||
|
msg = BAD_MODE % [mode.to_s, MODES]
|
||||||
|
add_error :mode, :not_allowed, msg
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute
|
def execute
|
||||||
|
|
|
@ -3,6 +3,7 @@ module Sensors
|
||||||
required { model :sensor, class: Peripheral }
|
required { model :sensor, class: Peripheral }
|
||||||
IN_USE = "Can't delete sensor because the following sequences are still " \
|
IN_USE = "Can't delete sensor because the following sequences are still " \
|
||||||
"using it: %s"
|
"using it: %s"
|
||||||
|
EDGE_NODE_NAME = "sensor_id"
|
||||||
|
|
||||||
def execute
|
def execute
|
||||||
sensor.destroy!
|
sensor.destroy!
|
||||||
|
@ -10,17 +11,16 @@ module Sensors
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def sequences_using_it
|
# def sequences_using_it
|
||||||
raise "UNCLEAR: Are we adding a `sensor_id` arg?"
|
# @sequences_using_it ||= EdgeNode
|
||||||
|
# .where(kind: EDGE_NODE_NAME, value: sensor.id)
|
||||||
@sequences_using_it ||= EdgeNode
|
# .pluck(:sequence_id)
|
||||||
.where(kind: "sensor_id", value: sensor.id)
|
# end
|
||||||
.pluck(:sequence_id)
|
|
||||||
end
|
|
||||||
|
|
||||||
def not_in_use?
|
def not_in_use?
|
||||||
names = Sequence.where(id: sequences_using_it).pluck(:name).join(", ")
|
puts "TOD: Add usage checks here. -RC"
|
||||||
add_error :sensor, :in_use, (IN_USE % [names]) if names.present?
|
# names = Sequence.where(id: sequences_using_it).pluck(:name).join(", ")
|
||||||
|
# add_error :sensor, :in_use, (IN_USE % [names]) if names.present?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
require "spec_helper"
|
||||||
|
|
||||||
|
describe Sensors::Create do
|
||||||
|
let(:device) { FactoryBot.create(:device) }
|
||||||
|
|
||||||
|
it "instantiates a sensor object" do
|
||||||
|
props = {device: device, pin: 1, label: "One", mode: 0}
|
||||||
|
obj = Sensors::Create.run!(props)
|
||||||
|
props.to_a.map { |(key, val)| expect(obj.send(key)).to eq(val)}
|
||||||
|
end
|
||||||
|
|
||||||
|
it "disallows bad PIN_MODEs" do
|
||||||
|
props = {device: device, pin: 1, label: "One", mode: -1}
|
||||||
|
result = Sensors::Create.run(props)
|
||||||
|
err = result.errors["mode"]
|
||||||
|
expect(err).to be
|
||||||
|
expect(err.message).to include("not a valid pin")
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue