debian-mirror-gitlab/spec/lib/gitlab/sql/union_spec.rb

41 lines
1.3 KiB
Ruby
Raw Normal View History

2015-11-26 14:37:03 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe Gitlab::SQL::Union do
2017-01-15 13:20:01 +05:30
let(:relation_1) { User.where(email: 'alice@example.com').select(:id) }
let(:relation_2) { User.where(email: 'bob@example.com').select(:id) }
def to_sql(relation)
relation.reorder(nil).to_sql
end
2015-11-26 14:37:03 +05:30
describe '#to_sql' do
it 'returns a String joining relations together using a UNION' do
2017-01-15 13:20:01 +05:30
union = described_class.new([relation_1, relation_2])
expect(union.to_sql).to eq("#{to_sql(relation_1)}\nUNION\n#{to_sql(relation_2)}")
end
2015-11-26 14:37:03 +05:30
2017-01-15 13:20:01 +05:30
it 'skips Model.none segements' do
empty_relation = User.none
union = described_class.new([empty_relation, relation_1, relation_2])
2015-11-26 14:37:03 +05:30
2018-03-17 18:26:18 +05:30
expect {User.where("users.id IN (#{union.to_sql})").to_a}.not_to raise_error
2017-01-15 13:20:01 +05:30
expect(union.to_sql).to eq("#{to_sql(relation_1)}\nUNION\n#{to_sql(relation_2)}")
2015-11-26 14:37:03 +05:30
end
2018-03-17 18:26:18 +05:30
it 'uses UNION ALL when removing duplicates is disabled' do
union = described_class
.new([relation_1, relation_2], remove_duplicates: false)
expect(union.to_sql).to include('UNION ALL')
end
it 'returns `NULL` if all relations are empty' do
empty_relation = User.none
union = described_class.new([empty_relation, empty_relation])
expect(union.to_sql).to eq('NULL')
end
2015-11-26 14:37:03 +05:30
end
end