diff options
Diffstat (limited to 'spec/models/calendar_spec.rb')
| -rw-r--r-- | spec/models/calendar_spec.rb | 110 |
1 files changed, 109 insertions, 1 deletions
diff --git a/spec/models/calendar_spec.rb b/spec/models/calendar_spec.rb index 86ce565cd..3cffd0f8a 100644 --- a/spec/models/calendar_spec.rb +++ b/spec/models/calendar_spec.rb @@ -32,7 +32,11 @@ RSpec.describe Calendar, :type => :model do describe 'validations' do it 'validates that dates and date_ranges do not overlap' do - expect(build(:calendar, dates: [Date.today], date_ranges: [Date.today..Date.tomorrow])).to_not be_valid + expect(build(:calendar, dates: [Date.today.beginning_of_week], date_ranges: [Date.today.beginning_of_week..Date.today])).to_not be_valid + end + + it 'validates that dates and date_ranges do not overlap but allow for days not in the list' do + expect(build(:calendar, dates: [Date.today.beginning_of_week], date_ranges: [Date.today.beginning_of_week..Date.today], int_day_types: Calendar::THURSDAY)).to be_valid end it 'validates that there are no duplicates in dates' do @@ -52,4 +56,108 @@ RSpec.describe Calendar, :type => :model do end end + describe "Update state" do + def calendar_to_state calendar + calendar.slice('id').tap do |item| + item['comment'] = calendar.name + item['day_types'] = "Di,Lu,Ma,Me,Je,Ve,Sa" + item['current_month'] = calendar.month_inspect(Date.today.beginning_of_month) + item['current_periode_range'] = Date.today.beginning_of_month.to_s + item['time_table_periods'] = calendar.periods.map{|p| {'id': p.id, 'period_start': p.period_start.to_s, 'period_end': p.period_end.to_s}} + end + end + + subject(:calendar){ create :calendar } + let(:state) { calendar_to_state subject } + + it 'should update time table periods association' do + period = state['time_table_periods'].first + period['period_start'] = (Date.today - 1.month).to_s + period['period_end'] = (Date.today + 1.month).to_s + + subject.state_update_periods state['time_table_periods'] + ['period_end', 'period_start'].each do |prop| + expect(subject.reload.periods.first.send(prop).to_s).to eq(period[prop]) + end + end + + it 'should create time table periods association' do + state['time_table_periods'] << { + 'id' => false, + 'period_start' => (Date.today + 1.year).to_s, + 'period_end' => (Date.today + 2.year).to_s + } + + expect { + subject.state_update_periods state['time_table_periods'] + }.to change {subject.periods.count}.by(1) + expect(state['time_table_periods'].last['id']).to eq subject.reload.periods.last.id + end + + it 'should delete time table periods association' do + state['time_table_periods'].first['deleted'] = true + expect { + subject.state_update_periods state['time_table_periods'] + }.to change {subject.periods.count}.by(-1) + end + + it 'should update name' do + state['comment'] = "Edited timetable name" + subject.state_update state + expect(subject.reload.name).to eq state['comment'] + end + + it 'should update day_types' do + state['day_types'] = "Di,Lu,Je,Ma" + subject.state_update state + expect(subject.reload.valid_days).to include(7, 1, 4, 2) + expect(subject.reload.valid_days).not_to include(3, 5, 6) + end + + it 'should delete date if date is set to neither include or excluded date' do + updated = state['current_month'].map do |day| + day['include_date'] = false if day['include_date'] + end + + expect { + subject.state_update state + }.to change {subject.dates.count}.by(-updated.compact.count) + end + + it 'should update date if date is set to excluded date' do + updated = state['current_month'].map do |day| + next unless day['include_date'] + day['include_date'] = false + day['excluded_date'] = true + end + + subject.state_update state + expect(subject.reload.excluded_days.count).to eq (updated.compact.count) + end + + it 'should create new include date' do + day = state['current_month'].find{|d| !d['excluded_date'] && !d['include_date'] } + date = Date.parse(day['date']) + day['include_date'] = true + expect(subject.included_days).not_to include(date) + + expect { + subject.state_update state + }.to change {subject.dates.count}.by(1) + expect(subject.reload.included_days).to include(date) + end + + it 'should create new exclude date' do + day = state['current_month'].find{|d| !d['excluded_date'] && !d['include_date']} + date = Date.parse(day['date']) + day['excluded_date'] = true + expect(subject.excluded_days).not_to include(date) + + expect { + subject.state_update state + }.to change {subject.all_dates.count}.by(1) + expect(subject.reload.excluded_days).to include(date) + end + end + end |
