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

fix wrapping multiple work packages WithHistoricAttributes

ulfertsbde8741694ed
diff --git a/spec/models/journable/with_historic_attributes_spec.rb b/spec/models/journable/with_historic_attributes_spec.rb
index 23809d40cd3..d2ac3671171 100644
--- a/spec/models/journable/with_historic_attributes_spec.rb
+++ b/spec/models/journable/with_historic_attributes_spec.rb
@@ -29,34 +29,64 @@
require 'spec_helper'
describe Journable::WithHistoricAttributes do
- let(:work_package) do
- new_work_package = create(:work_package, subject: "The current work package", project: project1)
+ create_shared_association_defaults_for_work_package_factory
+
+ shared_let(:baseline_time) { "2022-01-01".to_time }
+ shared_let(:created_at) { baseline_time - 1.day }
+
+ shared_let(:work_package1) do
+ new_work_package = create(:work_package, subject: "The current work package 1")
new_work_package.update_columns(created_at:)
new_work_package
end
- let(:original_journal) do
- create_journal(journable: work_package, timestamp: created_at,
+ shared_let(:original_journal_wp1) do
+ work_package1.journals.destroy_all
+ create_journal(journable: work_package1,
+ timestamp: created_at,
version: 1,
- attributes: { subject: "The original work package" })
+ attributes: { subject: "The original work package 1" })
end
- let(:current_journal) do
- create_journal(journable: work_package, timestamp: 1.day.ago,
+ shared_let(:current_journal_wp2) do
+ create_journal(journable: work_package1,
+ timestamp: 1.day.ago,
version: 2,
- attributes: { subject: "The current work package" })
+ attributes: { subject: "The current work package 1" })
+ end
+
+ shared_let(:work_package2) do
+ new_work_package = create(:work_package,
+ project: work_package1.project,
+ subject: "The current work package 2",
+ start_date: created_at - 3.days)
+ new_work_package.update_columns(created_at:)
+ new_work_package
+ end
+ shared_let(:original_journal_wp2) do
+ work_package2.journals.destroy_all
+ create_journal(journable: work_package2,
+ timestamp: created_at,
+ version: 1,
+ attributes: { start_date: created_at - 5.days,
+ subject: "The original work package 2" })
end
- let(:baseline_time) { "2022-01-01".to_time }
- let(:created_at) { baseline_time - 1.day }
- let(:project1) { create(:project) }
+ shared_let(:current_journal_wp2) do
+ create_journal(journable: work_package2,
+ timestamp: 1.day.ago,
+ version: 2,
+ attributes: { start_date: created_at - 3.days,
+ subject: "The current work package 2" })
+ end
+
let(:user1) do
create(:user,
firstname: 'user',
lastname: '1',
- member_in_project: project1,
+ member_in_project: work_package1.project,
member_with_permissions: %i[view_work_packages view_file_links])
end
def create_journal(journable:, version:, timestamp:, attributes: {})
- work_package_attributes = work_package.attributes.except("id")
+ work_package_attributes = journable.attributes.except("id")
journal_attributes = work_package_attributes \
.extract!(*Journal::WorkPackageJournal.attribute_names) \
.symbolize_keys.merge(attributes)
@@ -65,32 +95,24 @@ describe Journable::WithHistoricAttributes do
data: build(:journal_work_package_journal, journal_attributes))
end
- before do
- WorkPackage.destroy_all
- work_package
- Journal.destroy_all
- original_journal
- current_journal
- end
-
describe ".wrap" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
let(:query) { nil }
let(:include_only_changed_attributes) { nil }
- subject { described_class.wrap(work_package, timestamps:, query:, include_only_changed_attributes:) }
+ subject { described_class.wrap(work_package1, timestamps:, query:, include_only_changed_attributes:) }
it "returns a Journable::WithHistoricAttributes instance" do
expect(subject).to be_a described_class
end
it "provides access to the work-package attributes" do
- expect(subject.subject).to eq "The current work package"
+ expect(subject.subject).to eq "The current work package 1"
end
it "provides access to the work-package attributes at timestamps" do
- expect(subject.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package"
- expect(subject.attributes_by_timestamp["PT0S"].subject).to eq "The current work package"
+ expect(subject.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
+ expect(subject.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 1"
end
it "determines for each timestamp whether the journable exists at that timestamp" do
@@ -138,7 +160,7 @@ describe Journable::WithHistoricAttributes do
it "provides access to the work-package attributes at timestamps " \
"where the attribute is different from the work package's attribute" do
- expect(subject.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package"
+ expect(subject.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
end
specify "the attributes at timestamps do not include attributes that are the same as the work package's attribute" do
@@ -155,11 +177,11 @@ describe Journable::WithHistoricAttributes do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
it "provides access to the historic work-package attributes" do
- expect(subject.subject).to eq "The original work package"
+ expect(subject.subject).to eq "The original work package 1"
end
it "provides access to the historic work-package attributes at timestamps" do
- expect(subject.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package"
+ expect(subject.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
end
it "determines whether the journable attributes are historic" do
@@ -175,7 +197,7 @@ describe Journable::WithHistoricAttributes do
let(:timestamps) { [Timestamp.parse("2021-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
it "provides access to the work-package attributes" do
- expect(subject.subject).to eq "The current work package"
+ expect(subject.subject).to eq "The current work package 1"
end
it "has no attributes at the baseline date" do
@@ -201,19 +223,19 @@ describe Journable::WithHistoricAttributes do
subject { described_class.wrap_multiple(work_packages, timestamps:, query:, include_only_changed_attributes:) }
context "with a single work package" do
- let(:work_packages) { [work_package] }
+ let(:work_packages) { [work_package1, work_package2] }
it "returns an array of Journable::WithHistoricAttributes instances" do
expect(subject).to all be_a described_class
end
it "provides access to the work-package attributes" do
- expect(subject.first.subject).to eq "The current work package"
+ expect(subject.first.subject).to eq "The current work package 1"
end
it "provides access to the work-package attributes at timestamps" do
- expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package"
- expect(subject.first.attributes_by_timestamp["PT0S"].subject).to eq "The current work package"
+ expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
+ expect(subject.first.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 1"
end
describe "when providing a query" do
@@ -237,7 +259,7 @@ describe Journable::WithHistoricAttributes do
it "provides access to the work-package attributes at timestamps " \
"where the attribute is different from the work package's attribute" do
- expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package"
+ expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
end
specify "the attributes at timestamps do not include attributes that are the same as the work package's attribute" do
@@ -249,11 +271,11 @@ describe Journable::WithHistoricAttributes do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
it "provides access to the historic work-package attributes" do
- expect(subject.first.subject).to eq "The original work package"
+ expect(subject.first.subject).to eq "The original work package 1"
end
it "provides access to the historic work-package attributes at timestamps" do
- expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package"
+ expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
end
it "determines whether the journable attributes are historic" do
@@ -261,11 +283,145 @@ describe Journable::WithHistoricAttributes do
end
end
end
+
+ context "with an array of work packages" do
+ let(:work_packages) { [work_package1, work_package2] }
+
+ it "returns an array of Journable::WithHistoricAttributes instances" do
+ expect(subject).to all be_a described_class
+ end
+
+ it "provides access to the work-package attributes" do
+ expect(subject.map(&:subject)).to eq ["The current work package 1", "The current work package 2"]
+ end
+
+ it "provides access to the work-package attributes at timestamps" do
+ expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
+ expect(subject.first.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 1"
+ expect(subject.last.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 2"
+ expect(subject.last.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 2"
+ end
+
+ describe "when providing a query" do
+ let(:query) do
+ login_as(user1)
+ build(:query, user: nil, project: nil).tap do |query|
+ query.filters.clear
+ query.add_filter 'subject', '~', search_term
+ end
+ end
+ let(:search_term) { "original" }
+
+ it "determines for each timestamp whether the journables matches the query at that timestamp" do
+ expect(subject.first.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ expect(subject.first.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
+ expect(subject.last.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ expect(subject.last.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
+ end
+ end
+
+ describe "with include_only_changed_attributes: true" do
+ let(:include_only_changed_attributes) { true }
+
+ it "provides access to the work-package attributes at timestamps " \
+ "where the attribute is different from the work package's attribute" do
+ expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
+ end
+
+ specify "the attributes at timestamps do not include attributes that are the same as the work package's attribute" do
+ expect(subject.first.attributes_by_timestamp["PT0S"].subject).to be_nil
+ end
+ end
+
+ describe "when requesting only historic data" do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
+
+ it "provides access to the historic work-package attributes" do
+ expect(subject.map(&:subject)).to eq ["The original work package 1", "The original work package 2"]
+ end
+
+ it "provides access to the historic work-package attributes at timestamps" do
+ expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
+ expect(subject.last.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 2"
+ end
+
+ it "determines whether the journable attributes are historic" do
+ expect(subject).to all be_historic
+ end
+ end
+ end
+
+ context "with an AR of work packages" do
+ let(:work_packages) { WorkPackage.all }
+
+ it "returns an array of Journable::WithHistoricAttributes instances" do
+ expect(subject).to all be_a described_class
+ end
+
+ it "provides access to the work-package attributes" do
+ expect(subject.map(&:subject)).to eq ["The current work package 1", "The current work package 2"]
+ end
+
+ it "provides access to the work-package attributes at timestamps" do
+ expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
+ expect(subject.first.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 1"
+ expect(subject.last.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 2"
+ expect(subject.last.attributes_by_timestamp["PT0S"].subject).to eq "The current work package 2"
+ end
+
+ describe "when providing a query" do
+ let(:query) do
+ login_as(user1)
+ build(:query, user: nil, project: nil).tap do |query|
+ query.filters.clear
+ query.add_filter 'subject', '~', search_term
+ end
+ end
+ let(:search_term) { "original" }
+
+ it "determines for each timestamp whether the journables matches the query at that timestamp" do
+ expect(subject.first.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ expect(subject.first.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
+ expect(subject.last.matches_query_filters_at_timestamps).to include Timestamp.parse("2022-01-01T00:00:00Z")
+ expect(subject.last.matches_query_filters_at_timestamps).not_to include Timestamp.parse("PT0S")
+ end
+ end
+
+ describe "with include_only_changed_attributes: true" do
+ let(:include_only_changed_attributes) { true }
+
+ it "provides access to the work-package attributes at timestamps " \
+ "where the attribute is different from the work package's attribute" do
+ expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
+ end
+
+ specify "the attributes at timestamps do not include attributes that are the same as the work package's attribute" do
+ expect(subject.first.attributes_by_timestamp["PT0S"].subject).to be_nil
+ end
+ end
+
+ describe "when requesting only historic data" do
+ let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z")] }
+
+ it "provides access to the historic work-package attributes" do
+ expect(subject.map(&:subject)).to eq ["The original work package 1", "The original work package 2"]
+ end
+
+ it "provides access to the historic work-package attributes at timestamps" do
+ expect(subject.first.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 1"
+ expect(subject.last.attributes_by_timestamp["2022-01-01T00:00:00Z"].subject).to eq "The original work package 2"
+ end
+
+ it "determines whether the journable attributes are historic" do
+ expect(subject).to all be_historic
+ end
+ end
+ end
end
describe "#baseline_timestamp" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
- let(:journable) { described_class.wrap(work_package, timestamps:) }
+ let(:journable) { described_class.wrap(work_package1, timestamps:) }
subject { journable.baseline_timestamp }
@@ -276,18 +432,18 @@ describe Journable::WithHistoricAttributes do
describe "#baseline_attributes" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
- let(:journable) { described_class.wrap(work_package, timestamps:) }
+ let(:journable) { described_class.wrap(work_package1, timestamps:) }
subject { journable.baseline_attributes }
it "provides access to the work-package attributes at the baseline timestamp" do
- expect(subject.subject).to eq "The original work package"
+ expect(subject.subject).to eq "The original work package 1"
end
end
describe "#matches_query_filters_at_baseline_timestamp?" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
- let(:journable) { described_class.wrap(work_package, timestamps:, query:) }
+ let(:journable) { described_class.wrap(work_package1, timestamps:, query:) }
let(:query) do
login_as(user1)
build(:query, user: nil, project: nil).tap do |query|
@@ -317,7 +473,7 @@ describe Journable::WithHistoricAttributes do
describe "#current_timestamp" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
- let(:journable) { described_class.wrap(work_package, timestamps:) }
+ let(:journable) { described_class.wrap(work_package1, timestamps:) }
subject { journable.current_timestamp }
@@ -328,7 +484,7 @@ describe Journable::WithHistoricAttributes do
describe "#matches_query_filters_at_current_timestamp?" do
let(:timestamps) { [Timestamp.parse("2022-01-01T00:00:00Z"), Timestamp.parse("PT0S")] }
- let(:journable) { described_class.wrap(work_package, timestamps:, query:) }
+ let(:journable) { described_class.wrap(work_package1, timestamps:, query:) }
let(:query) do
login_as(user1)
build(:query, user: nil, project: nil).tap do |query|