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

be_indexed_by.rb « matchers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ae955624ae952abd4105e1865a1b938e91b94ba5 (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
# frozen_string_literal: true

# Assert all the given foreign keys are indexed:
#
# ```
# composite_foreign_keys = [['build_id', 'partition_id']]
# indexed_columns = [['build_id', 'name', 'partition_id'], ['partition_id', 'build_id', 'name']]
# expect(composite_foreign_keys).to be_indexed_by(indexed_columns)
# ```
#
RSpec::Matchers.define :be_indexed_by do |indexed_columns|
  match do |composite_foreign_keys|
    composite_foreign_keys.all? do |composite_foreign_key|
      indexed_columns.any? do |columns|
        # for example, [build_id, partition_id] should be covered by indexes e.g.
        # - [build_id, partition_id, name]
        # - [partition_id, build_id, name]
        # but not by [build_id, name, partition_id]
        # therefore, we just need to take the first few columns (same length as composite key)
        # e.g. [partition_id, build_id] of [partition_id, build_id, name]
        # and compare with [build_id, partition_id]
        (composite_foreign_key - columns.first(composite_foreign_key.length)).blank?
      end
    end
  end
end