2020-07-31 11:10:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-12-21 07:10:22 -05:00
|
|
|
RSpec.describe AuditEventArchived do
|
2020-07-31 11:10:14 -04:00
|
|
|
let(:source_table) { AuditEvent }
|
2020-12-21 07:10:22 -05:00
|
|
|
let(:destination_table) { described_class }
|
2020-07-31 11:10:14 -04:00
|
|
|
|
|
|
|
it 'has the same columns as the source table' do
|
2020-08-21 05:10:08 -04:00
|
|
|
column_names_from_source_table = column_names(source_table)
|
2020-12-21 07:10:22 -05:00
|
|
|
column_names_from_destination_table = column_names(destination_table)
|
2020-08-21 05:10:08 -04:00
|
|
|
|
2020-12-21 07:10:22 -05:00
|
|
|
expect(column_names_from_destination_table).to match_array(column_names_from_source_table)
|
2020-07-31 11:10:14 -04:00
|
|
|
end
|
|
|
|
|
2020-08-18 05:10:05 -04:00
|
|
|
it 'has the same null constraints as the source table' do
|
|
|
|
constraints_from_source_table = null_constraints(source_table)
|
2020-12-21 07:10:22 -05:00
|
|
|
constraints_from_destination_table = null_constraints(destination_table)
|
2020-08-18 05:10:05 -04:00
|
|
|
|
2020-12-21 07:10:22 -05:00
|
|
|
expect(constraints_from_destination_table.to_a).to match_array(constraints_from_source_table.to_a)
|
2020-08-18 05:10:05 -04:00
|
|
|
end
|
|
|
|
|
2020-07-31 11:10:14 -04:00
|
|
|
it 'inserts the same record as the one in the source table', :aggregate_failures do
|
2020-12-21 07:10:22 -05:00
|
|
|
expect { create(:audit_event) }.to change { destination_table.count }.by(1)
|
2020-07-31 11:10:14 -04:00
|
|
|
|
|
|
|
event_from_source_table = source_table.connection.select_one(
|
|
|
|
"SELECT * FROM #{source_table.table_name} ORDER BY created_at desc LIMIT 1"
|
|
|
|
)
|
2020-12-21 07:10:22 -05:00
|
|
|
event_from_destination_table = destination_table.connection.select_one(
|
|
|
|
"SELECT * FROM #{destination_table.table_name} ORDER BY created_at desc LIMIT 1"
|
2020-07-31 11:10:14 -04:00
|
|
|
)
|
|
|
|
|
2020-12-21 07:10:22 -05:00
|
|
|
expect(event_from_destination_table).to eq(event_from_source_table)
|
2020-07-31 11:10:14 -04:00
|
|
|
end
|
2020-08-18 05:10:05 -04:00
|
|
|
|
2020-08-21 05:10:08 -04:00
|
|
|
def column_names(table)
|
|
|
|
table.connection.select_all(<<~SQL)
|
|
|
|
SELECT c.column_name
|
|
|
|
FROM information_schema.columns c
|
|
|
|
WHERE c.table_name = '#{table.table_name}'
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2020-08-18 05:10:05 -04:00
|
|
|
def null_constraints(table)
|
|
|
|
table.connection.select_all(<<~SQL)
|
|
|
|
SELECT c.column_name, c.is_nullable
|
|
|
|
FROM information_schema.columns c
|
|
|
|
WHERE c.table_name = '#{table.table_name}'
|
|
|
|
AND c.column_name != 'created_at'
|
|
|
|
SQL
|
|
|
|
end
|
2020-07-31 11:10:14 -04:00
|
|
|
end
|