summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/schema_validation/structure_sql_spec.rb
blob: cc0bd4125ef82e02468ffa8e92e31660d6173e9e (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::SchemaValidation::StructureSql, feature_category: :database do
  let(:structure_file_path) { Rails.root.join('spec/fixtures/structure.sql') }
  let(:schema_name) { 'public' }

  subject(:structure_sql) { described_class.new(structure_file_path, schema_name) }

  context 'when having indexes' do
    describe '#index_exists?' do
      subject(:index_exists) { structure_sql.index_exists?(index_name) }

      context 'when the index does not exist' do
        let(:index_name) { 'non-existent-index' }

        it 'returns false' do
          expect(index_exists).to be_falsey
        end
      end

      context 'when the index exists' do
        let(:index_name) { 'index' }

        it 'returns true' do
          expect(index_exists).to be_truthy
        end
      end
    end

    describe '#indexes' do
      it 'returns indexes' do
        indexes = structure_sql.indexes

        expected_indexes = %w[
          missing_index
          wrong_index
          index
          index_namespaces_public_groups_name_id
          index_on_deploy_keys_id_and_type_and_public
          index_users_on_public_email_excluding_null_and_empty
        ]

        expect(indexes).to all(be_a(Gitlab::Database::SchemaValidation::SchemaObjects::Index))
        expect(indexes.map(&:name)).to eq(expected_indexes)
      end
    end
  end

  context 'when having triggers' do
    describe '#trigger_exists?' do
      subject(:trigger_exists) { structure_sql.trigger_exists?(name) }

      context 'when the trigger does not exist' do
        let(:name) { 'non-existent-trigger' }

        it 'returns false' do
          expect(trigger_exists).to be_falsey
        end
      end

      context 'when the trigger exists' do
        let(:name) { 'trigger' }

        it 'returns true' do
          expect(trigger_exists).to be_truthy
        end
      end
    end

    describe '#triggers' do
      it 'returns triggers' do
        triggers = structure_sql.triggers
        expected_triggers = %w[trigger wrong_trigger missing_trigger_1 projects_loose_fk_trigger]

        expect(triggers).to all(be_a(Gitlab::Database::SchemaValidation::SchemaObjects::Trigger))
        expect(triggers.map(&:name)).to eq(expected_triggers)
      end
    end
  end
end