Merge pull request #1134 from consul/more-budget-model-specs

More budget model specs
This commit is contained in:
Juanjo Bazán
2016-05-27 17:43:51 +02:00
4 changed files with 76 additions and 13 deletions

View File

@@ -11,12 +11,12 @@ class Budget
investments.sum(:price).to_i
end
def amount_spent(heading)
investments.by_heading(heading).sum(:price).to_i
def amount_spent(heading_id)
investments.by_heading(heading_id).sum(:price).to_i
end
def amount_available(heading)
budget.heading_price(heading) - amount_spent(heading)
budget.heading_price(heading) - amount_spent(heading.try(:id))
end
end
end

View File

@@ -42,6 +42,7 @@ class Budget
scope :undecided, -> { where(feasibility: "undecided") }
scope :with_supports, -> { where('cached_votes_up > 0') }
scope :by_heading, -> (heading_id) { where(heading_id: heading_id) }
scope :by_budget, -> (budget_id) { where(budget_id: budget_id) }
scope :by_admin, -> (admin_id) { where(administrator_id: admin_id) }
scope :by_tag, -> (tag_name) { tagged_with(tag_name) }

View File

@@ -16,9 +16,7 @@ describe Budget::Ballot do
expect(ballot.total_amount_spent).to eq 30000
end
end
describe "#amount_spent by heading" do
it "returns the amount spent on all investments assigned to a specific heading" do
heading = create(:budget_heading)
inv1 = create(:budget_investment, :feasible, price: 10000, heading: heading)
@@ -40,4 +38,31 @@ describe Budget::Ballot do
end
end
end
describe "#amount_available" do
it "returns how much is left after taking some investments" do
budget = create(:budget, price: 200000)
heading = create(:budget_heading, budget: budget)
inv1 = create(:budget_investment, :feasible, price: 10000, heading: heading)
inv2 = create(:budget_investment, :feasible, price: 20000, heading: create(:budget_heading))
inv3 = create(:budget_investment, :feasible, price: 25000)
inv4 = create(:budget_investment, :feasible, price: 40000, heading: heading)
inv1 = create(:budget_investment, :feasible, price: 10000)
inv2 = create(:budget_investment, :feasible, price: 20000)
ballot = create(:budget_ballot, budget: budget)
ballot.investments << inv1
ballot.investments << inv2
expect(ballot.amount_available(heading)).to eq 1000000
expect(ballot.amount_available(nil)).to eq 170000
ballot.investments << inv3
ballot.investments << inv4
expect(ballot.amount_available(heading)).to eq 960000
expect(ballot.amount_available(nil)).to eq 145000
end
end
end

View File

@@ -1,15 +1,52 @@
require 'rails_helper'
describe Budget do
it "validates the phase" do
budget = create(:budget)
Budget::VALID_PHASES.each do |phase|
budget.phase = phase
expect(budget).to be_valid
describe "phase" do
let(:budget) { create(:budget) }
it "is validated" do
Budget::VALID_PHASES.each do |phase|
budget.phase = phase
expect(budget).to be_valid
end
budget.phase = 'inexisting'
expect(budget).to_not be_valid
end
budget.phase = 'inexisting'
expect(budget).to_not be_valid
it "produces auxiliary methods" do
budget.phase = "on_hold"
expect(budget).to be_on_hold
budget.phase = "accepting"
expect(budget).to be_accepting
budget.phase = "selecting"
expect(budget).to be_selecting
budget.phase = "balloting"
expect(budget).to be_balloting
budget.phase = "finished"
expect(budget).to be_finished
end
end
describe "heading_price" do
let(:budget) { create(:budget, price: 1000) }
it "returns the budget price if no heading is provided" do
expect(budget.heading_price(nil)).to eq(1000)
end
it "returns the heading price if the heading provided is part of the budget" do
heading = create(:budget_heading, price: 100, budget: budget)
expect(budget.heading_price(heading)).to eq(100)
end
it "returns -1 if the heading provided is not part of the budget" do
expect(budget.heading_price(create(:budget_heading))).to eq(-1)
end
end
end