Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#55962] Add tests for new StoragesFilter

Judith Roth9b4af6ea346c
diff --git a/modules/storages/app/models/queries/storages/projects/filter/storages_filter.rb b/modules/storages/app/models/queries/storages/projects/filter/storages_filter.rb
index 6c8dd3eed75..46c910216c1 100644
--- a/modules/storages/app/models/queries/storages/projects/filter/storages_filter.rb
+++ b/modules/storages/app/models/queries/storages/projects/filter/storages_filter.rb
@@ -42,10 +42,8 @@ module Queries::Storages::Projects::Filter
end
def allowed_values
- @allowed_values ||= Storages::ProjectStorage
- .includes(:storage)
- .distinct
- .pluck(:name, :storage_id)
+ @allowed_values ||= Storages::Storage
+ .pluck(:name, :id)
end
def available?
diff --git a/modules/storages/spec/models/queries/storages/projects/filter/storages_filter_spec.rb b/modules/storages/spec/models/queries/storages/projects/filter/storages_filter_spec.rb
new file mode 100644
index 00000000000..bb28cf9f3e4
--- /dev/null
+++ b/modules/storages/spec/models/queries/storages/projects/filter/storages_filter_spec.rb
@@ -0,0 +1,105 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 Queries::Storages::Projects::Filter::StoragesFilter do
+ it_behaves_like "basic query filter" do
+ let(:class_key) { :storages }
+ let(:type) { :list }
+ end
+
+ it_behaves_like "list query filter", scope: false do
+ shared_let(:project) { create(:project) }
+ shared_let(:storage1) { create(:storage, :as_generic) }
+ shared_let(:storage2) { create(:storage, :as_generic) }
+
+ let(:valid_values) do
+ [storage1.id, storage2.id]
+ end
+ let(:name) { "Available project attributes" }
+
+ describe "#apply_to" do
+ let(:values) { valid_values }
+
+ let(:project_ids_from_project_storages_handwritten_sql) do
+ <<-SQL.squish
+ SELECT "project_storages"."project_id"
+ FROM "project_storages"
+ WHERE "project_storages"."storage_id"
+ IN (#{values.join(', ')})
+ SQL
+ end
+
+ context 'for "="' do
+ let(:operator) { "=" }
+
+ it "is the same as handwriting the query" do
+ handwritten_scope_sql = <<-SQL.squish
+ SELECT "projects".* FROM "projects"
+ WHERE "projects"."id" IN (#{project_ids_from_project_storages_handwritten_sql})
+ SQL
+
+ expect(instance.apply_to(Project).to_sql).to eql handwritten_scope_sql
+ end
+ end
+
+ context 'for "!"' do
+ let(:operator) { "!" }
+
+ it "is the same as handwriting the query" do
+ handwritten_scope_sql = <<-SQL.squish
+ SELECT "projects".* FROM "projects"
+ WHERE "projects"."id" NOT IN (#{project_ids_from_project_storages_handwritten_sql})
+ SQL
+
+ expect(instance.apply_to(Project).to_sql).to eql handwritten_scope_sql
+ end
+ end
+
+ context "for an unsupported operator" do
+ let(:operator) { "!=" }
+
+ it "raises an error" do
+ expect { instance.apply_to(Project) }.to raise_error("unsupported operator")
+ end
+ end
+ end
+
+ describe "#allowed_values" do
+ it "is a list of the possible values" do
+ expected = [
+ [storage1.name, storage1.id],
+ [storage2.name, storage2.id]
+ ]
+
+ expect(instance.allowed_values).to match_array(expected)
+ end
+ end
+ end
+end