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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.shared_examples "table validators" do |validator, expected_result|
subject(:result) { validator.new(structure_file, database).execute }
let(:structure_file_path) { 'spec/fixtures/structure.sql' }
let(:inconsistency_type) { validator.to_s }
# rubocop:disable RSpec/VerifiedDoubleReference
let(:connection) { instance_double('connection', exec_query: database_tables, current_schema: 'public') }
# rubocop:enable RSpec/VerifiedDoubleReference
let(:schema) { 'public' }
let(:database) { Gitlab::Schema::Validation::Sources::Database.new(connection) }
let(:structure_file) { Gitlab::Schema::Validation::Sources::StructureSql.new(structure_file_path, schema) }
let(:database_tables) do
[
{
'table_name' => 'wrong_table',
'column_name' => 'id',
'not_null' => true,
'data_type' => 'integer',
'column_default' => "nextval('audit_events_id_seq'::regclass)"
},
{
'table_name' => 'wrong_table',
'column_name' => 'description',
'not_null' => true,
'data_type' => 'character varying',
'column_default' => nil
},
{
'table_name' => 'extra_table',
'column_name' => 'id',
'not_null' => true,
'data_type' => 'integer',
'column_default' => "nextval('audit_events_id_seq'::regclass)"
},
{
'table_name' => 'extra_table',
'column_name' => 'email',
'not_null' => true,
'data_type' => 'character varying',
'column_default' => nil
},
{
'table_name' => 'extra_table_columns',
'column_name' => 'id',
'not_null' => true,
'data_type' => 'bigint',
'column_default' => "nextval('audit_events_id_seq'::regclass)"
},
{
'table_name' => 'extra_table_columns',
'column_name' => 'name',
'not_null' => true,
'data_type' => 'character varying(255)',
'column_default' => nil
},
{
'table_name' => 'extra_table_columns',
'column_name' => 'extra_column',
'not_null' => true,
'data_type' => 'character varying(255)',
'column_default' => nil
},
{
'table_name' => 'missing_table_columns',
'column_name' => 'id',
'not_null' => true,
'data_type' => 'bigint',
'column_default' => 'NOT NULL'
}
]
end
it 'returns table inconsistencies' do
expect(result.map(&:object_name)).to match_array(expected_result)
expect(result.map(&:type)).to all(eql inconsistency_type)
end
end
|