Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/models/purchase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Purchase < ApplicationRecord

validates :amount_spent_in_cents, numericality: { greater_than: 0 }
validate :total_equal_to_all_categories
validate :line_items_quantity_is_positive
before_destroy :check_no_intervening_snapshot

validates :amount_spent_on_diapers_cents, numericality: { greater_than_or_equal_to: 0 }
Expand Down Expand Up @@ -140,4 +141,8 @@ def check_no_intervening_snapshot
raise "We can't delete purchases entered before #{intervening.event_time.to_date}."
end
end

def line_items_quantity_is_positive
line_items_quantity_is_at_least(1)
end
end
14 changes: 14 additions & 0 deletions spec/models/purchase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@
expect(d).not_to be_valid
end

it "is not valid if any line item has zero quantity" do
item = create(:item)
p = build(:purchase)
p.line_items.build(item_id: item.id, quantity: 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to, but it is also fun to test for a quantity less than zero :)

expect(p).not_to be_valid
end

it "is not valid if any line item has negative quantity" do
item = create(:item)
p = build(:purchase)
p.line_items.build(item_id: item.id, quantity: -1)
expect(p).not_to be_valid
end

it "is valid if all categories are positive and add up to the total" do
d = build(:purchase, amount_spent_in_cents: 1150,
amount_spent_on_diapers_cents: 200,
Expand Down
5 changes: 4 additions & 1 deletion spec/queries/low_inventory_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
context "when minimum_quantity is 0 and recommended_quantity is nil and item quantity is 0" do
let(:item) { create :item, organization: organization }
let(:minimum_quantity) { 0 }
let(:inventory_item_quantity) { 0 }

before do
TestInventory.create_inventory(organization, { storage_location.id => { item.id => 0 } })
end

it { is_expected.to eq [] }
end
Expand Down