Add specs to test some db unique constraints.

- For projects.identifier
- For friendly_id_slugs(slug, sluggable_type, scope)
This commit is contained in:
Pavel Balashou
2026-05-19 12:02:50 +02:00
parent 5af7d211c8
commit 09c5c443f4
2 changed files with 58 additions and 0 deletions
@@ -0,0 +1,46 @@
# frozen_string_literal: true
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
require "spec_helper"
RSpec.describe FriendlyId::Slug do
describe "friendly_id_slugs(slug, sluggable_type, scope) unique constraints" do
it "ensures uniqueness with null scope" do
p = create(:project)
expect do
described_class.create!(sluggable: p, slug: "my-app", scope: nil)
described_class.create!(sluggable: p, slug: "my-app", scope: nil)
end.to raise_error(
ActiveRecord::RecordNotUnique,
/PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope"\n/ # rubocop:disable Layout/LineLength
)
end
end
end
+12
View File
@@ -606,4 +606,16 @@ RSpec.describe Project do
end
end
end
describe "projects.identifier unique database constraint" do
let!(:other_project) { create(:project, identifier: "my-app") }
let(:project) { build(:project, identifier: "my-app") }
it "ensures uniqueness with disabled validation" do
expect { project.save!(validate: false) }.to raise_error(
ActiveRecord::RecordNotUnique,
/PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_projects_on_lower_identifier"\n/
)
end
end
end