Merge pull request #23515 from opf/pluck-matcher

Pluck matcher
This commit is contained in:
Ivan Kuchin
2026-06-09 22:19:42 +02:00
committed by GitHub
3 changed files with 561 additions and 428 deletions
File diff suppressed because it is too large Load Diff
@@ -75,55 +75,53 @@ RSpec.describe Backlogs::WorkPackages::RebuildPositionsService, "integration", t
shared_let(:bucket2_wp2) { create_work_package(subject: "Bucket 2 WorkPackage 2", backlog_bucket: bucket2, position: nil) }
shared_let(:bucket2_wp3) { create_work_package(subject: "Bucket 2 WorkPackage 3", backlog_bucket: bucket2, position: nil) }
def have_positions(**) # rubocop:disable Naming/PredicatePrefix
pluck(:position).eq(**)
end
context "with the project provided" do
before do
described_class.new(project: project1).call
end
it "fixes the positions in that project while keeping those that have some in the same order" do # rubocop:disable RSpec/ExampleLength
expect(WorkPackage.where(sprint: sprint1).to_h { [it.subject, it.position] })
.to eql(
sprint1_wp2.subject => 1,
sprint1_wp3.subject => 2,
sprint1_wp4.subject => 3,
sprint1_wp1.subject => 4,
sprint1_wp5.subject => 5
)
expect(WorkPackage.where(sprint: sprint1)).to have_positions(
sprint1_wp2 => 1,
sprint1_wp3 => 2,
sprint1_wp4 => 3,
sprint1_wp1 => 4,
sprint1_wp5 => 5
)
expect(WorkPackage.where(sprint: sprint2).to_h { [it.subject, it.position] })
.to eql(
sprint2_wp3.subject => 1,
sprint2_wp2.subject => 2,
sprint2_wp1.subject => 3
)
expect(WorkPackage.where(sprint: sprint2)).to have_positions(
sprint2_wp3 => 1,
sprint2_wp2 => 2,
sprint2_wp1 => 3
)
expect(WorkPackage.where(sprint: nil, backlog_bucket: nil, project: project1).to_h { [it.subject, it.position] })
.to eql(
inbox_wp1.subject => 1,
inbox_wp2.subject => 2,
inbox_wp3.subject => 3
)
expect(WorkPackage.where(sprint: nil, backlog_bucket: nil, project: project1)).to have_positions(
inbox_wp1 => 1,
inbox_wp2 => 2,
inbox_wp3 => 3
)
expect(WorkPackage.where(sprint: sprint3).to_h { [it.subject, it.position] })
.to eql(
sprint3_wp1.subject => nil,
sprint3_wp2.subject => nil,
sprint3_wp3.subject => nil
)
expect(WorkPackage.where(sprint: sprint3)).to have_positions(
sprint3_wp1 => nil,
sprint3_wp2 => nil,
sprint3_wp3 => nil
)
expect(WorkPackage.where(backlog_bucket: bucket1).to_h { [it.subject, it.position] })
.to eql(
bucket1_wp3.subject => 1,
bucket1_wp2.subject => 2,
bucket1_wp1.subject => 3
)
expect(WorkPackage.where(backlog_bucket: bucket1)).to have_positions(
bucket1_wp3 => 1,
bucket1_wp2 => 2,
bucket1_wp1 => 3
)
expect(WorkPackage.where(backlog_bucket: bucket2).to_h { [it.subject, it.position] })
.to eql(
bucket2_wp1.subject => nil,
bucket2_wp2.subject => nil,
bucket2_wp3.subject => nil
)
expect(WorkPackage.where(backlog_bucket: bucket2)).to have_positions(
bucket2_wp1 => nil,
bucket2_wp2 => nil,
bucket2_wp3 => nil
)
end
end
@@ -133,49 +131,43 @@ RSpec.describe Backlogs::WorkPackages::RebuildPositionsService, "integration", t
end
it "fixes only the work packages in the other project" do # rubocop:disable RSpec/ExampleLength
expect(WorkPackage.where(sprint: sprint1).to_h { [it.subject, it.position] })
.to eql(
sprint1_wp1.subject => nil,
sprint1_wp2.subject => 1,
sprint1_wp3.subject => 2,
sprint1_wp4.subject => 2,
sprint1_wp5.subject => nil
)
expect(WorkPackage.where(sprint: sprint1)).to have_positions(
sprint1_wp1 => nil,
sprint1_wp2 => 1,
sprint1_wp3 => 2,
sprint1_wp4 => 2,
sprint1_wp5 => nil
)
expect(WorkPackage.where(sprint: sprint2).to_h { [it.subject, it.position] })
.to eql(
sprint2_wp3.subject => 1,
sprint2_wp2.subject => 2,
sprint2_wp1.subject => 3
)
expect(WorkPackage.where(sprint: sprint2)).to have_positions(
sprint2_wp3 => 1,
sprint2_wp2 => 2,
sprint2_wp1 => 3
)
expect(WorkPackage.where(sprint: nil, backlog_bucket: nil, project: project1).to_h { [it.subject, it.position] })
.to eql(
inbox_wp1.subject => nil,
inbox_wp2.subject => nil,
inbox_wp3.subject => nil
)
expect(WorkPackage.where(sprint: nil, backlog_bucket: nil, project: project1)).to have_positions(
inbox_wp1 => nil,
inbox_wp2 => nil,
inbox_wp3 => nil
)
expect(WorkPackage.where(sprint: sprint3).to_h { [it.subject, it.position] })
.to eql(
sprint3_wp1.subject => 1,
sprint3_wp2.subject => 2,
sprint3_wp3.subject => 3
)
expect(WorkPackage.where(sprint: sprint3)).to have_positions(
sprint3_wp1 => 1,
sprint3_wp2 => 2,
sprint3_wp3 => 3
)
expect(WorkPackage.where(backlog_bucket: bucket1).to_h { [it.subject, it.position] })
.to eql(
bucket1_wp1.subject => nil,
bucket1_wp2.subject => 2,
bucket1_wp3.subject => 1
)
expect(WorkPackage.where(backlog_bucket: bucket1)).to have_positions(
bucket1_wp1 => nil,
bucket1_wp2 => 2,
bucket1_wp3 => 1
)
expect(WorkPackage.where(backlog_bucket: bucket2).to_h { [it.subject, it.position] })
.to eql(
bucket2_wp1.subject => 1,
bucket2_wp2.subject => 2,
bucket2_wp3.subject => 3
)
expect(WorkPackage.where(backlog_bucket: bucket2)).to have_positions(
bucket2_wp1 => 1,
bucket2_wp2 => 2,
bucket2_wp3 => 3
)
end
end
@@ -185,49 +177,43 @@ RSpec.describe Backlogs::WorkPackages::RebuildPositionsService, "integration", t
end
it "fixes the positions while keeping those that have some in the same order in all projects" do # rubocop:disable RSpec/ExampleLength
expect(WorkPackage.where(sprint: sprint1).to_h { [it.subject, it.position] })
.to eql(
sprint1_wp2.subject => 1,
sprint1_wp3.subject => 2,
sprint1_wp4.subject => 3,
sprint1_wp1.subject => 4,
sprint1_wp5.subject => 5
)
expect(WorkPackage.where(sprint: sprint1)).to have_positions(
sprint1_wp2 => 1,
sprint1_wp3 => 2,
sprint1_wp4 => 3,
sprint1_wp1 => 4,
sprint1_wp5 => 5
)
expect(WorkPackage.where(sprint: sprint2).to_h { [it.subject, it.position] })
.to eql(
sprint2_wp3.subject => 1,
sprint2_wp2.subject => 2,
sprint2_wp1.subject => 3
)
expect(WorkPackage.where(sprint: sprint2)).to have_positions(
sprint2_wp3 => 1,
sprint2_wp2 => 2,
sprint2_wp1 => 3
)
expect(WorkPackage.where(sprint: nil, backlog_bucket: nil, project: project1).to_h { [it.subject, it.position] })
.to eql(
inbox_wp1.subject => 1,
inbox_wp2.subject => 2,
inbox_wp3.subject => 3
)
expect(WorkPackage.where(sprint: nil, backlog_bucket: nil, project: project1)).to have_positions(
inbox_wp1 => 1,
inbox_wp2 => 2,
inbox_wp3 => 3
)
expect(WorkPackage.where(sprint: sprint3).to_h { [it.subject, it.position] })
.to eql(
sprint3_wp1.subject => 1,
sprint3_wp2.subject => 2,
sprint3_wp3.subject => 3
)
expect(WorkPackage.where(sprint: sprint3)).to have_positions(
sprint3_wp1 => 1,
sprint3_wp2 => 2,
sprint3_wp3 => 3
)
expect(WorkPackage.where(backlog_bucket: bucket1).to_h { [it.subject, it.position] })
.to eql(
bucket1_wp3.subject => 1,
bucket1_wp2.subject => 2,
bucket1_wp1.subject => 3
)
expect(WorkPackage.where(backlog_bucket: bucket1)).to have_positions(
bucket1_wp3 => 1,
bucket1_wp2 => 2,
bucket1_wp1 => 3
)
expect(WorkPackage.where(backlog_bucket: bucket2).to_h { [it.subject, it.position] })
.to eql(
bucket2_wp1.subject => 1,
bucket2_wp2.subject => 2,
bucket2_wp3.subject => 3
)
expect(WorkPackage.where(backlog_bucket: bucket2)).to have_positions(
bucket2_wp1 => 1,
bucket2_wp2 => 2,
bucket2_wp3 => 3
)
end
end
end
+85
View File
@@ -0,0 +1,85 @@
# 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.
# ++
# When you need to check an attribute for multiple records:
#
# expect(WorkPackage.where(sprint:)).to pluck(:position).eq(
# sprint1_wp2 => 1,
# sprint1_wp3 => 2,
# sprint1_wp4 => 3,
# sprint1_wp1 => 4,
# sprint1_wp5 => 6
# )
#
# Fails with:
#
# 582 => 1,
# 583 => 2,
# 584 => 3,
# -585 => 6,
# +585 => 5,
#
# Specify `identified_by` attribute to be used instead of `id` to differentiate
# records in failurediff, it must be unique:
#
# expect(WorkPackage.where(sprint:)).to pluck(:position, identified_by: :subject).eq(
# sprint1_wp2 => 1,
# sprint1_wp3 => 2,
# sprint1_wp4 => 3,
# sprint1_wp1 => 4,
# sprint1_wp5 => 6
# )
#
# Fails with:
#
# "Sprint 1 WorkPackage 2" => 1,
# "Sprint 1 WorkPackage 3" => 2,
# "Sprint 1 WorkPackage 4" => 3,
# -"Sprint 1 WorkPackage 5" => 6,
# +"Sprint 1 WorkPackage 5" => 5,
RSpec::Matchers.define :pluck do |attribute, identified_by: :id|
chain :eq do |expected|
@expected = expected.transform_keys { it.public_send(identified_by) }
end
match do |actual|
@actual = actual.pluck(identified_by, attribute).to_h
@actual == @expected
end
diffable
define_method(:expected) { @expected }
failure_message do |_|
"expected #{attribute.inspect} (identified by #{identified_by.inspect}) to match"
end
end