diff --git a/app/models/survey_question.rb b/app/models/survey_question.rb index e80129ab..e7c3a019 100644 --- a/app/models/survey_question.rb +++ b/app/models/survey_question.rb @@ -1,5 +1,6 @@ class SurveyQuestion < ActiveRecord::Base belongs_to :survey + has_many :survey_replies # Order of this list should not be changed without proper action! enum kind: [:boolean, :choice, :string, :text, :datetime, :numeric] @@ -12,7 +13,6 @@ class SurveyQuestion < ActiveRecord::Base validates :max_choices, numericality: { greater_than_or_equal_to: 1 }, allow_blank: true validate :max_choices_greater_than_min - has_many :survey_replies def single_choice? choice? && max_choices == 1 && min_choices == 1 diff --git a/spec/controllers/surveys_controller_spec.rb b/spec/controllers/surveys_controller_spec.rb new file mode 100644 index 00000000..0b08bc75 --- /dev/null +++ b/spec/controllers/surveys_controller_spec.rb @@ -0,0 +1,34 @@ +require 'spec_helper' + +describe SurveysController do + let(:admin) { create(:admin) } + let(:user) { create(:user) } + + let!(:conference) { create(:conference, start_date: Date.current - 1.day, end_date: Date.current + 1.day, timezone: Time.current.zone) } + let!(:survey_future) { create(:survey, surveyable: conference, start_date: Date.current + 2.days, end_date: Date.current + 3.days) } + let!(:survey_past) { create(:survey, surveyable: conference, start_date: Date.current - 1.day, end_date: Date.current - 1.day) } + let!(:survey_present) { create(:survey, surveyable: conference, start_date: Date.current - 1.day, end_date: Date.current + 1.day) } + + describe 'GET #index' do + context 'guest' do + before :each do + get :index, conference_id: conference.short_title + end + + it '@sureveys variable is nil' do + expect(assigns(:surveys)).to eq [survey_present] + end + end + + context 'signed in user' do + before :each do + sign_in user + get :index, conference_id: conference.short_title + end + + it 'assigns @surveys with active surveys' do + expect(assigns(:surveys)).to eq [survey_present] + end + end + end +end diff --git a/spec/factories/survey_questions.rb b/spec/factories/survey_questions.rb index 0f7cd271..b3e2240f 100644 --- a/spec/factories/survey_questions.rb +++ b/spec/factories/survey_questions.rb @@ -2,6 +2,10 @@ FactoryGirl.define do factory :survey_question do survey title 'What about this question?' + kind :boolean + min_choices nil + max_choices nil + possible_answers nil factory :boolean_non_mandatory do title 'Have you attended the conference before? (Non mandatory)' diff --git a/spec/factories/surveys.rb b/spec/factories/surveys.rb index f88631d5..b4303b84 100644 --- a/spec/factories/surveys.rb +++ b/spec/factories/surveys.rb @@ -1,6 +1,9 @@ FactoryGirl.define do factory :survey do title 'This is my survey' + start_date Date.current - 1.day + end_date Date.current + 1.day + factory :conference_survey do association :surveyable, factory: :conference end diff --git a/spec/models/survey_question_spec.rb b/spec/models/survey_question_spec.rb new file mode 100644 index 00000000..7151a889 --- /dev/null +++ b/spec/models/survey_question_spec.rb @@ -0,0 +1,98 @@ +require 'spec_helper' + +describe SurveyQuestion do + # subject needs to be of kind 'choice', so that optional validations also run + # eg. numericality of min_choices and max_choices + subject { create(:choice_mandatory_1_reply) } + + let(:single_choice_question) { create(:choice_mandatory_1_reply) } + let(:multiple_choice_question) { create(:choice_mandatory_2_replies) } + let(:boolean_question) { create(:boolean_question, min_choices: 3)} + + describe 'association' do + it { is_expected.to belong_to(:survey) } + it { is_expected.to have_many(:survey_replies) } + end + + describe 'validation' do + it { is_expected.to validate_presence_of(:title) } + it { is_expected.to validate_numericality_of(:min_choices).is_greater_than_or_equal_to(1) } + it { is_expected.to validate_numericality_of(:max_choices).is_greater_than_or_equal_to(1) } + + it 'field presence, when of type choice?' do + survey_question = build(:survey_question, kind: :choice) + expect(survey_question).to validate_presence_of(:min_choices) + expect(survey_question).to validate_presence_of(:max_choices) + expect(survey_question).to validate_presence_of(:possible_answers) + end + + it 'max_choices > min_choices' do + survey_question = build(:survey_question, kind: :choice, min_choices: 3, max_choices: 2) + expect(survey_question.valid?).to eq false + expect(survey_question.errors[:max_choices]).to eq ['Max choices should not be less than min choices'] + end + end + + describe '#multiple_choice?' do + it 'returns false, when choice with 1 max_choice' do + expect(single_choice_question.multiple_choice?).to eq false + end + + it 'returns true, when choice with 2 max_choices' do + expect(multiple_choice_question.multiple_choice?).to eq true + end + end + + describe '#single_choice?' do + it 'returns true, when choice with 1 max_choice' do + expect(single_choice_question.single_choice?).to eq true + end + + it 'returns false, when choice with 2 max_choices' do + expect(multiple_choice_question.single_choice?).to eq false + end + end + + describe 'min_choices value' do + it 'nil, when boolean question' do + boolean_question = create(:boolean_mandatory, min_choices: 3) + expect(boolean_question.min_choices).to eq nil + end + + it 'not nil, when choice question' do + boolean_question = create(:survey_question, kind: :choice, possible_answers: 'Yes, No', min_choices: 3, max_choices: 4) + expect(boolean_question.min_choices).to eq 3 + end + end + + describe 'optional field' do + it 'min_choices is set when question is choice' do + question = create(:choice_mandatory_2_replies, min_choices: 2) + expect(question.min_choices).to eq 2 + end + + it 'max_choices is set when question is choice' do + question = create(:choice_mandatory_2_replies, max_choices: 2) + expect(question.max_choices).to eq 2 + end + + it 'possible_answers is set when question is choice' do + question = create(:choice_mandatory_2_replies, possible_answers: 'sth, sth else') + expect(question.possible_answers).to eq 'sth, sth else' + end + + shared_examples 'is nil' do |question_kind, field| + scenario "when question is #{question_kind} and field is #{field}" do + question = create(:survey_question, kind: question_kind.to_sym, field => 3) + expect(question.send(field)).to eq nil + end + end + + fields = ['min_choices', 'max_choices', 'possible_answers'] + (SurveyQuestion.kinds.keys - ['choice']).each do |kind| + fields.each do |field| + it_behaves_like 'is nil', kind, field + end + end + end +end diff --git a/spec/models/survey_reply_spec.rb b/spec/models/survey_reply_spec.rb new file mode 100644 index 00000000..0c340c83 --- /dev/null +++ b/spec/models/survey_reply_spec.rb @@ -0,0 +1,14 @@ +require 'spec_helper' + +describe SurveyReply do + subject { create(:survey_reply) } + + describe 'association' do + it { is_expected.to belong_to(:user) } + it { is_expected.to belong_to(:survey_question) } + end + + describe 'validation' do + it { is_expected.to validate_uniqueness_of(:survey_question_id).scoped_to(:user_id) } + end +end diff --git a/spec/models/survey_spec.rb b/spec/models/survey_spec.rb new file mode 100644 index 00000000..caed4264 --- /dev/null +++ b/spec/models/survey_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +describe Survey do + subject { create(:survey) } + let(:survey_active) { create(:conference_survey, start_date: Date.current - 1.day, end_date: Date.current + 1.day) } + let(:survey_inactive) { create(:conference_survey, start_date: Date.current - 2.day, end_date: Date.current - 1.day) } + + describe 'association' do + it { is_expected.to have_many(:survey_questions) } + it { is_expected.to have_many(:survey_submissions) } + end + + describe 'validation' do + it { is_expected.to validate_presence_of(:title) } + end + + describe '#active?' do + it { expect(survey_active.active?).to eq true } + it { expect(survey_inactive.active?).to eq false } + it 'returns false, if start_date is not set' do + expect(create(:survey, start_date: nil, end_date: Date.current + 1.day).active?).to eq false + end + + it 'returns false, if end_date is not set' do + expect(create(:survey, start_date: Date.current + 1.day, end_date: nil).active?).to eq false + end + end +end diff --git a/spec/models/survey_submission_spec.rb b/spec/models/survey_submission_spec.rb new file mode 100644 index 00000000..fdf3ca4f --- /dev/null +++ b/spec/models/survey_submission_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe SurveySubmission do + subject { create(:survey_submission) } + + describe 'association' do + it { is_expected.to belong_to(:survey) } + it { is_expected.to belong_to(:user) } + it { is_expected.to have_many(:survey_replies).through(:user) } + it { is_expected.to accept_nested_attributes_for(:survey_replies) } + end + + describe 'validation' do + it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(:survey_id) } + end +end