Commit 177a1430 authored by Alex Kalderimis's avatar Alex Kalderimis

Abstract over nils_last ordering

This adds a NilsLast abstraction that makes generating correct
sort-orders in Ruby simpler.
parent e8b0d6c5
...@@ -277,7 +277,7 @@ RSpec.describe Resolvers::BaseResolver do ...@@ -277,7 +277,7 @@ RSpec.describe Resolvers::BaseResolver do
describe '#offset_pagination' do describe '#offset_pagination' do
let(:instance) { resolver_instance(resolver) } let(:instance) { resolver_instance(resolver) }
it 'is sugar for OffsetActiveRecordRelationConnection.new' do it 'is sugar for OffsetPaginatedRelation.new' do
expect(instance.offset_pagination(User.none)).to be_a(::Gitlab::Graphql::Pagination::OffsetPaginatedRelation) expect(instance.offset_pagination(User.none)).to be_a(::Gitlab::Graphql::Pagination::OffsetPaginatedRelation)
end end
end end
......
...@@ -4,6 +4,7 @@ require 'spec_helper' ...@@ -4,6 +4,7 @@ require 'spec_helper'
RSpec.describe Resolvers::MergeRequestsResolver do RSpec.describe Resolvers::MergeRequestsResolver do
include GraphqlHelpers include GraphqlHelpers
include SortingHelper
let_it_be(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let_it_be(:milestone) { create(:milestone, project: project) } let_it_be(:milestone) { create(:milestone, project: project) }
...@@ -40,7 +41,7 @@ RSpec.describe Resolvers::MergeRequestsResolver do ...@@ -40,7 +41,7 @@ RSpec.describe Resolvers::MergeRequestsResolver do
# AND "merge_requests"."iid" = 1 ORDER BY "merge_requests"."id" DESC # AND "merge_requests"."iid" = 1 ORDER BY "merge_requests"."id" DESC
# SELECT "projects".* FROM "projects" WHERE "projects"."id" = 2 # SELECT "projects".* FROM "projects" WHERE "projects"."id" = 2
# SELECT "project_features".* FROM "project_features" WHERE "project_features"."project_id" = 2 # SELECT "project_features".* FROM "project_features" WHERE "project_features"."project_id" = 2
let(:queries_per_project) { 4 } let(:queries_per_project) { 3 }
context 'no arguments' do context 'no arguments' do
it 'returns all merge requests' do it 'returns all merge requests' do
...@@ -250,17 +251,17 @@ RSpec.describe Resolvers::MergeRequestsResolver do ...@@ -250,17 +251,17 @@ RSpec.describe Resolvers::MergeRequestsResolver do
it 'sorts merge requests ascending' do it 'sorts merge requests ascending' do
expect(resolve_mr(project, sort: :merged_at_asc)) expect(resolve_mr(project, sort: :merged_at_asc))
.to match_array(mrs) .to match_array(mrs)
.and be_sorted(->(mr) { [merged_at(mr, 1.year.from_now).to_i, -mr.id] }) .and be_sorted(->(mr) { [merged_at(mr), -mr.id] })
end end
it 'sorts merge requests descending' do it 'sorts merge requests descending' do
expect(resolve_mr(project, sort: :merged_at_desc)) expect(resolve_mr(project, sort: :merged_at_desc))
.to match_array(mrs) .to match_array(mrs)
.and be_sorted(->(mr) { [-merged_at(mr).to_i, -mr.id] }) .and be_sorted(->(mr) { [-merged_at(mr), -mr.id] })
end end
def merged_at(mr, when_nil = nil) def merged_at(mr)
mr.metrics.merged_at || when_nil nils_last(mr.metrics.merged_at)
end end
context 'when label filter is given and the optimized_issuable_label_filter feature flag is off' do context 'when label filter is given and the optimized_issuable_label_filter feature flag is off' do
......
...@@ -17,4 +17,35 @@ module SortingHelper ...@@ -17,4 +17,35 @@ module SortingHelper
click_link value click_link value
end end
end end
def nils_last(value)
NilsLast.new(value)
end
class NilsLast
include Comparable
attr_reader :value
delegate :==, :eql?, :hash, to: :value
def initialize(value)
@value = value
@reverse = false
end
def <=>(other)
return unless other.is_a?(self.class)
return 0 if value.nil? && other.value.nil?
return 1 if value.nil?
return -1 if other.value.nil?
int = value <=> other.value
@reverse ? -int : int
end
def -@
@reverse = true
self
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