diff --git a/Gemfile.lock b/Gemfile.lock index 9bf2799a8..4c909d000 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -110,8 +110,8 @@ GEM rbtree3 (~> 0.6) ast (2.4.3) aws-eventstream (1.4.0) - aws-partitions (1.1180.0) - aws-sdk-core (3.236.0) + aws-partitions (1.1186.0) + aws-sdk-core (3.239.0) aws-eventstream (~> 1, >= 1.3.0) aws-partitions (~> 1, >= 1.992.0) aws-sigv4 (~> 1.9) @@ -119,10 +119,10 @@ GEM bigdecimal jmespath (~> 1, >= 1.6.1) logger - aws-sdk-kms (1.116.0) + aws-sdk-kms (1.117.0) aws-sdk-core (~> 3, >= 3.234.0) aws-sigv4 (~> 1.5) - aws-sdk-s3 (1.202.0) + aws-sdk-s3 (1.205.0) aws-sdk-core (~> 3, >= 3.234.0) aws-sdk-kms (~> 1) aws-sigv4 (~> 1.5) @@ -168,9 +168,9 @@ GEM docile (1.4.1) dotenv (3.1.8) drb (2.2.3) - erb (5.1.3) + erb (6.0.0) erubi (1.13.1) - factory_bot (6.5.5) + factory_bot (6.5.6) activesupport (>= 6.1.0) factory_bot_rails (6.5.1) factory_bot (~> 6.5) @@ -183,8 +183,8 @@ GEM logger faraday-http-cache (2.5.1) faraday (>= 0.8) - faraday-net_http (3.4.1) - net-http (>= 0.5.0) + faraday-net_http (3.4.2) + net-http (~> 0.5) faraday-retry (2.3.2) faraday (~> 2.0) github_changelog_generator (1.15.2) @@ -214,7 +214,7 @@ GEM rdoc (>= 4.0.0) reline (>= 0.4.2) jmespath (1.6.2) - json (2.15.2) + json (2.16.0) json_schemer (2.4.0) bigdecimal hana (~> 1.3) @@ -250,10 +250,10 @@ GEM method_source (1.1.0) mini_mime (1.1.5) mini_portile2 (2.8.9) - minitest (5.26.0) + minitest (5.26.2) multi_json (1.17.0) - net-http (0.6.0) - uri + net-http (0.8.0) + uri (>= 0.11.1) net-imap (0.5.12) date net-protocol @@ -342,19 +342,19 @@ GEM psych (>= 4.0.0) tsort regexp_parser (2.11.3) - reline (0.6.2) + reline (0.6.3) io-console (~> 0.5) retriable (3.1.2) rexml (3.4.4) roo (2.10.1) nokogiri (~> 1) rubyzip (>= 1.3.0, < 3.0.0) - rspec-core (3.13.5) + rspec-core (3.13.6) rspec-support (~> 3.13.0) rspec-expectations (3.13.5) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) - rspec-mocks (3.13.5) + rspec-mocks (3.13.7) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) rspec-rails (8.0.2) @@ -379,16 +379,16 @@ GEM rubocop-ast (>= 1.47.1, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 2.4.0, < 4.0) - rubocop-ast (1.47.1) + rubocop-ast (1.48.0) parser (>= 3.3.7.2) prism (~> 1.4) rubocop-capybara (2.22.1) lint_roller (~> 1.1) rubocop (~> 1.72, >= 1.72.1) - rubocop-factory_bot (2.27.1) + rubocop-factory_bot (2.28.0) lint_roller (~> 1.1) rubocop (~> 1.72, >= 1.72.1) - rubocop-rails (2.33.4) + rubocop-rails (2.34.0) activesupport (>= 4.2.0) lint_roller (~> 1.1) rack (>= 1.1) @@ -397,10 +397,10 @@ GEM rubocop-rake (0.7.1) lint_roller (~> 1.1) rubocop (>= 1.72.1) - rubocop-rspec (3.7.0) + rubocop-rspec (3.8.0) lint_roller (~> 1.1) - rubocop (~> 1.72, >= 1.72.1) - rubocop-rspec_rails (2.31.0) + rubocop (~> 1.81) + rubocop-rspec_rails (2.32.0) lint_roller (~> 1.1) rubocop (~> 1.72, >= 1.72.1) rubocop-rspec (~> 3.5) @@ -431,7 +431,7 @@ GEM actionpack (>= 6.1) activesupport (>= 6.1) sprockets (>= 3.0.0) - stringio (3.1.7) + stringio (3.1.8) thor (1.4.0) timecop (0.9.10) timeout (0.4.4) diff --git a/app/controllers/pafs_core/application_controller.rb b/app/controllers/pafs_core/application_controller.rb index b6cca88f4..f726627e1 100644 --- a/app/controllers/pafs_core/application_controller.rb +++ b/app/controllers/pafs_core/application_controller.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module PafsCore - class ApplicationController < ActionController::Base + class ApplicationController < ::ApplicationController include PafsCore::ApplicationHelper include PafsCore::CustomHeaders diff --git a/app/models/pafs_core/area.rb b/app/models/pafs_core/area.rb index 56a10275f..2d7f757c1 100644 --- a/app/models/pafs_core/area.rb +++ b/app/models/pafs_core/area.rb @@ -78,7 +78,7 @@ def ea_parent elsif rma? parent.parent else - raise "Cannot find ea parent for #{name}" + raise PafsCore::AreaParentError, "Cannot find ea parent for #{name}" end end diff --git a/app/models/pafs_core/funding_value.rb b/app/models/pafs_core/funding_value.rb index 8bb8168fd..c3b0a585c 100644 --- a/app/models/pafs_core/funding_value.rb +++ b/app/models/pafs_core/funding_value.rb @@ -34,9 +34,11 @@ def self.to_financial_year(year) next if FundingSources::REMOVED_FROM_FUNDING_VALUES.include?(source) define_method("#{source}_total") do - return public_send(source).to_i unless FundingSources::AGGREGATE_SOURCES.include?(source) - - (public_send(source) || []).map(&:amount).compact.sum.to_i + if FundingSources::AGGREGATE_SOURCES.include?(source) + (public_send(source) || []).map(&:amount).compact.sum.to_i + else + public_send(source).to_i + end end end diff --git a/app/presenters/pafs_core/carbon_impact_presenter.rb b/app/presenters/pafs_core/carbon_impact_presenter.rb index f7fb51f56..d286e39e5 100644 --- a/app/presenters/pafs_core/carbon_impact_presenter.rb +++ b/app/presenters/pafs_core/carbon_impact_presenter.rb @@ -255,23 +255,35 @@ def carbon_impact_rates # of 3.5 would be picked up from the 2028/29 field as this is the latest available year. def carbon_impact_rate_for_year(year_string, rate_label) rate_not_present_in_mid_year_data = false + result = nil # check if year present year_present = carbon_impact_rates.find { |rates| rates["Year"] == year_string }.present? carbon_impact_rates.reverse.each do |rates| # year not present in data at all, return the previous year rate available - return rates[rate_label] if !year_present && rates[rate_label].present? + if !year_present && rates[rate_label].present? + result = rates[rate_label] + break + end if rates["Year"] == year_string - return rates[rate_label] if rates[rate_label].present? + if rates[rate_label].present? + result = rates[rate_label] + break + end rate_not_present_in_mid_year_data = true end # year present but rate is missing, return the rate from the previous year available - return rates[rate_label] if rate_not_present_in_mid_year_data && rates[rate_label].present? + if rate_not_present_in_mid_year_data && rates[rate_label].present? + result = rates[rate_label] + break + end end + + result end # Cap DN / Cap Do Nothing Intensity rate for the mid year diff --git a/app/presenters/pafs_core/project_summary_presenter.rb b/app/presenters/pafs_core/project_summary_presenter.rb index bef86c1a6..d31b5c16a 100644 --- a/app/presenters/pafs_core/project_summary_presenter.rb +++ b/app/presenters/pafs_core/project_summary_presenter.rb @@ -97,7 +97,7 @@ def standard_of_protection_step elsif protects_against_coastal_erosion? :standard_of_protection_coastal else - raise "Risks not set prior to standard of protection" + raise PafsCore::RisksNotSetError, "Risks not set prior to standard of protection" end end diff --git a/app/services/pafs_core/area_importer.rb b/app/services/pafs_core/area_importer.rb index 7d0e2ffc4..29db66da3 100644 --- a/app/services/pafs_core/area_importer.rb +++ b/app/services/pafs_core/area_importer.rb @@ -1,9 +1,9 @@ # frozen_string_literal: true +require "csv" + module PafsCore class AreaImporter - - require "csv" HEADERS = ["name", "parent area", "type", @@ -15,7 +15,7 @@ def initialize end def full_import(path_to_file) - abort("Areas already exist") if PafsCore::Area.any? + raise PafsCore::AreaImportError, "Areas already exist" if PafsCore::Area.any? extract_csv_data(path_to_file) @@ -35,7 +35,7 @@ def extract_csv_data(path_to_file) end def import_areas - abort("Headers incorrect.") unless (@areas_to_be_imported.first.keys - HEADERS).empty? + raise PafsCore::AreaImportError, "Headers incorrect." unless (@areas_to_be_imported.first.keys - HEADERS).empty? remove_areas_that_have_already_been_imported diff --git a/app/services/pafs_core/calculator_parser.rb b/app/services/pafs_core/calculator_parser.rb index 80d94bef7..d4b8f611b 100644 --- a/app/services/pafs_core/calculator_parser.rb +++ b/app/services/pafs_core/calculator_parser.rb @@ -19,13 +19,14 @@ class CalculatorParser ].freeze def initialize(calculator, project) - calculator_sheet_name = calculator.sheets.grep(/PF Calculator/i).first || raise("No calculator sheet found") + calculator_sheet_name = calculator.sheets.grep(/PF Calculator/i).first || + raise(PafsCore::CalculatorSheetNotFoundError, "No calculator sheet found") @sheet = calculator.sheet(calculator_sheet_name) @project = project end def self.parse(file, project) - raise "require an xlsx file" unless File.extname(file.path) == ".xlsx" + raise PafsCore::InvalidFileTypeError, "require an xlsx file" unless File.extname(file.path) == ".xlsx" begin calculator = Roo::Excelx.new(file.path) diff --git a/app/services/pafs_core/project_service.rb b/app/services/pafs_core/project_service.rb index e312976f1..554fba91d 100644 --- a/app/services/pafs_core/project_service.rb +++ b/app/services/pafs_core/project_service.rb @@ -194,7 +194,7 @@ def initial_attributes(area_name = nil) end def derive_rfcc_code_from_user(area_name) - raise "User has no RFCC area code" unless user.rfcc_code(area_name) + raise PafsCore::MissingRfccAreaCodeError, "User has no RFCC area code" unless user.rfcc_code(area_name) user.rfcc_code(area_name) end diff --git a/app/services/pafs_core/spreadsheet_service.rb b/app/services/pafs_core/spreadsheet_service.rb index a9f56f7e4..1bd0db22b 100644 --- a/app/services/pafs_core/spreadsheet_service.rb +++ b/app/services/pafs_core/spreadsheet_service.rb @@ -166,7 +166,8 @@ def add_project_to_sheet(sheet, project, row_no) value = use_value ? project.send(col[:field_name]) : 0 sheet[row_no][column_index(col[:column])].change_contents(value) rescue StandardError => e - raise "Boom - Project (#{project.slug}) Row no (#{row_no}) col (#{col}) " \ + raise PafsCore::SpreadsheetProcessingError, + "Boom - Project (#{project.slug}) Row no (#{row_no}) col (#{col}) " \ "field_name (#{col[:field_name]}) value (#{value}): exception #{e}" end end diff --git a/app/steps/pafs_core/benefit_area_file_step.rb b/app/steps/pafs_core/benefit_area_file_step.rb index 217d1b327..7e0762779 100644 --- a/app/steps/pafs_core/benefit_area_file_step.rb +++ b/app/steps/pafs_core/benefit_area_file_step.rb @@ -68,6 +68,8 @@ def step_params(params) end def contins_required_gis_files(uploaded_file) + all_extensions_present = true + Zip::File.open(uploaded_file.tempfile.path) do |zip_file| entries = zip_file.entries.map(&:name) @@ -76,11 +78,12 @@ def contins_required_gis_files(uploaded_file) errors.add(:base, "The selected file must be a zip file, " \ "containing the following mandatory files: dbf. shx. shp. prj.") - return false + all_extensions_present = false + break end end - true + all_extensions_present rescue Zip::Error errors.add(:base, "The selected file must be a zip file, " \ "containing the following mandatory files: dbf. shx. shp. prj.") diff --git a/app/steps/pafs_core/coastal_erosion_protection_outcomes_step.rb b/app/steps/pafs_core/coastal_erosion_protection_outcomes_step.rb index 241d15ead..08e1fb04c 100644 --- a/app/steps/pafs_core/coastal_erosion_protection_outcomes_step.rb +++ b/app/steps/pafs_core/coastal_erosion_protection_outcomes_step.rb @@ -32,19 +32,27 @@ def update(params) private def values? - coastal_erosion_protection_outcomes.each do |outcome| + error_found = false + + outcomes = coastal_erosion_protection_outcomes.each do |outcome| %i[households_at_reduced_risk households_protected_from_loss_in_next_20_years households_protected_from_loss_in_20_percent_most_deprived].each do |attr| if outcome.send(attr).to_i.positive? - return errors.add( - :base, - "In the applicable year(s), tell us how many households moved to a lower flood risk category " \ - "(column A), OR if this does not apply select the checkbox." - ) + error_found = true + break end end + break if error_found end + + return outcomes unless error_found + + errors.add( + :base, + "In the applicable year(s), tell us how many households moved to a lower flood risk category " \ + "(column A), OR if this does not apply select the checkbox." + ) end def values_make_sense diff --git a/app/views/pafs_core/projects/_submit_project.html.erb b/app/views/pafs_core/projects/_submit_project.html.erb index b192c5b99..83864e9cd 100644 --- a/app/views/pafs_core/projects/_submit_project.html.erb +++ b/app/views/pafs_core/projects/_submit_project.html.erb @@ -20,7 +20,7 @@ <% else %>
-

+

EA Projects are now managed in PoL

diff --git a/app/views/pafs_core/projects/steps/natural_flood_risk_measures.html.erb b/app/views/pafs_core/projects/steps/natural_flood_risk_measures.html.erb index 73e591f54..2e54a9c61 100644 --- a/app/views/pafs_core/projects/steps/natural_flood_risk_measures.html.erb +++ b/app/views/pafs_core/projects/steps/natural_flood_risk_measures.html.erb @@ -32,26 +32,15 @@ <% end %>
<%# %> - <% if f.object.other_flood_measures_selected %> - - <% else %> - - <% end %> + + onclick="toggleBoxVisibility()" + onkeydown="toggleBoxVisibility()" + > diff --git a/app/views/pafs_core/summary/_approach.html.erb b/app/views/pafs_core/summary/_approach.html.erb index 578e216f1..e6ef3efb4 100644 --- a/app/views/pafs_core/summary/_approach.html.erb +++ b/app/views/pafs_core/summary/_approach.html.erb @@ -23,7 +23,7 @@
-

<%= project.summary_label(:approach_title) %>

+

<%= project.summary_label(:approach_title) %>

<% unless project.archived? %> diff --git a/app/views/pafs_core/summary/_carbon.html.erb b/app/views/pafs_core/summary/_carbon.html.erb index 8468d7fca..fc189ea03 100644 --- a/app/views/pafs_core/summary/_carbon.html.erb +++ b/app/views/pafs_core/summary/_carbon.html.erb @@ -152,7 +152,7 @@
-

+

<%= project.summary_label(:carbon_title) %>

diff --git a/app/views/pafs_core/summary/_confidence.html.erb b/app/views/pafs_core/summary/_confidence.html.erb index 481bae635..7063a9870 100644 --- a/app/views/pafs_core/summary/_confidence.html.erb +++ b/app/views/pafs_core/summary/_confidence.html.erb @@ -46,7 +46,7 @@
-

+

<%= project.summary_label(:confidence_title) %>

diff --git a/app/views/pafs_core/summary/_earliest_start.html.erb b/app/views/pafs_core/summary/_earliest_start.html.erb index 0c33fe8b7..462f21cd7 100644 --- a/app/views/pafs_core/summary/_earliest_start.html.erb +++ b/app/views/pafs_core/summary/_earliest_start.html.erb @@ -36,7 +36,7 @@
-

Earliest start

+

Earliest start

<% unless project.archived? %> diff --git a/app/views/pafs_core/summary/_environmental_outcomes.html.erb b/app/views/pafs_core/summary/_environmental_outcomes.html.erb index f54e2d4d5..5634a6599 100644 --- a/app/views/pafs_core/summary/_environmental_outcomes.html.erb +++ b/app/views/pafs_core/summary/_environmental_outcomes.html.erb @@ -82,7 +82,7 @@
-

+

<%= project.summary_label(:environmental_outcomes_title) %>

diff --git a/app/views/pafs_core/summary/_funding_calculator.html.erb b/app/views/pafs_core/summary/_funding_calculator.html.erb index 08dc6c4cc..22040ba3d 100644 --- a/app/views/pafs_core/summary/_funding_calculator.html.erb +++ b/app/views/pafs_core/summary/_funding_calculator.html.erb @@ -45,7 +45,7 @@
-

<%= project.summary_label(:funding_calculator_title) %>

+

<%= project.summary_label(:funding_calculator_title) %>

<% unless project.archived? %> diff --git a/app/views/pafs_core/summary/_funding_sources.html.erb b/app/views/pafs_core/summary/_funding_sources.html.erb index 1e51edc07..fad3f2702 100644 --- a/app/views/pafs_core/summary/_funding_sources.html.erb +++ b/app/views/pafs_core/summary/_funding_sources.html.erb @@ -52,7 +52,7 @@
-

Funding sources and spending

+

Funding sources and spending

<% unless project.archived? %> diff --git a/app/views/pafs_core/summary/_funding_values.html.erb b/app/views/pafs_core/summary/_funding_values.html.erb index 6e8448f79..2bd54d6c4 100644 --- a/app/views/pafs_core/summary/_funding_values.html.erb +++ b/app/views/pafs_core/summary/_funding_values.html.erb @@ -28,7 +28,7 @@
-

Funding values

+

Funding values

<% unless project.archived? %> diff --git a/app/views/pafs_core/summary/_key_dates.html.erb b/app/views/pafs_core/summary/_key_dates.html.erb index ae5243022..32294eee5 100644 --- a/app/views/pafs_core/summary/_key_dates.html.erb +++ b/app/views/pafs_core/summary/_key_dates.html.erb @@ -52,7 +52,7 @@
-

Important dates

+

Important dates

<% unless project.archived? %> diff --git a/app/views/pafs_core/summary/_location.html.erb b/app/views/pafs_core/summary/_location.html.erb index fbe3da963..6f2183b0a 100644 --- a/app/views/pafs_core/summary/_location.html.erb +++ b/app/views/pafs_core/summary/_location.html.erb @@ -88,7 +88,7 @@
-

+

<%= project.summary_label(:location_title) %>

diff --git a/app/views/pafs_core/summary/_natural_flood_risk_measures.html.erb b/app/views/pafs_core/summary/_natural_flood_risk_measures.html.erb index 89c78542b..62a9109b5 100644 --- a/app/views/pafs_core/summary/_natural_flood_risk_measures.html.erb +++ b/app/views/pafs_core/summary/_natural_flood_risk_measures.html.erb @@ -68,7 +68,7 @@
-

Natural flood measures

+

Natural flood measures

<% unless project.archived? %> diff --git a/app/views/pafs_core/summary/_risks.html.erb b/app/views/pafs_core/summary/_risks.html.erb index 1cbdde3ed..d01169d09 100644 --- a/app/views/pafs_core/summary/_risks.html.erb +++ b/app/views/pafs_core/summary/_risks.html.erb @@ -66,10 +66,10 @@ <% if !project.draft? %>
- + View households affected by flooding detail -
+
<%= render partial: "pafs_core/summary/flood_protection_outcomes_detail", locals: {project: @project} %>
@@ -109,10 +109,10 @@ <% if !project.draft? %>
- + View households affected by flooding by 2040 detail -
+
<%= render partial: "pafs_core/summary/flood_protection_outcomes2040_detail", locals: {project: @project} %>
@@ -154,10 +154,10 @@ <% if !project.draft? %>
- + View properties affected by coastal erosion detail -
+
<%= render partial: "pafs_core/summary/coastal_erosion_protection_outcomes_detail", locals: {project: @project} %>
@@ -169,7 +169,7 @@
-

Risks and properties benefitting

+

Risks and properties benefitting

<% unless project.archived? %> diff --git a/app/views/pafs_core/summary/_standard_of_protection.html.erb b/app/views/pafs_core/summary/_standard_of_protection.html.erb index b2e7f79e7..e724601d0 100644 --- a/app/views/pafs_core/summary/_standard_of_protection.html.erb +++ b/app/views/pafs_core/summary/_standard_of_protection.html.erb @@ -81,7 +81,7 @@
-

+

<%= project.summary_label(:standard_of_protection_title) %>

diff --git a/app/views/pafs_core/summary/_urgency.html.erb b/app/views/pafs_core/summary/_urgency.html.erb index 7fca203d4..78f6ad732 100644 --- a/app/views/pafs_core/summary/_urgency.html.erb +++ b/app/views/pafs_core/summary/_urgency.html.erb @@ -37,7 +37,7 @@
-

<%= project.summary_label(:urgency_title) %>

+

<%= project.summary_label(:urgency_title) %>

<% unless project.archived? %> diff --git a/lib/pafs_core/calculator_maps/base.rb b/lib/pafs_core/calculator_maps/base.rb index a755114dd..f27f54c2c 100644 --- a/lib/pafs_core/calculator_maps/base.rb +++ b/lib/pafs_core/calculator_maps/base.rb @@ -10,7 +10,7 @@ def initialize(sheet) end def data - raise "Override #data in subclass" + raise NotImplementedError, "Override #data in subclass" end def binary_value?(value) diff --git a/lib/pafs_core/carbon.rb b/lib/pafs_core/carbon.rb index 9c0e0da5b..7d473538b 100644 --- a/lib/pafs_core/carbon.rb +++ b/lib/pafs_core/carbon.rb @@ -17,9 +17,7 @@ def carbon_required_information_present? ] # check required fields - required.each do |field_name| - return false if project.send(field_name).nil? - end + return false unless required.all? { |field_name| project.send(field_name).present? } # check funding sources return false unless PafsCore::ValidationPresenter.new(project).funding_sources_complete? diff --git a/lib/pafs_core/errors.rb b/lib/pafs_core/errors.rb index 20fd3060b..db8484305 100644 --- a/lib/pafs_core/errors.rb +++ b/lib/pafs_core/errors.rb @@ -22,4 +22,25 @@ class FileNotFoundError < RuntimeError class MapServiceError < RuntimeError end + + class AreaImportError < StandardError + end + + class AreaParentError < StandardError + end + + class InvalidFileTypeError < StandardError + end + + class CalculatorSheetNotFoundError < StandardError + end + + class MissingRfccAreaCodeError < StandardError + end + + class RisksNotSetError < StandardError + end + + class SpreadsheetProcessingError < StandardError + end end diff --git a/lib/pafs_core/funding_calculator_version.rb b/lib/pafs_core/funding_calculator_version.rb index 86349f51f..dabf4b3fa 100644 --- a/lib/pafs_core/funding_calculator_version.rb +++ b/lib/pafs_core/funding_calculator_version.rb @@ -21,11 +21,11 @@ def initialize(sheet) end def calculator_version - VERSION_MAP.each do |k, v| - return k if sheet.cell(v[:column], v[:row]).to_s.match(v[:version_text]) + version_entry = VERSION_MAP.find do |_k, v| + sheet.cell(v[:column], v[:row]).to_s.match(v[:version_text]) end - nil + version_entry&.first end end diff --git a/lib/pafs_core/funding_sources.rb b/lib/pafs_core/funding_sources.rb index ec320448e..b77486fa5 100644 --- a/lib/pafs_core/funding_sources.rb +++ b/lib/pafs_core/funding_sources.rb @@ -43,11 +43,11 @@ module FundingSources end def current_funding_values - funding_values.select { |fv| fv.financial_year <= project_end_financial_year }.sort_by(&:financial_year) # if this is a db query we lose inputted data when there are errors # and we send the user back to fix it # It also breaks validating that every column has at least one value overall # funding_values.to_financial_year(project_end_financial_year) + funding_values.select { |fv| fv.financial_year <= project_end_financial_year }.sort_by(&:financial_year) end def selected_funding_sources diff --git a/lib/pafs_core/mapper/funding_calculator_maps/base.rb b/lib/pafs_core/mapper/funding_calculator_maps/base.rb index 82bf1e962..2a2ce11a3 100644 --- a/lib/pafs_core/mapper/funding_calculator_maps/base.rb +++ b/lib/pafs_core/mapper/funding_calculator_maps/base.rb @@ -11,7 +11,7 @@ def initialize(sheet) end def extract_data - raise "Override #extract_data" + raise NotImplementedError, "Override #extract_data" end end end diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index 0deac6d62..aeff99c58 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -11,9 +11,9 @@ require "action_view/railtie" require "sprockets/railtie" # require "rails/test_unit/railtie" +require "pafs_core" Bundler.require(*Rails.groups) -require "pafs_core" module Dummy class Application < Rails::Application diff --git a/spec/dummy/config/boot.rb b/spec/dummy/config/boot.rb index 59459d4ae..2bb3ccd80 100644 --- a/spec/dummy/config/boot.rb +++ b/spec/dummy/config/boot.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true +require "bundler/setup" + # Set up gems listed in the Gemfile. ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../../Gemfile", __dir__) -require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"]) $LOAD_PATH.unshift File.expand_path("../../../lib", __dir__) diff --git a/spec/dummy/config/environments/production.rb b/spec/dummy/config/environments/production.rb index b24adff1f..a7e12e855 100644 --- a/spec/dummy/config/environments/production.rb +++ b/spec/dummy/config/environments/production.rb @@ -24,7 +24,7 @@ # Disable serving static files from the `/public` folder by default since # Apache or NGINX already handles this. - config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present? + config.public_file_server.enabled = ENV.fetch("RAILS_SERVE_STATIC_FILES", nil).present? # Compress JavaScripts and CSS. config.assets.js_compressor = :uglifier diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index e63a3d12f..87267c970 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true Rails.application.routes.draw do + root to: redirect("/pafs") mount PafsCore::Engine => "/pafs" end diff --git a/spec/fixtures/areas/bad_headers.csv b/spec/fixtures/areas/bad_headers.csv new file mode 100644 index 000000000..be98cd27d --- /dev/null +++ b/spec/fixtures/areas/bad_headers.csv @@ -0,0 +1,2 @@ +area_name,"parent area","type","sub type" +England,,Country, diff --git a/spec/lib/tasks/pafs_core_tasks_spec.rb b/spec/lib/tasks/pafs_core_tasks_spec.rb index 96829a8fd..4847aacc9 100644 --- a/spec/lib/tasks/pafs_core_tasks_spec.rb +++ b/spec/lib/tasks/pafs_core_tasks_spec.rb @@ -7,16 +7,15 @@ include_context "rake" - original_stdout = $stdout - before do # suppress noisy outputs during unit test + @original_stdout = $stdout $stdout = StringIO.new create(:full_project) end - after { $stdout = original_stdout } + after { $stdout = @original_stdout } describe "pafs:bulk_export_to_pol" do it { expect { Rake::Task["pafs:bulk_export_to_pol"].invoke }.not_to raise_error } diff --git a/spec/models/pafs_core/area_spec.rb b/spec/models/pafs_core/area_spec.rb index 636a86ca4..03df7555a 100644 --- a/spec/models/pafs_core/area_spec.rb +++ b/spec/models/pafs_core/area_spec.rb @@ -4,27 +4,30 @@ RSpec.describe PafsCore::Area do describe "attributes" do - area_levels = [ - { - level: :country, - parent_id: nil - }, - { - level: :ea_area, - parent_id: 1 - }, - { - level: :pso_area, - parent_id: 1 - }, - { - level: :rma_area, - parent_id: 1 - } - ] - area = area_levels.sample subject { create(area[:level], parent_id: area[:parent_id]) } + let(:area_levels) do + [ + { + level: :country, + parent_id: nil + }, + { + level: :ea_area, + parent_id: 1 + }, + { + level: :pso_area, + parent_id: 1 + }, + { + level: :rma_area, + parent_id: 1 + } + ] + end + let(:area) { area_levels.sample } + it { is_expected.to validate_presence_of :name } it { is_expected.to validate_presence_of :area_type } it { is_expected.to validate_inclusion_of(:area_type).in_array(PafsCore::Area::AREA_TYPES) } diff --git a/spec/presenters/pafs_core/project_summary_presenter_spec.rb b/spec/presenters/pafs_core/project_summary_presenter_spec.rb index d1db39692..c9ba19d9b 100644 --- a/spec/presenters/pafs_core/project_summary_presenter_spec.rb +++ b/spec/presenters/pafs_core/project_summary_presenter_spec.rb @@ -257,7 +257,7 @@ context "when the project does not protect against coastal erosion" do it "raises an error" do expect { subject.standard_of_protection_step } - .to raise_error(RuntimeError, "Risks not set prior to standard of protection") + .to raise_error(PafsCore::RisksNotSetError, "Risks not set prior to standard of protection") end end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index bf86779f8..f505d8f5f 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -4,10 +4,6 @@ require "spec_helper" ENV["RAILS_ENV"] ||= "test" require File.expand_path("dummy/config/environment", __dir__) - -# Prevent database truncation if the environment is production -abort("The Rails environment is running in production mode!") if Rails.env.production? - require "rspec/rails" # require "capybara/rspec" require "factory_bot_rails" @@ -15,6 +11,9 @@ require "vcr" require "webmock/rspec" +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? + Rails.backtrace_cleaner.remove_silencers! # Add additional requires below this line. Rails is not loaded until this point! diff --git a/spec/services/pafs_core/area_importer_spec.rb b/spec/services/pafs_core/area_importer_spec.rb index 56b1244ff..28d440b3a 100644 --- a/spec/services/pafs_core/area_importer_spec.rb +++ b/spec/services/pafs_core/area_importer_spec.rb @@ -11,11 +11,38 @@ expect(areas.size).to eq(4) end - it "does not import faulty data" do - file_path = Rails.root.join("../fixtures/areas/faulty_areas_data.csv") - described_class.new.full_import(file_path) - areas = PafsCore::Area.all - expect(areas.size).to eq(3) + describe "does not import faulty data" do + context "when CSV headers are incorrect" do + let(:file_path) { Rails.root.join("../fixtures/areas/bad_headers.csv") } + + it "raises an AreaImportError" do + expect do + described_class.new.full_import(file_path) + end.to raise_error(PafsCore::AreaImportError, "Headers incorrect.") + end + end + + context "when areas already exist" do + let(:file_path) { Rails.root.join("../fixtures/areas/areas.csv") } + + before { described_class.new.full_import(file_path) } + + it "raises an AreaImportError" do + expect do + described_class.new.full_import(file_path) + end.to raise_error(PafsCore::AreaImportError, "Areas already exist") + end + end + + context "when areas data is faulty" do + let(:file_path) { Rails.root.join("../fixtures/areas/faulty_areas_data.csv") } + + it "does not import faulty data" do + described_class.new.full_import(file_path) + areas = PafsCore::Area.all + expect(areas.size).to eq(3) + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a898c9351..3a6c20bec 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,7 +2,7 @@ # Require and run our simplecov initializer as the very first thing we do. # This is as per its docs https://github.com/colszowka/simplecov#getting-started -require "./spec/support/simplecov" +require_relative "support/simplecov" require "rspec/retry" # This file was generated by the `rails generate rspec:install` command. Conventionally, all