2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe FromUnion do
|
2018-12-05 23:21:45 +05:30
|
|
|
describe '.from_union' do
|
|
|
|
let(:model) do
|
|
|
|
Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = 'users'
|
|
|
|
|
|
|
|
include FromUnion
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'selects from the results of the UNION' do
|
|
|
|
query = model.from_union([model.where(id: 1), model.where(id: 2)])
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
expect(query.to_sql).to match(/FROM \(\(SELECT.+\)\nUNION\n\(SELECT.+\)\) users/m)
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports the use of a custom alias for the sub query' do
|
|
|
|
query = model.from_union(
|
|
|
|
[model.where(id: 1), model.where(id: 2)],
|
|
|
|
alias_as: 'kittens'
|
|
|
|
)
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
expect(query.to_sql).to match(/FROM \(\(SELECT.+\)\nUNION\n\(SELECT.+\)\) kittens/m)
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports keeping duplicate rows' do
|
|
|
|
query = model.from_union(
|
|
|
|
[model.where(id: 1), model.where(id: 2)],
|
|
|
|
remove_duplicates: false
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(query.to_sql)
|
2019-12-26 22:10:19 +05:30
|
|
|
.to match(/FROM \(\(SELECT.+\)\nUNION ALL\n\(SELECT.+\)\) users/m)
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|