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

require 'spec_helper'

RSpec.describe Gitlab::Database::SchemaValidation::Adapters::ColumnStructureSqlAdapter, feature_category: :database do
  subject(:adapter) { described_class.new(table_name, column_def) }

  let(:table_name) { 'my_table' }
  let(:file_path) { Rails.root.join('spec/fixtures/structure.sql') }
  let(:table_stmts) { PgQuery.parse(File.read(file_path)).tree.stmts.filter_map { |s| s.stmt.create_stmt } }
  let(:column_stmts) { table_stmts.find { |table| table.relation.relname == 'test_table' }.table_elts }
  let(:column_def) { column_stmts.find { |col| col.column_def.colname == column_name }.column_def }

  where(:column_name, :data_type, :default_value, :nullable) do
    [
      ['id', 'bigint', nil, 'NOT NULL'],
      ['integer_column', 'integer', nil, nil],
      ['integer_with_default_column', 'integer', 'DEFAULT 1', nil],
      ['smallint_with_default_column', 'smallint', 'DEFAULT 0', 'NOT NULL'],
      ['double_precision_with_default_column', 'double precision', 'DEFAULT 1.0', nil],
      ['numeric_with_default_column', 'numeric', 'DEFAULT 1.0', 'NOT NULL'],
      ['boolean_with_default_colum', 'boolean', 'DEFAULT true', 'NOT NULL'],
      ['varying_with_default_column', 'character varying', "DEFAULT 'DEFAULT'::character varying", 'NOT NULL'],
      ['varying_with_limit_and_default_column', 'character varying(255)', "DEFAULT 'DEFAULT'::character varying", nil],
      ['text_with_default_column', 'text', "DEFAULT ''::text", 'NOT NULL'],
      ['array_with_default_column', 'character varying(255)[]', "DEFAULT '{one,two}'::character varying[]", 'NOT NULL'],
      ['jsonb_with_default_column', 'jsonb', "DEFAULT '[]'::jsonb", 'NOT NULL'],
      ['timestamptz_with_default_column', 'timestamp(6) with time zone', "DEFAULT now()", nil],
      ['timestamp_with_default_column', 'timestamp(6) without time zone',
        "DEFAULT '2022-01-23 00:00:00+00'::timestamp without time zone", 'NOT NULL'],
      ['date_with_default_column', 'date', 'DEFAULT 2023-04-05', nil],
      ['inet_with_default_column', 'inet', "DEFAULT '0.0.0.0'::inet", 'NOT NULL'],
      ['macaddr_with_default_column', 'macaddr', "DEFAULT '00-00-00-00-00-000'::macaddr", 'NOT NULL'],
      ['uuid_with_default_column', 'uuid', "DEFAULT '00000000-0000-0000-0000-000000000000'::uuid", 'NOT NULL'],
      ['bytea_with_default_column', 'bytea', "DEFAULT '\\xDEADBEEF'::bytea", nil]
    ]
  end

  with_them do
    describe '#name' do
      it { expect(adapter.name).to eq(column_name) }
    end

    describe '#table_name' do
      it { expect(adapter.table_name).to eq(table_name) }
    end

    describe '#data_type' do
      it { expect(adapter.data_type).to eq(data_type) }
    end

    describe '#nullable' do
      it { expect(adapter.nullable).to eq(nullable) }
    end

    describe '#default' do
      it { expect(adapter.default).to eq(default_value) }
    end
  end

  context 'when the data type is not mapped' do
    let(:column_name) { 'unmapped_column_type' }
    let(:error_class) { Gitlab::Database::SchemaValidation::Adapters::UndefinedPGType }

    describe '#data_type' do
      it { expect { adapter.data_type }.to raise_error(error_class) }
    end
  end
end