Welcome to mirror list, hosted at ThFree Co, Russian Federation.

subquery_spec.rb « concerns « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 95487fd8c2d803b2735baf0f38688d65bec1508d (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Subquery do
  let_it_be(:projects) { create_list :project, 3 }
  let_it_be(:project_ids) { projects.map(&:id) }
  let(:relation) { Project.where(id: projects) }

  subject { relation.subquery(:id) }

  shared_examples 'subquery as array values' do
    specify { is_expected.to match_array project_ids }
    specify { expect { subject }.not_to make_queries }
  end

  shared_examples 'subquery as relation' do
    it { is_expected.to be_a ActiveRecord::Relation }
    specify { expect { subject.load }.to make_queries }
  end

  shared_context 'when array size exceeds max_limit' do
    subject { relation.subquery(:id, max_limit: 1) }
  end

  context 'when relation is not loaded' do
    it_behaves_like 'subquery as relation'

    context 'when array size exceeds max_limit' do
      include_context 'when array size exceeds max_limit'

      it_behaves_like 'subquery as relation'
    end
  end

  context 'when relation is loaded' do
    before do
      relation.load
    end

    it_behaves_like 'subquery as array values'

    context 'when array size exceeds max_limit' do
      include_context 'when array size exceeds max_limit'

      it_behaves_like 'subquery as relation'
    end

    context 'with a select' do
      let(:relation) { Project.where(id: projects).select(:id) }

      it_behaves_like 'subquery as array values'

      context 'and querying with an unloaded column' do
        subject { relation.subquery(:namespace_id) }

        it { expect { subject }.to raise_error(ActiveModel::MissingAttributeError) }
      end
    end
  end
end