Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Aug 23, 2024
1 parent 02ae467 commit 5b6f1fa
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 30 deletions.
3 changes: 3 additions & 0 deletions spec/factories/custom_field_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,11 @@
factory :float_wp_custom_field, traits: [:float]
factory :date_wp_custom_field, traits: [:date]
factory :list_wp_custom_field, traits: [:list]
factory :multi_list_wp_custom_field, traits: [:multi_list]
factory :version_wp_custom_field, traits: [:version]
factory :multi_version_wp_custom_field, traits: [:multi_version]
factory :user_wp_custom_field, traits: [:user]
factory :multi_user_wp_custom_field, traits: [:multi_user]
factory :link_wp_custom_field, traits: [:link]
end

Expand Down
166 changes: 136 additions & 30 deletions spec/models/query/results_cf_sorting_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

require "spec_helper"

RSpec.describe Query::Results, "Sorting of custom field floats" do
RSpec.describe Query::Results, "Sorting by custom field" do
let(:query_results) do
Query::Results.new query
described_class.new query
end
let(:user) do
create(:user,
Expand All @@ -45,22 +45,6 @@
types: [type],
work_package_custom_fields: [custom_field])
end
let(:work_package_with_float) do
create(:work_package,
type:,
project:,
custom_values: { custom_field.id => "6.25" })
end

let(:work_package_without_float) do
create(:work_package,
type:,
project:)
end

let(:custom_field) do
create(:float_wp_custom_field, name: "MyFloat")
end

let(:query) do
build(:query,
Expand All @@ -74,25 +58,147 @@

before do
login_as(user)
work_package_with_float
work_package_without_float
end

describe "sorting ASC by float cf" do
let(:sort_criteria) { [[custom_field.column_name, "asc"]] }
shared_examples "it sorts" do
context "in ascending order" do
let(:sort_criteria) { [[custom_field.column_name, "asc"]] }

it "returns the correctly sorted result" do
expect(query_results.work_packages.map(&:id))
.to eq work_packages.map(&:id)
end
end

context "in descending order" do
let(:sort_criteria) { [[custom_field.column_name, "desc"]] }

it "returns the correctly sorted result" do
expect(query_results.work_packages.map(&:id))
.to eq work_packages.reverse.map(&:id)
end
end
end

context "for string format" do
let(:custom_field) { create(:string_wp_custom_field) }

let!(:work_packages) do
[
create(:work_package, type:, project:),
create(:work_package, type:, project:, custom_values: { custom_field.id => "16" }),
create(:work_package, type:, project:, custom_values: { custom_field.id => "6.25" })
]
end

include_examples "it sorts"
end

context "for link format" do
let(:custom_field) { create(:link_wp_custom_field) }

let!(:work_packages) do
[
create(:work_package, type:, project:),
create(:work_package, type:, project:, custom_values: { custom_field.id => "https://openproject.org/intro/" }),
create(:work_package, type:, project:, custom_values: { custom_field.id => "https://openproject.org/pricing/" })
]
end

include_examples "it sorts"
end

context "for int format" do
let(:custom_field) { create(:integer_wp_custom_field) }

it "returns the correctly sorted result" do
expect(query_results.work_packages.pluck(:id))
.to match [work_package_without_float, work_package_with_float].map(&:id)
let!(:work_packages) do
[
create(:work_package, type:, project:),
create(:work_package, type:, project:, custom_values: { custom_field.id => "6" }),
create(:work_package, type:, project:, custom_values: { custom_field.id => "16" })
]
end

include_examples "it sorts"
end

context "for float format" do
let(:custom_field) { create(:float_wp_custom_field) }

let!(:work_packages) do
[
create(:work_package, type:, project:),
create(:work_package, type:, project:, custom_values: { custom_field.id => "6.25" }),
create(:work_package, type:, project:, custom_values: { custom_field.id => "16" })
]
end

include_examples "it sorts"
end

context "for date format" do
let(:custom_field) { create(:date_wp_custom_field) }

let!(:work_packages) do
[
create(:work_package, type:, project:),
# create(:work_package, type:, project:, custom_values: { custom_field.id => "999-01-01" }), # TODO: order is wrong
create(:work_package, type:, project:, custom_values: { custom_field.id => "2024-01-01" }),
create(:work_package, type:, project:, custom_values: { custom_field.id => "2030-01-01" })
]
end

include_examples "it sorts"
end

describe "sorting DESC by float cf" do
let(:sort_criteria) { [[custom_field.column_name, "desc"]] }
context "for bool format" do
let(:custom_field) { create(:boolean_wp_custom_field) }

it "returns the correctly sorted result" do
expect(query_results.work_packages.pluck(:id))
.to match [work_package_with_float, work_package_without_float].map(&:id)
let!(:work_packages) do
[
create(:work_package, type:, project:),
create(:work_package, type:, project:, custom_values: { custom_field.id => "0" }),
create(:work_package, type:, project:, custom_values: { custom_field.id => "1" })
]
end

include_examples "it sorts"
end

context "for list format" do
let(:custom_field) { create(:list_wp_custom_field, possible_values: ["100", "3", "20"]) }
let(:possible_value_ids) { custom_field.possible_values.ids }

let!(:work_packages) do
[
create(:work_package, type:, project:, custom_values: { custom_field.id => possible_value_ids[0] }),
create(:work_package, type:, project:, custom_values: { custom_field.id => possible_value_ids[1] }),
create(:work_package, type:, project:, custom_values: { custom_field.id => possible_value_ids[2] }),
create(:work_package, type:, project:), # TODO: why is it here?

Check notice on line 177 in spec/models/query/results_cf_sorting_integration_spec.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] spec/models/query/results_cf_sorting_integration_spec.rb#L177 <Style/TrailingCommaInArrayLiteral>

Avoid comma after the last item of an array.
Raw output
spec/models/query/results_cf_sorting_integration_spec.rb:177:47: C: Style/TrailingCommaInArrayLiteral: Avoid comma after the last item of an array.
]
end

include_examples "it sorts"
end

context "for list format allowing multi select" do
let(:custom_field) { create(:multi_list_wp_custom_field, possible_values: ["100", "3", "20"]) }
let(:possible_value_ids) { custom_field.possible_values.ids }

let!(:work_packages) do
[
create(:work_package, type:, project:),
# TODO: sorting is done by values sorted by position and joined by `.`, why?
create(:work_package, type:, project:, custom_values: { custom_field.id => possible_value_ids.values_at(0) }),
create(:work_package, type:, project:, custom_values: { custom_field.id => possible_value_ids.values_at(2, 0) }),
create(:work_package, type:, project:, custom_values: { custom_field.id => possible_value_ids.values_at(1, 0) }),
create(:work_package, type:, project:, custom_values: { custom_field.id => possible_value_ids.values_at(0, 1, 2) }),
create(:work_package, type:, project:, custom_values: { custom_field.id => possible_value_ids.values_at(2) }),
create(:work_package, type:, project:, custom_values: { custom_field.id => possible_value_ids.values_at(1) }),
create(:work_package, type:, project:, custom_values: { custom_field.id => possible_value_ids.values_at(1, 2) })
]
end

include_examples "it sorts"
end
end

0 comments on commit 5b6f1fa

Please sign in to comment.