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

Add status column to table helpers

Christophe Bliarda0cfa092a775
diff --git a/spec/support/table_helpers/column.rb b/spec/support/table_helpers/column.rb
index cbf25500e63..d24a1255b78 100644
--- a/spec/support/table_helpers/column.rb
+++ b/spec/support/table_helpers/column.rb
@@ -58,6 +58,8 @@ module TableHelpers
Percentage.new(header:, attribute: :derived_done_ratio)
when /end date/i
Generic.new(header:, attribute: :due_date)
+ when /status/
+ Status.new(header:)
when /subject/
Subject.new(header:)
when /hierarchy/
@@ -102,6 +104,10 @@ module TableHelpers
:ljust
end
+ def attribute_value_for(work_package)
+ work_package.read_attribute(attribute)
+ end
+
def read_and_update_work_packages_data(work_packages_data)
work_packages_data.each do |work_package_data|
work_package_data => { attributes:, row: }
@@ -111,6 +117,10 @@ module TableHelpers
end
end
+ def attributes_for_work_package(work_package)
+ { attribute => work_package.read_attribute(attribute) }
+ end
+
def attributes_for_raw_value(raw_value, _data, _work_packages_data)
{ attribute => parse(raw_value) }
end
@@ -120,7 +130,7 @@ module TableHelpers
end
end
- module Identifiable
+ module WithIdentifierMetadata
include Identifier
def metadata_for_value(raw_value, *)
@@ -166,12 +176,25 @@ module TableHelpers
end
end
+ class Status < Generic
+ def attributes_for_work_package(work_package)
+ { status: work_package.status.name }
+ end
+ end
+
class Subject < Generic
- include Identifiable
+ include WithIdentifierMetadata
end
class Hierarchy < Generic
- include Identifiable
+ include WithIdentifierMetadata
+
+ def attributes_for_work_package(work_package)
+ {
+ parent: to_identifier(work_package.parent&.subject),
+ subject: work_package.subject
+ }
+ end
def attributes_for_raw_value(raw_value, data, work_packages_data)
{
diff --git a/spec/support/table_helpers/identifier.rb b/spec/support/table_helpers/identifier.rb
index 5289dbe7d73..9f1adc73bd0 100644
--- a/spec/support/table_helpers/identifier.rb
+++ b/spec/support/table_helpers/identifier.rb
@@ -33,6 +33,8 @@ module TableHelpers
extend self
def to_identifier(name)
+ return if name.nil?
+
name = name.downcase
name.strip!
name.tr!(' \-', "_")
diff --git a/spec/support/table_helpers/table_data.rb b/spec/support/table_helpers/table_data.rb
index c367758d216..0659f89280f 100644
--- a/spec/support/table_helpers/table_data.rb
+++ b/spec/support/table_helpers/table_data.rb
@@ -39,10 +39,10 @@ module TableHelpers
end
def self.from_work_packages(work_packages, columns)
- attribute_names = columns.map(&:attribute)
work_packages_data = work_packages.map do |work_package|
- attributes = attribute_names.to_h { [_1, work_package.read_attribute(_1)] }
- attributes[:parent] &&= to_identifier(attributes[:parent].subject)
+ attributes = columns.reduce({}) do |attrs, column|
+ attrs.merge!(column.attributes_for_work_package(work_package))
+ end
row = columns.to_h { [_1.title, nil] }
identifier = to_identifier(work_package.subject)
{
@@ -102,6 +102,9 @@ module TableHelpers
@work_packages_by_identifier[identifier] ||= begin
attributes = work_package_attributes(identifier)
attributes[:parent] = lookup_parent(attributes[:parent])
+ if status = lookup_status(attributes[:status])
+ attributes[:status] = status
+ end
FactoryBot.create(:work_package, attributes)
end
end
@@ -112,6 +115,19 @@ module TableHelpers
end
end
+ def lookup_status(status_name)
+ if status_name
+ statuses_by_name.fetch(status_name) do
+ raise NameError, "No status with name \"#{status_name}\" found. " \
+ "Available statuses are: #{statuses_by_name.keys}."
+ end
+ end
+ end
+
+ def statuses_by_name
+ @statuses_by_name ||= Status.all.index_by(&:name)
+ end
+
def work_package_attributes(identifier)
data = table_data.work_packages_data.find { |wpa| wpa[:identifier] == identifier.to_sym }
data[:attributes]
diff --git a/spec/support_spec/table_helpers/identifier_spec.rb b/spec/support_spec/table_helpers/identifier_spec.rb
index c15d4727e49..2412889bc93 100644
--- a/spec/support_spec/table_helpers/identifier_spec.rb
+++ b/spec/support_spec/table_helpers/identifier_spec.rb
@@ -38,6 +38,7 @@ module TableHelpers
end
end
+ include_examples "an identifier", name: nil, expected_identifier: nil
include_examples "an identifier", name: "Subject", expected_identifier: :subject
include_examples "an identifier", name: "Work package", expected_identifier: :work_package
include_examples "an identifier", name: "grand-child", expected_identifier: :grand_child
diff --git a/spec/support_spec/table_helpers/table_data_spec.rb b/spec/support_spec/table_helpers/table_data_spec.rb
index 11672ece463..c6d6339a850 100644
--- a/spec/support_spec/table_helpers/table_data_spec.rb
+++ b/spec/support_spec/table_helpers/table_data_spec.rb
@@ -51,19 +51,24 @@ module TableHelpers
describe ".from_work_packages" do
it "reads data from work packages according to the given columns" do
table = <<~TABLE
- | subject | remaining work |
- | work package | 3h |
- | another one | |
+ | subject | status | remaining work |
+ | work package | To do | 3h |
+ | another one | Done | |
TABLE
columns = described_class.for(table).columns
- work_package = build(:work_package, subject: "work package", remaining_hours: 3)
- another_one = build(:work_package, subject: "another one")
+ status_todo = build(:status, name: "To do")
+ status_done = build(:status, name: "Done")
+ work_package = build(:work_package, subject: "work package", status: status_todo, remaining_hours: 3)
+ another_one = build(:work_package, subject: "another one", status: status_done)
table_data = described_class.from_work_packages([work_package, another_one], columns)
expect(table_data.work_packages_data.size).to eq(2)
- expect(table_data.columns.size).to eq(2)
- expect(table_data.headers).to eq(["subject", "remaining work"])
+ expect(table_data.columns.size).to eq(3)
+ expect(table_data.headers).to eq(["subject", "status", "remaining work"])
+ expect(table_data.values_for_attribute(:subject)).to eq(["work package", "another one"])
+ expect(table_data.values_for_attribute(:status)).to eq(["To do", "Done"])
+ expect(table_data.values_for_attribute(:remaining_hours)).to eq([3.0, nil])
expect(table_data.work_package_identifiers).to eq(%i[work_package another_one])
end
end
@@ -81,5 +86,40 @@ module TableHelpers
expect(table_data.values_for_attribute(:subject)).to eq(["work package", "another one"])
end
end
+
+ describe "#create_work_packages" do
+ it "creates work packages out of the table data" do
+ status = create(:status, name: "To do")
+ table_representation = <<~TABLE
+ subject | status | work |
+ My wp | To do | 5h |
+ TABLE
+
+ table_data = described_class.for(table_representation)
+ table = table_data.create_work_packages
+ expect(table.work_packages.count).to eq(1)
+ expect(table.work_package(:my_wp))
+ .to have_attributes(subject: "My wp", status:, estimated_hours: 5.0)
+ end
+
+ it "raises an error if a given status name does not exist" do
+ table_representation = <<~TABLE
+ subject | status |
+ My wp | To do |
+ TABLE
+
+ expect { described_class.for(table_representation).create_work_packages }
+ .to raise_error(NameError, 'No status with name "To do" found. Available statuses are: [].')
+
+ create(:status, name: "Doing")
+ create(:status, name: "Done")
+ expect { described_class.for(table_representation).create_work_packages }
+ .to raise_error(NameError, 'No status with name "To do" found. Available statuses are: ["Doing", "Done"].')
+
+ create(:status, name: "To do")
+ expect { described_class.for(table_representation).create_work_packages }
+ .not_to raise_error
+ end
+ end
end
end
diff --git a/spec/support_spec/table_helpers/table_parser_spec.rb b/spec/support_spec/table_helpers/table_parser_spec.rb
index 3a14ebd645e..12d0b34e2f1 100644
--- a/spec/support_spec/table_helpers/table_parser_spec.rb
+++ b/spec/support_spec/table_helpers/table_parser_spec.rb
@@ -147,4 +147,18 @@ RSpec.describe TableHelpers::TableParser do
expect(parsed_data.first[:attributes]).to include(derived_remaining_hours: 9)
end
end
+
+ describe "status column" do
+ let!(:status) { create(:status, name: "New") }
+ let(:table) do
+ <<~TABLE
+ subject | status
+ wp | New
+ TABLE
+ end
+
+ it "sets the status attribute to its name, to be looked up later" do
+ expect(parsed_data.first[:attributes]).to include(status: "New")
+ end
+ end
end