summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorBrett Walker <brett@digitalmoksha.com>2018-05-08 00:14:00 +0000
committerStan Hu <stanhu@gmail.com>2018-05-08 00:14:00 +0000
commit18094a17458aab8c9d629b86946b61e0e7bab251 (patch)
tree3c4cce15d0cd5ce5bb89bd60e6317c71d93956d0 /spec
parent9a2d09e3a9ae16f62cba317d8064d9409c95321a (diff)
downloadgitlab-ce-18094a17458aab8c9d629b86946b61e0e7bab251.tar.gz
Backport: Keep ShaAttribute from halting startup when we can’t connect to a database
Diffstat (limited to 'spec')
-rw-r--r--spec/models/concerns/sha_attribute_spec.rb67
1 files changed, 54 insertions, 13 deletions
diff --git a/spec/models/concerns/sha_attribute_spec.rb b/spec/models/concerns/sha_attribute_spec.rb
index 21893e0cbaa..592feddf1dc 100644
--- a/spec/models/concerns/sha_attribute_spec.rb
+++ b/spec/models/concerns/sha_attribute_spec.rb
@@ -13,33 +13,74 @@ describe ShaAttribute do
end
describe '#sha_attribute' do
- context 'when the table exists' do
+ context 'when in non-production' do
before do
- allow(model).to receive(:table_exists?).and_return(true)
+ allow(Rails.env).to receive(:production?).and_return(false)
end
- it 'defines a SHA attribute for a binary column' do
- expect(model).to receive(:attribute)
- .with(:sha1, an_instance_of(Gitlab::Database::ShaAttribute))
+ context 'when the table exists' do
+ before do
+ allow(model).to receive(:table_exists?).and_return(true)
+ end
- model.sha_attribute(:sha1)
+ 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
+ it 'allows the attribute to be added' do
+ 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
end
- it 'raises ArgumentError when the column type is not :binary' do
- expect { model.sha_attribute(:name) }.to raise_error(ArgumentError)
+ context 'when the column does not exist' do
+ it 'raises ArgumentError' do
+ allow(model).to receive(:table_exists?).and_return(true)
+
+ expect(model).to receive(:columns)
+ expect(model).not_to receive(:attribute)
+
+ expect { model.sha_attribute(:no_name) }.to raise_error(ArgumentError)
+ 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
end
end
- context 'when the table does not exist' do
+ context 'when in production' do
before do
- allow(model).to receive(:table_exists?).and_return(false)
+ allow(Rails.env).to receive(:production?).and_return(true)
end
- it 'does nothing' do
+ it 'defines a SHA attribute' do
+ expect(model).not_to receive(:table_exists?)
expect(model).not_to receive(:columns)
- expect(model).not_to receive(:attribute)
+ expect(model).to receive(:attribute).with(:sha1, an_instance_of(Gitlab::Database::ShaAttribute))
- model.sha_attribute(:name)
+ model.sha_attribute(:sha1)
end
end
end