Browse Source

Add archived forms to the form handler

pull/2149/head
Kat 2 years ago
parent
commit
7f4cd7bd99
  1. 3
      app/models/form_handler.rb
  2. 84
      spec/models/form_handler_spec.rb

3
app/models/form_handler.rb

@ -41,6 +41,7 @@ class FormHandler
"current_sales" => Form.new(nil, current_collection_start_year, SALES_SECTIONS, "sales"), "current_sales" => Form.new(nil, current_collection_start_year, SALES_SECTIONS, "sales"),
"previous_sales" => Form.new(nil, previous_collection_start_year, SALES_SECTIONS, "sales"), "previous_sales" => Form.new(nil, previous_collection_start_year, SALES_SECTIONS, "sales"),
"next_sales" => Form.new(nil, next_collection_start_year, SALES_SECTIONS, "sales"), "next_sales" => Form.new(nil, next_collection_start_year, SALES_SECTIONS, "sales"),
"archived_sales" => Form.new(nil, previous_collection_start_year - 1, SALES_SECTIONS, "sales"),
} }
end end
@ -115,7 +116,7 @@ class FormHandler
end end
def form_name_from_start_year(year, type) def form_name_from_start_year(year, type)
form_mappings = { 0 => "current_#{type}", 1 => "previous_#{type}", -1 => "next_#{type}" } form_mappings = { 0 => "current_#{type}", 1 => "previous_#{type}", -1 => "next_#{type}", 2 => "archived_#{type}" }
form_mappings[current_collection_start_year - year] form_mappings[current_collection_start_year - year]
end end

84
spec/models/form_handler_spec.rb

@ -41,21 +41,36 @@ RSpec.describe FormHandler do
all_forms = form_handler.forms all_forms = form_handler.forms
expect(all_forms.keys).not_to include nil expect(all_forms.keys).not_to include nil
end end
it "loads archived forms" do
all_forms = form_handler.forms
expect(all_forms.keys).to include("archived_sales")
expect(all_forms.keys).to include("archived_lettings")
end
end end
end end
describe "Get specific form" do describe "Get specific form" do
let(:now) { Time.utc(2023, 9, 20) }
it "is able to load a current lettings form" do it "is able to load a current lettings form" do
form = form_handler.get_form("current_lettings") form = form_handler.get_form("current_lettings")
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.pages.count).to be_positive expect(form.pages.count).to be_positive
expect(form.name).to eq("2022_2023_lettings") expect(form.name).to eq("2023_2024_lettings")
end end
it "is able to load a previous lettings form" do it "is able to load a previous lettings form" do
form = form_handler.get_form("previous_lettings") form = form_handler.get_form("previous_lettings")
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.pages.count).to be_positive expect(form.pages.count).to be_positive
expect(form.name).to eq("2022_2023_lettings")
end
it "is able to load a archived lettings form" do
form = form_handler.get_form("archived_lettings")
expect(form).to be_a(Form)
expect(form.pages.count).to be_positive
expect(form.name).to eq("2021_2022_lettings") expect(form.name).to eq("2021_2022_lettings")
end end
@ -63,13 +78,20 @@ RSpec.describe FormHandler do
form = form_handler.get_form("current_sales") form = form_handler.get_form("current_sales")
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.pages.count).to be_positive expect(form.pages.count).to be_positive
expect(form.name).to eq("2022_2023_sales") expect(form.name).to eq("2023_2024_sales")
end end
it "is able to load a previous sales form" do it "is able to load a previous sales form" do
form = form_handler.get_form("previous_sales") form = form_handler.get_form("previous_sales")
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.pages.count).to be_positive expect(form.pages.count).to be_positive
expect(form.name).to eq("2022_2023_sales")
end
it "is able to load a archived sales form" do
form = form_handler.get_form("archived_sales")
expect(form).to be_a(Form)
expect(form.pages.count).to be_positive
expect(form.name).to eq("2021_2022_sales") expect(form.name).to eq("2021_2022_sales")
end end
end end
@ -84,70 +106,86 @@ RSpec.describe FormHandler do
describe "Current collection start year" do describe "Current collection start year" do
context "when the date is after 1st of April" do context "when the date is after 1st of April" do
let(:now) { Time.utc(2022, 8, 3) } let(:now) { Time.utc(2023, 8, 3) }
it "returns the same year as the current start year" do it "returns the same year as the current start year" do
expect(form_handler.current_collection_start_year).to eq(2022) expect(form_handler.current_collection_start_year).to eq(2023)
end end
it "returns the correct current lettings form name" do it "returns the correct current lettings form name" do
expect(form_handler.form_name_from_start_year(2022, "lettings")).to eq("current_lettings") expect(form_handler.form_name_from_start_year(2023, "lettings")).to eq("current_lettings")
end end
it "returns the correct previous lettings form name" do it "returns the correct previous lettings form name" do
expect(form_handler.form_name_from_start_year(2021, "lettings")).to eq("previous_lettings") expect(form_handler.form_name_from_start_year(2022, "lettings")).to eq("previous_lettings")
end end
it "returns the correct next lettings form name" do it "returns the correct next lettings form name" do
expect(form_handler.form_name_from_start_year(2023, "lettings")).to eq("next_lettings") expect(form_handler.form_name_from_start_year(2024, "lettings")).to eq("next_lettings")
end
it "returns the correct archived lettings form name" do
expect(form_handler.form_name_from_start_year(2021, "lettings")).to eq("archived_lettings")
end end
it "returns the correct current sales form name" do it "returns the correct current sales form name" do
expect(form_handler.form_name_from_start_year(2022, "sales")).to eq("current_sales") expect(form_handler.form_name_from_start_year(2023, "sales")).to eq("current_sales")
end end
it "returns the correct previous sales form name" do it "returns the correct previous sales form name" do
expect(form_handler.form_name_from_start_year(2021, "sales")).to eq("previous_sales") expect(form_handler.form_name_from_start_year(2022, "sales")).to eq("previous_sales")
end end
it "returns the correct next sales form name" do it "returns the correct next sales form name" do
expect(form_handler.form_name_from_start_year(2023, "sales")).to eq("next_sales") expect(form_handler.form_name_from_start_year(2024, "sales")).to eq("next_sales")
end
it "returns the correct archived sales form name" do
expect(form_handler.form_name_from_start_year(2021, "sales")).to eq("archived_sales")
end end
it "returns the correct current start date" do it "returns the correct current start date" do
expect(form_handler.current_collection_start_date).to eq(Time.zone.local(2022, 4, 1)) expect(form_handler.current_collection_start_date).to eq(Time.zone.local(2023, 4, 1))
end end
end end
context "with the date before 1st of April" do context "with the date before 1st of April" do
let(:now) { Time.utc(2022, 2, 3) } let(:now) { Time.utc(2023, 2, 3) }
it "returns the previous year as the current start year" do it "returns the previous year as the current start year" do
expect(form_handler.current_collection_start_year).to eq(2021) expect(form_handler.current_collection_start_year).to eq(2022)
end end
it "returns the correct current lettings form name" do it "returns the correct current lettings form name" do
expect(form_handler.form_name_from_start_year(2021, "lettings")).to eq("current_lettings") expect(form_handler.form_name_from_start_year(2022, "lettings")).to eq("current_lettings")
end end
it "returns the correct previous lettings form name" do it "returns the correct previous lettings form name" do
expect(form_handler.form_name_from_start_year(2020, "lettings")).to eq("previous_lettings") expect(form_handler.form_name_from_start_year(2021, "lettings")).to eq("previous_lettings")
end end
it "returns the correct next lettings form name" do it "returns the correct next lettings form name" do
expect(form_handler.form_name_from_start_year(2022, "lettings")).to eq("next_lettings") expect(form_handler.form_name_from_start_year(2023, "lettings")).to eq("next_lettings")
end
it "returns the correct archived lettings form name" do
expect(form_handler.form_name_from_start_year(2020, "lettings")).to eq("archived_lettings")
end end
it "returns the correct current sales form name" do it "returns the correct current sales form name" do
expect(form_handler.form_name_from_start_year(2021, "sales")).to eq("current_sales") expect(form_handler.form_name_from_start_year(2022, "sales")).to eq("current_sales")
end end
it "returns the correct previous sales form name" do it "returns the correct previous sales form name" do
expect(form_handler.form_name_from_start_year(2020, "sales")).to eq("previous_sales") expect(form_handler.form_name_from_start_year(2021, "sales")).to eq("previous_sales")
end end
it "returns the correct next sales form name" do it "returns the correct next sales form name" do
expect(form_handler.form_name_from_start_year(2022, "sales")).to eq("next_sales") expect(form_handler.form_name_from_start_year(2023, "sales")).to eq("next_sales")
end
it "returns the correct archived sales form name" do
expect(form_handler.form_name_from_start_year(2020, "sales")).to eq("archived_sales")
end end
end end
end end
@ -197,6 +235,8 @@ RSpec.describe FormHandler do
let(:now) { Time.utc(2023, 9, 20) } let(:now) { Time.utc(2023, 9, 20) }
it "creates current_lettings and next_lettings forms from ruby form objects" do it "creates current_lettings and next_lettings forms from ruby form objects" do
expect(form_handler.lettings_forms["archived_lettings"]).to be_present
expect(form_handler.lettings_forms["archived_lettings"].start_date.year).to eq(2021)
expect(form_handler.lettings_forms["previous_lettings"]).to be_present expect(form_handler.lettings_forms["previous_lettings"]).to be_present
expect(form_handler.lettings_forms["previous_lettings"].start_date.year).to eq(2022) expect(form_handler.lettings_forms["previous_lettings"].start_date.year).to eq(2022)
expect(form_handler.lettings_forms["current_lettings"]).to be_present expect(form_handler.lettings_forms["current_lettings"]).to be_present
@ -206,10 +246,12 @@ RSpec.describe FormHandler do
end end
end end
context "when no form is defined in JSON (current collection start year 2024 onwards)" do context "when only archived form form is defined in JSON (current collection start year 2024 onwards)" do
let(:now) { Time.utc(2024, 9, 20) } let(:now) { Time.utc(2024, 9, 20) }
it "creates previous_lettings, current_lettings and next_lettings forms from ruby form objects" do it "creates previous_lettings, current_lettings and next_lettings forms from ruby form objects and archived form from json" do
expect(form_handler.lettings_forms["archived_lettings"]).to be_present
expect(form_handler.lettings_forms["archived_lettings"].start_date.year).to eq(2022)
expect(form_handler.lettings_forms["previous_lettings"]).to be_present expect(form_handler.lettings_forms["previous_lettings"]).to be_present
expect(form_handler.lettings_forms["previous_lettings"].start_date.year).to eq(2023) expect(form_handler.lettings_forms["previous_lettings"].start_date.year).to eq(2023)
expect(form_handler.lettings_forms["current_lettings"]).to be_present expect(form_handler.lettings_forms["current_lettings"]).to be_present

Loading…
Cancel
Save