Signed-off-by: Alex Coles <alex@alexbcoles.com>pull/6827/head
commit
c851461a74
@ -0,0 +1,75 @@ |
||||
#-- copyright |
||||
# OpenProject Reporting Plugin |
||||
# |
||||
# Copyright (C) 2010 - 2014 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# version 3. |
||||
# |
||||
# 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. |
||||
#++ |
||||
|
||||
module CostQuery::Cache |
||||
class << self |
||||
|
||||
def check |
||||
reset! if reset_required? |
||||
end |
||||
|
||||
def reset! |
||||
update_reset_on |
||||
|
||||
CostQuery::Filter.reset! |
||||
CostQuery::Filter::CustomFieldEntries.reset! |
||||
CostQuery::GroupBy.reset! |
||||
CostQuery::GroupBy::CustomFieldEntries.reset! |
||||
end |
||||
|
||||
protected |
||||
|
||||
attr_accessor :latest_custom_field_change, |
||||
:custom_field_count |
||||
|
||||
def invalid? |
||||
changed_on = fetch_latest_custom_field_change |
||||
field_count = fetch_current_custom_field_count |
||||
|
||||
latest_custom_field_change != changed_on || |
||||
custom_field_count != field_count |
||||
end |
||||
|
||||
def update_reset_on |
||||
return if caching_disabled? |
||||
|
||||
self.latest_custom_field_change = fetch_latest_custom_field_change |
||||
self.custom_field_count = fetch_current_custom_field_count |
||||
end |
||||
|
||||
def fetch_latest_custom_field_change |
||||
WorkPackageCustomField.maximum(:updated_at) |
||||
end |
||||
|
||||
def fetch_current_custom_field_count |
||||
WorkPackageCustomField.count |
||||
end |
||||
|
||||
def caching_disabled? |
||||
!OpenProject::Configuration.cost_reporting_cache_filter_classes |
||||
end |
||||
|
||||
def reset_required? |
||||
caching_disabled? || invalid? |
||||
end |
||||
end |
||||
|
||||
# initialize to 0 to avoid forced cache reset on first request |
||||
self.custom_field_count = 0 |
||||
end |
@ -0,0 +1,6 @@ |
||||
if Rails.env.test? |
||||
require 'rspec/example_disabler' |
||||
RSpec::ExampleDisabler.disable_example('Top menu items as a user with permissions displays all options', 'plugin openproject-reporting removes the menu item') |
||||
RSpec::ExampleDisabler.disable_example('Top menu items as an admin visits the time sheet page', 'plugin openproject-reporting removes the menu item') |
||||
RSpec::ExampleDisabler.disable_example('Top menu items as an admin displays all items', 'plugin openproject-reporting removes the menu item') |
||||
end |
@ -0,0 +1,47 @@ |
||||
#-- copyright |
||||
# OpenProject Reporting Plugin |
||||
# |
||||
# Copyright (C) 2010 - 2014 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# version 3. |
||||
# |
||||
# 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. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
|
||||
describe 'OpenProject::Configuration' do |
||||
context '.cost_reporting_cache_filter_classes' do |
||||
before do |
||||
# This prevents the values from the actual configuration file to influence |
||||
# the test outcome. |
||||
# |
||||
# TODO: I propose to port this over to the core to always prevent this for specs. |
||||
OpenProject::Configuration.load(file: 'bogus') |
||||
end |
||||
|
||||
after do |
||||
# resetting for now to avoid braking specs, who by now rely on having the file read. |
||||
OpenProject::Configuration.load |
||||
end |
||||
|
||||
it 'is a true by default via the method' do |
||||
expect(OpenProject::Configuration.cost_reporting_cache_filter_classes).to be_truthy |
||||
end |
||||
|
||||
|
||||
it 'is true by default via the hash' do |
||||
expect(OpenProject::Configuration['cost_reporting_cache_filter_classes']).to be_truthy |
||||
end |
||||
|
||||
end |
||||
end |
@ -0,0 +1,124 @@ |
||||
#-- copyright |
||||
# OpenProject Reporting Plugin |
||||
# |
||||
# Copyright (C) 2010 - 2014 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# version 3. |
||||
# |
||||
# 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. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
require File.join(File.dirname(__FILE__), '..', '..', 'support', 'configuration_helper') |
||||
|
||||
describe CostQuery::Cache do |
||||
include OpenProject::Reporting::SpecHelper::ConfigurationHelper |
||||
|
||||
def all_caches |
||||
[ CostQuery::GroupBy::CustomFieldEntries, |
||||
CostQuery::GroupBy, |
||||
CostQuery::Filter::CustomFieldEntries, |
||||
CostQuery::Filter ] |
||||
end |
||||
|
||||
def expect_reset_on_caches |
||||
all_caches.each do |klass| |
||||
expect(klass).to receive(:reset!) |
||||
end |
||||
end |
||||
|
||||
def expect_no_reset_on_caches |
||||
all_caches.each do |klass| |
||||
expect(klass).to_not receive(:reset!) |
||||
end |
||||
end |
||||
|
||||
def reset_cache_keys |
||||
# resetting internal caching keys to avoid dependencies with other specs |
||||
described_class.send(:latest_custom_field_change=, nil) |
||||
described_class.send(:custom_field_count=, 0) |
||||
end |
||||
|
||||
def custom_fields_exist |
||||
allow(WorkPackageCustomField).to receive(:maximum).and_return(Time.now) |
||||
allow(WorkPackageCustomField).to receive(:count).and_return(23) |
||||
end |
||||
|
||||
def no_custom_fields_exist |
||||
allow(WorkPackageCustomField).to receive(:maximum).and_return(nil) |
||||
allow(WorkPackageCustomField).to receive(:count).and_return(0) |
||||
end |
||||
|
||||
before do |
||||
reset_cache_keys |
||||
end |
||||
|
||||
after do |
||||
reset_cache_keys |
||||
end |
||||
|
||||
describe '.check' do |
||||
|
||||
context 'with cache_classes configuration enabled' do |
||||
before do |
||||
mock_cache_classes_setting_with(true) |
||||
end |
||||
|
||||
it 'resets the caches on filters and group by' do |
||||
custom_fields_exist |
||||
expect_reset_on_caches |
||||
|
||||
described_class.check |
||||
end |
||||
|
||||
it 'stores when the last update was made and does not reset again if nothing changed' do |
||||
custom_fields_exist |
||||
expect_reset_on_caches |
||||
|
||||
described_class.check |
||||
|
||||
expect_no_reset_on_caches |
||||
|
||||
described_class.check |
||||
end |
||||
|
||||
it 'does reset the cache if last CustomField is removed' do |
||||
custom_fields_exist |
||||
expect_reset_on_caches |
||||
|
||||
described_class.check |
||||
|
||||
no_custom_fields_exist |
||||
expect_reset_on_caches |
||||
|
||||
described_class.check |
||||
end |
||||
end |
||||
|
||||
context 'with_cache_classes configuration disabled' do |
||||
before do |
||||
mock_cache_classes_setting_with(false) |
||||
end |
||||
|
||||
it 'resets the cache again even if nothing changed' do |
||||
custom_fields_exist |
||||
expect_reset_on_caches |
||||
|
||||
described_class.check |
||||
|
||||
expect_reset_on_caches |
||||
|
||||
described_class.check |
||||
end |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,129 @@ |
||||
#-- copyright |
||||
# OpenProject Reporting Plugin |
||||
# |
||||
# Copyright (C) 2010 - 2014 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# version 3. |
||||
# |
||||
# 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. |
||||
#++ |
||||
|
||||
require 'spec_helper' |
||||
require File.join(File.dirname(__FILE__), '..', 'support', 'custom_field_filter') |
||||
require File.join(File.dirname(__FILE__), '..', 'support', 'configuration_helper') |
||||
|
||||
describe 'Custom field filter and group by caching', type: :request do |
||||
include OpenProject::Reporting::SpecHelper::CustomFieldFilterHelper |
||||
include OpenProject::Reporting::SpecHelper::ConfigurationHelper |
||||
|
||||
let(:project) { FactoryGirl.create(:valid_project) } |
||||
let(:user) { FactoryGirl.create(:admin) } |
||||
let(:custom_field) { FactoryGirl.build(:work_package_custom_field) } |
||||
let(:custom_field2) { FactoryGirl.build(:work_package_custom_field) } |
||||
|
||||
before do |
||||
allow(User).to receive(:current).and_return(user) |
||||
|
||||
custom_field.save! |
||||
end |
||||
|
||||
def expect_group_by_all_to_include(custom_field) |
||||
expect(CostQuery::GroupBy.all).to include(group_by_class_name_string(custom_field).constantize) |
||||
end |
||||
|
||||
def expect_filter_all_to_include(custom_field) |
||||
expect(CostQuery::Filter.all).to include(filter_class_name_string(custom_field).constantize) |
||||
end |
||||
|
||||
def expect_group_by_all_to_not_exist(custom_field) |
||||
# can not check for whether the element is included in CostQuery::GroupBy.all if it does not exist |
||||
expect { group_by_class_name_string(custom_field).constantize }.to raise_error NameError |
||||
end |
||||
|
||||
def expect_filter_all_to_not_exist(custom_field) |
||||
# can not check for whether the element is included in CostQuery::Filter.all if it does not exist |
||||
expect { filter_class_name_string(custom_field).constantize }.to raise_error NameError |
||||
end |
||||
|
||||
def visit_cost_reports_index |
||||
get "projects/#{project.id}/cost_reports" |
||||
end |
||||
|
||||
it 'removes the filter/group_by if the custom field is removed' do |
||||
custom_field2.save! |
||||
|
||||
visit_cost_reports_index |
||||
|
||||
expect_group_by_all_to_include(custom_field) |
||||
expect_group_by_all_to_include(custom_field2) |
||||
|
||||
expect_filter_all_to_include(custom_field) |
||||
expect_filter_all_to_include(custom_field2) |
||||
|
||||
custom_field2.destroy |
||||
|
||||
visit_cost_reports_index |
||||
|
||||
expect_group_by_all_to_include(custom_field) |
||||
expect_group_by_all_to_not_exist(custom_field2) |
||||
|
||||
expect_filter_all_to_include(custom_field) |
||||
expect_filter_all_to_not_exist(custom_field2) |
||||
end |
||||
|
||||
it 'removes the filter/group_by if the last custom field is removed' do |
||||
visit_cost_reports_index |
||||
|
||||
expect_group_by_all_to_include(custom_field) |
||||
expect_filter_all_to_include(custom_field) |
||||
|
||||
custom_field.destroy |
||||
|
||||
visit_cost_reports_index |
||||
|
||||
expect_group_by_all_to_not_exist(custom_field) |
||||
expect_filter_all_to_not_exist(custom_field) |
||||
end |
||||
|
||||
it 'allows for changing the db table between requests if no caching is done' do |
||||
old_table_name = WorkPackageCustomField.table_name |
||||
new_table_name = 'custom_fields_clone' |
||||
new_id = custom_field.id + 1 |
||||
|
||||
begin |
||||
mock_cache_classes_setting_with(false) |
||||
|
||||
visit_cost_reports_index |
||||
|
||||
expect_group_by_all_to_include(custom_field) |
||||
expect_filter_all_to_include(custom_field) |
||||
|
||||
ActiveRecord::Base.connection.execute("CREATE TABLE #{new_table_name} AS SELECT * from custom_fields;") |
||||
ActiveRecord::Base.connection.execute("UPDATE #{new_table_name} SET id = #{new_id} WHERE id = #{custom_field.id};") |
||||
CustomField::Translation.where(custom_field_id: custom_field.id).update_all(custom_field_id: new_id) |
||||
|
||||
WorkPackageCustomField.table_name = new_table_name |
||||
|
||||
visit_cost_reports_index |
||||
|
||||
expect_group_by_all_to_not_exist(custom_field) |
||||
expect_filter_all_to_not_exist(custom_field) |
||||
|
||||
expect_group_by_all_to_include(new_id) |
||||
expect_filter_all_to_include(new_id) |
||||
|
||||
ensure |
||||
WorkPackageCustomField.table_name = old_table_name |
||||
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS #{new_table_name}") |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,31 @@ |
||||
#-- copyright |
||||
# OpenProject Reporting Plugin |
||||
# |
||||
# Copyright (C) 2010 - 2014 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# version 3. |
||||
# |
||||
# 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. |
||||
#++ |
||||
|
||||
module OpenProject::Reporting::SpecHelper |
||||
module ConfigurationHelper |
||||
def mock_cache_classes_setting_with(value) |
||||
allow(OpenProject::Configuration).to receive(:[]).and_call_original |
||||
allow(OpenProject::Configuration).to receive(:[]) |
||||
.with('cost_reporting_cache_filter_classes') |
||||
.and_return(value) |
||||
allow(OpenProject::Configuration).to receive(:cost_reporting_cache_filter_classes) |
||||
.and_return(value) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,34 @@ |
||||
#-- copyright |
||||
# OpenProject Reporting Plugin |
||||
# |
||||
# Copyright (C) 2010 - 2014 the OpenProject Foundation (OPF) |
||||
# |
||||
# This program is free software; you can redistribute it and/or |
||||
# modify it under the terms of the GNU General Public License |
||||
# version 3. |
||||
# |
||||
# 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. |
||||
#++ |
||||
|
||||
module OpenProject::Reporting::SpecHelper |
||||
module CustomFieldFilterHelper |
||||
def group_by_class_name_string(custom_field) |
||||
id = custom_field.is_a?(ActiveRecord::Base) ? custom_field.id : custom_field |
||||
|
||||
"CostQuery::GroupBy::CustomField#{id}" |
||||
end |
||||
|
||||
def filter_class_name_string(custom_field) |
||||
id = custom_field.is_a?(ActiveRecord::Base) ? custom_field.id : custom_field |
||||
|
||||
"CostQuery::Filter::CustomField#{id}" |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue