From f567187d45be8f828657346dafe0ab6a3f64ce96 Mon Sep 17 00:00:00 2001 From: Stella Rouzi Date: Sun, 21 Feb 2016 23:38:17 +0200 Subject: [PATCH] Add validations for registration_period --- Gemfile.lock | 3 - app/models/registration_period.rb | 21 +++++- .../registration_periods_controller_spec.rb | 2 +- spec/features/registration_periods_spec.rb | 4 +- spec/models/conference_spec.rb | 50 +++++++------ spec/models/registration_period_spec.rb | 74 +++++++++++++++++++ 6 files changed, 122 insertions(+), 32 deletions(-) create mode 100644 spec/models/registration_period_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index 669b82ab..a5aa50ef 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -557,6 +557,3 @@ DEPENDENCIES uglifier (>= 1.3.0) web-console (~> 2.0) whenever - -BUNDLED WITH - 1.11.2 diff --git a/app/models/registration_period.rb b/app/models/registration_period.rb index 6a87b4f7..b4e04a95 100644 --- a/app/models/registration_period.rb +++ b/app/models/registration_period.rb @@ -1,5 +1,22 @@ class RegistrationPeriod < ActiveRecord::Base - validates :start_date, :end_date, presence: true - belongs_to :conference + + validates :start_date, :end_date, presence: true + validate :before_end_of_conference + validate :start_date_before_end_date + + private + + def before_end_of_conference + errors. + add(:start_date, "can't be after the conference end date (#{conference.end_date})") if conference && conference.end_date && start_date && (start_date > conference.end_date) + + errors. + add(:end_date, "can't be after the conference end date (#{conference.end_date})") if conference && conference.end_date && end_date && (end_date > conference.end_date) + end + + def start_date_before_end_date + errors. + add(:start_date, "can't be after the end date") if start_date && end_date && start_date > end_date + end end diff --git a/spec/controllers/admin/registration_periods_controller_spec.rb b/spec/controllers/admin/registration_periods_controller_spec.rb index ae4586ea..01015ea1 100644 --- a/spec/controllers/admin/registration_periods_controller_spec.rb +++ b/spec/controllers/admin/registration_periods_controller_spec.rb @@ -27,7 +27,7 @@ describe Admin::RegistrationPeriodsController do end it 'changes registration period attributes' do - the_date = 10.days.from_now.to_date + the_date = conference.end_date - 10 patch :update, conference_id: conference.short_title, registration_period: attributes_for(:registration_period, start_date: the_date) diff --git a/spec/features/registration_periods_spec.rb b/spec/features/registration_periods_spec.rb index 341edbe9..c0515e97 100644 --- a/spec/features/registration_periods_spec.rb +++ b/spec/features/registration_periods_spec.rb @@ -25,7 +25,7 @@ feature RegistrationPeriod do "#{Date.today.strftime('%d/%m/%Y')}')") page. execute_script("$('#registration-period-end-datepicker').val('" + - "#{(Date.today + 7).strftime('%d/%m/%Y')}')") + "#{(Date.today + 5).strftime('%d/%m/%Y')}')") click_button 'Save Registration Period' @@ -35,7 +35,7 @@ feature RegistrationPeriod do registration_period = RegistrationPeriod.where(conference_id: conference.id).first registration_period.reload expect(registration_period.start_date).to eq(Date.today) - expect(registration_period.end_date).to eq(Date.today + 7) + expect(registration_period.end_date).to eq(Date.today + 5) end end diff --git a/spec/models/conference_spec.rb b/spec/models/conference_spec.rb index ccf2c745..d040d00b 100755 --- a/spec/models/conference_spec.rb +++ b/spec/models/conference_spec.rb @@ -4,7 +4,7 @@ require 'spec_helper' describe Conference do - let(:subject) { create(:conference, end_date: '2014-06-30') } + let(:subject) { create(:conference, start_date: Date.new(2014, 06, 30), end_date: Date.new(2014, 06, 30)) } describe '#write_event_distribution_to_db' do @@ -982,8 +982,8 @@ describe Conference do it 'calculates correct for conference with registration' do subject.registration_period = create(:registration_period, - start_date: Date.today, - end_date: Date.today + 14) + start_date: subject.end_date - 14, + end_date: subject.end_date, conference: subject) subject.program.cfp = nil subject.venue = nil subject.program.event_types = [] @@ -1000,8 +1000,8 @@ describe Conference do it 'calculates correct for conference with registration, cfp' do subject.registration_period = create(:registration_period, - start_date: Date.today, - end_date: Date.today + 14) + start_date: subject.end_date - 14, + end_date: subject.end_date, conference: subject) subject.program.cfp = create(:cfp) subject.venue = nil subject.program.tracks = [] @@ -1017,9 +1017,10 @@ describe Conference do end it 'calculates correct for conference with registration, cfp, venue' do + expect(subject.end_date).to eq Date.new(2014, 06, 30) subject.registration_period = create(:registration_period, - start_date: Date.today, - end_date: Date.today + 14) + start_date: subject.end_date - 14, + end_date: subject.end_date, conference: subject) subject.program.cfp = create(:cfp) subject.venue = create(:venue, conference: subject) subject.venue.rooms = [] @@ -1039,7 +1040,7 @@ describe Conference do it 'calculates correct for conference with registration, cfp, venue, rooms' do subject.registration_period = create(:registration_period, start_date: Date.today, - end_date: Date.today + 14) + end_date: Date.today + 14, conference: subject) subject.program.cfp = create(:cfp) subject.venue = create(:venue, conference: subject) subject.venue.rooms = [create(:room, venue: subject.venue)] @@ -1063,7 +1064,7 @@ describe Conference do subject.program.tracks = [create(:track)] subject.registration_period = create(:registration_period, start_date: Date.today, - end_date: Date.today + 14) + end_date: Date.today + 14, conference: subject) subject.program.cfp = create(:cfp) subject.program.event_types = [] subject.program.difficulty_levels = [] @@ -1085,7 +1086,7 @@ describe Conference do subject.program.event_types = [create(:event_type)] subject.registration_period = create(:registration_period, start_date: Date.today, - end_date: Date.today + 14) + end_date: Date.today + 14, conference: subject) subject.program.cfp = create(:cfp) subject.venue = create(:venue, conference: subject) subject.venue.rooms = [create(:room, venue: subject.venue)] @@ -1109,7 +1110,7 @@ describe Conference do subject.program.difficulty_levels = [create(:difficulty_level)] subject.registration_period = create(:registration_period, start_date: Date.today, - end_date: Date.today + 14) + end_date: Date.today + 14, conference: subject) subject.program.cfp = create(:cfp) subject.venue = create(:venue, conference: subject) subject.venue.rooms = [create(:room, venue: subject.venue)] @@ -1131,21 +1132,21 @@ describe Conference do it 'is one if start and end are 6 days apart' do subject.registration_period = create(:registration_period, start_date: Date.new(2014, 05, 26), - end_date: Date.new(2014, 05, 26) + 6) + end_date: Date.new(2014, 05, 26) + 6, conference: subject) expect(subject.registration_weeks).to eq(1) end it 'is one if start and end date are the same' do subject.registration_period = create(:registration_period, start_date: Date.new(2014, 05, 26), - end_date: Date.new(2014, 05, 26)) + end_date: Date.new(2014, 05, 26), conference: subject) expect(subject.registration_weeks).to eq(1) end it 'is two if start and end are 10 days apart' do subject.registration_period = create(:registration_period, - start_date: Date.new(2014, 05, 26), - end_date: Date.new(2014, 05, 26) + 10) + start_date: Date.new(2014, 05, 17), + end_date: Date.new(2014, 05, 15) + 10, conference: subject) expect(subject.registration_weeks).to eq(2) end end @@ -1279,7 +1280,7 @@ describe Conference do it 'pads with zeros if there are no registrations' do subject.registration_period = create(:registration_period, start_date: Date.new(2014, 05, 26), - end_date: Date.new(2014, 05, 26) + 21) + end_date: Date.new(2014, 05, 26) + 21, conference: subject) expect(subject.get_registrations_per_week).to eq([0, 0, 0, 0]) end @@ -1287,7 +1288,7 @@ describe Conference do it 'summarized correct if there are no registrations in one week' do subject.registration_period = create(:registration_period, start_date: Date.new(2014, 05, 26), - end_date: Date.new(2014, 05, 26) + 28) + end_date: Date.new(2014, 05, 26) + 28, conference: subject) create(:registration, conference: subject, created_at: Date.new(2014, 05, 26) + 7) @@ -1302,7 +1303,7 @@ describe Conference do it 'returns [1] if there is one registration on the first day' do subject.registration_period = create(:registration_period, start_date: Date.new(2014, 05, 26), - end_date: Date.new(2014, 05, 26) + 7) + end_date: Date.new(2014, 05, 26) + 7, conference: subject) create(:registration, conference: subject, created_at: Date.new(2014, 05, 26)) @@ -1312,7 +1313,7 @@ describe Conference do it 'summarized correct if there are registrations every week' do subject.registration_period = create(:registration_period, start_date: Date.new(2014, 05, 26), - end_date: Date.new(2014, 05, 26) + 21) + end_date: Date.new(2014, 05, 26) + 21, conference: subject) create(:registration, conference: subject, created_at: Date.new(2014, 05, 26)) create(:registration, conference: subject, @@ -1326,7 +1327,7 @@ describe Conference do it 'summarized correct if there are registrations every week except the first' do subject.registration_period = create(:registration_period, start_date: Date.new(2014, 05, 26), - end_date: Date.new(2014, 05, 26) + 28) + end_date: Date.new(2014, 05, 26) + 28, conference: subject) create(:registration, conference: subject, created_at: Date.new(2014, 05, 26) + 7) @@ -1341,7 +1342,7 @@ describe Conference do it 'pads left' do subject.registration_period = create(:registration_period, start_date: Date.new(2014, 05, 26), - end_date: Date.new(2014, 05, 26) + 35) + end_date: Date.new(2014, 05, 26) + 35, conference: subject) create(:registration, conference: subject, created_at: Date.new(2014, 05, 26) + 21) @@ -1356,7 +1357,7 @@ describe Conference do it 'pads middle' do subject.registration_period = create(:registration_period, start_date: Date.new(2014, 05, 26), - end_date: Date.new(2014, 05, 26) + 35) + end_date: Date.new(2014, 05, 26) + 35, conference: subject) create(:registration, conference: subject, created_at: Date.new(2014, 05, 26)) @@ -1369,7 +1370,7 @@ describe Conference do it 'pads right' do subject.registration_period = create(:registration_period, start_date: Date.new(2014, 05, 26), - end_date: Date.new(2014, 05, 26) + 35) + end_date: Date.new(2014, 05, 26) + 35, conference: subject) create(:registration, conference: subject, created_at: Date.new(2014, 05, 26)) @@ -1411,9 +1412,10 @@ describe Conference do context 'open registration' do before do + subject.end_date = Date.today + 7 enrollment = create(:registration_period, start_date: Date.today - 1, - end_date: Date.today + 7) + end_date: Date.today + 7, conference: subject) subject.registration_period = enrollment end diff --git a/spec/models/registration_period_spec.rb b/spec/models/registration_period_spec.rb new file mode 100644 index 00000000..ef28bab1 --- /dev/null +++ b/spec/models/registration_period_spec.rb @@ -0,0 +1,74 @@ +require 'spec_helper' + +describe RegistrationPeriod do + let!(:conference) { create(:conference, start_date: Date.today, end_date: Date.today + 6) } + let!(:registration_period) { create(:registration_period, start_date: Date.today - 2, end_date: Date.today - 1, conference: conference) } + + describe 'validations' do + it 'has a valid factory' do + expect(build(:registration_period)).to be_valid + end + + it 'is not valid without a start_date' do + should validate_presence_of(:start_date) + end + + it 'is not valid without an end_date' do + should validate_presence_of(:end_date) + end + end + + describe '#before_end_of_conference' do + context 'is valid' do + it 'when start_date and end_date are before conference end_date' do + registration_period.start_date = conference.end_date - 2 + registration_period.end_date = conference.end_date - 1 + expect(registration_period.valid?).to eq true + end + + it 'when start_date and end_date are the same day as conference end_date' do + registration_period.start_date = conference.end_date + registration_period.end_date = conference.end_date + expect(registration_period.valid?).to eq true + end + end + + context 'is invalid' do + it 'when start_date and end_date are after conference end_date' do + registration_period.start_date = conference.end_date + 1 + registration_period.end_date = conference.end_date + 2 + expect(registration_period.valid?).to eq false + end + + it 'when end_date is after conference end_date' do + registration_period.start_date = conference.end_date - 1 + registration_period.end_date = conference.end_date + 1 + expect(registration_period.valid?).to eq false + end + end + end + + describe '#start_date_before_end_date' do + context 'is valid' do + it 'when start_date is before end_date' do + registration_period.start_date = conference.end_date - 2 + registration_period.end_date = conference.end_date - 1 + expect(registration_period.valid?).to eq true + end + + it 'when start_date and end_date are on the same day' do + registration_period.start_date = conference.end_date - 2 + registration_period.end_date = conference.end_date - 2 + expect(registration_period.valid?).to eq true + end + end + + context 'is invalid' do + it 'when start_date is after end_date' do + registration_period.start_date = conference.start_date + 2 + registration_period.end_date = conference.start_date + 1 + expect(registration_period.valid?).to eq false + end + end + end +end