Files
nairobi/spec/models/budget/ballot/line_spec.rb
Javi Martín 7ca55c44e0 Apply Rails/SaveBang rubocop rule
Having exceptions is better than having silent bugs.

There are a few methods I've kept the same way they were.

The `RelatedContentScore#score_with_opposite` method is a bit peculiar:
it creates scores for both itself and the opposite related content,
which means the opposite related content will try to create the same
scores as well.

We've already got a test to check `Budget::Ballot#add_investment` when
creating a line fails ("Edge case voting a non-elegible investment").

Finally, the method `User#send_oauth_confirmation_instructions` doesn't
update the record when the email address isn't already present, leading
to the test "Try to register with the email of an already existing user,
when an unconfirmed email was provided by oauth" fo fail if we raise an
exception for an invalid user. That's because updating a user's email
doesn't update the database automatically, but instead a confirmation
email is sent.

There are also a few false positives for classes which don't have bang
methods (like the GraphQL classes) or destroying attachments.

For these reasons, I'm adding the rule with a "Refactor" severity,
meaning it's a rule we can break if necessary.
2019-10-23 14:39:31 +02:00

85 lines
2.8 KiB
Ruby

require "rails_helper"
describe Budget::Ballot::Line do
let(:budget) { create(:budget) }
let(:group) { create(:budget_group, budget: budget) }
let(:heading) { create(:budget_heading, group: group, price: 10000000) }
let(:investment) { create(:budget_investment, :selected, price: 5000000, heading: heading) }
let(:ballot) { create(:budget_ballot, budget: budget) }
let(:ballot_line) { build(:budget_ballot_line, ballot: ballot, investment: investment) }
describe "Validations" do
it "is valid and automatically denormallyze budget, group and heading when validated" do
expect(ballot_line).to be_valid
expect(ballot_line.budget).to eq(budget)
expect(ballot_line.group).to eq(group)
expect(ballot_line.heading).to eq(heading)
end
describe "Money" do
it "is not valid if insufficient funds" do
investment.update!(price: heading.price + 1)
expect(ballot_line).not_to be_valid
end
it "is valid if sufficient funds" do
investment.update!(price: heading.price - 1)
expect(ballot_line).to be_valid
end
end
describe "Selectibility" do
it "is not valid if investment is unselected" do
investment.update!(selected: false)
expect(ballot_line).not_to be_valid
end
it "is valid if investment is selected" do
investment.update!(selected: true, price: 20000)
expect(ballot_line).to be_valid
end
end
end
describe "#store_user_heading" do
it "stores the heading where the user has voted" do
user = create(:user, :level_two)
investment = create(:budget_investment, :selected)
ballot = create(:budget_ballot, user: user, budget: investment.budget)
create(:budget_ballot_line, ballot: ballot, investment: investment)
expect(user.balloted_heading_id).to eq(investment.heading.id)
end
end
describe "scopes" do
describe "by_investment" do
it "returns ballot lines for an investment" do
investment1 = create(:budget_investment, :selected, heading: heading)
investment2 = create(:budget_investment, :selected, heading: heading)
ballot1 = create(:budget_ballot, budget: budget)
ballot2 = create(:budget_ballot, budget: budget)
ballot3 = create(:budget_ballot, budget: budget)
ballot_line1 = create(:budget_ballot_line, ballot: ballot1, investment: investment1)
ballot_line2 = create(:budget_ballot_line, ballot: ballot2, investment: investment1)
ballot_line3 = create(:budget_ballot_line, ballot: ballot3, investment: investment2)
ballot_lines_by_investment = Budget::Ballot::Line.by_investment(investment1.id)
expect(ballot_lines_by_investment).to match_array [ballot_line1, ballot_line2]
expect(ballot_lines_by_investment).not_to include ballot_line3
end
end
end
end