Test for hard / soft point limits

pull/1109/head
Rick Carlino 2019-02-08 17:15:05 -06:00
parent 071c209524
commit 8aa0ddce15
3 changed files with 67 additions and 13 deletions

View File

@ -9,11 +9,20 @@ module Points
# many plants
# * An XL bot at 100% capacity and 1000 evenly space plants =
# 5 inch point grid. Smaller bed = higher resolution.
POINT_HARD_LIMIT = 1 # 1000 # Not allowed to exceed this.
POINT_SOFT_LIMIT = 2 # (POINT_HARD_LIMIT * 0.8).to_i
POINT_HARD_LIMIT = 1000 # Not allowed to exceed this.
POINT_SOFT_LIMIT = (POINT_HARD_LIMIT * 0.8).to_i
BAD_TOOL_ID = "Can't find tool with id %s"
DEFAULT_NAME = "Untitled %s"
KINDS = Point::POINTER_KINDS
GETTING_CLOSE = "Your account is "\
"approaching the allowed point limit of "\
"#{POINT_HARD_LIMIT}. Consider deleting old"\
" points to avoid adverse performance."
TOO_MANY = "Your device has hit the "\
"max limit for point usage (currently "\
"#{POINT_HARD_LIMIT}). Please delete unused"\
" map points and plants to create more "
BAD_POINTER_TYPE = \
"Please provide a JSON object with a `poin"\
"ter_type` that matches one of the followi"\
@ -57,11 +66,13 @@ module Points
private
def validate_resource_count
case Point.where(device_id: device.id).count
when (POINT_SOFT_LIMIT..POINT_HARD_LIMIT)
binding.pry
when (POINT_HARD_LIMIT..nil)
binding.pry
actual = \
Point.where(device_id: device.id).count
case actual
when POINT_SOFT_LIMIT
device.tell(GETTING_CLOSE % { actual: actual }, ["fatal_email"])
when POINT_HARD_LIMIT...nil
add_error(:point_limit, :point_limit, TOO_MANY)
end
end

View File

@ -147,11 +147,5 @@ describe Api::PointsController do
expect(ToolSlot.count).to eq old_tool_count
expect(json[:tool_id]).to include("Can't find tool")
end
# it "warns users when they hit the soft resource limit" do
# const_reassign(User, :ENFORCE_TOS, "http://farm.bot/tos")
# post :create, body: payload.to_json, params: {format: :json}
# expect(response.status).to eq(200)
# end
end
end

View File

@ -0,0 +1,49 @@
require 'spec_helper'
describe Points::Create do
let(:device) { FactoryBot.create(:device) }
let(:params) do
{ x: 0, y: 0, z: 0, device: device, pointer_type: "GenericPointer" }
end
def with_fake_limits(soft_limit = 2, hard_limit = 3)
old_soft_limit = Points::Create::POINT_SOFT_LIMIT
old_hard_limit = Points::Create::POINT_HARD_LIMIT
const_reassign(Points::Create, :POINT_SOFT_LIMIT, soft_limit)
const_reassign(Points::Create, :POINT_HARD_LIMIT, hard_limit)
yield
const_reassign(Points::Create, :POINT_SOFT_LIMIT, old_soft_limit)
const_reassign(Points::Create, :POINT_HARD_LIMIT, old_hard_limit)
end
it "warns users when they hit the soft resource limit" do
with_fake_limits do
allow(device)
.to receive(:tell).with(Points::Create::GETTING_CLOSE, ["fatal_email"])
Points::Create::POINT_SOFT_LIMIT.times do
expect(Points::Create.run(params).errors).to be nil
end
end
end
it "stops users when they hit the hard limit" do
with_fake_limits do
params = { x: 0,
y: 0,
z: 0,
device: device,
pointer_type: "GenericPointer", }
Points::Create::POINT_HARD_LIMIT.times do
expect(Points::Create.run(params).errors).to be nil
end
errors = Points::Create.run(params).errors
expect(errors).to be
expect(errors.fetch("point_limit")).to be
end
end
end