Files
nairobi/spec/system/officing/results_spec.rb
Javi Martín 38b38d1fcc Rename Poll::Question::Answer to Poll::Question::Option
Having a class named `Poll::Question::Answer` and another class named
`Poll::Answer` was so confusing that no developer working on the project
has ever been capable of remembering which is which for more than a few
seconds.

Furthermore, we're planning to add open answers to polls, and we might
add a reference from the `poll_answers` table to the
`poll_question_answers` table to property differentiate between open
answers and closed answers. Having yet another thing named answer would
be more than what our brains can handle (we know it because we did this
once in a prototype).

So we're renaming `Poll::Question::Answer` to `Poll::Question::Option`.
Hopefully that'll make it easier to remember. The name is also (more or
less) consistent with the `Legislation::QuestionOption` class, which is
similar.

We aren't changing the table or columns names for now in order to avoid
possible issues when upgrading (old code running with the new database
tables/columns after running the migrations but before deployment has
finished, for instance). We might do it in the future.

I've tried not to change the internationalization keys either so
existing translations would still be valid. However, since we have to
change the keys in `activerecord.yml` so methods like
`human_attribute_name` keep working, I'm also changing them in places
where similar keys were used (like `poll_question_answer` or
`poll/question/answer`).

Note that it isn't clear whether we should use `option` or
`question_option` in some cases. In order to keep things simple, we're
using `option` where we were using `answer` and `question_option` where
we were using `question_answer`.

Also note we're adding tests for the admin menu component, since at
first I forgot to change the `answers` reference there and all tests
passed.
2024-06-13 19:13:01 +02:00

177 lines
6.1 KiB
Ruby

require "rails_helper"
describe "Officing Results", :with_frozen_time do
let(:poll) { create(:poll, ends_at: 1.day.ago) }
let(:booth) { create(:poll_booth, polls: [poll]) }
let(:poll_officer) { create(:poll_officer) }
let(:question_1) { create(:poll_question, poll: poll) }
let(:question_2) { create(:poll_question, poll: poll) }
before do
create(:poll_shift, :recount_scrutiny_task, officer: poll_officer, booth: booth, date: Date.current)
create(:poll_question_option, title: "Yes", question: question_1, given_order: 1)
create(:poll_question_option, title: "No", question: question_1, given_order: 2)
create(:poll_question_option, title: "Today", question: question_2, given_order: 1)
create(:poll_question_option, title: "Tomorrow", question: question_2, given_order: 2)
login_as(poll_officer.user)
set_officing_booth(booth)
end
scenario "Only polls where user is officer for results are accessible" do
not_allowed_poll_1 = create(:poll, :expired)
not_allowed_poll_2 = create(:poll, officers: [poll_officer], ends_at: 1.day.ago)
not_allowed_poll_3 = create(:poll, officers: [poll_officer])
visit root_path
click_link "Menu"
click_link "Polling officers"
expect(page).to have_content("Poll officing")
within("#side_menu") do
click_link "Total recounts and results"
end
expect(page).not_to have_content(not_allowed_poll_1.name)
expect(page).not_to have_content(not_allowed_poll_2.name)
expect(page).not_to have_content(not_allowed_poll_3.name)
expect(page).to have_content(poll.name)
visit new_officing_poll_result_path(not_allowed_poll_1)
expect(page).to have_content("You are not allowed to add results for this poll")
end
scenario "Add results" do
visit officing_root_path
within("#side_menu") do
click_link "Total recounts and results"
end
within("#poll_#{poll.id}") do
expect(page).to have_content(poll.name)
click_link "Add results"
end
expect(page).not_to have_content("Your results")
select booth.name, from: "officer_assignment_id"
fill_in "questions[#{question_1.id}][0]", with: "100"
fill_in "questions[#{question_1.id}][1]", with: "200"
fill_in "questions[#{question_2.id}][0]", with: "333"
fill_in "questions[#{question_2.id}][1]", with: "444"
fill_in "whites", with: "66"
fill_in "nulls", with: "77"
fill_in "total", with: "88"
click_button "Save"
expect(page).to have_content("Your results")
within "tbody tr" do
expect(page).to have_content(I18n.l(Date.current, format: :long))
expect(page).to have_content(booth.name)
end
end
scenario "Edit result" do
partial_result = create(
:poll_partial_result,
officer_assignment: poll_officer.officer_assignments.first,
booth_assignment: poll_officer.officer_assignments.first.booth_assignment,
date: Date.current,
question: question_1,
answer: question_1.question_options.first.title,
author: poll_officer.user,
amount: 7777
)
visit officing_poll_results_path(poll,
date: I18n.l(partial_result.date),
booth_assignment_id: partial_result.booth_assignment_id)
within("#question_#{question_1.id}_0_result") { expect(page).to have_content("7777") }
visit new_officing_poll_result_path(poll)
booth_name = partial_result.booth_assignment.booth.name
select booth_name, from: "officer_assignment_id"
fill_in "questions[#{question_1.id}][0]", with: "5555"
fill_in "questions[#{question_1.id}][1]", with: "200"
fill_in "whites", with: "6"
fill_in "nulls", with: "7"
fill_in "total", with: "8"
click_button "Save"
within("#results_#{partial_result.booth_assignment_id}_#{partial_result.date.strftime("%Y%m%d")}") do
expect(page).to have_content(I18n.l(partial_result.date, format: :long))
expect(page).to have_content(partial_result.booth_assignment.booth.name)
click_link "See results"
end
within("#question_#{question_1.id}_0_result") do
expect(page).to have_content("5555")
expect(page).not_to have_content("7777")
end
within("#white_results") { expect(page).to have_content("6") }
within("#null_results") { expect(page).to have_content("7") }
within("#total_results") { expect(page).to have_content("8") }
within("#question_#{question_1.id}_0_result") { expect(page).to have_content("5555") }
within("#question_#{question_1.id}_1_result") { expect(page).to have_content("200") }
end
scenario "Index lists all questions and answers" do
officer_assignment = poll_officer.officer_assignments.first
booth_assignment = officer_assignment.booth_assignment
booth = booth_assignment.booth
create(
:poll_partial_result,
officer_assignment: officer_assignment,
booth_assignment: booth_assignment,
date: poll.ends_at,
question: question_1,
amount: 33
)
create(
:poll_recount,
officer_assignment: officer_assignment,
booth_assignment: booth_assignment,
date: poll.ends_at,
white_amount: 21,
null_amount: 44,
total_amount: 66
)
visit officing_poll_results_path(poll,
date: I18n.l(poll.ends_at.to_date),
booth_assignment_id: officer_assignment.booth_assignment_id)
expect(page).to have_content(I18n.l(poll.ends_at.to_date, format: :long))
expect(page).to have_content(booth.name)
expect(page).to have_content(question_1.title)
question_1.question_options.each_with_index do |answer, i|
within("#question_#{question_1.id}_#{i}_result") { expect(page).to have_content(answer.title) }
end
expect(page).to have_content(question_2.title)
question_2.question_options.each_with_index do |answer, i|
within("#question_#{question_2.id}_#{i}_result") { expect(page).to have_content(answer.title) }
end
within("#white_results") { expect(page).to have_content("21") }
within("#null_results") { expect(page).to have_content("44") }
within("#total_results") { expect(page).to have_content("66") }
end
end