Browse Source

Cldc 1600 refactor form handler (#875)

* Add previous, current and next forms to form handler

* Add current, previous and next sales forms to form handler

* Implement current_lettings_form, current_sales_form and store year and form type in form

* refactor lettings_forms

* Use current, previous and next forms in lettings log model

* Use current, previous and next forms in sales log model

* use current, previous and next forms in csv service

* Remove "startyear_endyear" forms from form handler

* Remove name from form initializer and add an optional start year

* refactor get_form

* update csv test, fix form initialize

* rebase fix

* Refactor form_name_from_start_year method out

* remove unused variable

* fix typo, add date tests

* rebase, fix tests

* add comment to before test block

* Change the FormHandler back to only contain the form objects

* extract name
pull/876/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
ac4ee2a1fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      app/models/form.rb
  2. 30
      app/models/form_handler.rb
  3. 4
      app/models/lettings_log.rb
  4. 2
      app/models/rent_period.rb
  5. 4
      app/models/sales_log.rb
  6. 8
      app/models/validations/date_validations.rb
  7. 8
      app/services/csv/lettings_log_csv_service.rb
  8. 2
      spec/features/form/check_answers_page_spec.rb
  9. 2
      spec/features/form/conditional_questions_spec.rb
  10. 2
      spec/features/form/form_navigation_spec.rb
  11. 13
      spec/features/form/validations_spec.rb
  12. 5
      spec/helpers/conditional_questions_helper_spec.rb
  13. 5
      spec/helpers/question_attribute_helper_spec.rb
  14. 5
      spec/helpers/tasklist_helper_spec.rb
  15. 5
      spec/jobs/email_csv_job_spec.rb
  16. 5
      spec/models/form/page_spec.rb
  17. 5
      spec/models/form/question_spec.rb
  18. 5
      spec/models/form/section_spec.rb
  19. 2
      spec/models/form/subsection_spec.rb
  20. 146
      spec/models/form_handler_spec.rb
  21. 4
      spec/models/form_spec.rb
  22. 11
      spec/models/lettings_log_spec.rb
  23. 4
      spec/models/rent_period_spec.rb
  24. 2
      spec/models/sales_log_spec.rb
  25. 5
      spec/models/validations/financial_validations_spec.rb
  26. 5
      spec/models/validations/household_validations_spec.rb
  27. 5
      spec/models/validations/shared_validations_spec.rb
  28. 28
      spec/requests/form_controller_spec.rb
  29. 2
      spec/requests/lettings_logs_controller_spec.rb
  30. 2
      spec/services/csv/lettings_log_csv_service_spec.rb
  31. 8
      spec/services/exports/lettings_log_export_service_spec.rb
  32. 4
      spec/services/imports/lettings_logs_field_import_service_spec.rb
  33. 8
      spec/services/imports/lettings_logs_import_service_spec.rb

9
app/models/form.rb

@ -3,9 +3,8 @@ class Form
:start_date, :end_date, :type, :name, :setup_definition, :start_date, :end_date, :type, :name, :setup_definition,
:setup_sections, :form_sections :setup_sections, :form_sections
def initialize(form_path, name, sections_in_form = [], type = "lettings") def initialize(form_path, start_year = "", sections_in_form = [], type = "lettings")
if type == "sales" if type == "sales"
@name = name
@setup_sections = [Form::Sales::Sections::Setup.new(nil, nil, self)] @setup_sections = [Form::Sales::Sections::Setup.new(nil, nil, self)]
@form_sections = sections_in_form.map { |sec| sec.new(nil, nil, self) } @form_sections = sections_in_form.map { |sec| sec.new(nil, nil, self) }
@type = "sales" @type = "sales"
@ -13,8 +12,8 @@ class Form
@subsections = sections.flat_map(&:subsections) @subsections = sections.flat_map(&:subsections)
@pages = subsections.flat_map(&:pages) @pages = subsections.flat_map(&:pages)
@questions = pages.flat_map(&:questions) @questions = pages.flat_map(&:questions)
@start_date = Time.zone.local(name[0..3], 4, 1) @start_date = Time.zone.local(start_year, 4, 1)
@end_date = Time.zone.local(start_date.year + 1, 7, 1) @end_date = Time.zone.local(start_year + 1, 7, 1)
@form_definition = { @form_definition = {
"form_type" => type, "form_type" => type,
"start_date" => start_date, "start_date" => start_date,
@ -24,7 +23,6 @@ class Form
else else
raise "No form definition file exists for given year".freeze unless File.exist?(form_path) raise "No form definition file exists for given year".freeze unless File.exist?(form_path)
@name = name
@setup_sections = [Form::Lettings::Sections::Setup.new(nil, nil, self)] @setup_sections = [Form::Lettings::Sections::Setup.new(nil, nil, self)]
@form_definition = JSON.parse(File.open(form_path).read) @form_definition = JSON.parse(File.open(form_path).read)
@form_sections = form_definition["sections"].map { |id, s| Form::Section.new(id, s, self) } @form_sections = form_definition["sections"].map { |id, s| Form::Section.new(id, s, self) }
@ -36,6 +34,7 @@ class Form
@start_date = Time.iso8601(form_definition["start_date"]) @start_date = Time.iso8601(form_definition["start_date"])
@end_date = Time.iso8601(form_definition["end_date"]) @end_date = Time.iso8601(form_definition["end_date"])
end end
@name = "#{start_date.year}_#{end_date.year}_#{type}"
end end
def get_subsection(id) def get_subsection(id)

30
app/models/form_handler.rb

@ -10,26 +10,46 @@ class FormHandler
@forms[form] @forms[form]
end end
def current_form def current_lettings_form
forms[forms.keys.max_by(&:to_i)] forms["current_lettings"]
end
def current_sales_form
forms["current_sales"]
end end
def sales_forms def sales_forms
sales_sections = [] # Add section classes here e.g. Form::Sales::Property::Sections::PropertyInformation sales_sections = [] # Add section classes here e.g. Form::Sales::Property::Sections::PropertyInformation
{ "2022_2023_sales" => Form.new(nil, "2022_2023_sales", sales_sections, "sales") } current_form = Form.new(nil, current_collection_start_year, sales_sections, "sales")
previous_form = Form.new(nil, current_collection_start_year - 1, sales_sections, "sales")
{ "current_sales" => current_form,
"previous_sales" => previous_form }
end end
def lettings_forms def lettings_forms
forms = {} forms = {}
directories.each do |directory| directories.each do |directory|
Dir.glob("#{directory}/*.json").each do |form_path| Dir.glob("#{directory}/*.json").each do |form_path|
form_name = File.basename(form_path, ".json") form = Form.new(form_path)
forms[form_name] = Form.new(form_path, form_name)
form_to_set = form_name_from_start_year(form.start_date.year, "lettings")
forms[form_to_set] = form if forms[form_to_set].blank?
end end
end end
forms forms
end end
def current_collection_start_year
today = Time.zone.now
window_end_date = Time.zone.local(today.year, 4, 1)
today < window_end_date ? today.year - 1 : today.year
end
def form_name_from_start_year(year, type)
form_mappings = { 0 => "current_#{type}", 1 => "previous_#{type}", -1 => "next_#{type}" }
form_mappings[current_collection_start_year - year]
end
private private
def get_all_forms def get_all_forms

4
app/models/lettings_log.rb

@ -55,7 +55,7 @@ class LettingsLog < Log
RETIREMENT_AGES = { "M" => 67, "F" => 60, "X" => 67 }.freeze RETIREMENT_AGES = { "M" => 67, "F" => 60, "X" => 67 }.freeze
def form def form
FormHandler.instance.get_form(form_name) || FormHandler.instance.forms.first.second FormHandler.instance.get_form(form_name) || FormHandler.instance.current_lettings_form
end end
def recalculate_start_year! def recalculate_start_year!
@ -66,7 +66,7 @@ class LettingsLog < Log
def form_name def form_name
return unless startdate return unless startdate
"#{collection_start_year}_#{collection_start_year + 1}" FormHandler.instance.form_name_from_start_year(collection_start_year, "lettings")
end end
def self.editable_fields def self.editable_fields

2
app/models/rent_period.rb

@ -1,5 +1,5 @@
class RentPeriod class RentPeriod
def self.rent_period_mappings def self.rent_period_mappings
FormHandler.instance.current_form.get_question("period", nil).answer_options FormHandler.instance.current_lettings_form.get_question("period", nil).answer_options
end end
end end

4
app/models/sales_log.rb

@ -23,11 +23,11 @@ class SalesLog < Log
def form_name def form_name
return unless saledate return unless saledate
"#{collection_start_year}_#{collection_start_year + 1}_sales" FormHandler.instance.form_name_from_start_year(collection_start_year, "sales")
end end
def form def form
FormHandler.instance.get_form(form_name) || FormHandler.instance.get_form("2022_2023_sales") FormHandler.instance.get_form(form_name) || FormHandler.instance.current_sales_form
end end
def optional_fields def optional_fields

8
app/models/validations/date_validations.rb

@ -60,19 +60,19 @@ module Validations::DateValidations
private private
def first_collection_start_date def first_collection_start_date
@first_collection_start_date ||= FormHandler.instance.forms.map { |form| form.second.start_date }.compact.min @first_collection_start_date ||= FormHandler.instance.forms.map { |_name, form| form.start_date }.compact.min
end end
def first_collection_end_date def first_collection_end_date
@first_collection_end_date ||= FormHandler.instance.forms.map { |form| form.second.end_date }.compact.min @first_collection_end_date ||= FormHandler.instance.forms.map { |_name, form| form.end_date }.compact.min
end end
def second_collection_start_date def second_collection_start_date
@second_collection_start_date ||= FormHandler.instance.forms.map { |form| form.second.start_date }.compact.max @second_collection_start_date ||= FormHandler.instance.forms.map { |_name, form| form.start_date }.compact.max
end end
def second_collection_end_date def second_collection_end_date
@second_collection_end_date ||= FormHandler.instance.forms.map { |form| form.second.end_date }.compact.max @second_collection_end_date ||= FormHandler.instance.forms.map { |_name, form| form.end_date }.compact.max
end end
def date_valid?(question, record) def date_valid?(question, record)

8
app/services/csv/lettings_log_csv_service.rb

@ -51,7 +51,13 @@ module Csv
def ordered_form_questions def ordered_form_questions
downloaded_form_years = LettingsLog.all.map(&:collection_start_year).uniq.compact downloaded_form_years = LettingsLog.all.map(&:collection_start_year).uniq.compact
downloaded_form_fields = downloaded_form_years.count == 1 && downloaded_form_years[0].present? ? FormHandler.instance.get_form("#{downloaded_form_years[0]}_#{downloaded_form_years[0] + 1}").questions : FormHandler.instance.forms.first.second.questions
if downloaded_form_years.count == 1 && downloaded_form_years[0].present?
form_name = FormHandler.instance.form_name_from_start_year(downloaded_form_years[0], "lettings")
downloaded_form_fields = FormHandler.instance.get_form(form_name).questions
else
downloaded_form_fields = FormHandler.instance.current_lettings_form.questions
end
move_checkbox_answer_options(downloaded_form_fields) move_checkbox_answer_options(downloaded_form_fields)
end end

2
spec/features/form/check_answers_page_spec.rb

@ -39,9 +39,11 @@ RSpec.describe "Form Check Answers Page" do
) )
end end
let(:id) { lettings_log.id } let(:id) { lettings_log.id }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do before do
sign_in user sign_in user
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end end
context "when the user needs to check their answers for a subsection" do context "when the user needs to check their answers for a subsection" do

2
spec/features/form/conditional_questions_spec.rb

@ -13,9 +13,11 @@ RSpec.describe "Form Conditional Questions" do
) )
end end
let(:id) { lettings_log.id } let(:id) { lettings_log.id }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do before do
sign_in user sign_in user
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end end
context "with a page where some questions are only conditionally shown, depending on how you answer the first question" do context "with a page where some questions are only conditionally shown, depending on how you answer the first question" do

2
spec/features/form/form_navigation_spec.rb

@ -32,9 +32,11 @@ RSpec.describe "Form Navigation" do
hhmemb: { type: "numeric", answer: 1, path: "household-number-of-members" }, hhmemb: { type: "numeric", answer: 1, path: "household-number-of-members" },
} }
end end
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do before do
sign_in user sign_in user
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end end
describe "Create a new lettings log" do describe "Create a new lettings log" do

13
spec/features/form/validations_spec.rb

@ -2,11 +2,7 @@ require "rails_helper"
require_relative "helpers" require_relative "helpers"
RSpec.describe "validations" do RSpec.describe "validations" do
before do let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
sign_in user
end
include Helpers
let(:user) { FactoryBot.create(:user) } let(:user) { FactoryBot.create(:user) }
let(:lettings_log) do let(:lettings_log) do
FactoryBot.create( FactoryBot.create(
@ -35,6 +31,13 @@ RSpec.describe "validations" do
end end
let(:id) { lettings_log.id } let(:id) { lettings_log.id }
before do
sign_in user
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
include Helpers
describe "Question validation" do describe "Question validation" do
context "when the tenant age is invalid" do context "when the tenant age is invalid" do
it "shows validation for under 0" do it "shows validation for under 0" do

5
spec/helpers/conditional_questions_helper_spec.rb

@ -3,6 +3,11 @@ require "rails_helper"
RSpec.describe ConditionalQuestionsHelper do RSpec.describe ConditionalQuestionsHelper do
let(:lettings_log) { FactoryBot.build(:lettings_log) } let(:lettings_log) { FactoryBot.build(:lettings_log) }
let(:page) { lettings_log.form.get_page("armed_forces") } let(:page) { lettings_log.form.get_page("armed_forces") }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
describe "conditional questions for page" do describe "conditional questions for page" do
let(:conditional_pages) { %w[leftreg] } let(:conditional_pages) { %w[leftreg] }

5
spec/helpers/question_attribute_helper_spec.rb

@ -4,6 +4,11 @@ RSpec.describe QuestionAttributeHelper do
let(:lettings_log) { FactoryBot.build(:lettings_log) } let(:lettings_log) { FactoryBot.build(:lettings_log) }
let(:form) { lettings_log.form } let(:form) { lettings_log.form }
let(:questions) { form.get_page("rent").questions } let(:questions) { form.get_page("rent").questions }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
describe "html attributes" do describe "html attributes" do
it "returns empty hash if fields-to-add or result-field are empty " do it "returns empty hash if fields-to-add or result-field are empty " do

5
spec/helpers/tasklist_helper_spec.rb

@ -3,6 +3,11 @@ require "rails_helper"
RSpec.describe TasklistHelper do RSpec.describe TasklistHelper do
let(:empty_lettings_log) { FactoryBot.create(:lettings_log) } let(:empty_lettings_log) { FactoryBot.create(:lettings_log) }
let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, needstype: 1) } let(:lettings_log) { FactoryBot.create(:lettings_log, :in_progress, needstype: 1) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
describe "get next incomplete section" do describe "get next incomplete section" do
it "returns the first subsection name if it is not completed" do it "returns the first subsection name if it is not completed" do

5
spec/jobs/email_csv_job_spec.rb

@ -9,6 +9,11 @@ describe EmailCsvJob do
let(:user) { FactoryBot.create(:user) } let(:user) { FactoryBot.create(:user) }
let(:organisation) { user.organisation } let(:organisation) { user.organisation }
let(:other_organisation) { FactoryBot.create(:organisation) } let(:other_organisation) { FactoryBot.create(:organisation) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
context "when a log exists" do context "when a log exists" do
let!(:lettings_log) do let!(:lettings_log) do

5
spec/models/form/page_spec.rb

@ -14,6 +14,11 @@ RSpec.describe Form::Page, type: :model do
let(:subsection) { Form::Subsection.new(subsection_id, subsection_definition, section) } let(:subsection) { Form::Subsection.new(subsection_id, subsection_definition, section) }
let(:page_id) { "net_income" } let(:page_id) { "net_income" }
let(:page_definition) { subsection_definition["pages"][page_id] } let(:page_definition) { subsection_definition["pages"][page_id] }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
it "has an id" do it "has an id" do
expect(page.id).to eq(page_id) expect(page.id).to eq(page_id)

5
spec/models/form/question_spec.rb

@ -16,6 +16,11 @@ RSpec.describe Form::Question, type: :model do
let(:page) { Form::Page.new(page_id, page_definition, subsection) } let(:page) { Form::Page.new(page_id, page_definition, subsection) }
let(:question_id) { "earnings" } let(:question_id) { "earnings" }
let(:question_definition) { page_definition["questions"][question_id] } let(:question_definition) { page_definition["questions"][question_id] }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
it "has an id" do it "has an id" do
expect(question.id).to eq(question_id) expect(question.id).to eq(question_id)

5
spec/models/form/section_spec.rb

@ -7,6 +7,11 @@ RSpec.describe Form::Section, type: :model do
let(:form) { lettings_log.form } let(:form) { lettings_log.form }
let(:section_id) { "household" } let(:section_id) { "household" }
let(:section_definition) { form.form_definition["sections"][section_id] } let(:section_definition) { form.form_definition["sections"][section_id] }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
it "has an id" do it "has an id" do
expect(section.id).to eq(section_id) expect(section.id).to eq(section_id)

2
spec/models/form/subsection_spec.rb

@ -11,9 +11,11 @@ RSpec.describe Form::Subsection, type: :model do
let(:section) { Form::Section.new(section_id, section_definition, form) } let(:section) { Form::Section.new(section_id, section_definition, form) }
let(:subsection_id) { "household_characteristics" } let(:subsection_id) { "household_characteristics" }
let(:subsection_definition) { section_definition["subsections"][subsection_id] } let(:subsection_definition) { section_definition["subsections"][subsection_id] }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do before do
RequestHelper.stub_http_requests RequestHelper.stub_http_requests
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end end
it "has an id" do it "has an id" do

146
spec/models/form_handler_spec.rb

@ -1,43 +1,163 @@
require "rails_helper" require "rails_helper"
RSpec.describe FormHandler do RSpec.describe FormHandler do
let(:test_form_name) { "2021_2022" } let(:form_handler) { described_class.instance }
before { Singleton.__init__(described_class) } # reload FormHandler Instance to update form definitions between runs
context "when accessing a form in a different year" do
before do
Timecop.freeze(Time.utc(2021, 8, 3))
end
after do
Timecop.unfreeze
end
it "is able to load a current lettings form" do
form = form_handler.get_form("current_lettings")
expect(form).to be_a(Form)
expect(form.pages.count).to eq(45)
end
it "is able to load a next lettings form" do
form = form_handler.get_form("next_lettings")
expect(form).to be_a(Form)
expect(form.pages.count).to eq(12)
end
end
describe "Get all forms" do describe "Get all forms" do
it "is able to load all the forms" do it "is able to load all the forms" do
form_handler = described_class.instance
all_forms = form_handler.forms all_forms = form_handler.forms
expect(all_forms.count).to be >= 1 expect(all_forms.count).to be >= 1
expect(all_forms[test_form_name]).to be_a(Form) expect(all_forms["current_sales"]).to be_a(Form)
end end
end end
describe "Get specific form" do describe "Get specific form" do
it "is able to load a specific form" do it "is able to load a current lettings form" do
form_handler = described_class.instance form = form_handler.get_form("current_lettings")
form = form_handler.get_form(test_form_name) expect(form).to be_a(Form)
expect(form.pages.count).to eq(12)
end
it "is able to load a previous lettings form" do
form = form_handler.get_form("previous_lettings")
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.pages.count).to eq(45) expect(form.pages.count).to eq(45)
end end
it "is able to load a current sales form" do
form = form_handler.get_form("current_sales")
expect(form).to be_a(Form)
expect(form.pages.count).to eq(4)
expect(form.name).to eq("2022_2023_sales")
end
it "is able to load a previous sales form" do
form = form_handler.get_form("previous_sales")
expect(form).to be_a(Form)
expect(form.pages.count).to eq(4)
expect(form.name).to eq("2021_2022_sales")
end
end end
describe "Current form" do describe "Current form" do
it "returns the latest form by date" do it "returns the latest form by date" do
form_handler = described_class.instance form = form_handler.current_lettings_form
form = form_handler.current_form
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.start_date.year).to eq(2022) expect(form.start_date.year).to eq(2022)
end end
end end
describe "Current collection start year" do
context "when the date is after 1st of April" do
before do
Timecop.freeze(Time.utc(2022, 8, 3))
end
after do
Timecop.unfreeze
end
it "returns the same year as the the current start year" do
expect(form_handler.current_collection_start_year).to eq(2022)
end
it "returns the correct current lettings form name" do
expect(form_handler.form_name_from_start_year(2022, "lettings")).to eq("current_lettings")
end
it "returns the correct previous lettings form name" do
expect(form_handler.form_name_from_start_year(2021, "lettings")).to eq("previous_lettings")
end
it "returns the correct next lettings form name" do
expect(form_handler.form_name_from_start_year(2023, "lettings")).to eq("next_lettings")
end
it "returns the correct current sales form name" do
expect(form_handler.form_name_from_start_year(2022, "sales")).to eq("current_sales")
end
it "returns the correct previous sales form name" do
expect(form_handler.form_name_from_start_year(2021, "sales")).to eq("previous_sales")
end
it "returns the correct next sales form name" do
expect(form_handler.form_name_from_start_year(2023, "sales")).to eq("next_sales")
end
end
context "with the date before 1st of April" do
before do
Timecop.freeze(Time.utc(2022, 2, 3))
end
after do
Timecop.unfreeze
end
it "returns the previous year as the current start year" do
expect(form_handler.current_collection_start_year).to eq(2021)
end
it "returns the correct current lettings form name" do
expect(form_handler.form_name_from_start_year(2021, "lettings")).to eq("current_lettings")
end
it "returns the correct previous lettings form name" do
expect(form_handler.form_name_from_start_year(2020, "lettings")).to eq("previous_lettings")
end
it "returns the correct next lettings form name" do
expect(form_handler.form_name_from_start_year(2022, "lettings")).to eq("next_lettings")
end
it "returns the correct current sales form name" do
expect(form_handler.form_name_from_start_year(2021, "sales")).to eq("current_sales")
end
it "returns the correct previous sales form name" do
expect(form_handler.form_name_from_start_year(2020, "sales")).to eq("previous_sales")
end
it "returns the correct next sales form name" do
expect(form_handler.form_name_from_start_year(2022, "sales")).to eq("next_sales")
end
end
end
it "loads the form once at boot time" do it "loads the form once at boot time" do
form_handler = described_class.instance form_handler = described_class.instance
expect(Form).not_to receive(:new).with(:any, test_form_name) expect(Form).not_to receive(:new).with(:any, "current_sales")
expect(form_handler.get_form(test_form_name)).to be_a(Form) expect(form_handler.get_form("current_sales")).to be_a(Form)
end end
it "can get a saleslog form" do it "correctly sets form type and start year" do
form_handler = described_class.instance form = form_handler.forms["current_lettings"]
expect(form_handler.get_form("2022_2023_sales")).to be_a(Form) expect(form.type).to eq("lettings")
expect(form.start_date.year).to eq(2022)
end end
end end

4
spec/models/form_spec.rb

@ -209,9 +209,9 @@ RSpec.describe Form, type: :model do
describe "when creating a sales log" do describe "when creating a sales log" do
it "creates a valid sales form" do it "creates a valid sales form" do
sections = [] sections = []
form = described_class.new(nil, "2022_23_sales", sections, "sales") form = described_class.new(nil, 2022, sections, "sales")
expect(form.type).to eq("sales") expect(form.type).to eq("sales")
expect(form.name).to eq("2022_23_sales") expect(form.name).to eq("2022_2023_sales")
expect(form.setup_sections.count).to eq(1) expect(form.setup_sections.count).to eq(1)
expect(form.setup_sections[0].class).to eq(Form::Sales::Sections::Setup) expect(form.setup_sections[0].class).to eq(Form::Sales::Sections::Setup)
expect(form.sections.count).to eq(1) expect(form.sections.count).to eq(1)

11
spec/models/lettings_log_spec.rb

@ -4,6 +4,11 @@ RSpec.describe LettingsLog do
let(:owning_organisation) { FactoryBot.create(:organisation) } let(:owning_organisation) { FactoryBot.create(:organisation) }
let(:different_managing_organisation) { FactoryBot.create(:organisation) } let(:different_managing_organisation) { FactoryBot.create(:organisation) }
let(:created_by_user) { FactoryBot.create(:user) } let(:created_by_user) { FactoryBot.create(:user) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
it "inherits from log" do it "inherits from log" do
expect(described_class).to be < Log expect(described_class).to be < Log
@ -23,9 +28,9 @@ RSpec.describe LettingsLog do
it "has returns the correct form based on the start date" do it "has returns the correct form based on the start date" do
expect(lettings_log.form_name).to be_nil expect(lettings_log.form_name).to be_nil
expect(lettings_log.form).to be_a(Form) expect(lettings_log.form).to be_a(Form)
expect(lettings_log_2.form_name).to eq("2021_2022") expect(lettings_log_2.form_name).to eq("previous_lettings")
expect(lettings_log_2.form).to be_a(Form) expect(lettings_log_2.form).to be_a(Form)
expect(lettings_log_year_2.form_name).to eq("2023_2024") expect(lettings_log_year_2.form_name).to eq("next_lettings")
expect(lettings_log_year_2.form).to be_a(Form) expect(lettings_log_year_2.form).to be_a(Form)
end end
@ -1647,7 +1652,7 @@ RSpec.describe LettingsLog do
end end
context "when a lettings log is a supported housing log" do context "when a lettings log is a supported housing log" do
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") }
before do before do
lettings_log.needstype = 2 lettings_log.needstype = 2

4
spec/models/rent_period_spec.rb

@ -2,10 +2,10 @@ require "rails_helper"
RSpec.describe RentPeriod, type: :model do RSpec.describe RentPeriod, type: :model do
describe "rent period mapping" do describe "rent period mapping" do
let(:form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") } let(:form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do before do
allow(FormHandler.instance).to receive(:current_form).and_return(form) allow(FormHandler.instance).to receive(:current_lettings_form).and_return(form)
end end
it "maps rent period id to display names" do it "maps rent period id to display names" do

2
spec/models/sales_log_spec.rb

@ -34,7 +34,7 @@ RSpec.describe SalesLog, type: :model do
it "has returns the correct form based on the start date" do it "has returns the correct form based on the start date" do
expect(sales_log.form_name).to be_nil expect(sales_log.form_name).to be_nil
expect(sales_log.form).to be_a(Form) expect(sales_log.form).to be_a(Form)
expect(sales_log_2.form_name).to eq("2022_2023_sales") expect(sales_log_2.form_name).to eq("current_sales")
expect(sales_log_2.form).to be_a(Form) expect(sales_log_2.form).to be_a(Form)
end end
end end

5
spec/models/validations/financial_validations_spec.rb

@ -5,6 +5,11 @@ RSpec.describe Validations::FinancialValidations do
let(:validator_class) { Class.new { include Validations::FinancialValidations } } let(:validator_class) { Class.new { include Validations::FinancialValidations } }
let(:record) { FactoryBot.create(:lettings_log) } let(:record) { FactoryBot.create(:lettings_log) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
describe "earnings and income frequency" do describe "earnings and income frequency" do
it "when earnings are provided it validates that income frequency must be provided" do it "when earnings are provided it validates that income frequency must be provided" do

5
spec/models/validations/household_validations_spec.rb

@ -5,6 +5,11 @@ RSpec.describe Validations::HouseholdValidations do
let(:validator_class) { Class.new { include Validations::HouseholdValidations } } let(:validator_class) { Class.new { include Validations::HouseholdValidations } }
let(:record) { FactoryBot.create(:lettings_log) } let(:record) { FactoryBot.create(:lettings_log) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
describe "reasonable preference validations" do describe "reasonable preference validations" do
context "when reasonable preference is homeless" do context "when reasonable preference is homeless" do

5
spec/models/validations/shared_validations_spec.rb

@ -5,8 +5,13 @@ RSpec.describe Validations::SharedValidations do
let(:validator_class) { Class.new { include Validations::SharedValidations } } let(:validator_class) { Class.new { include Validations::SharedValidations } }
let(:record) { FactoryBot.create(:lettings_log) } let(:record) { FactoryBot.create(:lettings_log) }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
describe "numeric min max validations" do describe "numeric min max validations" do
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
context "when validating age" do context "when validating age" do
it "validates that person 1's age is a number" do it "validates that person 1's age is a number" do
record.age1 = "random" record.age1 = "random"

28
spec/requests/form_controller_spec.rb

@ -5,13 +5,6 @@ RSpec.describe FormController, type: :request do
let(:user) { FactoryBot.create(:user) } let(:user) { FactoryBot.create(:user) }
let(:organisation) { user.organisation } let(:organisation) { user.organisation }
let(:other_organisation) { FactoryBot.create(:organisation) } let(:other_organisation) { FactoryBot.create(:organisation) }
let!(:lettings_log) do
FactoryBot.create(
:lettings_log,
owning_organisation: organisation,
managing_organisation: organisation,
)
end
let!(:unauthorized_lettings_log) do let!(:unauthorized_lettings_log) do
FactoryBot.create( FactoryBot.create(
:lettings_log, :lettings_log,
@ -38,8 +31,21 @@ RSpec.describe FormController, type: :request do
) )
end end
let(:headers) { { "Accept" => "text/html" } } let(:headers) { { "Accept" => "text/html" } }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
context "when a user is not signed in" do context "when a user is not signed in" do
let!(:lettings_log) do
FactoryBot.create(
:lettings_log,
owning_organisation: organisation,
managing_organisation: organisation,
)
end
describe "GET" do describe "GET" do
it "does not let you get lettings logs pages you don't have access to" do it "does not let you get lettings logs pages you don't have access to" do
get "/lettings-logs/#{lettings_log.id}/person-1-age", headers: headers, params: {} get "/lettings-logs/#{lettings_log.id}/person-1-age", headers: headers, params: {}
@ -61,6 +67,14 @@ RSpec.describe FormController, type: :request do
end end
context "when a user is signed in" do context "when a user is signed in" do
let!(:lettings_log) do
FactoryBot.create(
:lettings_log,
owning_organisation: organisation,
managing_organisation: organisation,
)
end
before do before do
allow(user).to receive(:need_two_factor_authentication?).and_return(false) allow(user).to receive(:need_two_factor_authentication?).and_return(false)
sign_in user sign_in user

2
spec/requests/lettings_logs_controller_spec.rb

@ -18,11 +18,13 @@ RSpec.describe LettingsLogsController, type: :request do
"Authorization" => basic_credentials, "Authorization" => basic_credentials,
} }
end end
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
before do before do
allow(ENV).to receive(:[]) allow(ENV).to receive(:[])
allow(ENV).to receive(:[]).with("API_USER").and_return(api_username) allow(ENV).to receive(:[]).with("API_USER").and_return(api_username)
allow(ENV).to receive(:[]).with("API_KEY").and_return(api_password) allow(ENV).to receive(:[]).with("API_KEY").and_return(api_password)
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end end
describe "POST #create" do describe "POST #create" do

2
spec/services/csv/lettings_log_csv_service_spec.rb

@ -3,7 +3,7 @@ require "rails_helper"
RSpec.describe Csv::LettingsLogCsvService do RSpec.describe Csv::LettingsLogCsvService do
context "when the user is support" do context "when the user is support" do
let(:user) { FactoryBot.create(:user, :support) } let(:user) { FactoryBot.create(:user, :support) }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") }
before do before do
LettingsLog.create!(startdate: "2021-10-10", created_at: Time.utc(2022, 2, 8, 16, 52, 15)) LettingsLog.create!(startdate: "2021-10-10", created_at: Time.utc(2022, 2, 8, 16, 52, 15))

8
spec/services/exports/lettings_log_export_service_spec.rb

@ -8,8 +8,8 @@ RSpec.describe Exports::LettingsLogExportService do
let(:xml_export_file) { File.open("spec/fixtures/exports/general_needs_log.xml", "r:UTF-8") } let(:xml_export_file) { File.open("spec/fixtures/exports/general_needs_log.xml", "r:UTF-8") }
let(:local_manifest_file) { File.open("spec/fixtures/exports/manifest.xml", "r:UTF-8") } let(:local_manifest_file) { File.open("spec/fixtures/exports/manifest.xml", "r:UTF-8") }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") }
let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json", "2022_2023") } let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json") }
let(:expected_master_manifest_filename) { "Manifest_2022_05_01_0001.csv" } let(:expected_master_manifest_filename) { "Manifest_2022_05_01_0001.csv" }
let(:expected_master_manifest_rerun) { "Manifest_2022_05_01_0002.csv" } let(:expected_master_manifest_rerun) { "Manifest_2022_05_01_0002.csv" }
@ -33,8 +33,8 @@ RSpec.describe Exports::LettingsLogExportService do
allow(storage_service).to receive(:write_file) allow(storage_service).to receive(:write_file)
# Stub the form handler to use the real form # Stub the form handler to use the real form
allow(FormHandler.instance).to receive(:get_form).with("2021_2022").and_return(real_2021_2022_form) allow(FormHandler.instance).to receive(:get_form).with("previous_lettings").and_return(real_2021_2022_form)
allow(FormHandler.instance).to receive(:get_form).with("2022_2023").and_return(real_2022_2023_form) allow(FormHandler.instance).to receive(:get_form).with("current_lettings").and_return(real_2022_2023_form)
end end
context "when exporting daily lettings logs in XML" do context "when exporting daily lettings logs in XML" do

4
spec/services/imports/lettings_logs_field_import_service_spec.rb

@ -6,7 +6,7 @@ RSpec.describe Imports::LettingsLogsFieldImportService do
let(:storage_service) { instance_double(Storage::S3Service) } let(:storage_service) { instance_double(Storage::S3Service) }
let(:logger) { instance_double(ActiveSupport::Logger) } let(:logger) { instance_double(ActiveSupport::Logger) }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") }
let(:fixture_directory) { "spec/fixtures/imports/logs" } let(:fixture_directory) { "spec/fixtures/imports/logs" }
let(:lettings_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" } let(:lettings_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" }
@ -27,7 +27,7 @@ RSpec.describe Imports::LettingsLogsFieldImportService do
FactoryBot.create(:user, old_user_id:, organisation:) FactoryBot.create(:user, old_user_id:, organisation:)
# Stub the form handler to use the real form # Stub the form handler to use the real form
allow(FormHandler.instance).to receive(:get_form).with("2021_2022").and_return(real_2021_2022_form) allow(FormHandler.instance).to receive(:get_form).with("previous_lettings").and_return(real_2021_2022_form)
WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/LS166FT/) WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/LS166FT/)
.to_return(status: 200, body: '{"status":200,"result":{"codes":{"admin_district":"E08000035"}}}', headers: {}) .to_return(status: 200, body: '{"status":200,"result":{"codes":{"admin_district":"E08000035"}}}', headers: {})

8
spec/services/imports/lettings_logs_import_service_spec.rb

@ -6,8 +6,8 @@ RSpec.describe Imports::LettingsLogsImportService do
let(:storage_service) { instance_double(Storage::S3Service) } let(:storage_service) { instance_double(Storage::S3Service) }
let(:logger) { instance_double(ActiveSupport::Logger) } let(:logger) { instance_double(ActiveSupport::Logger) }
let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json", "2021_2022") } let(:real_2021_2022_form) { Form.new("config/forms/2021_2022.json") }
let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json", "2022_2023") } let(:real_2022_2023_form) { Form.new("config/forms/2022_2023.json") }
let(:fixture_directory) { "spec/fixtures/imports/logs" } let(:fixture_directory) { "spec/fixtures/imports/logs" }
let(:organisation) { FactoryBot.create(:organisation, old_visible_id: "1", provider_type: "PRP") } let(:organisation) { FactoryBot.create(:organisation, old_visible_id: "1", provider_type: "PRP") }
@ -35,8 +35,8 @@ RSpec.describe Imports::LettingsLogsImportService do
FactoryBot.create(:location, old_visible_id: 10, postcode: "LS166FT", scheme_id: scheme2.id, mobility_type: "W") FactoryBot.create(:location, old_visible_id: 10, postcode: "LS166FT", scheme_id: scheme2.id, mobility_type: "W")
# Stub the form handler to use the real form # Stub the form handler to use the real form
allow(FormHandler.instance).to receive(:get_form).with("2021_2022").and_return(real_2021_2022_form) allow(FormHandler.instance).to receive(:get_form).with("previous_lettings").and_return(real_2021_2022_form)
allow(FormHandler.instance).to receive(:get_form).with("2022_2023").and_return(real_2022_2023_form) allow(FormHandler.instance).to receive(:get_form).with("current_lettings").and_return(real_2022_2023_form)
end end
context "when importing lettings logs" do context "when importing lettings logs" do

Loading…
Cancel
Save