Skip to content

Commit b10d9d5

Browse files
committed
validates the item purchase fields for diapers,
adult incontinence, period, other as >= 0, added tests
1 parent b56f3b1 commit b10d9d5

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

app/models/purchase.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class Purchase < ApplicationRecord
5656
validates :amount_spent_in_cents, numericality: { greater_than: 0 }
5757
validate :total_equal_to_all_categories
5858

59+
validates :amount_spent_on_diapers_cents, numericality: { greater_than_or_equal_to: 0 }
60+
validates :amount_spent_on_adult_incontinence_cents, numericality: { greater_than_or_equal_to: 0 }
61+
validates :amount_spent_on_period_supplies_cents, numericality: { greater_than_or_equal_to: 0 }
62+
validates :amount_spent_on_other_cents, numericality: { greater_than_or_equal_to: 0 }
63+
5964
SummaryByDates = Data.define(
6065
:amount_spent,
6166
:recent_purchases,

spec/models/purchase_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,29 @@
5454
expect(d).to be_valid
5555
end
5656

57+
# re 5059-non-negative-purchase_value, adding in amount_spent_on_period_supplies_cents to test.
58+
# also adding in amount_spend_on_incontinence_cents because it was missing.
59+
60+
it "is not valid if any category negative" do
61+
d = build(:purchase, amount_spent_on_diapers_cents: -1)
62+
expect(d).not_to be_valid
63+
d = build(:purchase, amount_spent_on_adult_incontinence_cents: -2)
64+
expect(d).not_to be_valid
65+
d = build(:purchase, amount_spent_on_period_supplies_cents: -3)
66+
expect(d).not_to be_valid
67+
d = build(:purchase, amount_spent_on_other_cents: -4)
68+
expect(d).not_to be_valid
69+
end
70+
71+
it "is valid if all categories are positive" do
72+
d = build(:purchase, amount_spent_in_cents: 1150,
73+
amount_spent_on_diapers_cents: 200,
74+
amount_spent_on_adult_incontinence_cents: 300,
75+
amount_spent_on_period_supplies_cents: 400,
76+
amount_spent_on_other_cents: 250)
77+
expect(d).to be_valid
78+
end
79+
5780
# 2813 update annual reports -- this covers off making sure it's checking the case of period supplies only (which it won't be before we make it so)
5881

5982
it "is not valid if period supplies is non-zero but no other category is " do

0 commit comments

Comments
 (0)