From d1825631f2389c0cf066791b5d89c1874fede1c7 Mon Sep 17 00:00:00 2001 From: PaulDoyle-DEFRA <97455399+PaulDoyle-DEFRA@users.noreply.github.com> Date: Fri, 6 Oct 2023 11:32:16 +0100 Subject: [PATCH] Fix/previous years (#712) https://eaflood.atlassian.net/browse/RUBY-2554 https://eaflood.atlassian.net/browse/RUBY-2364 https://eaflood.atlassian.net/browse/RUBY-2556 --- .rubocop_todo.yml | 29 ++--- Gemfile.lock | 8 +- app/helpers/pafs_core/projects_helper.rb | 12 +- app/presenters/pafs_core/camc3_presenter.rb | 2 +- .../pafs_core/program_upload_service.rb | 2 +- .../remove_previous_years_service.rb | 29 +++++ .../contributors/coerce/financial_year.rb | 2 + .../spreadsheet/contributors/export.rb | 2 + app/services/pafs_core/spreadsheet_service.rb | 2 +- ...osion_protection_outcomes_summary.html.erb | 1 - .../projects/steps/funding_values.html.erb | 1 - .../shared/_contributor_values_form.html.erb | 1 - config/locales/en.yml | 1 + .../data_migration/remove_previous_years.rb | 50 ++++---- lib/tasks/pafs_core_tasks.rake | 9 +- .../helpers/pafs_core/projects_helper_spec.rb | 6 + .../remove_previous_years_spec.rb | 111 ++++++++++++------ spec/lib/tasks/pafs_core_tasks_spec.rb | 4 +- .../pafs_core/camc3_presenter_spec.rb | 3 + .../remove_previous_years_service_spec.rb | 85 ++++++++++++++ .../flood_protection_outcomes_details_spec.rb | 2 +- 21 files changed, 264 insertions(+), 98 deletions(-) create mode 100644 app/services/pafs_core/remove_previous_years_service.rb create mode 100644 spec/services/pafs_core/remove_previous_years_service_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 8a9b7dbc1..94a4e1e60 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2023-09-26 11:37:04 UTC using RuboCop version 1.56.3. +# on 2023-10-05 14:45:57 UTC using RuboCop version 1.56.4. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -14,14 +14,6 @@ FactoryBot/AssociationStyle: Exclude: - 'spec/factories/projects.rb' -# Offense count: 1 -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle. -# SupportedStyles: final_newline, final_blank_line -Layout/TrailingEmptyLines: - Exclude: - - 'spec/steps/pafs_core/flood_protection_outcomes_details_spec.rb' - # Offense count: 7 # Configuration parameters: AllowComments, AllowEmptyLambdas. Lint/EmptyBlock: @@ -29,14 +21,6 @@ Lint/EmptyBlock: - 'spec/factories/steps.rb' - 'spec/helpers/pafs_core/application_helper_spec.rb' -# Offense count: 3 -# This cop supports safe autocorrection (--autocorrect). -Lint/RedundantCopDisableDirective: - Exclude: - - 'spec/steps/pafs_core/coastal_erosion_protection_outcomes_detail_spec.rb' - - 'spec/steps/pafs_core/flood_protection_outcomes_details_spec.rb' - - 'spec/steps/pafs_core/funding_sources_and_spending_detail_spec.rb' - # Offense count: 18 # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. Metrics/AbcSize: @@ -52,7 +36,7 @@ Metrics/ClassLength: Metrics/CyclomaticComplexity: Max: 17 -# Offense count: 31 +# Offense count: 33 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: Max: 234 @@ -67,7 +51,7 @@ Metrics/ModuleLength: Metrics/PerceivedComplexity: Max: 17 -# Offense count: 662 +# Offense count: 661 # Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns. # SupportedStyles: snake_case, normalcase, non_integer # AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64 @@ -118,11 +102,11 @@ RSpec/MessageSpies: - 'spec/services/pafs_core/file_storage_service_spec.rb' - 'spec/steps/pafs_core/funding_calculator_step_spec.rb' -# Offense count: 213 +# Offense count: 214 RSpec/MultipleExpectations: Max: 9 -# Offense count: 66 +# Offense count: 68 # Configuration parameters: AllowSubject. RSpec/MultipleMemoizedHelpers: Max: 19 @@ -223,7 +207,7 @@ Rails/InverseOf: - 'app/models/pafs_core/funding_value.rb' - 'app/models/pafs_core/user.rb' -# Offense count: 20 +# Offense count: 21 # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: Include. # Include: app/**/*.rb, config/**/*.rb, db/**/*.rb, lib/**/*.rb @@ -233,6 +217,7 @@ Rails/Output: - 'lib/pafs_core/data_migration/export_to_pol.rb' - 'lib/pafs_core/data_migration/generate_funding_contributor_fcerm.rb' - 'lib/pafs_core/data_migration/move_funding_sources.rb' + - 'lib/pafs_core/data_migration/remove_previous_years.rb' - 'lib/pafs_core/data_migration/update_areas.rb' - 'lib/pafs_core/data_migration/update_projects.rb' - 'lib/pafs_core/files.rb' diff --git a/Gemfile.lock b/Gemfile.lock index 5330717bb..173b5a821 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -266,7 +266,7 @@ GEM faraday (>= 1, < 3) sawyer (~> 0.9) parallel (1.23.0) - parser (3.2.2.3) + parser (3.2.2.4) ast (~> 2.4.1) racc pg (1.5.4) @@ -346,7 +346,7 @@ GEM rspec-mocks (~> 3.12) rspec-support (~> 3.12) rspec-support (3.12.1) - rubocop (1.56.3) + rubocop (1.56.4) base64 (~> 0.1.1) json (~> 2.3) language_server-protocol (>= 3.17.0) @@ -364,7 +364,7 @@ GEM rubocop (~> 1.41) rubocop-factory_bot (2.24.0) rubocop (~> 1.33) - rubocop-rails (2.21.1) + rubocop-rails (2.21.2) activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.33.0, < 2.0) @@ -409,7 +409,7 @@ GEM unf (0.1.4) unf_ext unf_ext (0.0.8.2) - unicode-display_width (2.4.2) + unicode-display_width (2.5.0) vcr (6.2.0) webmock (3.19.1) addressable (>= 2.8.0) diff --git a/app/helpers/pafs_core/projects_helper.rb b/app/helpers/pafs_core/projects_helper.rb index f47f7be4a..fc7d5cfd9 100644 --- a/app/helpers/pafs_core/projects_helper.rb +++ b/app/helpers/pafs_core/projects_helper.rb @@ -104,11 +104,19 @@ def str_year(year) end def formatted_financial_year(year) - "#{year} to #{year + 1}" + if year.negative? + t("previous_years_label") + else + "#{year} to #{year + 1}" + end end def formatted_financial_month_and_year(year) - "April #{year} to March #{year + 1}" + if year.negative? + t("previous_years_label") + else + "April #{year} to March #{year + 1}" + end end def urgency_flag(project) diff --git a/app/presenters/pafs_core/camc3_presenter.rb b/app/presenters/pafs_core/camc3_presenter.rb index 4d307ac61..983ea077f 100644 --- a/app/presenters/pafs_core/camc3_presenter.rb +++ b/app/presenters/pafs_core/camc3_presenter.rb @@ -207,7 +207,7 @@ def attributes attr_accessor :project, :fcerm1_presenter, :pf_calculator_presenter, :fcerm1_mapper, :funding_sources_mapper def financial_years - Time.zone.today.uk_financial_year..project.project_end_financial_year + [-1] + (2015..project.project_end_financial_year).to_a end end end diff --git a/app/services/pafs_core/program_upload_service.rb b/app/services/pafs_core/program_upload_service.rb index 649bedb26..882a608d7 100644 --- a/app/services/pafs_core/program_upload_service.rb +++ b/app/services/pafs_core/program_upload_service.rb @@ -145,7 +145,7 @@ def build_project_from_row(row) if range # handle ranges start_column = column - years = (Time.zone.today.uk_financial_year..2027) + years = [-1].concat((2015..2027).to_a) values = [] years.each_with_index do |_year, i| cell = row[start_column + i] diff --git a/app/services/pafs_core/remove_previous_years_service.rb b/app/services/pafs_core/remove_previous_years_service.rb new file mode 100644 index 000000000..47a754743 --- /dev/null +++ b/app/services/pafs_core/remove_previous_years_service.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module PafsCore + class RemovePreviousYearsService + attr_reader :project, :current_year + + def initialize(project) + @project = project + @current_year = Time.zone.today.uk_financial_year + end + + def run + return if project.state.state == "submitted" + + ActiveRecord::Base.transaction do + prune_years(:funding_values) + prune_years(:flood_protection_outcomes) + prune_years(:flood_protection2040_outcomes) + prune_years(:coastal_erosion_protection_outcomes) + end + end + + private + + def prune_years(annual_attribute) + project.send(annual_attribute).where("financial_year < ?", current_year).destroy_all + end + end +end diff --git a/app/services/pafs_core/spreadsheet/contributors/coerce/financial_year.rb b/app/services/pafs_core/spreadsheet/contributors/coerce/financial_year.rb index da989e1d6..7bee44534 100644 --- a/app/services/pafs_core/spreadsheet/contributors/coerce/financial_year.rb +++ b/app/services/pafs_core/spreadsheet/contributors/coerce/financial_year.rb @@ -6,6 +6,8 @@ module Contributors module Coerce class FinancialYear < Base def perform + return -1 if value == "Previous years" + raise("unknown year") if matches.nil? matches[0] diff --git a/app/services/pafs_core/spreadsheet/contributors/export.rb b/app/services/pafs_core/spreadsheet/contributors/export.rb index dcee3bfea..394a79e52 100644 --- a/app/services/pafs_core/spreadsheet/contributors/export.rb +++ b/app/services/pafs_core/spreadsheet/contributors/export.rb @@ -34,6 +34,8 @@ def generate private def financial_year(value) + return "Previous years" if value == -1 + "#{value} - #{value + 1}" end diff --git a/app/services/pafs_core/spreadsheet_service.rb b/app/services/pafs_core/spreadsheet_service.rb index 05e98d916..998719c66 100644 --- a/app/services/pafs_core/spreadsheet_service.rb +++ b/app/services/pafs_core/spreadsheet_service.rb @@ -131,7 +131,7 @@ def add_project_to_sheet(sheet, project, row_no) if range start_column = column_index(col[:column]) - years = (Time.zone.today.uk_financial_year..2027) + years = [-1].concat((2015..2027).to_a) years.each_with_index do |year, i| value = use_value ? project.send(name, year) : 0 sheet[row_no][start_column + i].change_contents(value) diff --git a/app/views/pafs_core/projects/steps/coastal_erosion_protection_outcomes_summary.html.erb b/app/views/pafs_core/projects/steps/coastal_erosion_protection_outcomes_summary.html.erb index 26c273e13..28012a4be 100644 --- a/app/views/pafs_core/projects/steps/coastal_erosion_protection_outcomes_summary.html.erb +++ b/app/views/pafs_core/projects/steps/coastal_erosion_protection_outcomes_summary.html.erb @@ -28,7 +28,6 @@ <% @project.current_coastal_erosion_protection_outcomes.each do |cepo| %> - <% next if cepo.financial_year == -1 %> <%= formatted_financial_year(cepo.financial_year) %> diff --git a/app/views/pafs_core/projects/steps/funding_values.html.erb b/app/views/pafs_core/projects/steps/funding_values.html.erb index 1ec42da10..210b5c7b4 100644 --- a/app/views/pafs_core/projects/steps/funding_values.html.erb +++ b/app/views/pafs_core/projects/steps/funding_values.html.erb @@ -26,7 +26,6 @@ <%= f.fields_for :funding_values, @project.current_funding_values do |builder| %> - <% next if builder.object.financial_year == -1 %> <%= builder.hidden_field :financial_year %> <%= builder.hidden_field :id %> diff --git a/app/views/shared/_contributor_values_form.html.erb b/app/views/shared/_contributor_values_form.html.erb index 7f2650ba2..b649a98a4 100644 --- a/app/views/shared/_contributor_values_form.html.erb +++ b/app/views/shared/_contributor_values_form.html.erb @@ -36,7 +36,6 @@ <% contributors.each do |contributor| %> <%= f.fields_for :funding_contributors, contributor do |builder| %> - <% next if builder.object.funding_value.financial_year == -1 %> <%= formatted_financial_year(builder.object.funding_value.financial_year) %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 0bd050942..642d86762 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -21,6 +21,7 @@ en: back_to_home_label: "Back to home page" back_to_start_label: "Back to start page" overview_link: "Project overview" + previous_years_label: "Previous years" complete_project_label: "Submit" submit_project_label: "Submit" unlock_project_label: "Revert to draft" diff --git a/lib/pafs_core/data_migration/remove_previous_years.rb b/lib/pafs_core/data_migration/remove_previous_years.rb index 62fda8419..9b3afeea7 100644 --- a/lib/pafs_core/data_migration/remove_previous_years.rb +++ b/lib/pafs_core/data_migration/remove_previous_years.rb @@ -1,35 +1,45 @@ # frozen_string_literal: true -require "csv" - module PafsCore module DataMigration class RemovePreviousYears - attr_reader :project, :current_year - def self.perform_all - PafsCore::Project.find_each do |project| - new(project).perform - end - end + def self.perform(max_projects = 100) + processed_project_count = 0 + + # The funding_values cleanup for a given project will also clear out any previous + # years data for the other annualised attributes. We run the service for each of + # those attributes to handle any cases where previous years funding_values were not + # present but previous years data was present for another attribute. + %i[funding_values + flood_protection_outcomes + flood_protection2040_outcomes + coastal_erosion_protection_outcomes].each do |attribute| + projects = qualifying_projects(attribute, max_projects - processed_project_count) + puts "Processing #{projects.length}/#{max_projects} projects based on #{attribute}" unless Rails.env.test? + projects.each do |project| + PafsCore::RemovePreviousYearsService.new(project).run + end - def initialize(project) - @project = project + processed_project_count += projects.length + break if processed_project_count >= max_projects + end end - def perform - @current_year = Time.zone.today.uk_financial_year + def self.qualifying_projects(attribute, project_limit) + sanitized_attribute = ActiveRecord::Base.sanitize_sql(attribute) - ActiveRecord::Base.transaction do - prune_years(:funding_values) - prune_years(:flood_protection_outcomes) - prune_years(:flood_protection2040_outcomes) - prune_years(:coastal_erosion_protection_outcomes) - end + PafsCore::Project + .joins(:state) + .where.not(pafs_core_states: { state: :submitted }) + .joins(attribute) + .distinct + .where(["pafs_core_#{sanitized_attribute}.financial_year < ?", current_year]) + .limit(project_limit) end - def prune_years(annual_attribute) - @project.send(annual_attribute).where("financial_year < ?", current_year).destroy_all + def self.current_year + @current_year = Time.zone.today.uk_financial_year end end end diff --git a/lib/tasks/pafs_core_tasks.rake b/lib/tasks/pafs_core_tasks.rake index 86a613c20..3015a198a 100644 --- a/lib/tasks/pafs_core_tasks.rake +++ b/lib/tasks/pafs_core_tasks.rake @@ -40,8 +40,13 @@ namespace :pafs do end desc "Remove data for financial years before the current financial year" - task remove_previous_years: :environment do - PafsCore::DataMigration::RemovePreviousYears.perform_all + task :remove_previous_years, [:max_projects] => :environment do |_t, args| + project_limit = if args[:max_projects].present? + args[:max_projects].to_i + else + ENV.fetch("REMOVE_PREVIOUS_YEARS_MAX_PROJECTS", 100).to_i + end + PafsCore::DataMigration::RemovePreviousYears.perform(project_limit) end end # rubocop:enable Metrics/BlockLength diff --git a/spec/helpers/pafs_core/projects_helper_spec.rb b/spec/helpers/pafs_core/projects_helper_spec.rb index 331f3de95..b3ff02f2a 100644 --- a/spec/helpers/pafs_core/projects_helper_spec.rb +++ b/spec/helpers/pafs_core/projects_helper_spec.rb @@ -35,6 +35,12 @@ module PafsCore end describe "#formatted_financial_year" do + context "when given a year less than zero" do + it "returns the string 'Previous years'" do + expect(helper.formatted_financial_year(-1)).to eq "Previous years" + end + end + it "returns the financial year range as a string" do expect(helper.formatted_financial_year(2016)).to eq "2016 to 2017" end diff --git a/spec/lib/pafs_core/data_migration/remove_previous_years_spec.rb b/spec/lib/pafs_core/data_migration/remove_previous_years_spec.rb index 6e31f3d4c..a6aca0500 100644 --- a/spec/lib/pafs_core/data_migration/remove_previous_years_spec.rb +++ b/spec/lib/pafs_core/data_migration/remove_previous_years_spec.rb @@ -5,61 +5,94 @@ RSpec.describe PafsCore::DataMigration::RemovePreviousYears do describe "#perform_all" do - let!(:project_1) { create(:project) } - let!(:project_2) { create(:project) } - - before { allow(described_class).to receive(:new).and_call_original } - - it "processes each project" do - described_class.perform_all - - expect(described_class).to have_received(:new).with(project_1) - expect(described_class).to have_received(:new).with(project_2) + let(:projects_with_previous_years_data) do + create_list(:project, 5, funding_values: [], + flood_protection_outcomes: [], + flood_protection2040_outcomes: [], + coastal_erosion_protection_outcomes: []) + end + let(:projects_without_previous_years_data) do + create_list(:project, 2, funding_values: [], + flood_protection_outcomes: [], + flood_protection2040_outcomes: [], + coastal_erosion_protection_outcomes: []) end - end - - describe "#perform" do - let(:project) { create(:project) } - let(:step) { create(:funding_sources_step) } let(:current_year) { Time.zone.today.uk_financial_year } let(:data_start_year) { current_year - 2 } let(:data_end_year) { current_year + 2 } before do - (data_start_year..data_end_year).each do |year| - project.funding_values << - create(:funding_value, financial_year: year) - project.flood_protection_outcomes << - create(:flood_protection_outcomes, financial_year: year) - project.flood_protection2040_outcomes << - create(:flood_protection2040_outcomes, financial_year: year) - project.coastal_erosion_protection_outcomes << - create(:coastal_erosion_protection_outcomes, financial_year: year) + projects_with_previous_years_data.each do |project| + (data_start_year..data_end_year).each do |year| + add_financial_year_data(project, year) + end + end + projects_without_previous_years_data.each do |project| + (current_year..data_end_year - 1).each do |year| + add_financial_year_data(project, year) + end + end + + allow(described_class).to receive(:new).and_call_original + allow(PafsCore::RemovePreviousYearsService).to receive(:new).and_call_original + end + + context "with a project limit greater than the number of qualifying projects" do + + it "processes all projects with previous years data" do + described_class.perform(10) + + expect(PafsCore::RemovePreviousYearsService).to have_received(:new) + .exactly(projects_with_previous_years_data.length).times end + end + + context "with a project limit less than the number of qualifying projects" do + + it "processes only the specified number of projects" do + described_class.perform(3) - described_class.new(project).perform + expect(PafsCore::RemovePreviousYearsService).to have_received(:new) + .exactly(3).times + end end - shared_examples "prunes year values correctly" do |attribute| - it "removes #{attribute} values for previous years" do - expect(project.public_send(attribute).pluck(:financial_year)) - .not_to include(current_year - 2, current_year - 1) + context "when a project has outcomes but not funding values for previous years" do + let(:project) { projects_without_previous_years_data.sample } + + before do + project.flood_protection_outcomes << + create(:flood_protection_outcomes, project: project, financial_year: current_year - 1) end - it "does not remove #{attribute} values for the current financial year" do - expect(project.public_send(attribute).pluck(:financial_year)) - .to include(current_year) + it "processes the project" do + described_class.perform + + expect(PafsCore::RemovePreviousYearsService).to have_received(:new).with(project) end + end + + context "when a project is in a submitted state" do + let(:submitted_project) { projects_with_previous_years_data.sample } - it "does not remove #{attribute} values for future financial years" do - expect(project.public_send(attribute).pluck(:financial_year)) - .to include(current_year + 1, current_year + 2) + before { submitted_project.state.update(state: "submitted") } + + it "does not call the service for the submitted project" do + described_class.perform + + expect(PafsCore::RemovePreviousYearsService).not_to have_received(:new).with(submitted_project) end end + end - it_behaves_like "prunes year values correctly", :funding_values - it_behaves_like "prunes year values correctly", :flood_protection_outcomes - it_behaves_like "prunes year values correctly", :flood_protection2040_outcomes - it_behaves_like "prunes year values correctly", :coastal_erosion_protection_outcomes + def add_financial_year_data(project, year) + project.funding_values << + create(:funding_value, project: project, financial_year: year) + project.flood_protection_outcomes << + create(:flood_protection_outcomes, project: project, financial_year: year) + project.flood_protection2040_outcomes << + create(:flood_protection2040_outcomes, project: project, financial_year: year) + project.coastal_erosion_protection_outcomes << + create(:coastal_erosion_protection_outcomes, project: project, financial_year: year) end end diff --git a/spec/lib/tasks/pafs_core_tasks_spec.rb b/spec/lib/tasks/pafs_core_tasks_spec.rb index c9b5e654f..4c92c0d3f 100644 --- a/spec/lib/tasks/pafs_core_tasks_spec.rb +++ b/spec/lib/tasks/pafs_core_tasks_spec.rb @@ -52,11 +52,11 @@ subject(:task) { Rake::Task["pafs:remove_previous_years"] } it "runs without error" do - allow(PafsCore::DataMigration::RemovePreviousYears).to receive(:perform_all) + allow(PafsCore::DataMigration::RemovePreviousYears).to receive(:perform) expect { task.invoke }.not_to raise_error - expect(PafsCore::DataMigration::RemovePreviousYears).to have_received(:perform_all) + expect(PafsCore::DataMigration::RemovePreviousYears).to have_received(:perform) end end end diff --git a/spec/presenters/pafs_core/camc3_presenter_spec.rb b/spec/presenters/pafs_core/camc3_presenter_spec.rb index 53786210e..72cafb4fc 100644 --- a/spec/presenters/pafs_core/camc3_presenter_spec.rb +++ b/spec/presenters/pafs_core/camc3_presenter_spec.rb @@ -22,6 +22,7 @@ end let(:funding_values) do [ + { year: -1, value: 2000 }, { year: 2015, value: 200 }, { year: 2016, value: 250 }, { year: 2017, value: 350 }, @@ -31,6 +32,7 @@ end let(:funding_years) do [ + -1, 2015, 2016, 2017, @@ -41,6 +43,7 @@ end let(:outcome_measurements) do [ + { year: -1, value: 2000 }, { year: 2015, value: 200 }, { year: 2016, value: 250 }, { year: 2017, value: 350 }, diff --git a/spec/services/pafs_core/remove_previous_years_service_spec.rb b/spec/services/pafs_core/remove_previous_years_service_spec.rb new file mode 100644 index 000000000..b214953e5 --- /dev/null +++ b/spec/services/pafs_core/remove_previous_years_service_spec.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe PafsCore::RemovePreviousYearsService do + describe "#run" do + + subject(:service) { described_class.new(project) } + + let(:project) { create(:project) } + let(:current_year) { Time.zone.today.uk_financial_year } + let(:data_start_year) { current_year - 2 } + let(:data_end_year) { current_year + 2 } + + before do + + project.state.update(state: state) + + (data_start_year..data_end_year).each do |year| + project.funding_values << + create(:funding_value, financial_year: year) + project.flood_protection_outcomes << + create(:flood_protection_outcomes, financial_year: year) + project.flood_protection2040_outcomes << + create(:flood_protection2040_outcomes, financial_year: year) + project.coastal_erosion_protection_outcomes << + create(:coastal_erosion_protection_outcomes, financial_year: year) + end + + described_class.new(project).run + end + + shared_examples "removes previous year values" do |attribute| + it "removes #{attribute} values for previous years" do + expect(project.public_send(attribute).pluck(:financial_year)) + .not_to include(current_year - 2, current_year - 1) + end + + it "does not remove #{attribute} values for the current financial year" do + expect(project.public_send(attribute).pluck(:financial_year)) + .to include(current_year) + end + + it "does not remove #{attribute} values for future financial years" do + expect(project.public_send(attribute).pluck(:financial_year)) + .to include(current_year + 1, current_year + 2) + end + end + + shared_examples "removes previous years values for financial year attributes" do + it_behaves_like "removes previous year values", :funding_values + it_behaves_like "removes previous year values", :flood_protection_outcomes + it_behaves_like "removes previous year values", :flood_protection2040_outcomes + it_behaves_like "removes previous year values", :coastal_erosion_protection_outcomes + end + + shared_examples "does not remove previous year values" do |attribute| + it "does not remove #{attribute} values for any year" do + expect(project.public_send(attribute).pluck(:financial_year)) + .to include(current_year - 2, current_year + 2) + end + end + + context "with a submitted project" do + let(:state) { "submitted" } + + it_behaves_like "does not remove previous year values", :funding_values + it_behaves_like "does not remove previous year values", :flood_protection_outcomes + it_behaves_like "does not remove previous year values", :flood_protection2040_outcomes + it_behaves_like "does not remove previous year values", :coastal_erosion_protection_outcomes + end + + context "with a draft project" do + let(:state) { "draft" } + + it_behaves_like "removes previous years values for financial year attributes" + end + + context "with an archived project" do + let(:state) { "archived" } + + it_behaves_like "removes previous years values for financial year attributes" + end + end +end diff --git a/spec/steps/pafs_core/flood_protection_outcomes_details_spec.rb b/spec/steps/pafs_core/flood_protection_outcomes_details_spec.rb index 42e25832a..ad6bf2f54 100644 --- a/spec/steps/pafs_core/flood_protection_outcomes_details_spec.rb +++ b/spec/steps/pafs_core/flood_protection_outcomes_details_spec.rb @@ -32,4 +32,4 @@ .total_households_flood_protected_by_category(:households_protected_from_loss_in_20_percent_most_deprived)).to eq 100 end end -end \ No newline at end of file +end