Adds DB constraint to avoid duplicate balloted investments
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
class AddUniqIndexForBallotLines < ActiveRecord::Migration
|
||||
def change
|
||||
add_index :budget_ballot_lines, [:ballot_id, :investment_id], unique: true
|
||||
end
|
||||
end
|
||||
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20170428111355) do
|
||||
ActiveRecord::Schema.define(version: 20170503163330) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@@ -88,6 +88,7 @@ ActiveRecord::Schema.define(version: 20170428111355) do
|
||||
t.integer "heading_id"
|
||||
end
|
||||
|
||||
add_index "budget_ballot_lines", ["ballot_id", "investment_id"], name: "index_budget_ballot_lines_on_ballot_id_and_investment_id", unique: true, using: :btree
|
||||
add_index "budget_ballot_lines", ["ballot_id"], name: "index_budget_ballot_lines_on_ballot_id", using: :btree
|
||||
add_index "budget_ballot_lines", ["investment_id"], name: "index_budget_ballot_lines_on_investment_id", using: :btree
|
||||
|
||||
|
||||
@@ -2,6 +2,29 @@ require 'rails_helper'
|
||||
|
||||
describe Budget::Ballot do
|
||||
|
||||
describe "validations" do
|
||||
|
||||
it "should be valid" do
|
||||
budget = create(:budget)
|
||||
ballot = create(:budget_ballot, budget: budget)
|
||||
|
||||
expect(ballot).to be_valid
|
||||
end
|
||||
|
||||
it "should not be valid with the same investment twice" do
|
||||
budget = create(:budget)
|
||||
group = create(:budget_group, budget: budget)
|
||||
heading = create(:budget_heading, group: group)
|
||||
investment = create(:budget_investment, :selected, heading: heading)
|
||||
|
||||
ballot = create(:budget_ballot, budget: budget)
|
||||
ballot.investments << investment
|
||||
|
||||
expect { ballot.investments << investment }.to raise_error(ActiveRecord::RecordNotUnique)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "#amount_spent" do
|
||||
it "returns the total amount spent in investments" do
|
||||
budget = create(:budget)
|
||||
|
||||
Reference in New Issue
Block a user