Merge pull request #2073 from consul/feature/2072#remove_question_valid_answers

Remove valid answers usage
This commit is contained in:
BertoCQ
2017-10-18 11:52:14 +02:00
committed by GitHub
19 changed files with 83 additions and 73 deletions

View File

@@ -503,7 +503,13 @@ FactoryGirl.define do
poll
association :author, factory: :user
sequence(:title) { |n| "Question title #{n}" }
valid_answers { Faker::Lorem.words(3).join(', ') }
trait :with_answers do
after(:create) do |question, _evaluator|
create(:poll_question_answer, question: question, title: "Yes")
create(:poll_question_answer, question: question, title: "No")
end
end
end
factory :poll_question_answer, class: 'Poll::Question::Answer' do
@@ -574,16 +580,16 @@ FactoryGirl.define do
end
factory :poll_answer, class: 'Poll::Answer' do
association :question, factory: :poll_question
association :question, factory: [:poll_question, :with_answers]
association :author, factory: [:user, :level_two]
answer { question.valid_answers.sample }
answer { question.question_answers.sample.title }
end
factory :poll_partial_result, class: 'Poll::PartialResult' do
association :question, factory: :poll_question
association :question, factory: [:poll_question, :with_answers]
association :author, factory: :user
origin { 'web' }
answer { question.valid_answers.sample }
answer { question.question_answers.sample.title }
end
factory :poll_recount, class: 'Poll::Recount' do

View File

@@ -261,8 +261,13 @@ feature 'Admin polls' do
booth_assignment_2 = create(:poll_booth_assignment, poll: poll)
booth_assignment_3 = create(:poll_booth_assignment, poll: poll)
question_1 = create(:poll_question, poll: poll, valid_answers: "Yes,No")
question_2 = create(:poll_question, poll: poll, valid_answers: "Today,Tomorrow")
question_1 = create(:poll_question, poll: poll)
create(:poll_question_answer, title: 'Yes', question: question_1)
create(:poll_question_answer, title: 'No', question: question_1)
question_2 = create(:poll_question, poll: poll)
create(:poll_question_answer, title: 'Today', question: question_2)
create(:poll_question_answer, title: 'Tomorrow', question: question_2)
[booth_assignment_1, booth_assignment_2, booth_assignment_3].each do |ba|
create(:poll_partial_result,
@@ -279,30 +284,32 @@ feature 'Admin polls' do
create(:poll_recount,
booth_assignment: booth_assignment_1,
white_amount: 21,
null_amount: 44)
null_amount: 44,
total_amount: 66)
visit admin_poll_path(poll)
click_link "Results"
expect(page).to have_content(question_1.title)
question_1.valid_answers.each_with_index do |answer, i|
question_1.question_answers.each_with_index do |answer, i|
within("#question_#{question_1.id}_#{i}_result") do
expect(page).to have_content(answer)
expect(page).to have_content(answer.title)
expect(page).to have_content([33, 0][i])
end
end
expect(page).to have_content(question_2.title)
question_2.valid_answers.each_with_index do |answer, i|
question_2.question_answers.each_with_index do |answer, i|
within("#question_#{question_2.id}_#{i}_result") do
expect(page).to have_content(answer)
expect(page).to have_content(answer.title)
expect(page).to have_content([0, 15][i])
end
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
end

View File

@@ -7,8 +7,13 @@ feature 'Officing Results' do
@officer_assignment = create(:poll_officer_assignment, :final, officer: @poll_officer)
@poll = @officer_assignment.booth_assignment.poll
@poll.update(ends_at: 1.day.ago)
@question_1 = create(:poll_question, poll: @poll, valid_answers: "Yes,No")
@question_2 = create(:poll_question, poll: @poll, valid_answers: "Today,Tomorrow")
@question_1 = create(:poll_question, poll: @poll)
create(:poll_question_answer, title: 'Yes', question: @question_1)
create(:poll_question_answer, title: 'No', question: @question_1)
@question_2 = create(:poll_question, poll: @poll)
create(:poll_question_answer, title: 'Today', question: @question_2)
create(:poll_question_answer, title: 'Tomorrow', question: @question_2)
login_as(@poll_officer.user)
end
@@ -81,7 +86,7 @@ feature 'Officing Results' do
booth_assignment: @officer_assignment.booth_assignment,
date: Date.current,
question: @question_1,
answer: @question_1.valid_answers[0],
answer: @question_1.question_answers.first.title,
author: @poll_officer.user,
amount: 7777)
@@ -139,13 +144,13 @@ feature 'Officing Results' do
expect(page).to have_content(@officer_assignment.booth_assignment.booth.name)
expect(page).to have_content(@question_1.title)
@question_1.valid_answers.each_with_index do |answer, i|
within("#question_#{@question_1.id}_#{i}_result") { expect(page).to have_content(answer) }
@question_1.question_answers.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.valid_answers.each_with_index do |answer, i|
within("#question_#{@question_2.id}_#{i}_result") { expect(page).to have_content(answer) }
@question_2.question_answers.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') }

View File

@@ -25,9 +25,12 @@ describe Poll::Answer do
expect(answer).to_not be_valid
end
it "should be valid for answers included in the Poll::Question's list" do
skip "review when removing valid_answers"
question = create(:poll_question, valid_answers: 'One, Two, Three')
it "should be valid for answers included in the Poll::Question's question_answers list" do
question = create(:poll_question)
create(:poll_question_answer, title: 'One', question: question)
create(:poll_question_answer, title: 'Two', question: question)
create(:poll_question_answer, title: 'Three', question: question)
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
@@ -40,7 +43,7 @@ describe Poll::Answer do
let(:author) { create(:user, :level_two) }
let(:poll) { create(:poll) }
let(:question) { create(:poll_question, poll: poll, valid_answers: "Yes, No") }
let(:question) { create(:poll_question, :with_answers, poll: poll) }
it "creates a poll_voter with user and poll data" do
answer = create(:poll_answer, question: question, author: author, answer: "Yes")

View File

@@ -4,12 +4,16 @@ describe Poll::PartialResult do
describe "validations" do
it "validates that the answers are included in the Poll::Question's list" do
q = create(:poll_question, valid_answers: 'One, Two, Three')
expect(build(:poll_partial_result, question: q, answer: 'One')).to be_valid
expect(build(:poll_partial_result, question: q, answer: 'Two')).to be_valid
expect(build(:poll_partial_result, question: q, answer: 'Three')).to be_valid
question = create(:poll_question)
create(:poll_question_answer, title: 'One', question: question)
create(:poll_question_answer, title: 'Two', question: question)
create(:poll_question_answer, title: 'Three', question: question)
expect(build(:poll_partial_result, question: q, answer: 'Four')).to_not be_valid
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')).to_not be_valid
end
end

View File

@@ -3,13 +3,6 @@ require 'rails_helper'
RSpec.describe Poll::Question, type: :model do
let(:poll_question) { build(:poll_question) }
describe "#valid_answers" do
it "gets a comma-separated string, but returns an array" do
poll_question.valid_answers = "Yes, No"
expect(poll_question.valid_answers).to eq(["Yes", "No"])
end
end
describe "#poll_question_id" do
it "should be invalid if a poll is not selected" do
poll_question.poll_id = nil
@@ -27,7 +20,6 @@ RSpec.describe Poll::Question, type: :model do
create_list(:geozone, 3)
p = create(:proposal)
poll_question.copy_attributes_from_proposal(p)
expect(poll_question.valid_answers).to eq(['Yes', 'No'])
expect(poll_question.author).to eq(p.author)
expect(poll_question.author_visible_name).to eq(p.author.name)
expect(poll_question.proposal_id).to eq(p.id)