Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-09-08 18:12:29 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2022-09-08 18:12:29 +0300
commitf8c7f38d02ebf964cbf40d9445f0f9f843710701 (patch)
tree755d5c384a0f64ffc8aad26f4628844697499522 /spec/support_specs/database
parent8fea353b907d1fd571f5450a757cafee73cfbfd0 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/support_specs/database')
-rw-r--r--spec/support_specs/database/without_check_constraint_spec.rb85
1 files changed, 85 insertions, 0 deletions
diff --git a/spec/support_specs/database/without_check_constraint_spec.rb b/spec/support_specs/database/without_check_constraint_spec.rb
new file mode 100644
index 00000000000..d78eafd4a32
--- /dev/null
+++ b/spec/support_specs/database/without_check_constraint_spec.rb
@@ -0,0 +1,85 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe 'Database::WithoutCheckConstraint' do
+ include MigrationsHelpers
+
+ describe '.without_check_constraint' do
+ let(:connection) { ApplicationRecord.connection }
+ let(:table_name) { '_test_table' }
+ let(:constraint_name) { 'check_1234' }
+ let(:model) { table(table_name) }
+
+ before do
+ # Drop test table in case it's left from a previous execution.
+ connection.exec_query("DROP TABLE IF EXISTS #{table_name}")
+ # Model has an attribute called 'name' that can't be NULL.
+ connection.exec_query(<<-SQL)
+ CREATE TABLE #{table_name} (
+ name text
+ CONSTRAINT #{constraint_name} CHECK (name IS NOT NULL)
+ );
+ SQL
+ end
+
+ context 'with invalid table' do
+ subject do
+ without_check_constraint('no_such_table', constraint_name, connection: connection) {}
+ end
+
+ it 'raises exception' do
+ msg = "'no_such_table' does not exist"
+ expect { subject }.to raise_error(msg)
+ end
+ end
+
+ context 'with invalid constraint name' do
+ subject do
+ without_check_constraint(table_name, 'no_such_constraint', connection: connection) {}
+ end
+
+ it 'raises exception' do
+ msg = "'#{table_name}' table does not contain constraint called 'no_such_constraint'"
+ expect { subject }.to raise_error(msg)
+ end
+ end
+
+ context 'with constraint' do
+ subject { connection.check_constraints(table_name) }
+
+ it 'removes inside block' do
+ without_check_constraint(table_name, constraint_name, connection: connection) do
+ expect(subject).to be_empty
+ end
+ end
+
+ it 'restores outside block' do
+ saved_constraints = subject
+
+ without_check_constraint(table_name, constraint_name, connection: connection) do
+ end
+
+ expect(subject).to eq(saved_constraints)
+ end
+ end
+
+ context 'when creating an invalid record' do
+ subject(:invalid_record) { model.create!(name: nil) }
+
+ it 'enables invalid record creation inside block' do
+ without_check_constraint(table_name, constraint_name, connection: connection) do
+ expect(invalid_record).to be_persisted
+ expect(invalid_record.name).to be_nil
+ end
+ end
+
+ it 'rolls back changes made within the block' do
+ without_check_constraint(table_name, constraint_name, connection: connection) do
+ invalid_record
+ end
+ expect(model.all).to be_empty
+ end
+ end
+ end
+end