diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-06-12 10:46:31 +0000 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2018-06-12 10:46:31 +0000 |
commit | 450b9f6e30161e6719cf9ffb051b460eb48378c4 (patch) | |
tree | fd42a8e28ef14e40bfa9bea92041171978e93fdd /spec | |
parent | 9a4eadc87672de5b07b41df24810fbfe6c1f259e (diff) | |
parent | ed3a1be1e33388f3e3560d62fd658d0cc5f972ec (diff) | |
download | gitlab-ce-450b9f6e30161e6719cf9ffb051b460eb48378c4.tar.gz |
Merge branch 'da-port-cte-to-ce' into 'master'
Add Gitlab::SQL:CTE for easily building CTE statements
See merge request gitlab-org/gitlab-ce!19652
Diffstat (limited to 'spec')
-rw-r--r-- | spec/lib/gitlab/sql/cte_spec.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/spec/lib/gitlab/sql/cte_spec.rb b/spec/lib/gitlab/sql/cte_spec.rb new file mode 100644 index 00000000000..d6763c7b2e1 --- /dev/null +++ b/spec/lib/gitlab/sql/cte_spec.rb @@ -0,0 +1,42 @@ +require 'spec_helper' + +describe Gitlab::SQL::CTE, :postgresql do + describe '#to_arel' do + it 'generates an Arel relation for the CTE body' do + relation = User.where(id: 1) + cte = described_class.new(:cte_name, relation) + sql = cte.to_arel.to_sql + name = ActiveRecord::Base.connection.quote_table_name(:cte_name) + + sql1 = ActiveRecord::Base.connection.unprepared_statement do + relation.except(:order).to_sql + end + + expect(sql).to eq("#{name} AS (#{sql1})") + end + end + + describe '#alias_to' do + it 'returns an alias for the CTE' do + cte = described_class.new(:cte_name, nil) + table = Arel::Table.new(:kittens) + + source_name = ActiveRecord::Base.connection.quote_table_name(:cte_name) + alias_name = ActiveRecord::Base.connection.quote_table_name(:kittens) + + expect(cte.alias_to(table).to_sql).to eq("#{source_name} AS #{alias_name}") + end + end + + describe '#apply_to' do + it 'applies a CTE to an ActiveRecord::Relation' do + user = create(:user) + cte = described_class.new(:cte_name, User.where(id: user.id)) + + relation = cte.apply_to(User.all) + + expect(relation.to_sql).to match(/WITH .+cte_name/) + expect(relation.to_a).to eq(User.where(id: user.id).to_a) + end + end +end |