debian-mirror-gitlab/spec/models/concerns/sha_attribute_spec.rb

107 lines
2.9 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe ShaAttribute do
2022-07-16 23:28:13 +05:30
let(:model) do
Class.new(ActiveRecord::Base) do
include ShaAttribute
2017-09-10 17:25:29 +05:30
2022-07-16 23:28:13 +05:30
self.table_name = 'merge_requests'
end
2017-09-10 17:25:29 +05:30
end
2022-07-16 23:28:13 +05:30
let(:binary_column) { :merge_ref_sha }
let(:text_column) { :target_branch }
2017-09-10 17:25:29 +05:30
2022-07-16 23:28:13 +05:30
describe '.sha_attribute' do
it 'defines a SHA attribute with Gitlab::Database::ShaAttribute type' do
expect(model).to receive(:attribute)
.with(binary_column, an_instance_of(Gitlab::Database::ShaAttribute))
.and_call_original
2017-09-10 17:25:29 +05:30
2022-07-16 23:28:13 +05:30
model.sha_attribute(binary_column)
end
end
2018-10-15 14:42:47 +05:30
2022-07-16 23:28:13 +05:30
describe '.sha256_attribute' do
it 'defines a SHA256 attribute with Gitlab::Database::ShaAttribute type' do
expect(model).to receive(:attribute)
.with(binary_column, an_instance_of(Gitlab::Database::Sha256Attribute))
.and_call_original
2018-10-15 14:42:47 +05:30
2022-07-16 23:28:13 +05:30
model.sha256_attribute(binary_column)
end
end
2018-10-15 14:42:47 +05:30
2022-07-16 23:28:13 +05:30
describe '.load_schema!' do
# load_schema! is not a documented class method, so use a documented method
# that we know will call load_schema!
def load_schema!
expect(model).to receive(:load_schema!).and_call_original
2018-10-15 14:42:47 +05:30
2022-07-16 23:28:13 +05:30
model.new
end
2018-10-15 14:42:47 +05:30
2022-07-16 23:28:13 +05:30
using RSpec::Parameterized::TableSyntax
2017-09-10 17:25:29 +05:30
2022-07-16 23:28:13 +05:30
where(:column_name, :environment, :expected_error) do
ref(:binary_column) | 'development' | :no_error
ref(:binary_column) | 'production' | :no_error
ref(:text_column) | 'development' | :sha_mismatch_error
ref(:text_column) | 'production' | :no_error
:__non_existent_column | 'development' | :no_error
:__non_existent_column | 'production' | :no_error
end
2018-10-15 14:42:47 +05:30
2022-07-16 23:28:13 +05:30
let(:sha_mismatch_error) do
[
described_class::ShaAttributeTypeMismatchError,
/#{column_name}.* should be a :binary column/
]
end
2018-10-15 14:42:47 +05:30
2022-07-16 23:28:13 +05:30
with_them do
before do
stub_rails_env(environment)
2018-10-15 14:42:47 +05:30
end
2022-07-16 23:28:13 +05:30
context 'with sha_attribute' do
before do
model.sha_attribute(column_name)
2018-10-15 14:42:47 +05:30
end
2017-09-10 17:25:29 +05:30
2022-07-16 23:28:13 +05:30
it 'validates column type' do
2023-01-13 00:05:48 +05:30
case expected_error
when :no_error
2022-07-16 23:28:13 +05:30
expect { load_schema! }.not_to raise_error
2023-01-13 00:05:48 +05:30
when :sha_mismatch_error
2022-07-16 23:28:13 +05:30
expect { load_schema! }.to raise_error(
described_class::ShaAttributeTypeMismatchError,
/sha_attribute.*#{column_name}.* should be a :binary column/
)
end
end
2017-09-10 17:25:29 +05:30
end
2022-07-16 23:28:13 +05:30
context 'with sha256_attribute' do
before do
model.sha256_attribute(column_name)
end
2017-09-10 17:25:29 +05:30
2022-07-16 23:28:13 +05:30
it 'validates column type' do
2023-01-13 00:05:48 +05:30
case expected_error
when :no_error
2022-07-16 23:28:13 +05:30
expect { load_schema! }.not_to raise_error
2023-01-13 00:05:48 +05:30
when :sha_mismatch_error
2022-07-16 23:28:13 +05:30
expect { load_schema! }.to raise_error(
described_class::Sha256AttributeTypeMismatchError,
/sha256_attribute.*#{column_name}.* should be a :binary column/
)
end
end
2017-09-10 17:25:29 +05:30
end
end
end
end