Merge pull request #1550 from consul/budgets-unfeasible

Investments reclassified as unfeasible
This commit is contained in:
Juanjo Bazán
2017-05-18 14:37:32 +02:00
committed by GitHub
11 changed files with 236 additions and 47 deletions

View File

@@ -2,13 +2,14 @@ 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
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) }
it "should be valid and automatically denormallyze budget, group and heading when validated" do
expect(ballot_line).to be_valid
@@ -42,4 +43,30 @@ describe "Budget::Ballot::Line" do
end
end
describe "scopes" do
describe "by_investment" do
it "should return 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 include ballot_line1
expect(ballot_lines_by_investment).to include ballot_line2
expect(ballot_lines_by_investment).to_not include ballot_line3
end
end
end
end

View File

@@ -728,20 +728,20 @@ describe Budget::Investment do
let(:heading1) { create(:budget_heading, group: group) }
let(:heading2) { create(:budget_heading, group: group) }
describe "reclassified?" do
describe "heading_changed?" do
it "returns true if budget is in balloting phase and heading has changed" do
investment = create(:budget_investment, heading: heading1)
investment.heading = heading2
expect(investment.reclassified?).to eq(true)
expect(investment.heading_changed?).to eq(true)
end
it "returns false if heading has not changed" do
investment = create(:budget_investment)
investment.heading = investment.heading
expect(investment.reclassified?).to eq(false)
expect(investment.heading_changed?).to eq(false)
end
it "returns false if budget is not balloting phase" do
@@ -751,13 +751,13 @@ describe Budget::Investment do
investment.heading = heading2
expect(investment.reclassified?).to eq(false)
expect(investment.heading_changed?).to eq(false)
end
end
end
describe "log_reclassification" do
describe "log_heading_change" do
it "stores the previous heading before being reclassified" do
investment = create(:budget_investment, heading: heading1)
@@ -775,6 +775,30 @@ describe Budget::Investment do
end
describe "store_reclassified_votes" do
it "stores the votes for a reclassified investment" do
investment = create(:budget_investment, :selected, heading: heading1)
3.times do
ballot = create(:budget_ballot, budget: budget)
ballot.investments << investment
end
expect(investment.ballot_lines_count).to eq(3)
investment.heading = heading2
investment.store_reclassified_votes("heading_changed")
reclassified_vote = Budget::ReclassifiedVote.first
expect(Budget::ReclassifiedVote.count).to eq(3)
expect(reclassified_vote.investment_id).to eq(investment.id)
expect(reclassified_vote.user_id).to eq(Budget::Ballot.first.user.id)
expect(reclassified_vote.reason).to eq("heading_changed")
end
end
describe "remove_reclassified_votes" do
it "removes votes from invesment" do
@@ -798,7 +822,7 @@ describe Budget::Investment do
describe "check_for_reclassification" do
it "removes votes if an investment has been reclassified" do
it "stores reclassfied votes and removes actual votes if an investment has been reclassified" do
investment = create(:budget_investment, :selected, heading: heading1)
3.times do
@@ -813,9 +837,10 @@ describe Budget::Investment do
investment.reload
expect(investment.ballot_lines_count).to eq(0)
expect(Budget::ReclassifiedVote.count).to eq(3)
end
it "does not remove votes if the investment has not been reclassifed" do
it "does not store reclassified votes nor remove actual votes if the investment has not been reclassifed" do
investment = create(:budget_investment, :selected, heading: heading1)
3.times do
@@ -829,6 +854,7 @@ describe Budget::Investment do
investment.reload
expect(investment.ballot_lines_count).to eq(3)
expect(Budget::ReclassifiedVote.count).to eq(0)
end
end

View File

@@ -0,0 +1,40 @@
require 'rails_helper'
describe Budget::ReclassifiedVote do
describe "Validations" do
let(:reclassified_vote) { build(:budget_reclassified_vote) }
it "should be valid" do
expect(reclassified_vote).to be_valid
end
it "should not be valid without a user" do
reclassified_vote.user_id = nil
expect(reclassified_vote).to_not be_valid
end
it "should not be valid without an investment" do
reclassified_vote.investment_id = nil
expect(reclassified_vote).to_not be_valid
end
it "should not be valid without a valid reason" do
reclassified_vote.reason = nil
expect(reclassified_vote).to_not be_valid
reclassified_vote.reason = ""
expect(reclassified_vote).to_not be_valid
reclassified_vote.reason = "random"
expect(reclassified_vote).to_not be_valid
reclassified_vote.reason = "heading_changed"
expect(reclassified_vote).to be_valid
reclassified_vote.reason = "unfeasible"
expect(reclassified_vote).to be_valid
end
end
end