summaryrefslogtreecommitdiff
path: root/spec/models/concerns/as_cte_spec.rb
blob: 06d9650ec46a5bc88ed838ffe70874bc8d7f54aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe AsCte do
  let(:klass) do
    Class.new(ApplicationRecord) do
      include AsCte

      self.table_name = 'users'
    end
  end

  let(:query) { klass.where(id: [1, 2, 3]) }
  let(:name) { :klass_cte }

  describe '.as_cte' do
    subject { query.as_cte(name) }

    it { expect(subject).to be_a(Gitlab::SQL::CTE) }
    it { expect(subject.query).to eq(query) }
    it { expect(subject.table.name).to eq(name.to_s) }

    context 'with materialized parameter', if: Gitlab::Database::AsWithMaterialized.materialized_supported? do
      subject { query.as_cte(name, materialized: materialized).to_arel.to_sql }

      context 'as true' do
        let(:materialized) { true }

        it { expect(subject).to match /MATERIALIZE/ }
      end

      context 'as false' do
        let(:materialized) { false }

        it { expect(subject).not_to match /MATERIALIZE/ }
      end
    end
  end
end