Commit c83caa94 authored by Eulyeon Ko's avatar Eulyeon Ko

Fix due date condition

When a due date falls on the current date,
it should not be considered expired.
parent f4bf7be9
...@@ -123,7 +123,7 @@ class Milestone < ApplicationRecord ...@@ -123,7 +123,7 @@ class Milestone < ApplicationRecord
def self.sort_with_expired_last(method) def self.sort_with_expired_last(method)
# NOTE: this is a custom ordering of milestones # NOTE: this is a custom ordering of milestones
# to prioritize displaying non-expired milestones and milestones without due dates # to prioritize displaying non-expired milestones and milestones without due dates
sorted = reorder(Arel.sql('(CASE WHEN due_date IS NULL THEN 1 WHEN due_date > now() THEN 0 ELSE 2 END) ASC')) sorted = reorder(Arel.sql("(CASE WHEN due_date IS NULL THEN 1 WHEN due_date >= '#{Date.current}' THEN 0 ELSE 2 END) ASC"))
sorted = if method.to_s == 'expired_last_due_date_desc' sorted = if method.to_s == 'expired_last_due_date_desc'
sorted.order(due_date: :desc) sorted.order(due_date: :desc)
else else
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe MilestonesFinder do RSpec.describe MilestonesFinder do
let_it_be(:now) { Time.now } let_it_be(:now) { Date.current }
let_it_be(:group) { create(:group) } let_it_be(:group) { create(:group) }
let_it_be(:project_1) { create(:project, namespace: group) } let_it_be(:project_1) { create(:project, namespace: group) }
let_it_be(:project_2) { create(:project, namespace: group) } let_it_be(:project_2) { create(:project, namespace: group) }
...@@ -11,7 +11,7 @@ RSpec.describe MilestonesFinder do ...@@ -11,7 +11,7 @@ RSpec.describe MilestonesFinder do
let_it_be(:milestone_4) { create(:milestone, project: project_2, state: 'active', start_date: now + 4.days, due_date: now + 5.days) } let_it_be(:milestone_4) { create(:milestone, project: project_2, state: 'active', start_date: now + 4.days, due_date: now + 5.days) }
context 'without filters' do context 'without filters' do
let_it_be(:milestone_1) { create(:milestone, group: group, title: 'one test', start_date: now - 1.day, due_date: now) } let_it_be(:milestone_1) { create(:milestone, group: group, start_date: now - 1.day, due_date: now) }
let_it_be(:milestone_3) { create(:milestone, project: project_1, state: 'active', start_date: now + 2.days) } let_it_be(:milestone_3) { create(:milestone, project: project_1, state: 'active', start_date: now + 2.days) }
let_it_be(:milestone_5) { create(:milestone, group: group, due_date: now - 2.days) } let_it_be(:milestone_5) { create(:milestone, group: group, due_date: now - 2.days) }
...@@ -47,7 +47,7 @@ RSpec.describe MilestonesFinder do ...@@ -47,7 +47,7 @@ RSpec.describe MilestonesFinder do
let(:extra_params) { { sort: :expired_last_due_date_asc } } let(:extra_params) { { sort: :expired_last_due_date_asc } }
it 'current milestones are returned first, then milestones without due date followed by expired milestones, sorted by due date in ascending order' do it 'current milestones are returned first, then milestones without due date followed by expired milestones, sorted by due date in ascending order' do
expect(result).to eq([milestone_2, milestone_4, milestone_3, milestone_5, milestone_1]) expect(result).to eq([milestone_1, milestone_2, milestone_4, milestone_3, milestone_5])
end end
end end
end end
......
...@@ -451,18 +451,19 @@ RSpec.describe Milestone do ...@@ -451,18 +451,19 @@ RSpec.describe Milestone do
end end
describe '.sort_with_expired_last' do describe '.sort_with_expired_last' do
let_it_be(:milestone_1) { create(:milestone, title: 'Current 1', due_date: Time.current + 1.day) } let_it_be(:milestone) { create(:milestone, title: 'Due today', due_date: Date.current) }
let_it_be(:milestone_2) { create(:milestone, title: 'Current 2', due_date: Time.current + 2.days) } let_it_be(:milestone_1) { create(:milestone, title: 'Current 1', due_date: Date.current + 1.day) }
let_it_be(:milestone_2) { create(:milestone, title: 'Current 2', due_date: Date.current + 2.days) }
let_it_be(:milestone_3) { create(:milestone, title: 'Without due date') } let_it_be(:milestone_3) { create(:milestone, title: 'Without due date') }
let_it_be(:milestone_4) { create(:milestone, title: 'Expired 1', due_date: Time.current - 2.days) } let_it_be(:milestone_4) { create(:milestone, title: 'Expired 1', due_date: Date.current - 2.days) }
let_it_be(:milestone_5) { create(:milestone, title: 'Expired 2', due_date: Time.current - 1.day) } let_it_be(:milestone_5) { create(:milestone, title: 'Expired 2', due_date: Date.current - 1.day) }
let_it_be(:milestone_6) { create(:milestone, title: 'Without due date2') } let_it_be(:milestone_6) { create(:milestone, title: 'Without due date2') }
context 'ordering by due_date ascending' do context 'ordering by due_date ascending' do
it 'sorts by due date in ascending order (ties broken by id in desc order)', :aggregate_failures do it 'sorts by due date in ascending order (ties broken by id in desc order)', :aggregate_failures do
expect(milestone_3.id).to be < (milestone_6.id) expect(milestone_3.id).to be < (milestone_6.id)
expect(described_class.sort_with_expired_last(:expired_last_due_date_asc)) expect(described_class.sort_with_expired_last(:expired_last_due_date_asc))
.to eq([milestone_1, milestone_2, milestone_6, milestone_3, milestone_4, milestone_5]) .to eq([milestone, milestone_1, milestone_2, milestone_6, milestone_3, milestone_4, milestone_5])
end end
end end
...@@ -470,7 +471,7 @@ RSpec.describe Milestone do ...@@ -470,7 +471,7 @@ RSpec.describe Milestone do
it 'sorts by due date in descending order (ties broken by id in desc order)', :aggregate_failures do it 'sorts by due date in descending order (ties broken by id in desc order)', :aggregate_failures do
expect(milestone_3.id).to be < (milestone_6.id) expect(milestone_3.id).to be < (milestone_6.id)
expect(described_class.sort_with_expired_last(:expired_last_due_date_desc)) expect(described_class.sort_with_expired_last(:expired_last_due_date_desc))
.to eq([milestone_2, milestone_1, milestone_6, milestone_3, milestone_5, milestone_4]) .to eq([milestone_2, milestone_1, milestone, milestone_6, milestone_3, milestone_5, milestone_4])
end end
end end
end 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