diff options
author | Robert Speicher <robert@gitlab.com> | 2018-09-17 17:21:25 +0000 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2018-09-17 17:21:25 +0000 |
commit | ba7f64080cc81314f6a000758aa8c572f3696322 (patch) | |
tree | 653f8c6878e633f370f2bce006ad5145be4fdd9e /spec | |
parent | bff85a4b1c0c2b8e5dca1dc75bf9d1e902312bcf (diff) | |
parent | 8a72f5c427426a3f90a14b2711ee1ecec4e8ca40 (diff) | |
download | gitlab-ce-ba7f64080cc81314f6a000758aa8c572f3696322.tar.gz |
Merge branch 'select-from-union' into 'master'
Replace direct use of Gitlab::SQL::Union with a "from_union" method
Closes #51307
See merge request gitlab-org/gitlab-ce!21672
Diffstat (limited to 'spec')
-rw-r--r-- | spec/models/concerns/from_union_spec.rb | 40 | ||||
-rw-r--r-- | spec/rubocop/cop/gitlab/union_spec.rb | 25 |
2 files changed, 65 insertions, 0 deletions
diff --git a/spec/models/concerns/from_union_spec.rb b/spec/models/concerns/from_union_spec.rb new file mode 100644 index 00000000000..ee427a667c6 --- /dev/null +++ b/spec/models/concerns/from_union_spec.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe FromUnion do + 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)]) + + expect(query.to_sql).to match(/FROM \(SELECT.+UNION.+SELECT.+\) users/m) + 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' + ) + + expect(query.to_sql).to match(/FROM \(SELECT.+UNION.+SELECT.+\) kittens/m) + 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) + .to match(/FROM \(SELECT.+UNION ALL.+SELECT.+\) users/m) + end + end +end diff --git a/spec/rubocop/cop/gitlab/union_spec.rb b/spec/rubocop/cop/gitlab/union_spec.rb new file mode 100644 index 00000000000..5b06f30b25f --- /dev/null +++ b/spec/rubocop/cop/gitlab/union_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'rubocop' +require 'rubocop/rspec/support' +require_relative '../../../../rubocop/cop/gitlab/union' + +describe RuboCop::Cop::Gitlab::Union do + include CopHelper + + subject(:cop) { described_class.new } + + it 'flags the use of Gitlab::SQL::Union.new' do + expect_offense(<<~SOURCE) + Gitlab::SQL::Union.new([foo]) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use the `FromUnion` concern, instead of using `Gitlab::SQL::Union` directly + SOURCE + end + + it 'does not flag the use of Gitlab::SQL::Union in a spec' do + allow(cop).to receive(:in_spec?).and_return(true) + + expect_no_offenses('Gitlab::SQL::Union.new([foo])') + end +end |