Skip to content

Commit

Permalink
4310 - Update item quantity validation error message (#4412)
Browse files Browse the repository at this point in the history
* Updates Item validation error message

* Updates unit tests

* Updated related system specs

* Updated validation message

- Updated validation message
- Updated related unit and system specs

* Fixes system spec

* Adds a custom validation to handle edge cases

- Updates system specs and adds required unit tests

* Fixes bug in unit tests
  • Loading branch information
vsrnth authored Jun 23, 2024
1 parent 43c751e commit 4d0bad6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
11 changes: 10 additions & 1 deletion app/models/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@ class LineItem < ApplicationRecord
belongs_to :item

validates :item_id, presence: true
validates :quantity, numericality: { only_integer: true, less_than: MAX_INT, greater_than: MIN_INT }
validates :quantity, numericality: { only_integer: true, message: "is not a number. Note: commas are not allowed" }
validate :quantity_must_be_a_number_within_range

scope :active, -> { joins(:item).where(items: { active: true }) }

delegate :name, to: :item

def quantity_must_be_a_number_within_range
if quantity && quantity > MAX_INT
errors.add(:quantity, "must be less than #{MAX_INT}")
elsif quantity && quantity < MIN_INT
errors.add(:quantity, "must be greater than #{MIN_INT}")
end
end
end
20 changes: 15 additions & 5 deletions spec/models/line_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@
let(:line_item) { build :line_item }

context "Validations >" do
subject { line_item }
let(:line_item_1) { build :line_item, quantity: 2**33 }
let(:line_item_2) { build :line_item, quantity: -2**32 }
let(:line_item_3) { build :line_item, quantity: "91,203" }

it 'validates numericality of quantity' do
expect(line_item_1).not_to be_valid
expect(line_item_2).not_to be_valid
expect(line_item_1.errors[:quantity]).to include("must be less than 2147483648")
expect(line_item_2.errors[:quantity]).to include("must be greater than -2147483648")
end

it { should validate_numericality_of(:quantity).is_greater_than(-2**31) }
it { should validate_numericality_of(:quantity).is_less_than(2**31) }
it { should validate_numericality_of(:quantity).only_integer }
it 'validates numericality of quantity' do
expect(line_item_3).not_to be_valid
expect(line_item_3.errors[:quantity]).to include("is not a number. Note: commas are not allowed")
end
end

describe "package_count" do
it "is equal to the quanity divided by the package_size" do
it "is equal to the quantity divided by the package_size" do
item = create(:item, package_size: 10)
line_item = create(:line_item, :purchase, quantity: 100, item_id: item.id)
expect(line_item.package_count).to eq("10")
Expand Down
2 changes: 1 addition & 1 deletion spec/system/donation_system_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@
select Item.alphabetized.first.name, from: "donation_line_items_attributes_0_item_id"
fill_in "donation_line_items_attributes_0_quantity", with: "1,000"
click_button "Save"
expect(page).to have_content("Quantity is not a number")
expect(page).to have_content("Quantity is not a number. Note: commas are not allowed")
end

it "Displays nested errors" do
Expand Down

0 comments on commit 4d0bad6

Please sign in to comment.