2019-03-30 03:23:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-29 09:37:37 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 14:09:03 -04:00
|
|
|
RSpec.describe ShaAttribute do
|
2017-06-29 09:37:37 -04:00
|
|
|
let(:model) { Class.new { include ShaAttribute } }
|
|
|
|
|
|
|
|
before do
|
|
|
|
columns = [
|
|
|
|
double(:column, name: 'name', type: :text),
|
|
|
|
double(:column, name: 'sha1', type: :binary)
|
|
|
|
]
|
|
|
|
|
|
|
|
allow(model).to receive(:columns).and_return(columns)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#sha_attribute' do
|
2020-07-30 14:09:39 -04:00
|
|
|
context 'when in development' do
|
2017-07-07 05:57:34 -04:00
|
|
|
before do
|
2019-09-03 21:57:25 -04:00
|
|
|
stub_rails_env('development')
|
2017-07-07 05:57:34 -04:00
|
|
|
end
|
2017-06-29 09:37:37 -04:00
|
|
|
|
2018-05-07 20:14:00 -04:00
|
|
|
context 'when the table exists' do
|
|
|
|
before do
|
|
|
|
allow(model).to receive(:table_exists?).and_return(true)
|
|
|
|
end
|
2017-07-07 05:57:34 -04:00
|
|
|
|
2018-05-07 20:14:00 -04:00
|
|
|
it 'defines a SHA attribute for a binary column' do
|
|
|
|
expect(model).to receive(:attribute)
|
|
|
|
.with(:sha1, an_instance_of(Gitlab::Database::ShaAttribute))
|
|
|
|
|
|
|
|
model.sha_attribute(:sha1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises ArgumentError when the column type is not :binary' do
|
|
|
|
expect { model.sha_attribute(:name) }.to raise_error(ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the table does not exist' do
|
2020-07-30 14:09:39 -04:00
|
|
|
it 'allows the attribute to be added' do
|
2018-05-07 20:14:00 -04:00
|
|
|
allow(model).to receive(:table_exists?).and_return(false)
|
|
|
|
|
|
|
|
expect(model).not_to receive(:columns)
|
|
|
|
expect(model).to receive(:attribute)
|
|
|
|
|
|
|
|
model.sha_attribute(:name)
|
|
|
|
end
|
2017-07-07 05:57:34 -04:00
|
|
|
end
|
|
|
|
|
2018-05-07 20:14:00 -04:00
|
|
|
context 'when the column does not exist' do
|
2020-07-30 14:09:39 -04:00
|
|
|
it 'allows the attribute to be added' do
|
2018-05-07 20:14:00 -04:00
|
|
|
allow(model).to receive(:table_exists?).and_return(true)
|
|
|
|
|
|
|
|
expect(model).to receive(:columns)
|
2018-05-10 11:00:32 -04:00
|
|
|
expect(model).to receive(:attribute)
|
2018-05-07 20:14:00 -04:00
|
|
|
|
2018-05-10 11:00:32 -04:00
|
|
|
model.sha_attribute(:no_name)
|
2018-05-07 20:14:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when other execeptions are raised' do
|
|
|
|
it 'logs and re-rasises the error' do
|
|
|
|
allow(model).to receive(:table_exists?).and_raise(ActiveRecord::NoDatabaseError.new('does not exist'))
|
|
|
|
|
|
|
|
expect(model).not_to receive(:columns)
|
|
|
|
expect(model).not_to receive(:attribute)
|
|
|
|
expect(Gitlab::AppLogger).to receive(:error)
|
|
|
|
|
|
|
|
expect { model.sha_attribute(:name) }.to raise_error(ActiveRecord::NoDatabaseError)
|
|
|
|
end
|
2017-07-07 05:57:34 -04:00
|
|
|
end
|
2017-06-29 09:37:37 -04:00
|
|
|
end
|
|
|
|
|
2018-05-07 20:14:00 -04:00
|
|
|
context 'when in production' do
|
2017-07-07 05:57:34 -04:00
|
|
|
before do
|
2019-09-03 21:57:25 -04:00
|
|
|
stub_rails_env('production')
|
2017-07-07 05:57:34 -04:00
|
|
|
end
|
|
|
|
|
2018-05-07 20:14:00 -04:00
|
|
|
it 'defines a SHA attribute' do
|
|
|
|
expect(model).not_to receive(:table_exists?)
|
2017-07-07 05:57:34 -04:00
|
|
|
expect(model).not_to receive(:columns)
|
2018-05-07 20:14:00 -04:00
|
|
|
expect(model).to receive(:attribute).with(:sha1, an_instance_of(Gitlab::Database::ShaAttribute))
|
2017-07-07 05:57:34 -04:00
|
|
|
|
2018-05-07 20:14:00 -04:00
|
|
|
model.sha_attribute(:sha1)
|
2017-07-07 05:57:34 -04:00
|
|
|
end
|
2017-06-29 09:37:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|