2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::Graphql::Pagination::Keyset::Conditions::NotNullCondition do
|
2019-12-26 22:10:19 +05:30
|
|
|
describe '#build' do
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:operators) { ['>', '>'] }
|
|
|
|
let(:before_or_after) { :after }
|
|
|
|
let(:condition) { described_class.new(arel_table, order_list, values, operators, before_or_after) }
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
context 'when there is only one ordering field' do
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:arel_table) { Issue.arel_table }
|
|
|
|
let(:order_list) { [double(named_function: nil, attribute_name: 'id')] }
|
|
|
|
let(:values) { [500] }
|
|
|
|
let(:operators) { ['>'] }
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
it 'generates a single condition sql' do
|
|
|
|
expected_sql = <<~SQL
|
|
|
|
("issues"."id" > 500)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
expect(condition.build.squish).to eq expected_sql.squish
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
context 'when ordering by a column attribute' do
|
|
|
|
let(:arel_table) { Issue.arel_table }
|
|
|
|
let(:order_list) { [double(named_function: nil, attribute_name: 'relative_position'), double(named_function: nil, attribute_name: 'id')] }
|
|
|
|
let(:values) { [1500, 500] }
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
shared_examples ':after condition' do
|
|
|
|
it 'generates :after sql' do
|
|
|
|
expected_sql = <<~SQL
|
|
|
|
("issues"."relative_position" > 1500)
|
|
|
|
OR (
|
|
|
|
"issues"."relative_position" = 1500
|
|
|
|
AND
|
|
|
|
"issues"."id" > 500
|
|
|
|
)
|
|
|
|
OR ("issues"."relative_position" IS NULL)
|
|
|
|
SQL
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(condition.build.squish).to eq expected_sql.squish
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when :after' do
|
|
|
|
it_behaves_like ':after condition'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when :before' do
|
|
|
|
let(:before_or_after) { :before }
|
|
|
|
|
|
|
|
it 'generates :before sql' do
|
|
|
|
expected_sql = <<~SQL
|
|
|
|
("issues"."relative_position" > 1500)
|
|
|
|
OR (
|
|
|
|
"issues"."relative_position" = 1500
|
|
|
|
AND
|
|
|
|
"issues"."id" > 500
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
expect(condition.build.squish).to eq expected_sql.squish
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when :foo' do
|
|
|
|
let(:before_or_after) { :foo }
|
|
|
|
|
|
|
|
it_behaves_like ':after condition'
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
context 'when ordering by LOWER' do
|
|
|
|
let(:arel_table) { Project.arel_table }
|
|
|
|
let(:relation) { Project.order(arel_table['name'].lower.asc).order(:id) }
|
2020-04-22 19:07:51 +05:30
|
|
|
let(:order_list) { Gitlab::Graphql::Pagination::Keyset::OrderInfo.build_order_list(relation) }
|
2020-03-13 15:44:24 +05:30
|
|
|
let(:values) { ['Test', 500] }
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
context 'when :after' do
|
|
|
|
it 'generates :after sql' do
|
|
|
|
expected_sql = <<~SQL
|
|
|
|
(LOWER("projects"."name") > 'test')
|
|
|
|
OR (
|
|
|
|
LOWER("projects"."name") = 'test'
|
|
|
|
AND
|
|
|
|
"projects"."id" > 500
|
|
|
|
)
|
|
|
|
OR (LOWER("projects"."name") IS NULL)
|
|
|
|
SQL
|
2019-12-26 22:10:19 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
expect(condition.build.squish).to eq expected_sql.squish
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when :before' do
|
|
|
|
let(:before_or_after) { :before }
|
|
|
|
|
|
|
|
it 'generates :before sql' do
|
|
|
|
expected_sql = <<~SQL
|
|
|
|
(LOWER("projects"."name") > 'test')
|
|
|
|
OR (
|
|
|
|
LOWER("projects"."name") = 'test'
|
|
|
|
AND
|
|
|
|
"projects"."id" > 500
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
expect(condition.build.squish).to eq expected_sql.squish
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|