Add option_id to partial results and unique index

Similar to what we did in PR "Avoid duplicate records in poll answers" 5539,
specifically in commit 503369166, we want to stop relying on the plain text
"answer" and start using "option_id" to avoid issues with counts across
translations and to add consistency to the poll_partial_results table.

Note that we also moved the `possible_answers` method from Poll::Question to
Poll::Question::Option, since the list of valid answers really comes from the
options of a question and not from the question itself. Tests were updated
to validate answers against the translations of the assigned option.

Additionally, we renamed lambda parameters in validations to improve clarity.
This commit is contained in:
taitus
2025-09-10 12:22:08 +02:00
parent f2153f2b4d
commit a29eeaf2e2
13 changed files with 137 additions and 51 deletions

View File

@@ -89,17 +89,18 @@ describe Poll::Answer do
expect { answer.save }.not_to raise_error
end
it "is valid for answers included in the Poll::Question's question_options list" do
it "is valid for answers included in the list of titles for the option" do
question = create(:poll_question)
create(:poll_question_option, title: "One", question: question)
option = create(:poll_question_option, title_en: "One", title_es: "Uno", question: question)
create(:poll_question_option, title: "Two", question: question)
create(:poll_question_option, title: "Three", question: question)
create(:poll_question_option, title: "Three", question: create(:poll_question, poll: create(:poll)))
expect(build(:poll_answer, question: question, answer: "One")).to be_valid
expect(build(:poll_answer, question: question, answer: "Two")).to be_valid
expect(build(:poll_answer, question: question, answer: "Three")).to be_valid
expect(build(:poll_answer, question: question, answer: "Four")).not_to be_valid
expect(build(:poll_answer, option: option, answer: "One")).to be_valid
expect(build(:poll_answer, option: option, answer: "Uno")).to be_valid
expect(build(:poll_answer, option: option, answer: "Two")).not_to be_valid
expect(build(:poll_answer, option: option, answer: "Three")).not_to be_valid
expect(build(:poll_answer, option: option, answer: "Any")).not_to be_valid
end
end
end

View File

@@ -2,17 +2,18 @@ require "rails_helper"
describe Poll::PartialResult do
describe "validations" do
it "validates that the answers are included in the Poll::Question's list" do
it "validates that the answers are included in the list of titles for the option" do
question = create(:poll_question)
create(:poll_question_option, title: "One", question: question)
option = create(:poll_question_option, title_en: "One", title_es: "Uno", question: question)
create(:poll_question_option, title: "Two", question: question)
create(:poll_question_option, title: "Three", question: question)
create(:poll_question_option, title: "Three", question: create(:poll_question, poll: create(:poll)))
expect(build(:poll_partial_result, question: question, answer: "One")).to be_valid
expect(build(:poll_partial_result, question: question, answer: "Two")).to be_valid
expect(build(:poll_partial_result, question: question, answer: "Three")).to be_valid
expect(build(:poll_partial_result, question: question, answer: "Four")).not_to be_valid
expect(build(:poll_partial_result, option: option, answer: "One")).to be_valid
expect(build(:poll_partial_result, option: option, answer: "Uno")).to be_valid
expect(build(:poll_partial_result, option: option, answer: "Two")).not_to be_valid
expect(build(:poll_partial_result, option: option, answer: "Three")).not_to be_valid
expect(build(:poll_partial_result, option: option, answer: "Any")).not_to be_valid
end
it "dynamically validates the valid origins" do
@@ -21,6 +22,75 @@ describe Poll::PartialResult do
expect(build(:poll_partial_result, origin: "custom")).to be_valid
expect(build(:poll_partial_result, origin: "web")).not_to be_valid
end
describe "option_id uniqueness" do
let(:booth_assignment) { create(:poll_booth_assignment) }
it "is not valid when there are two identical partial results" do
question = create(:poll_question_multiple, :abc)
option = question.question_options.first
create(:poll_partial_result,
question: question,
booth_assignment: booth_assignment,
date: Date.current,
option: option,
answer: "Answer A")
partial_result = build(:poll_partial_result,
question: question,
booth_assignment: booth_assignment,
date: Date.current,
option: option,
answer: "Answer A")
expect(partial_result).not_to be_valid
expect { partial_result.save(validate: false) }.to raise_error ActiveRecord::RecordNotUnique
end
it "is not valid when there are two results with the same option and different answer" do
question = create(:poll_question_multiple, :abc)
option = question.question_options.first
create(:poll_partial_result,
question: question,
booth_assignment: booth_assignment,
date: Date.current,
option: option,
answer: "Answer A")
partial_result = build(:poll_partial_result,
question: question,
booth_assignment: booth_assignment,
date: Date.current,
option: option,
answer: "Answer B")
expect(partial_result).not_to be_valid
expect { partial_result.save(validate: false) }.to raise_error ActiveRecord::RecordNotUnique
end
it "is valid when there are two identical results and the option is nil" do
question = create(:poll_question_multiple, :abc)
create(:poll_partial_result,
question: question,
booth_assignment: booth_assignment,
date: Date.current,
option: nil,
answer: "Answer A")
partial_result = build(:poll_partial_result,
question: question,
booth_assignment: booth_assignment,
date: Date.current,
option: nil,
answer: "Answer A")
expect(partial_result).to be_valid
expect { partial_result.save }.not_to raise_error
end
end
end
describe "logging changes" do