Skip to content

Commit

Permalink
add curve_id validation to points
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielburnworth committed Aug 28, 2024
1 parent 72c6805 commit 67d5778
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/mutations/points/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Points
class Create < Mutations::Command
include Points::Helpers
# WHY 1000?:
# * This limit is placed for _technical_
# reasons, not business reasons. If it were
Expand Down Expand Up @@ -65,6 +66,9 @@ def validate
return unless safe_pointer_kind? # Security critical always goes first.
validate_resource_count
validate_tool if klass_ == ToolSlot
validate_water_curve_id
validate_spread_curve_id
validate_height_curve_id
name ||= default_name
end

Expand Down
23 changes: 23 additions & 0 deletions app/mutations/points/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Points
module Helpers
BAD_CURVE_ID = "Curve ID is not valid"

def validate_water_curve_id
if water_curve_id && !device.curves.exists?(water_curve_id)
add_error :water_curve_id, :water_curve_id, BAD_CURVE_ID
end
end

def validate_spread_curve_id
if spread_curve_id && !device.curves.exists?(spread_curve_id)
add_error :spread_curve_id, :spread_curve_id, BAD_CURVE_ID
end
end

def validate_height_curve_id
if height_curve_id && !device.curves.exists?(height_curve_id)
add_error :height_curve_id, :height_curve_id, BAD_CURVE_ID
end
end
end
end
4 changes: 4 additions & 0 deletions app/mutations/points/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Points
class Update < Mutations::Command
include Points::Helpers
required do
model :device, class: Device
model :point, class: Point
Expand All @@ -28,6 +29,9 @@ class Update < Mutations::Command

def validate
prevent_removal_of_in_use_tools
validate_water_curve_id
validate_spread_curve_id
validate_height_curve_id
end

def execute
Expand Down
33 changes: 33 additions & 0 deletions spec/mutations/points/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,37 @@ def with_fake_limits(soft_limit = 2, hard_limit = 3)
expect(errors.fetch("point_limit")).to be
end
end

it "validates curve ids: ok" do
water_curve = FactoryBot.create(:curve, type: "water", device: device)
spread_curve = FactoryBot.create(:curve, type: "spread", device: device)
height_curve = FactoryBot.create(:curve, type: "height", device: device)
params = { x: 0,
y: 0,
z: 0,
water_curve_id: water_curve.id,
spread_curve_id: spread_curve.id,
height_curve_id: height_curve.id,
device: device,
pointer_type: "GenericPointer" }
expect(Points::Create.run(params).errors).to be nil
end

it "validates curve ids: ko" do
curve = FactoryBot.create(:curve, type: "water", device: device)
params = { x: 0,
y: 0,
z: 0,
water_curve_id: -1,
height_curve_id: -1,
spread_curve_id: -1,
device: device,
pointer_type: "GenericPointer" }

errors = Points::Create.run(params).errors
expect(errors).to be
expect(errors.fetch("water_curve_id")).to be
expect(errors.fetch("spread_curve_id")).to be
expect(errors.fetch("height_curve_id")).to be
end
end

0 comments on commit 67d5778

Please sign in to comment.