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

record_loader_strategy_spec.rb « strategies « in_operator_optimization « keyset « pagination « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3fe858f33da396c811c0e9c906227990ef5963fb (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Pagination::Keyset::InOperatorOptimization::Strategies::RecordLoaderStrategy do
  let(:finder_query) { -> (created_at_value, id_value) { model.where(model.arel_table[:id].eq(id_value)) } }
  let(:model) { Project }

  let(:keyset_scope) do
    scope, _ = Gitlab::Pagination::Keyset::SimpleOrderBuilder.build(
      model.order(:created_at, :id)
    )

    scope
  end

  let(:keyset_order) do
    Gitlab::Pagination::Keyset::Order.extract_keyset_order_object(keyset_scope)
  end

  let(:order_by_columns) do
    Gitlab::Pagination::Keyset::InOperatorOptimization::OrderByColumns.new(keyset_order.column_definitions, model.arel_table)
  end

  let_it_be(:ignored_column_model) do
    Class.new(ApplicationRecord) do
      self.table_name = 'projects'

      include IgnorableColumns

      ignore_column :name, remove_with: '16.4', remove_after: '2023-08-22'
    end
  end

  subject(:strategy) { described_class.new(finder_query, model, order_by_columns) }

  describe '#initializer_columns' do
    # Explanation:
    # > SELECT NULL::projects AS records
    #
    # The query returns one row and one column. The column may contain a full project row.
    # In this particular case the row is NULL.
    it 'returns a NULL table row as the result column' do
      expect(strategy.initializer_columns).to eq(["NULL::projects AS records"])
    end
  end

  describe '#columns' do
    # Explanation:
    # > SELECT (SELECT projects FROM projects limit 1)
    #
    # Selects one row from the database and collapses it into one column.
    #
    # Side note: Due to the type casts, columns and initializer_columns can be also UNION-ed:
    # SELECT * FROM (
    #   (
    #     SELECT NULL::projects AS records
    #     UNION
    #     SELECT (SELECT projects FROM projects limit 1)
    #   )
    # ) as records
    it 'uses the finder query to load the row in the result column' do
      expected_loader_query = <<~SQL
        (SELECT projects FROM "projects" WHERE "projects"."id" = recursive_keyset_cte.projects_id_array[position] LIMIT 1)
      SQL

      expect(strategy.columns).to eq([expected_loader_query.chomp])
    end
  end

  describe '#final_projections' do
    context 'when model does not have ignored columns' do
      it 'does not specify the selected column names' do
        expect(strategy.final_projections).to contain_exactly("(#{described_class::RECORDS_COLUMN}).*")
      end
    end

    context 'when model has ignored columns' do
      let(:model) { ignored_column_model }

      it 'specifies the selected column names' do
        expect(strategy.final_projections).to match_array(
          model.default_select_columns.map { |column| "(#{described_class::RECORDS_COLUMN}).#{column.name}" }
        )
      end
    end
  end
end