diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb new file mode 100644 index 000000000..2b3542813 --- /dev/null +++ b/app/models/derived_variables/sales_log_variables.rb @@ -0,0 +1,5 @@ +module DerivedVariables::SalesLogVariables + def set_derived_fields! + self.ethnic = 17 if ethnic_refused? + end +end diff --git a/app/models/form/question.rb b/app/models/form/question.rb index 4478f63fd..7449d6932 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -50,9 +50,11 @@ class Form::Question answer = label_from_value(log[id]) if log[id].present? answer_label = [prefix, format_value(answer), suffix_label(log)].join("") if answer - return answer_label if answer_label - has_inferred_check_answers_value?(log) ? inferred_check_answers_value["value"] : "" + inferred = inferred_check_answers_value["value"] if has_inferred_check_answers_value?(log) + return inferred if inferred.present? + + answer_label end def get_inferred_answers(log) diff --git a/app/models/form/sales/pages/buyer1_ethnic_background_arab.rb b/app/models/form/sales/pages/buyer1_ethnic_background_arab.rb new file mode 100644 index 000000000..93fb1afe0 --- /dev/null +++ b/app/models/form/sales/pages/buyer1_ethnic_background_arab.rb @@ -0,0 +1,18 @@ +class Form::Sales::Pages::Buyer1EthnicBackgroundArab < ::Form::Page + def initialize(id, hsh, subsection) + super + @id = "buyer_1_ethnic_background_arab" + @header = "" + @description = "" + @subsection = subsection + @depends_on = [{ + "ethnic_group" => 4, + }] + end + + def questions + @questions ||= [ + Form::Sales::Questions::Buyer1EthnicBackgroundArab.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/sales/pages/buyer1_ethnic_background_asian.rb b/app/models/form/sales/pages/buyer1_ethnic_background_asian.rb new file mode 100644 index 000000000..dd6802a99 --- /dev/null +++ b/app/models/form/sales/pages/buyer1_ethnic_background_asian.rb @@ -0,0 +1,18 @@ +class Form::Sales::Pages::Buyer1EthnicBackgroundAsian < ::Form::Page + def initialize(id, hsh, subsection) + super + @id = "buyer_1_ethnic_background_asian" + @header = "" + @description = "" + @subsection = subsection + @depends_on = [{ + "ethnic_group" => 2, + }] + end + + def questions + @questions ||= [ + Form::Sales::Questions::Buyer1EthnicBackgroundAsian.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/sales/pages/buyer1_ethnic_background_black.rb b/app/models/form/sales/pages/buyer1_ethnic_background_black.rb new file mode 100644 index 000000000..ce6cf3a0b --- /dev/null +++ b/app/models/form/sales/pages/buyer1_ethnic_background_black.rb @@ -0,0 +1,18 @@ +class Form::Sales::Pages::Buyer1EthnicBackgroundBlack < ::Form::Page + def initialize(id, hsh, subsection) + super + @id = "buyer_1_ethnic_background_black" + @header = "" + @description = "" + @subsection = subsection + @depends_on = [{ + "ethnic_group" => 3, + }] + end + + def questions + @questions ||= [ + Form::Sales::Questions::Buyer1EthnicBackgroundBlack.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/sales/pages/buyer1_ethnic_background_mixed.rb b/app/models/form/sales/pages/buyer1_ethnic_background_mixed.rb new file mode 100644 index 000000000..5225feed1 --- /dev/null +++ b/app/models/form/sales/pages/buyer1_ethnic_background_mixed.rb @@ -0,0 +1,18 @@ +class Form::Sales::Pages::Buyer1EthnicBackgroundMixed < ::Form::Page + def initialize(id, hsh, subsection) + super + @id = "buyer_1_ethnic_background_mixed" + @header = "" + @description = "" + @subsection = subsection + @depends_on = [{ + "ethnic_group" => 1, + }] + end + + def questions + @questions ||= [ + Form::Sales::Questions::Buyer1EthnicBackgroundMixed.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/sales/pages/buyer1_ethnic_background_white.rb b/app/models/form/sales/pages/buyer1_ethnic_background_white.rb new file mode 100644 index 000000000..6351e50d7 --- /dev/null +++ b/app/models/form/sales/pages/buyer1_ethnic_background_white.rb @@ -0,0 +1,18 @@ +class Form::Sales::Pages::Buyer1EthnicBackgroundWhite < ::Form::Page + def initialize(id, hsh, subsection) + super + @id = "buyer_1_ethnic_background_white" + @header = "" + @description = "" + @subsection = subsection + @depends_on = [{ + "ethnic_group" => 0, + }] + end + + def questions + @questions ||= [ + Form::Sales::Questions::Buyer1EthnicBackgroundWhite.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/sales/pages/buyer1_ethnic_group.rb b/app/models/form/sales/pages/buyer1_ethnic_group.rb new file mode 100644 index 000000000..4daad4f1d --- /dev/null +++ b/app/models/form/sales/pages/buyer1_ethnic_group.rb @@ -0,0 +1,15 @@ +class Form::Sales::Pages::Buyer1EthnicGroup < ::Form::Page + def initialize(id, hsh, subsection) + super + @id = "buyer_1_ethnic_group" + @header = "" + @description = "" + @subsection = subsection + end + + def questions + @questions ||= [ + Form::Sales::Questions::Buyer1EthnicGroup.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/sales/questions/buyer1_ethnic_background_arab.rb b/app/models/form/sales/questions/buyer1_ethnic_background_arab.rb new file mode 100644 index 000000000..f6bf163b0 --- /dev/null +++ b/app/models/form/sales/questions/buyer1_ethnic_background_arab.rb @@ -0,0 +1,17 @@ +class Form::Sales::Questions::Buyer1EthnicBackgroundArab < ::Form::Question + def initialize(id, hsh, page) + super + @id = "ethnic" + @check_answer_label = "Buyer 1’s ethnic background" + @header = "Which of the following best describes the buyer 1’s Arab background?" + @type = "radio" + @answer_options = ANSWER_OPTIONS + @page = page + @hint_text = "Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest." + end + + ANSWER_OPTIONS = { + "19" => { "value" => "Arab" }, + "16" => { "value" => "Other ethnic group" }, + }.freeze +end diff --git a/app/models/form/sales/questions/buyer1_ethnic_background_asian.rb b/app/models/form/sales/questions/buyer1_ethnic_background_asian.rb new file mode 100644 index 000000000..a6b8ea64f --- /dev/null +++ b/app/models/form/sales/questions/buyer1_ethnic_background_asian.rb @@ -0,0 +1,20 @@ +class Form::Sales::Questions::Buyer1EthnicBackgroundAsian < ::Form::Question + def initialize(id, hsh, page) + super + @id = "ethnic" + @check_answer_label = "Buyer 1’s ethnic background" + @header = "Which of the following best describes the buyer 1’s Asian or Asian British background?" + @type = "radio" + @answer_options = ANSWER_OPTIONS + @page = page + @hint_text = "Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest." + end + + ANSWER_OPTIONS = { + "10" => { "value" => "Bangladeshi" }, + "15" => { "value" => "Chinese" }, + "8" => { "value" => "Indian" }, + "9" => { "value" => "Pakistani" }, + "11" => { "value" => "Any other Asian or Asian British background" }, + }.freeze +end diff --git a/app/models/form/sales/questions/buyer1_ethnic_background_black.rb b/app/models/form/sales/questions/buyer1_ethnic_background_black.rb new file mode 100644 index 000000000..841086ec3 --- /dev/null +++ b/app/models/form/sales/questions/buyer1_ethnic_background_black.rb @@ -0,0 +1,18 @@ +class Form::Sales::Questions::Buyer1EthnicBackgroundBlack < ::Form::Question + def initialize(id, hsh, page) + super + @id = "ethnic" + @check_answer_label = "Buyer 1’s ethnic background" + @header = "Which of the following best describes the buyer 1’s Black, African, Caribbean or Black British background?" + @type = "radio" + @answer_options = ANSWER_OPTIONS + @page = page + @hint_text = "Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest." + end + + ANSWER_OPTIONS = { + "13" => { "value" => "African" }, + "12" => { "value" => "Caribbean" }, + "14" => { "value" => "Any other Black, African or Caribbean background" }, + }.freeze +end diff --git a/app/models/form/sales/questions/buyer1_ethnic_background_mixed.rb b/app/models/form/sales/questions/buyer1_ethnic_background_mixed.rb new file mode 100644 index 000000000..69451e9e9 --- /dev/null +++ b/app/models/form/sales/questions/buyer1_ethnic_background_mixed.rb @@ -0,0 +1,19 @@ +class Form::Sales::Questions::Buyer1EthnicBackgroundMixed < ::Form::Question + def initialize(id, hsh, page) + super + @id = "ethnic" + @check_answer_label = "Buyer 1’s ethnic background" + @header = "Which of the following best describes the buyer 1’s Mixed or Multiple ethnic groups background?" + @type = "radio" + @answer_options = ANSWER_OPTIONS + @page = page + @hint_text = "Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest." + end + + ANSWER_OPTIONS = { + "4" => { "value" => "White and Black Caribbean" }, + "5" => { "value" => "White and Black African" }, + "6" => { "value" => "White and Asian" }, + "7" => { "value" => "Any other Mixed or Multiple ethnic background" }, + }.freeze +end diff --git a/app/models/form/sales/questions/buyer1_ethnic_background_white.rb b/app/models/form/sales/questions/buyer1_ethnic_background_white.rb new file mode 100644 index 000000000..cb9f2918f --- /dev/null +++ b/app/models/form/sales/questions/buyer1_ethnic_background_white.rb @@ -0,0 +1,19 @@ +class Form::Sales::Questions::Buyer1EthnicBackgroundWhite < ::Form::Question + def initialize(id, hsh, page) + super + @id = "ethnic" + @check_answer_label = "Buyer 1’s ethnic background" + @header = "Which of the following best describes the buyer 1’s White background?" + @type = "radio" + @answer_options = ANSWER_OPTIONS + @page = page + @hint_text = "Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest." + end + + ANSWER_OPTIONS = { + "1" => { "value" => "English, Welsh, Northern Irish, Scottish or British" }, + "2" => { "value" => "Irish" }, + "18" => { "value" => "Gypsy or Irish Traveller" }, + "3" => { "value" => "Any other White background" }, + }.freeze +end diff --git a/app/models/form/sales/questions/buyer1_ethnic_group.rb b/app/models/form/sales/questions/buyer1_ethnic_group.rb new file mode 100644 index 000000000..cb1a6da28 --- /dev/null +++ b/app/models/form/sales/questions/buyer1_ethnic_group.rb @@ -0,0 +1,28 @@ +class Form::Sales::Questions::Buyer1EthnicGroup < ::Form::Question + def initialize(id, hsh, page) + super + @id = "ethnic_group" + @check_answer_label = "Buyer 1’s ethnic group" + @header = "What is buyer 1’s ethnic group?" + @type = "radio" + @answer_options = ANSWER_OPTIONS + @page = page + @hint_text = "Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest." + @inferred_check_answers_value = { + "condition" => { + "ethnic_group" => 17, + }, + "value" => "Prefers not to say", + } + end + + ANSWER_OPTIONS = { + "0" => { "value" => "White" }, + "1" => { "value" => "Mixed or Multiple ethnic groups" }, + "2" => { "value" => "Asian or Asian British" }, + "3" => { "value" => "Black, African, Caribbean or Black British" }, + "4" => { "value" => "Arab or other ethnic group" }, + "divider" => { "value" => true }, + "17" => { "value" => "Buyer 1 prefers not to say" }, + }.freeze +end diff --git a/app/models/form/sales/subsections/household_characteristics.rb b/app/models/form/sales/subsections/household_characteristics.rb index 6a6cb2824..219547067 100644 --- a/app/models/form/sales/subsections/household_characteristics.rb +++ b/app/models/form/sales/subsections/household_characteristics.rb @@ -13,6 +13,12 @@ class Form::Sales::Subsections::HouseholdCharacteristics < ::Form::Subsection Form::Sales::Pages::GenderIdentity1.new(nil, nil, self), Form::Sales::Pages::Buyer1LiveInProperty.new(nil, nil, self), Form::Sales::Pages::Buyer2RelationshipToBuyer1.new(nil, nil, self), + Form::Sales::Pages::Buyer1EthnicGroup.new(nil, nil, self), + Form::Sales::Pages::Buyer1EthnicBackgroundBlack.new(nil, nil, self), + Form::Sales::Pages::Buyer1EthnicBackgroundAsian.new(nil, nil, self), + Form::Sales::Pages::Buyer1EthnicBackgroundArab.new(nil, nil, self), + Form::Sales::Pages::Buyer1EthnicBackgroundMixed.new(nil, nil, self), + Form::Sales::Pages::Buyer1EthnicBackgroundWhite.new(nil, nil, self), Form::Sales::Pages::Age2.new(nil, nil, self), ] end diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index f97568f59..9e38138eb 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -372,10 +372,6 @@ class LettingsLog < Log hb == 7 end - def ethnic_refused? - ethnic_group == 17 - end - def receives_housing_related_benefits? if collection_start_year <= 2021 receives_housing_benefit_only? || receives_uc_with_housing_element_excl_housing_benefit? || diff --git a/app/models/log.rb b/app/models/log.rb index e24a205fc..54770515f 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -36,6 +36,10 @@ class Log < ApplicationRecord false end + def ethnic_refused? + ethnic_group == 17 + end + private def update_status! diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 0f0ca54c7..e1458caa9 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -3,10 +3,15 @@ class SalesLogValidator < ActiveModel::Validator end class SalesLog < Log + include DerivedVariables::SalesLogVariables + self.inheritance_column = :_type_disabled + has_paper_trail validates_with SalesLogValidator + before_validation :set_derived_fields! + before_validation :reset_invalidated_dependent_fields! scope :filter_by_year, ->(year) { where(saledate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } scope :search_by, ->(param) { filter_by_id(param) } diff --git a/db/migrate/20220927100350_add_background_to_sales_log.rb b/db/migrate/20220927100350_add_background_to_sales_log.rb new file mode 100644 index 000000000..21dc70d82 --- /dev/null +++ b/db/migrate/20220927100350_add_background_to_sales_log.rb @@ -0,0 +1,6 @@ +class AddBackgroundToSalesLog < ActiveRecord::Migration[7.0] + change_table :sales_logs, bulk: true do |t| + t.column :ethnic, :integer + t.column :ethnic_group, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index fa885644b..f8dbff468 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -345,6 +345,8 @@ ActiveRecord::Schema[7.0].define(version: 2022_09_29_125204) do t.string "otherrelat2" t.integer "age2" t.integer "age2_known" + t.integer "ethnic" + t.integer "ethnic_group" t.index ["created_by_id"], name: "index_sales_logs_on_created_by_id" t.index ["managing_organisation_id"], name: "index_sales_logs_on_managing_organisation_id" t.index ["owning_organisation_id"], name: "index_sales_logs_on_owning_organisation_id" diff --git a/spec/factories/sales_log.rb b/spec/factories/sales_log.rb index 0a8a56ee0..987f69e2c 100644 --- a/spec/factories/sales_log.rb +++ b/spec/factories/sales_log.rb @@ -29,6 +29,8 @@ FactoryBot.define do age2_known { 0 } age2 { 35 } builtype { 1 } + ethnic { 3 } + ethnic_group { 12 } end end end diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index faab8e674..ac02a62a2 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -303,6 +303,12 @@ "leftreg": [ 1 ] + }, + "inferred_check_answers_value": { + "condition": { + "armedforces": 3 + }, + "value": "Prefers not to say" } }, "leftreg": { diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb index da59ad354..637407bc1 100644 --- a/spec/models/form/question_spec.rb +++ b/spec/models/form/question_spec.rb @@ -340,6 +340,29 @@ RSpec.describe Form::Question, type: :model do expect(question.answer_label(lettings_log)).to eq("£500.00 every year") end end + + context "with inferred_check_answers_value" do + context "when Lettings form" do + let(:section_id) { "household" } + let(:subsection_id) { "household_needs" } + let(:page_id) { "armed_forces" } + let(:question_id) { "armedforces" } + + it "returns the inferred label value" do + lettings_log.armedforces = 3 + expect(question.answer_label(lettings_log)).to eq("Prefers not to say") + end + end + + context "when Sales form" do + let(:sales_log) { FactoryBot.create(:sales_log, :completed, ethnic_group: 17) } + let(:question) { sales_log.form.get_question("ethnic_group", sales_log) } + + it "returns the inferred label value" do + expect(question.answer_label(sales_log)).to eq("Prefers not to say") + end + end + end end describe ".completed?" do diff --git a/spec/models/form/sales/pages/buyer1_ethnic_background_arab_spec.rb b/spec/models/form/sales/pages/buyer1_ethnic_background_arab_spec.rb new file mode 100644 index 000000000..821874a0f --- /dev/null +++ b/spec/models/form/sales/pages/buyer1_ethnic_background_arab_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Pages::Buyer1EthnicBackgroundArab, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { nil } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + it "has correct subsection" do + expect(page.subsection).to eq(subsection) + end + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ethnic]) + end + + it "has the correct id" do + expect(page.id).to eq("buyer_1_ethnic_background_arab") + end + + it "has the correct header" do + expect(page.header).to eq("") + end + + it "has the correct description" do + expect(page.description).to eq("") + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([{ "ethnic_group" => 4 }]) + end +end diff --git a/spec/models/form/sales/pages/buyer1_ethnic_background_asian_spec.rb b/spec/models/form/sales/pages/buyer1_ethnic_background_asian_spec.rb new file mode 100644 index 000000000..ad8b47846 --- /dev/null +++ b/spec/models/form/sales/pages/buyer1_ethnic_background_asian_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Pages::Buyer1EthnicBackgroundAsian, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { nil } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + it "has correct subsection" do + expect(page.subsection).to eq(subsection) + end + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ethnic]) + end + + it "has the correct id" do + expect(page.id).to eq("buyer_1_ethnic_background_asian") + end + + it "has the correct header" do + expect(page.header).to eq("") + end + + it "has the correct description" do + expect(page.description).to eq("") + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([{ "ethnic_group" => 2 }]) + end +end diff --git a/spec/models/form/sales/pages/buyer1_ethnic_background_black_spec.rb b/spec/models/form/sales/pages/buyer1_ethnic_background_black_spec.rb new file mode 100644 index 000000000..1f5618a0b --- /dev/null +++ b/spec/models/form/sales/pages/buyer1_ethnic_background_black_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Pages::Buyer1EthnicBackgroundBlack, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { nil } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + it "has correct subsection" do + expect(page.subsection).to eq(subsection) + end + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ethnic]) + end + + it "has the correct id" do + expect(page.id).to eq("buyer_1_ethnic_background_black") + end + + it "has the correct header" do + expect(page.header).to eq("") + end + + it "has the correct description" do + expect(page.description).to eq("") + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([{ "ethnic_group" => 3 }]) + end +end diff --git a/spec/models/form/sales/pages/buyer1_ethnic_background_mixed_spec.rb b/spec/models/form/sales/pages/buyer1_ethnic_background_mixed_spec.rb new file mode 100644 index 000000000..648734242 --- /dev/null +++ b/spec/models/form/sales/pages/buyer1_ethnic_background_mixed_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Pages::Buyer1EthnicBackgroundMixed, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { nil } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + it "has correct subsection" do + expect(page.subsection).to eq(subsection) + end + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ethnic]) + end + + it "has the correct id" do + expect(page.id).to eq("buyer_1_ethnic_background_mixed") + end + + it "has the correct header" do + expect(page.header).to eq("") + end + + it "has the correct description" do + expect(page.description).to eq("") + end + + it "has correct depends_on" do + expect(page.depends_on).to eq([{ "ethnic_group" => 1 }]) + end +end diff --git a/spec/models/form/sales/pages/buyer1_ethnic_background_white_spec.rb b/spec/models/form/sales/pages/buyer1_ethnic_background_white_spec.rb new file mode 100644 index 000000000..620918b0e --- /dev/null +++ b/spec/models/form/sales/pages/buyer1_ethnic_background_white_spec.rb @@ -0,0 +1,29 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Pages::Buyer1EthnicBackgroundWhite, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { nil } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + it "has correct subsection" do + expect(page.subsection).to eq(subsection) + end + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ethnic]) + end + + it "has the correct id" do + expect(page.id).to eq("buyer_1_ethnic_background_white") + end + + it "has the correct header" do + expect(page.header).to eq("") + end + + it "has the correct description" do + expect(page.description).to eq("") + end +end diff --git a/spec/models/form/sales/pages/buyer1_ethnic_group_spec.rb b/spec/models/form/sales/pages/buyer1_ethnic_group_spec.rb new file mode 100644 index 000000000..406f78676 --- /dev/null +++ b/spec/models/form/sales/pages/buyer1_ethnic_group_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Pages::Buyer1EthnicGroup, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { nil } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + it "has correct subsection" do + expect(page.subsection).to eq(subsection) + end + + it "has correct questions" do + expect(page.questions.map(&:id)).to eq(%w[ethnic_group]) + end + + it "has the correct id" do + expect(page.id).to eq("buyer_1_ethnic_group") + end + + it "has the correct header" do + expect(page.header).to eq("") + end + + it "has the correct description" do + expect(page.description).to eq("") + end + + it "has correct depends_on" do + expect(page.depends_on).to be_nil + end +end diff --git a/spec/models/form/sales/questions/buyer1_ethnic_background_arab_spec.rb b/spec/models/form/sales/questions/buyer1_ethnic_background_arab_spec.rb new file mode 100644 index 000000000..c13eda0c5 --- /dev/null +++ b/spec/models/form/sales/questions/buyer1_ethnic_background_arab_spec.rb @@ -0,0 +1,44 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::Buyer1EthnicBackgroundArab, type: :model do + subject(:question) { described_class.new(question_id, question_definition, page) } + + let(:question_id) { nil } + let(:question_definition) { nil } + let(:page) { instance_double(Form::Page) } + + it "has correct page" do + expect(question.page).to eq(page) + end + + it "has the correct id" do + expect(question.id).to eq("ethnic") + end + + it "has the correct header" do + expect(question.header).to eq("Which of the following best describes the buyer 1’s Arab background?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Buyer 1’s ethnic background") + end + + it "has the correct type" do + expect(question.type).to eq("radio") + end + + it "is not marked as derived" do + expect(question.derived?).to be false + end + + it "has the correct hint_text" do + expect(question.hint_text).to eq("Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest.") + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq({ + "16" => { "value" => "Other ethnic group" }, + "19" => { "value" => "Arab" }, + }) + end +end diff --git a/spec/models/form/sales/questions/buyer1_ethnic_background_asian_spec.rb b/spec/models/form/sales/questions/buyer1_ethnic_background_asian_spec.rb new file mode 100644 index 000000000..3fe9cee96 --- /dev/null +++ b/spec/models/form/sales/questions/buyer1_ethnic_background_asian_spec.rb @@ -0,0 +1,47 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::Buyer1EthnicBackgroundAsian, type: :model do + subject(:question) { described_class.new(question_id, question_definition, page) } + + let(:question_id) { nil } + let(:question_definition) { nil } + let(:page) { instance_double(Form::Page) } + + it "has correct page" do + expect(question.page).to eq(page) + end + + it "has the correct id" do + expect(question.id).to eq("ethnic") + end + + it "has the correct header" do + expect(question.header).to eq("Which of the following best describes the buyer 1’s Asian or Asian British background?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Buyer 1’s ethnic background") + end + + it "has the correct type" do + expect(question.type).to eq("radio") + end + + it "is not marked as derived" do + expect(question.derived?).to be false + end + + it "has the correct hint_text" do + expect(question.hint_text).to eq("Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest.") + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq({ + "10" => { "value" => "Bangladeshi" }, + "11" => { "value" => "Any other Asian or Asian British background" }, + "15" => { "value" => "Chinese" }, + "8" => { "value" => "Indian" }, + "9" => { "value" => "Pakistani" }, + }) + end +end diff --git a/spec/models/form/sales/questions/buyer1_ethnic_background_black_spec.rb b/spec/models/form/sales/questions/buyer1_ethnic_background_black_spec.rb new file mode 100644 index 000000000..28949b598 --- /dev/null +++ b/spec/models/form/sales/questions/buyer1_ethnic_background_black_spec.rb @@ -0,0 +1,45 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::Buyer1EthnicBackgroundBlack, type: :model do + subject(:question) { described_class.new(question_id, question_definition, page) } + + let(:question_id) { nil } + let(:question_definition) { nil } + let(:page) { instance_double(Form::Page) } + + it "has correct page" do + expect(question.page).to eq(page) + end + + it "has the correct id" do + expect(question.id).to eq("ethnic") + end + + it "has the correct header" do + expect(question.header).to eq("Which of the following best describes the buyer 1’s Black, African, Caribbean or Black British background?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Buyer 1’s ethnic background") + end + + it "has the correct type" do + expect(question.type).to eq("radio") + end + + it "is not marked as derived" do + expect(question.derived?).to be false + end + + it "has the correct hint_text" do + expect(question.hint_text).to eq("Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest.") + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq({ + "12" => { "value" => "Caribbean" }, + "13" => { "value" => "African" }, + "14" => { "value" => "Any other Black, African or Caribbean background" }, + }) + end +end diff --git a/spec/models/form/sales/questions/buyer1_ethnic_background_mixed_spec.rb b/spec/models/form/sales/questions/buyer1_ethnic_background_mixed_spec.rb new file mode 100644 index 000000000..48ad38fa8 --- /dev/null +++ b/spec/models/form/sales/questions/buyer1_ethnic_background_mixed_spec.rb @@ -0,0 +1,46 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::Buyer1EthnicBackgroundMixed, type: :model do + subject(:question) { described_class.new(question_id, question_definition, page) } + + let(:question_id) { nil } + let(:question_definition) { nil } + let(:page) { instance_double(Form::Page) } + + it "has correct page" do + expect(question.page).to eq(page) + end + + it "has the correct id" do + expect(question.id).to eq("ethnic") + end + + it "has the correct header" do + expect(question.header).to eq("Which of the following best describes the buyer 1’s Mixed or Multiple ethnic groups background?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Buyer 1’s ethnic background") + end + + it "has the correct type" do + expect(question.type).to eq("radio") + end + + it "is not marked as derived" do + expect(question.derived?).to be false + end + + it "has the correct hint_text" do + expect(question.hint_text).to eq("Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest.") + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq({ + "4" => { "value" => "White and Black Caribbean" }, + "5" => { "value" => "White and Black African" }, + "6" => { "value" => "White and Asian" }, + "7" => { "value" => "Any other Mixed or Multiple ethnic background" }, + }) + end +end diff --git a/spec/models/form/sales/questions/buyer1_ethnic_background_white_spec.rb b/spec/models/form/sales/questions/buyer1_ethnic_background_white_spec.rb new file mode 100644 index 000000000..4b72afb92 --- /dev/null +++ b/spec/models/form/sales/questions/buyer1_ethnic_background_white_spec.rb @@ -0,0 +1,46 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::Buyer1EthnicBackgroundWhite, type: :model do + subject(:question) { described_class.new(question_id, question_definition, page) } + + let(:question_id) { nil } + let(:question_definition) { nil } + let(:page) { instance_double(Form::Page) } + + it "has correct page" do + expect(question.page).to eq(page) + end + + it "has the correct id" do + expect(question.id).to eq("ethnic") + end + + it "has the correct header" do + expect(question.header).to eq("Which of the following best describes the buyer 1’s White background?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Buyer 1’s ethnic background") + end + + it "has the correct type" do + expect(question.type).to eq("radio") + end + + it "is not marked as derived" do + expect(question.derived?).to be false + end + + it "has the correct hint_text" do + expect(question.hint_text).to eq("Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest.") + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq({ + "1" => { "value" => "English, Welsh, Northern Irish, Scottish or British" }, + "18" => { "value" => "Gypsy or Irish Traveller" }, + "2" => { "value" => "Irish" }, + "3" => { "value" => "Any other White background" }, + }) + end +end diff --git a/spec/models/form/sales/questions/buyer1_ethnic_group_spec.rb b/spec/models/form/sales/questions/buyer1_ethnic_group_spec.rb new file mode 100644 index 000000000..4823b43c8 --- /dev/null +++ b/spec/models/form/sales/questions/buyer1_ethnic_group_spec.rb @@ -0,0 +1,49 @@ +require "rails_helper" + +RSpec.describe Form::Sales::Questions::Buyer1EthnicGroup, type: :model do + subject(:question) { described_class.new(question_id, question_definition, page) } + + let(:question_id) { nil } + let(:question_definition) { nil } + let(:page) { instance_double(Form::Page) } + + it "has correct page" do + expect(question.page).to eq(page) + end + + it "has the correct id" do + expect(question.id).to eq("ethnic_group") + end + + it "has the correct header" do + expect(question.header).to eq("What is buyer 1’s ethnic group?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Buyer 1’s ethnic group") + end + + it "has the correct type" do + expect(question.type).to eq("radio") + end + + it "is not marked as derived" do + expect(question.derived?).to be false + end + + it "has the correct hint_text" do + expect(question.hint_text).to eq("Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest.") + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq({ + "0" => { "value" => "White" }, + "1" => { "value" => "Mixed or Multiple ethnic groups" }, + "17" => { "value" => "Buyer 1 prefers not to say" }, + "2" => { "value" => "Asian or Asian British" }, + "3" => { "value" => "Black, African, Caribbean or Black British" }, + "4" => { "value" => "Arab or other ethnic group" }, + "divider" => { "value" => true }, + }) + end +end diff --git a/spec/models/form/sales/subsections/household_characteristics_spec.rb b/spec/models/form/sales/subsections/household_characteristics_spec.rb index 71a8e898d..cde59cfd5 100644 --- a/spec/models/form/sales/subsections/household_characteristics_spec.rb +++ b/spec/models/form/sales/subsections/household_characteristics_spec.rb @@ -13,7 +13,19 @@ RSpec.describe Form::Sales::Subsections::HouseholdCharacteristics, type: :model it "has correct pages" do expect(household_characteristics.pages.map(&:id)).to eq( - %w[buyer_1_age buyer_1_gender_identity buyer_1_live_in_property buyer_2_relationship_to_buyer_1 buyer_2_age], + %w[ + buyer_1_age + buyer_1_gender_identity + buyer_1_live_in_property + buyer_2_relationship_to_buyer_1 + buyer_1_ethnic_group + buyer_1_ethnic_background_black + buyer_1_ethnic_background_asian + buyer_1_ethnic_background_arab + buyer_1_ethnic_background_mixed + buyer_1_ethnic_background_white + buyer_2_age + ], ) end diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index e323fdaea..cb9258803 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -61,14 +61,14 @@ RSpec.describe FormHandler do 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(20) + expect(form.pages.count).to eq(26) 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(20) + expect(form.pages.count).to eq(26) expect(form.name).to eq("2021_2022_sales") end end diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 6251e050f..dc4a646fe 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -1,4 +1,5 @@ require "rails_helper" +require "shared/shared_examples_for_derived_fields" RSpec.describe LettingsLog do let(:owning_organisation) { FactoryBot.create(:organisation) } @@ -10,6 +11,8 @@ RSpec.describe LettingsLog do allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form) end + include_examples "shared examples for derived fields", :lettings_log + it "inherits from log" do expect(described_class).to be < Log expect(described_class).to be < ApplicationRecord diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 31a41d411..3ee033971 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -1,9 +1,12 @@ require "rails_helper" +require "shared/shared_examples_for_derived_fields" RSpec.describe SalesLog, type: :model do let(:owning_organisation) { FactoryBot.create(:organisation) } let(:created_by_user) { FactoryBot.create(:user) } + include_examples "shared examples for derived fields", :sales_log + it "inherits from log" do expect(described_class).to be < Log expect(described_class).to be < ApplicationRecord diff --git a/spec/shared/shared_examples_for_derived_fields.rb b/spec/shared/shared_examples_for_derived_fields.rb new file mode 100644 index 000000000..6382ef4f4 --- /dev/null +++ b/spec/shared/shared_examples_for_derived_fields.rb @@ -0,0 +1,17 @@ +require "rails_helper" + +RSpec.shared_examples "shared examples for derived fields" do |log_type| + describe "sets ethnic based on the value of ethnic_refused" do + it "is set to 17 when ethnic_group is 17" do + log = FactoryBot.build(log_type, ethnic_group: 17, ethnic: nil) + + expect { log.set_derived_fields! }.to change(log, :ethnic).from(nil).to(17) + end + + it "is is not modified otherwise" do + log = FactoryBot.build(log_type, ethnic_group: nil, ethnic: nil) + + expect { log.set_derived_fields! }.not_to change(log, :ethnic) + end + end +end