Commit 87102e1c authored by Beat Seeliger's avatar Beat Seeliger
Browse files

Merge branch 'fix/with_billable_hours_and_hours_semester_scope_date_between_scope' into 'develop'

fix wrong date between scope for with_billable_hours and hours semester scopes

See merge request !803
parents 7c38ce11 46860ad3
Pipeline #23656 passed with stage
in 34 minutes and 29 seconds
......@@ -19,11 +19,15 @@ class Hour < ApplicationRecord
scope :billable, (-> { where(billing_expense: nil) })
scope :billed, (-> { where.not(billing_expense: nil) })
scope :semester, lambda { |date = nil|
if date.present?
date = Time.zone.parse(date) unless date.is_a? Time
return if date.blank?
date_between(:meeting_date, date, date.advance(months: BillingExpense::SEMESTER_LENGTH))
end
return all if date.blank?
date = Time.zone.parse(date) unless date.is_a? Time
return all if date.blank?
semester_with_date(date)
}
scope :semester_with_date, lambda { |date|
date_between_inclusion(:meeting_date, date.advance(days: 1),
date.advance(months: BillingExpense::SEMESTER_LENGTH))
}
scope :since_last_submitted, lambda { |submitted_at|
......
......@@ -208,8 +208,8 @@ class Volunteer < ApplicationRecord
scope :with_billable_hours_meeting_date_semester, lambda { |date|
return all if date.blank?
where('hours.meeting_date < :end_date AND hours.meeting_date >= :start_date',
start_date: date,
where('hours.meeting_date BETWEEN :start_date AND :end_date',
start_date: date.advance(days: 1),
end_date: date.advance(months: BillingExpense::SEMESTER_LENGTH))
}
......
......@@ -12,31 +12,65 @@ class HourTest < ActiveSupport::TestCase
test 'semester returns hours for a billing_expense semester' do
travel_to time_z(2018, 5, 25)
volunteer = create :volunteer
prev_hour1 = create :hour, volunteer: volunteer, hours: 1, meeting_date: time_z(2016, 11, 15)
prev_hour2 = create :hour, volunteer: volunteer, hours: 1, meeting_date: time_z(2016, 10, 1)
this_hour1 = create :hour, volunteer: volunteer, hours: 1, meeting_date: time_z(2016, 12, 1)
this_hour2 = create :hour, volunteer: volunteer, hours: 1, meeting_date: time_z(2017, 5, 11)
other_hour = create :hour, volunteer: volunteer, hours: 1, meeting_date: time_z(2013, 11, 21)
assignment = create :assignment
hours = {
this_hour1: hour_for_meeting_date(time_z(2016, 12, 1), assignment),
this_hour2: hour_for_meeting_date(time_z(2017, 4, 10), assignment),
this_hour3: hour_for_meeting_date(time_z(2017, 5, 31), assignment),
prev_hour1: hour_for_meeting_date(time_z(2016, 11, 15), assignment),
prev_hour2: hour_for_meeting_date(time_z(2016, 10, 1), assignment),
prev_hour3: hour_for_meeting_date(time_z(2016, 7, 1), assignment),
two_prev_hour1: hour_for_meeting_date(time_z(2015, 12, 1), assignment),
two_prev_hour2: hour_for_meeting_date(time_z(2016, 4, 10), assignment),
two_prev_hour3: hour_for_meeting_date(time_z(2016, 5, 31), assignment),
other_hour: hour_for_meeting_date(time_z(2013, 11, 21), assignment)
}
current_semester_hours = Hour.semester '2016-12-01'
last_semester_hours = Hour.semester '2016-06-01'
last_first_semester_hours = Hour.semester '2015-12-01'
all_semester_hours = Hour.semester
assert_includes current_semester_hours, this_hour1
assert_includes current_semester_hours, this_hour2
assert_not_includes current_semester_hours, prev_hour1
assert_not_includes current_semester_hours, prev_hour2
assert_not_includes current_semester_hours, other_hour
assert_includes current_semester_hours, hours[:this_hour1]
assert_includes current_semester_hours, hours[:this_hour2]
assert_includes current_semester_hours, hours[:this_hour3]
assert_not_includes current_semester_hours, hours[:prev_hour1]
assert_not_includes current_semester_hours, hours[:prev_hour2]
assert_not_includes current_semester_hours, hours[:prev_hour3]
assert_not_includes current_semester_hours, hours[:two_prev_hour1]
assert_not_includes current_semester_hours, hours[:two_prev_hour2]
assert_not_includes current_semester_hours, hours[:two_prev_hour3]
assert_not_includes current_semester_hours, hours[:other_hour]
assert_includes last_semester_hours, prev_hour1
assert_includes last_semester_hours, prev_hour2
assert_not_includes last_semester_hours, this_hour1
assert_not_includes last_semester_hours, this_hour2
assert_not_includes last_semester_hours, other_hour
assert_not_includes last_semester_hours, hours[:this_hour1]
assert_not_includes last_semester_hours, hours[:this_hour2]
assert_not_includes last_semester_hours, hours[:this_hour3]
assert_includes last_semester_hours, hours[:prev_hour1]
assert_includes last_semester_hours, hours[:prev_hour2]
assert_includes last_semester_hours, hours[:prev_hour3]
assert_not_includes last_semester_hours, hours[:two_prev_hour1]
assert_not_includes last_semester_hours, hours[:two_prev_hour2]
assert_not_includes last_semester_hours, hours[:two_prev_hour3]
assert_not_includes last_semester_hours, hours[:other_hour]
[prev_hour1, prev_hour2, this_hour1, this_hour2, other_hour].map do |hour|
assert_not_includes last_first_semester_hours, hours[:this_hour1]
assert_not_includes last_first_semester_hours, hours[:this_hour2]
assert_not_includes last_first_semester_hours, hours[:this_hour3]
assert_not_includes last_first_semester_hours, hours[:prev_hour1]
assert_not_includes last_first_semester_hours, hours[:prev_hour2]
assert_not_includes last_first_semester_hours, hours[:prev_hour3]
assert_includes last_first_semester_hours, hours[:two_prev_hour1]
assert_includes last_first_semester_hours, hours[:two_prev_hour2]
assert_includes last_first_semester_hours, hours[:two_prev_hour3]
assert_not_includes last_first_semester_hours, hours[:other_hour]
hours.values.each do |hour|
assert_includes all_semester_hours, hour
end
end
def hour_for_meeting_date(meeting_date, assignment)
create :hour, volunteer: assignment.volunteer, hours: 1, meeting_date: meeting_date,
hourable: assignment
end
end
Supports Markdown
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