|
|
|
@ -28,21 +28,13 @@ |
|
|
|
|
|
|
|
|
|
require 'spec_helper' |
|
|
|
|
|
|
|
|
|
describe ::Query::Results, type: :model do |
|
|
|
|
describe ::Query::Results, type: :model, with_mail: false do |
|
|
|
|
let(:query) do |
|
|
|
|
FactoryBot.build :query, |
|
|
|
|
show_hierarchies: false |
|
|
|
|
end |
|
|
|
|
let(:query_results) do |
|
|
|
|
::Query::Results.new query, |
|
|
|
|
include: %i( |
|
|
|
|
assigned_to |
|
|
|
|
type |
|
|
|
|
priority |
|
|
|
|
category |
|
|
|
|
fixed_version |
|
|
|
|
), |
|
|
|
|
order: 'work_packages.root_id DESC, work_packages.lft ASC' |
|
|
|
|
::Query::Results.new query |
|
|
|
|
end |
|
|
|
|
let(:project_1) { FactoryBot.create :project } |
|
|
|
|
let(:role_pm) do |
|
|
|
@ -77,10 +69,27 @@ describe ::Query::Results, type: :model do |
|
|
|
|
let(:query) do |
|
|
|
|
FactoryBot.build :query, |
|
|
|
|
show_hierarchies: false, |
|
|
|
|
group_by: group_by |
|
|
|
|
group_by: group_by, |
|
|
|
|
project: project_1 |
|
|
|
|
end |
|
|
|
|
let(:type_1) do |
|
|
|
|
FactoryBot.create(:type) |
|
|
|
|
end |
|
|
|
|
let(:type_2) do |
|
|
|
|
FactoryBot.create(:type) |
|
|
|
|
end |
|
|
|
|
let(:work_package1) do |
|
|
|
|
FactoryBot.create(:work_package, |
|
|
|
|
type: type_1, |
|
|
|
|
project: project_1) |
|
|
|
|
end |
|
|
|
|
let(:work_package2) do |
|
|
|
|
FactoryBot.create(:work_package, |
|
|
|
|
type: type_2, |
|
|
|
|
project: project_1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'when grouping by responsible' do |
|
|
|
|
context 'grouping by responsible' do |
|
|
|
|
let(:group_by) { 'responsible' } |
|
|
|
|
|
|
|
|
|
it 'should produce a valid SQL statement' do |
|
|
|
@ -88,7 +97,7 @@ describe ::Query::Results, type: :model do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'when grouping and filtering by text' do |
|
|
|
|
context 'grouping and filtering by text' do |
|
|
|
|
let(:group_by) { 'responsible' } |
|
|
|
|
|
|
|
|
|
before do |
|
|
|
@ -99,6 +108,175 @@ describe ::Query::Results, type: :model do |
|
|
|
|
expect { query_results.work_package_count_by_group }.not_to raise_error |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'grouping by type' do |
|
|
|
|
let(:group_by) { 'type' } |
|
|
|
|
|
|
|
|
|
before do |
|
|
|
|
work_package1 |
|
|
|
|
work_package2 |
|
|
|
|
|
|
|
|
|
login_as(user_1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it 'returns the groups sorted by type`s position' do |
|
|
|
|
type_1.update_column(:position, 1) |
|
|
|
|
type_2.update_column(:position, 2) |
|
|
|
|
|
|
|
|
|
result = query_results.work_package_count_by_group |
|
|
|
|
|
|
|
|
|
expect(result.length) |
|
|
|
|
.to eql 2 |
|
|
|
|
|
|
|
|
|
expect(result.keys.map(&:id)) |
|
|
|
|
.to eql [type_1.id, type_2.id] |
|
|
|
|
|
|
|
|
|
type_1.update_column(:position, 2) |
|
|
|
|
type_2.update_column(:position, 1) |
|
|
|
|
|
|
|
|
|
new_results = ::Query::Results.new(query) |
|
|
|
|
|
|
|
|
|
result = new_results.work_package_count_by_group |
|
|
|
|
|
|
|
|
|
expect(result.length) |
|
|
|
|
.to eql 2 |
|
|
|
|
|
|
|
|
|
expect(result.keys.map(&:id)) |
|
|
|
|
.to eql [type_2.id, type_1.id] |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'grouping by list custom field and filtering for it at the same time' do |
|
|
|
|
let!(:custom_field) do |
|
|
|
|
FactoryBot.create(:list_wp_custom_field, |
|
|
|
|
is_for_all: true, |
|
|
|
|
is_filter: true, |
|
|
|
|
multi_value: true).tap do |cf| |
|
|
|
|
work_package1.type.custom_fields << cf |
|
|
|
|
work_package2.type.custom_fields << cf |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
let(:first_value) do |
|
|
|
|
custom_field.custom_options.first |
|
|
|
|
end |
|
|
|
|
let(:last_value) do |
|
|
|
|
custom_field.custom_options.last |
|
|
|
|
end |
|
|
|
|
let(:group_by) { "cf_#{custom_field.id}" } |
|
|
|
|
|
|
|
|
|
before do |
|
|
|
|
login_as(user_1) |
|
|
|
|
|
|
|
|
|
work_package1.send(:"custom_field_#{custom_field.id}=", first_value) |
|
|
|
|
work_package1.save! |
|
|
|
|
work_package2.send(:"custom_field_#{custom_field.id}=", [first_value, |
|
|
|
|
last_value]) |
|
|
|
|
work_package2.save! |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it 'yields no error but rather returns the result' do |
|
|
|
|
expect { query_results.work_package_count_by_group }.not_to raise_error |
|
|
|
|
|
|
|
|
|
group_count = query_results.work_package_count_by_group |
|
|
|
|
expected_groups = [[first_value], [first_value, last_value]] |
|
|
|
|
|
|
|
|
|
group_count.each do |key, count| |
|
|
|
|
expect(count).to eql 1 |
|
|
|
|
expect(expected_groups.any? { |group| group & key == key & group }).to be_truthy |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'grouping by int custom field' do |
|
|
|
|
let!(:custom_field) do |
|
|
|
|
FactoryBot.create(:int_wp_custom_field, is_for_all: true, is_filter: true) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
let(:group_by) { "cf_#{custom_field.id}" } |
|
|
|
|
|
|
|
|
|
before do |
|
|
|
|
login_as(user_1) |
|
|
|
|
|
|
|
|
|
wp_p1[0].type.custom_fields << custom_field |
|
|
|
|
project_1.work_package_custom_fields << custom_field |
|
|
|
|
|
|
|
|
|
wp_p1[0].update_attribute(:"custom_field_#{custom_field.id}", 42) |
|
|
|
|
wp_p1[0].save |
|
|
|
|
wp_p1[1].update_attribute(:"custom_field_#{custom_field.id}", 42) |
|
|
|
|
wp_p1[1].save |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it 'returns a hash of counts by value' do |
|
|
|
|
expect(query_results.work_package_count_by_group).to eql(42 => 2, nil => 1) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'grouping by user custom field' do |
|
|
|
|
let!(:custom_field) do |
|
|
|
|
FactoryBot.create(:user_wp_custom_field, is_for_all: true, is_filter: true) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
let(:group_by) { "cf_#{custom_field.id}" } |
|
|
|
|
|
|
|
|
|
before do |
|
|
|
|
login_as(user_1) |
|
|
|
|
|
|
|
|
|
wp_p1[0].type.custom_fields << custom_field |
|
|
|
|
project_1.work_package_custom_fields << custom_field |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it 'returns nil as user custom fields are not groupable' do |
|
|
|
|
expect(query_results.work_package_count_by_group).to be_nil |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'grouping by bool custom field' do |
|
|
|
|
let!(:custom_field) do |
|
|
|
|
FactoryBot.create(:bool_wp_custom_field, is_for_all: true, is_filter: true) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
let(:group_by) { "cf_#{custom_field.id}" } |
|
|
|
|
|
|
|
|
|
before do |
|
|
|
|
login_as(user_1) |
|
|
|
|
|
|
|
|
|
wp_p1[0].type.custom_fields << custom_field |
|
|
|
|
project_1.work_package_custom_fields << custom_field |
|
|
|
|
|
|
|
|
|
wp_p1[0].update_attribute(:"custom_field_#{custom_field.id}", true) |
|
|
|
|
wp_p1[0].save |
|
|
|
|
wp_p1[1].update_attribute(:"custom_field_#{custom_field.id}", true) |
|
|
|
|
wp_p1[1].save |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it 'returns a hash of counts by value' do |
|
|
|
|
expect(query_results.work_package_count_by_group).to eql(true => 2, nil => 1) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'grouping by date custom field' do |
|
|
|
|
let!(:custom_field) do |
|
|
|
|
FactoryBot.create(:date_wp_custom_field, is_for_all: true, is_filter: true) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
let(:group_by) { "cf_#{custom_field.id}" } |
|
|
|
|
|
|
|
|
|
before do |
|
|
|
|
login_as(user_1) |
|
|
|
|
|
|
|
|
|
wp_p1[0].type.custom_fields << custom_field |
|
|
|
|
project_1.work_package_custom_fields << custom_field |
|
|
|
|
|
|
|
|
|
wp_p1[0].update_attribute(:"custom_field_#{custom_field.id}", Date.today) |
|
|
|
|
wp_p1[0].save |
|
|
|
|
wp_p1[1].update_attribute(:"custom_field_#{custom_field.id}", Date.today) |
|
|
|
|
wp_p1[1].save |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it 'returns a hash of counts by value' do |
|
|
|
|
expect(query_results.work_package_count_by_group).to eql(Date.today => 2, nil => 1) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe '#work_packages' do |
|
|
|
@ -587,85 +765,4 @@ describe ::Query::Results, type: :model do |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'when grouping by custom field' do |
|
|
|
|
let!(:custom_field) do |
|
|
|
|
FactoryBot.create(:int_wp_custom_field, is_for_all: true, is_filter: true) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
before do |
|
|
|
|
allow(User).to receive(:current).and_return(user_1) |
|
|
|
|
|
|
|
|
|
wp_p1[0].type.custom_fields << custom_field |
|
|
|
|
project_1.work_package_custom_fields << custom_field |
|
|
|
|
|
|
|
|
|
wp_p1[0].update_attribute(:"custom_field_#{custom_field.id}", 42) |
|
|
|
|
wp_p1[0].save |
|
|
|
|
wp_p1[1].update_attribute(:"custom_field_#{custom_field.id}", 42) |
|
|
|
|
wp_p1[1].save |
|
|
|
|
|
|
|
|
|
query.project = project_1 |
|
|
|
|
query.group_by = "cf_#{custom_field.id}" |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe '#work_package_count_by_group' do |
|
|
|
|
it 'returns a hash of counts by value' do |
|
|
|
|
expect(query.results.work_package_count_by_group).to eql(42 => 2, nil => 1) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
context 'when grouping by list custom field and filtering for it at the same time' do |
|
|
|
|
let!(:custom_field) do |
|
|
|
|
FactoryBot.create(:list_wp_custom_field, |
|
|
|
|
is_for_all: true, |
|
|
|
|
is_filter: true, |
|
|
|
|
multi_value: true).tap do |cf| |
|
|
|
|
work_package1.type.custom_fields << cf |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
let(:first_value) do |
|
|
|
|
custom_field.custom_options.first |
|
|
|
|
end |
|
|
|
|
let(:last_value) do |
|
|
|
|
custom_field.custom_options.last |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
let(:work_package1) do |
|
|
|
|
FactoryBot.create(:work_package, |
|
|
|
|
project: project_1) |
|
|
|
|
end |
|
|
|
|
let(:work_package2) do |
|
|
|
|
FactoryBot.create(:work_package, |
|
|
|
|
type: work_package1.type, |
|
|
|
|
project: project_1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
before do |
|
|
|
|
allow(User).to receive(:current).and_return(user_1) |
|
|
|
|
|
|
|
|
|
query.group_by = "cf_#{custom_field.id}" |
|
|
|
|
query.project = project_1 |
|
|
|
|
|
|
|
|
|
work_package1.send(:"custom_field_#{custom_field.id}=", first_value) |
|
|
|
|
work_package1.save! |
|
|
|
|
work_package2.send(:"custom_field_#{custom_field.id}=", [first_value, |
|
|
|
|
last_value]) |
|
|
|
|
work_package2.save! |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe '#work_package_count_by_group' do |
|
|
|
|
it 'yields no error but rather returns the result' do |
|
|
|
|
expect { query.results.work_package_count_by_group }.not_to raise_error |
|
|
|
|
|
|
|
|
|
group_count = query.results.work_package_count_by_group |
|
|
|
|
expected_groups = [[first_value], [first_value, last_value]] |
|
|
|
|
|
|
|
|
|
group_count.each do |key, count| |
|
|
|
|
expect(count).to eql 1 |
|
|
|
|
expect(expected_groups.any? { |group| group & key == key & group }).to be_truthy |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|