Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validates the item purchase fields for diapers, #5076

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -56,6 +56,11 @@ class Purchase < ApplicationRecord
validates :amount_spent_in_cents, numericality: { greater_than: 0 }
validate :total_equal_to_all_categories

validates :amount_spent_on_diapers_cents, numericality: { greater_than_or_equal_to: 0 }
validates :amount_spent_on_adult_incontinence_cents, numericality: { greater_than_or_equal_to: 0 }
validates :amount_spent_on_period_supplies_cents, numericality: { greater_than_or_equal_to: 0 }
validates :amount_spent_on_other_cents, numericality: { greater_than_or_equal_to: 0 }

SummaryByDates = Data.define(
:amount_spent,
:recent_purchases,
Expand Down
23 changes: 23 additions & 0 deletions spec/models/purchase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@
expect(d).to be_valid
end

# re 5059-non-negative-purchase_value, adding in amount_spent_on_period_supplies_cents to test.
# also adding in amount_spend_on_incontinence_cents because it was missing.

it "is not valid if any category negative" do
d = build(:purchase, amount_spent_on_diapers_cents: -1)
expect(d).not_to be_valid
d = build(:purchase, amount_spent_on_adult_incontinence_cents: -2)
expect(d).not_to be_valid
d = build(:purchase, amount_spent_on_period_supplies_cents: -3)
expect(d).not_to be_valid
d = build(:purchase, amount_spent_on_other_cents: -4)
expect(d).not_to be_valid
end

it "is valid if all categories are positive" do
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would say "is valid if all categories are positive and add up to the total"

d = build(:purchase, amount_spent_in_cents: 1150,
amount_spent_on_diapers_cents: 200,
amount_spent_on_adult_incontinence_cents: 300,
amount_spent_on_period_supplies_cents: 400,
amount_spent_on_other_cents: 250)
expect(d).to be_valid
end

# 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)

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