From 3a0e709e14a8311f528537a9fb19769ad8743373 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Tue, 3 Oct 2017 23:34:09 +0200 Subject: [PATCH 1/4] Correct composed index on poll Shift model adding date --- ...20171003212958_add_date_to_poll_shift_composed_index.rb | 7 +++++++ db/schema.rb | 5 ++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20171003212958_add_date_to_poll_shift_composed_index.rb diff --git a/db/migrate/20171003212958_add_date_to_poll_shift_composed_index.rb b/db/migrate/20171003212958_add_date_to_poll_shift_composed_index.rb new file mode 100644 index 000000000..b59a859c1 --- /dev/null +++ b/db/migrate/20171003212958_add_date_to_poll_shift_composed_index.rb @@ -0,0 +1,7 @@ +class AddDateToPollShiftComposedIndex < ActiveRecord::Migration + def change + remove_index "poll_shifts", name: "index_poll_shifts_on_booth_id_and_officer_id_and_task" + remove_index "poll_shifts", name: "index_poll_shifts_on_task" + add_index :poll_shifts, [:booth_id, :officer_id, :date, :task], unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 8b3e89a19..9a6fe919d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20171003095936) do +ActiveRecord::Schema.define(version: 20171003212958) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -719,10 +719,9 @@ ActiveRecord::Schema.define(version: 20171003095936) do t.integer "task", default: 0, null: false end - add_index "poll_shifts", ["booth_id", "officer_id", "task"], name: "index_poll_shifts_on_booth_id_and_officer_id_and_task", unique: true, using: :btree + add_index "poll_shifts", ["booth_id", "officer_id", "date", "task"], name: "index_poll_shifts_on_booth_id_and_officer_id_and_date_and_task", unique: true, using: :btree add_index "poll_shifts", ["booth_id"], name: "index_poll_shifts_on_booth_id", using: :btree add_index "poll_shifts", ["officer_id"], name: "index_poll_shifts_on_officer_id", using: :btree - add_index "poll_shifts", ["task"], name: "index_poll_shifts_on_task", using: :btree create_table "poll_total_results", force: :cascade do |t| t.integer "author_id" From b893c0bacef22fd4281089b15b3d0d810e94c508 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Tue, 3 Oct 2017 23:45:02 +0200 Subject: [PATCH 2/4] Add shift task presence validation expectation --- spec/models/poll/shift_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/models/poll/shift_spec.rb b/spec/models/poll/shift_spec.rb index b62f220da..686dc41db 100644 --- a/spec/models/poll/shift_spec.rb +++ b/spec/models/poll/shift_spec.rb @@ -24,6 +24,11 @@ describe :shift do expect(shift).to_not be_valid end + it "should not be valid without a task" do + shift.task = nil + expect(shift).to_not be_valid + end + end describe "officer_assignments" do From 19750995455f6cbe9d876e6048e2b4288da0b346 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Tue, 3 Oct 2017 23:53:20 +0200 Subject: [PATCH 3/4] Refactor & cleanup let statements on Shift model spec --- spec/models/poll/shift_spec.rb | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/spec/models/poll/shift_spec.rb b/spec/models/poll/shift_spec.rb index 686dc41db..be564421c 100644 --- a/spec/models/poll/shift_spec.rb +++ b/spec/models/poll/shift_spec.rb @@ -1,9 +1,14 @@ require 'rails_helper' describe :shift do - let(:shift) { build(:poll_shift) } + let(:poll) { create(:poll) } + let(:booth) { create(:poll_booth) } + let(:user) { create(:user, username: "Ana", email: "ana@example.com") } + let(:officer) { create(:poll_officer, user: user) } + let(:recount_shift) { build(:poll_shift, booth: booth, officer: officer, date: Date.current, task: :recount_scrutiny) } describe "validations" do + let(:shift) { build(:poll_shift) } it "should be valid" do expect(shift).to be_valid @@ -34,14 +39,10 @@ describe :shift do describe "officer_assignments" do it "should create and destroy corresponding officer_assignments" do - poll1 = create(:poll) poll2 = create(:poll) poll3 = create(:poll) - booth = create(:poll_booth) - officer = create(:poll_officer) - - booth_assignment1 = create(:poll_booth_assignment, poll: poll1, booth: booth) + booth_assignment1 = create(:poll_booth_assignment, poll: poll, booth: booth) booth_assignment2 = create(:poll_booth_assignment, poll: poll2, booth: booth) expect { create(:poll_shift, booth: booth, officer: officer, date: Date.current) }.to change {Poll::OfficerAssignment.all.count}.by(2) @@ -66,13 +67,8 @@ describe :shift do end it "should create final officer_assignments" do - poll = create(:poll) - booth = create(:poll_booth) - officer = create(:poll_officer) - booth_assignment = create(:poll_booth_assignment, poll: poll, booth: booth) - - shift = create(:poll_shift, booth: booth, officer: officer, date: Date.current, task: :recount_scrutiny) + recount_shift.save officer_assignments = Poll::OfficerAssignment.all expect(officer_assignments.count).to eq(1) @@ -88,10 +84,7 @@ describe :shift do end describe "#persist_data" do - - let(:user) { create(:user, username: "Ana", email: "ana@example.com") } - let(:officer) { create(:poll_officer, user: user) } - let(:shift) { create(:poll_shift, officer: officer) } + let(:shift) { create(:poll_shift, officer: officer, booth: booth) } it "should maintain officer data after destroying associated user" do shift.officer.user.destroy From b1eb6698be18674dfb968aac923d6fff24f457df Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 4 Oct 2017 00:13:10 +0200 Subject: [PATCH 4/4] Add validation expectations for shift date/task/booth/officer combinations --- spec/models/poll/shift_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/models/poll/shift_spec.rb b/spec/models/poll/shift_spec.rb index be564421c..cba4285ae 100644 --- a/spec/models/poll/shift_spec.rb +++ b/spec/models/poll/shift_spec.rb @@ -34,6 +34,24 @@ describe :shift do expect(shift).to_not be_valid end + it "should not be valid with same booth, officer, date and task" do + recount_shift.save + + expect(build(:poll_shift, booth: booth, officer: officer, date: Date.current, task: :recount_scrutiny)).to_not be_valid + end + + it "should be valid with same booth, officer and date but different task" do + recount_shift.save + + expect(build(:poll_shift, booth: booth, officer: officer, date: Date.current, task: :vote_collection)).to be_valid + end + + it "should be valid with same booth, officer and task but different date" do + recount_shift.save + + expect(build(:poll_shift, booth: booth, officer: officer, date: Date.tomorrow, task: :recount_scrutiny)).to be_valid + end + end describe "officer_assignments" do