Skip to content

Commit a1eb791

Browse files
authored
Merge pull request #5287 from DasTapan/td-5059-non-negative-purchase_value
Td 5059 non negative purchase value
2 parents 2fa170c + c6b5aaf commit a1eb791

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

app/models/purchase.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class Purchase < ApplicationRecord
6060
validates :amount_spent_in_cents, numericality: { greater_than: 0 }
6161
validate :total_equal_to_all_categories
6262

63+
validates :amount_spent_on_diapers_cents, numericality: { greater_than_or_equal_to: 0 }
64+
validates :amount_spent_on_adult_incontinence_cents, numericality: { greater_than_or_equal_to: 0 }
65+
validates :amount_spent_on_period_supplies_cents, numericality: { greater_than_or_equal_to: 0 }
66+
validates :amount_spent_on_other_cents, numericality: { greater_than_or_equal_to: 0 }
67+
6368
SummaryByDates = Data.define(
6469
:amount_spent,
6570
:recent_purchases,

spec/models/purchase_spec.rb

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

57+
it "is not valid if any category negative" do
58+
d = build(:purchase, amount_spent_on_diapers_cents: -1)
59+
expect(d).not_to be_valid
60+
d = build(:purchase, amount_spent_on_adult_incontinence_cents: -2)
61+
expect(d).not_to be_valid
62+
d = build(:purchase, amount_spent_on_period_supplies_cents: -3)
63+
expect(d).not_to be_valid
64+
d = build(:purchase, amount_spent_on_other_cents: -4)
65+
expect(d).not_to be_valid
66+
end
67+
68+
it "is valid if all categories are positive and add up to the total" do
69+
d = build(:purchase, amount_spent_in_cents: 1150,
70+
amount_spent_on_diapers_cents: 200,
71+
amount_spent_on_adult_incontinence_cents: 300,
72+
amount_spent_on_period_supplies_cents: 400,
73+
amount_spent_on_other_cents: 250)
74+
expect(d).to be_valid
75+
end
76+
5777
# 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)
5878

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

0 commit comments

Comments
 (0)