Better `mode` validation errors + tests
parent
1635e146a6
commit
fa3bd1e22f
|
@ -1,3 +1,8 @@
|
|||
class Sensor < ApplicationRecord
|
||||
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
|
||||
|
|
|
@ -1,10 +1,20 @@
|
|||
module Sensors
|
||||
class Create < Mutations::Command
|
||||
MODES = CeleryScriptSettingsBag::ALLOWED_PIN_MODES
|
||||
BAD_MODE = CeleryScriptSettingsBag::BAD_ALLOWED_PIN_MODES
|
||||
|
||||
required do
|
||||
model :device, class: Device
|
||||
integer :pin
|
||||
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
|
||||
|
||||
def execute
|
||||
|
|
|
@ -3,6 +3,7 @@ module Sensors
|
|||
required { model :sensor, class: Peripheral }
|
||||
IN_USE = "Can't delete sensor because the following sequences are still " \
|
||||
"using it: %s"
|
||||
EDGE_NODE_NAME = "sensor_id"
|
||||
|
||||
def execute
|
||||
sensor.destroy!
|
||||
|
@ -10,17 +11,16 @@ module Sensors
|
|||
|
||||
private
|
||||
|
||||
def sequences_using_it
|
||||
raise "UNCLEAR: Are we adding a `sensor_id` arg?"
|
||||
|
||||
@sequences_using_it ||= EdgeNode
|
||||
.where(kind: "sensor_id", value: sensor.id)
|
||||
.pluck(:sequence_id)
|
||||
end
|
||||
# def sequences_using_it
|
||||
# @sequences_using_it ||= EdgeNode
|
||||
# .where(kind: EDGE_NODE_NAME, value: sensor.id)
|
||||
# .pluck(:sequence_id)
|
||||
# end
|
||||
|
||||
def not_in_use?
|
||||
names = Sequence.where(id: sequences_using_it).pluck(:name).join(", ")
|
||||
add_error :sensor, :in_use, (IN_USE % [names]) if names.present?
|
||||
puts "TOD: Add usage checks here. -RC"
|
||||
# names = Sequence.where(id: sequences_using_it).pluck(:name).join(", ")
|
||||
# add_error :sensor, :in_use, (IN_USE % [names]) if names.present?
|
||||
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