Commit 551229f3 authored by Jiri Strojil's avatar Jiri Strojil

Fixing tests and logic

parent b2143b21
Pipeline #33048 passed with stage
in 34 minutes and 54 seconds
......@@ -16,6 +16,8 @@ class Semester
11 => 2
}.freeze
SEMESTER_OFFSET = 4.months
class << self
def parse(string)
return unless string
......@@ -90,10 +92,10 @@ class Semester
# semester_number - required if year integer
def initialize(year = nil, number = 1)
@context = if year.nil?
Time.zone.now
Time.zone.now + SEMESTER_OFFSET
else
year -= 1 if number == 1
Time.zone.local(year, (18 - number * 6), 1)
Time.zone.local(year, (18 - number * 6), 1) + SEMESTER_OFFSET
end
end
......@@ -112,7 +114,7 @@ class Semester
# running semester where in now
def current
@current ||= Semester.semester_range_from_start(
Semester.semester_start_time(@context.advance(months: 4))
Semester.semester_start_time(@context)
)
end
......
require 'test_helper'
class SemesterServiceTest < ActionView::TestCase
test '#initialize' do
subject = Semester.new
assert_equal Time.zone.now.to_date, subject.context.to_date
subject = Semester.new(2018, 2)
assert_equal time_z(2018, 6, 1), subject.context
subject = Semester.new(2018, 1)
assert_equal time_z(2017, 12, 1), subject.context
end
test '#to_s' do
travel_to time_z(2018, 8, 10)
travel_to time_z(2018, 6, 10)
subject = Semester.new
assert_equal '2018,2', subject.to_s
travel_to time_z(2017, 12, 10)
travel_to time_z(2017, 7, 10)
subject = Semester.new
assert_equal '2018,1', subject.to_s
assert_equal '2017,2', subject.to_s
travel_to time_z(2018, 2, 10)
subject = Semester.new
assert_equal '2018,1', subject.to_s
assert_equal '2018,2', subject.to_s
end
test '#number' do
travel_to time_z(2018, 8, 10)
travel_to time_z(2018, 6, 10)
subject = Semester.new
assert_equal 2, subject.number
travel_to time_z(2018, 4, 10)
subject = Semester.new
assert_equal 1, subject.number
assert_equal 2, subject.number
travel_to time_z(2017, 12, 10)
subject = Semester.new
assert_equal 1, subject.number
......@@ -47,7 +36,11 @@ class SemesterServiceTest < ActionView::TestCase
test '#current' do
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal time_z(2018, 12, 1).to_date...time_z(2019, 5, 1).end_of_month.to_date, subject.current
subject = Semester.new(2018, 2)
assert_equal time_z(2018, 6, 1).to_date...time_z(2018, 11, 1).end_of_month.to_date, subject.current
subject = Semester.new(2017, 2)
......@@ -61,40 +54,52 @@ class SemesterServiceTest < ActionView::TestCase
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal time_z(2017, 12, 1).to_date...time_z(2018, 5, 1).end_of_month.to_date, subject.previous
assert_equal time_z(2017, 6, 1).to_date...time_z(2017, 11, 1).end_of_month.to_date, subject.previous(2)
assert_equal time_z(2016, 12, 1).to_date...time_z(2017, 5, 1).end_of_month.to_date, subject.previous(3)
assert_equal time_z(2018, 6, 1).to_date...time_z(2018, 11, 1).end_of_month.to_date, subject.previous
assert_equal time_z(2017, 12, 1).to_date...time_z(2018, 5, 1).end_of_month.to_date, subject.previous(2)
assert_equal time_z(2017, 6, 1).to_date...time_z(2017, 11, 1).end_of_month.to_date, subject.previous(3)
assert_equal time_z(2016, 12, 1).to_date...time_z(2017, 5, 1).end_of_month.to_date, subject.previous(4)
end
test '#next_semester' do
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal time_z(2018, 12, 1).to_date...time_z(2019, 5, 1).end_of_month.to_date, subject.next_semester
assert_equal time_z(2019, 6, 1).to_date...time_z(2019, 11, 1).end_of_month.to_date, subject.next_semester(2)
assert_equal time_z(2019, 12, 1).to_date...time_z(2020, 5, 1).end_of_month.to_date, subject.next_semester(3)
assert_equal time_z(2018, 12, 1).to_date...time_z(2019, 5, 1).end_of_month.to_date, subject.current
assert_equal time_z(2019, 6, 1).to_date...time_z(2019, 11, 1).end_of_month.to_date, subject.next_semester
assert_equal time_z(2019, 12, 1).to_date...time_z(2020, 5, 1).end_of_month.to_date, subject.next_semester(2)
end
test '#previous_s' do
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal '2018,2', subject.previous_s
assert_equal '2018,1', subject.previous_s(2)
assert_equal '2017,2', subject.previous_s(3)
travel_to time_z(2018, 6, 10)
subject = Semester.new
assert_equal '2018,1', subject.previous_s
assert_equal '2017,2', subject.previous_s(2)
assert_equal '2017,1', subject.previous_s(3)
end
test '#next_s' do
travel_to time_z(2018, 8, 10)
travel_to time_z(2018, 6, 10)
subject = Semester.new
assert_equal '2019,1', subject.next_s
assert_equal '2019,2', subject.next_s(2)
assert_equal '2020,1', subject.next_s(3)
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal '2019,1', subject.to_s
assert_equal '2020,1', subject.next_s(2)
end
test '#list' do
travel_to time_z(2018, 8, 10)
travel_to time_z(2018, 6, 10)
subject = Semester.new
assert_equal [
......@@ -122,6 +127,15 @@ class SemesterServiceTest < ActionView::TestCase
time_z(2019, 6, 1).to_date...time_z(2019, 11, 1).end_of_month.to_date,
time_z(2019, 12, 1).to_date...time_z(2020, 5, 1).end_of_month.to_date
], subject.list(direction: :next_semester)
travel_to time_z(2018, 8, 10)
subject = Semester.new
assert_equal [
time_z(2018, 12, 1).to_date...time_z(2019, 5, 1).end_of_month.to_date,
time_z(2018, 6, 1).to_date...time_z(2018, 11, 1).end_of_month.to_date,
time_z(2017, 12, 1).to_date...time_z(2018, 5, 1).end_of_month.to_date,
time_z(2017, 6, 1).to_date...time_z(2017, 11, 1).end_of_month.to_date
], subject.list
end
test '#collection' do
......@@ -129,10 +143,10 @@ class SemesterServiceTest < ActionView::TestCase
subject = Semester.new
assert_equal [
collection_entry(2, 2018, time_z(2018, 6, 1).to_date, time_z(2018, 11, 1).end_of_month.to_date),
collection_entry(1, 2018, time_z(2017, 12, 1).to_date, time_z(2018, 5, 1).end_of_month.to_date),
collection_entry(2, 2017, time_z(2017, 6, 1).to_date, time_z(2017, 11, 1).end_of_month.to_date),
collection_entry(1, 2017, time_z(2016, 12, 1).to_date, time_z(2017, 5, 1).end_of_month.to_date),
collection_entry(1, 2019, time_z(2018, 12, 1).to_date, time_z(2019, 5, 31).end_of_month.to_date),
collection_entry(2, 2018, time_z(2018, 6, 1).to_date, time_z(2018, 11, 30).end_of_month.to_date),
collection_entry(1, 2018, time_z(2017, 12, 1).to_date, time_z(2018, 5, 31).end_of_month.to_date),
collection_entry(2, 2017, time_z(2017, 6, 1).to_date, time_z(2017, 11, 30).end_of_month.to_date),
], subject.collection
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment