Browse Source

Merge branch 'main' into CLDC-510-income-ranges

pull/58/head
baarkerlounger 4 years ago
parent
commit
213bda7090
  1. 10
      Gemfile.lock
  2. 53
      app/models/case_log.rb
  3. 10
      config/forms/2021_2022.json
  4. 8
      db/migrate/20211026123542_change_to_person1_gender_age.rb
  5. 7
      db/migrate/20211027091521_rename_person1_fields.rb
  6. 8
      db/schema.rb
  7. 25
      docs/api/DLUHC-CORE-Data.v1.json
  8. 2
      spec/factories/case_log.rb
  9. 87
      spec/features/case_log_spec.rb
  10. 8
      spec/fixtures/complete_case_log.json
  11. 10
      spec/fixtures/forms/test_form.json
  12. 2
      spec/helpers/check_answers_helper_spec.rb
  13. 77
      spec/models/case_log_spec.rb
  14. 6
      spec/models/form_spec.rb
  15. 14
      spec/requests/case_log_controller_spec.rb

10
Gemfile.lock

@ -26,7 +26,7 @@ GIT
GIT GIT
remote: https://github.com/rspec/rspec-rails.git remote: https://github.com/rspec/rspec-rails.git
revision: 3f0e35085f5765decf96fee179ec9a2e132a67c1 revision: cfe4db707cc5a0c9437aa90e3059256f30368da4
branch: main branch: main
specs: specs:
rspec-rails (5.1.0.pre) rspec-rails (5.1.0.pre)
@ -40,7 +40,7 @@ GIT
GIT GIT
remote: https://github.com/rspec/rspec-support.git remote: https://github.com/rspec/rspec-support.git
revision: 28526172c42302858c18681d7d7580490d885b4d revision: 2a17194b9fc868a4b4a41d7168103dca825c3004
branch: main branch: main
specs: specs:
rspec-support (3.11.0.pre) rspec-support (3.11.0.pre)
@ -128,8 +128,9 @@ GEM
msgpack (~> 1.0) msgpack (~> 1.0)
builder (3.2.4) builder (3.2.4)
byebug (11.1.3) byebug (11.1.3)
capybara (3.35.3) capybara (3.36.0)
addressable addressable
matrix
mini_mime (>= 0.1.3) mini_mime (>= 0.1.3)
nokogiri (~> 1.8) nokogiri (~> 1.8)
rack (>= 1.6.0) rack (>= 1.6.0)
@ -171,7 +172,7 @@ GEM
formtastic_i18n (0.7.0) formtastic_i18n (0.7.0)
globalid (0.5.2) globalid (0.5.2)
activesupport (>= 5.0) activesupport (>= 5.0)
govuk-components (2.1.3) govuk-components (2.1.4)
activemodel (>= 6.0) activemodel (>= 6.0)
railties (>= 6.0) railties (>= 6.0)
view_component (~> 2.39.0) view_component (~> 2.39.0)
@ -220,6 +221,7 @@ GEM
mail (2.7.1) mail (2.7.1)
mini_mime (>= 0.1.1) mini_mime (>= 0.1.1)
marcel (1.0.2) marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0) method_source (1.0.0)
mini_mime (1.1.2) mini_mime (1.1.2)
minitest (5.14.4) minitest (5.14.4)

53
app/models/case_log.rb

@ -3,9 +3,9 @@ class CaseLogValidator < ActiveModel::Validator
# followed by field name this is how the metaprogramming of the method # followed by field name this is how the metaprogramming of the method
# name being call in the validate method works. # name being call in the validate method works.
def validate_tenant_age(record) def validate_person_1_age(record)
if record.tenant_age && !/^[1-9][0-9]?$|^120$/.match?(record.tenant_age.to_s) if record.person_1_age && !/^[1-9][0-9]?$|^120$/.match?(record.person_1_age.to_s)
record.errors.add :tenant_age, "Tenant age must be between 0 and 120" record.errors.add :person_1_age, "Tenant age must be between 0 and 120"
end end
end end
@ -67,6 +67,39 @@ class CaseLogValidator < ActiveModel::Validator
end end
end end
EMPLOYED_STATUSES = ["Full-time - 30 hours or more", "Part-time - Less than 30 hours"].freeze
def validate_net_income_uc_proportion(record)
(1..8).any? do |n|
economic_status = record["person_#{n}_economic_status"]
is_employed = EMPLOYED_STATUSES.include?(economic_status)
relationship = record["person_#{n}_relationship"]
is_partner_or_main = relationship == "Partner" || (relationship.nil? && economic_status.present?)
if is_employed && is_partner_or_main && record.net_income_uc_proportion == "All"
record.errors.add :net_income_uc_proportion, "income is from Universal Credit, state pensions or benefits cannot be All if the tenant or the partner works part or full time"
end
end
end
def validate_household_pregnancy(record)
if (record.pregnancy == "Yes" || record.pregnancy == "Prefer not to say") && !women_of_child_bearing_age_in_household(record)
record.errors.add :pregnancy, "You must answer no as there are no female tenants aged 16-50 in the property"
end
end
def validate_fixed_term_tenancy(record)
is_present = record.fixed_term_tenancy.present?
is_in_range = record.fixed_term_tenancy.to_i.between?(2, 99)
is_secure = record.tenancy_type == "Fixed term – Secure"
is_ast = record.tenancy_type == "Fixed term – Assured Shorthold Tenancy (AST)"
conditions = [
{ condition: !(is_secure || is_ast) && is_present, error: "You must only answer the fixed term tenancy length question if the tenancy type is fixed term" },
{ condition: is_ast && !is_in_range, error: "Fixed term – Assured Shorthold Tenancy (AST) should be between 2 and 99 years" },
{ condition: is_secure && (!is_in_range && is_present), error: "Fixed term – Secure should be between 2 and 99 years or not specified" },
]
conditions.each { |condition| condition[:condition] ? (record.errors.add :fixed_term_tenancy, condition[:error]) : nil }
end
def validate(record) def validate(record)
# If we've come from the form UI we only want to validate the specific fields # If we've come from the form UI we only want to validate the specific fields
# that have just been submitted. If we're submitting a log via API or Bulk Upload # that have just been submitted. If we're submitting a log via API or Bulk Upload
@ -83,6 +116,16 @@ class CaseLogValidator < ActiveModel::Validator
validation_methods.each { |meth| public_send(meth, record) } validation_methods.each { |meth| public_send(meth, record) }
end end
end end
private
def women_of_child_bearing_age_in_household(record)
(1..8).any? do |n|
next if record["person_#{n}_gender"].nil? || record["person_#{n}_age"].nil?
record["person_#{n}_gender"] == "Female" && record["person_#{n}_age"] >= 16 && record["person_#{n}_age"] <= 50
end
end
end end
class CaseLog < ApplicationRecord class CaseLog < ApplicationRecord
@ -170,6 +213,10 @@ private
dynamically_not_required << "net_income_frequency" dynamically_not_required << "net_income_frequency"
end end
if tenancy_type == "Fixed term – Secure"
dynamically_not_required << "fixed_term_tenancy"
end
required.delete_if { |key, _value| dynamically_not_required.include?(key) } required.delete_if { |key, _value| dynamically_not_required.include?(key) }
end end
end end

10
config/forms/2021_2022.json

@ -21,11 +21,11 @@
} }
} }
}, },
"tenant_age": { "person_1_age": {
"header": "", "header": "",
"description": "", "description": "",
"questions": { "questions": {
"tenant_age": { "person_1_age": {
"check_answer_label": "Tenant's age", "check_answer_label": "Tenant's age",
"header": "What is the tenant's age?", "header": "What is the tenant's age?",
"hint_text": "", "hint_text": "",
@ -36,11 +36,11 @@
} }
} }
}, },
"tenant_gender": { "person_1_gender": {
"header": "", "header": "",
"description": "", "description": "",
"questions": { "questions": {
"tenant_gender": { "person_1_gender": {
"check_answer_label": "Tenant's gender", "check_answer_label": "Tenant's gender",
"header": "Which of these best describes the tenant's gender identity?", "header": "Which of these best describes the tenant's gender identity?",
"hint_text": "", "hint_text": "",
@ -121,7 +121,7 @@
"header": "", "header": "",
"description": "", "description": "",
"questions": { "questions": {
"tenant_economic_status": { "person_1_economic_status": {
"check_answer_label": "Work", "check_answer_label": "Work",
"header": "Which of these best describes the tenant's working situation?", "header": "Which of these best describes the tenant's working situation?",
"hint_text": "", "hint_text": "",

8
db/migrate/20211026123542_change_to_person1_gender_age.rb

@ -0,0 +1,8 @@
class ChangeToPerson1GenderAge < ActiveRecord::Migration[6.1]
def change
change_table :case_logs, bulk: true do |t|
t.rename :tenant_age, :person_1_age
t.rename :tenant_gender, :person_1_gender
end
end
end

7
db/migrate/20211027091521_rename_person1_fields.rb

@ -0,0 +1,7 @@
class RenamePerson1Fields < ActiveRecord::Migration[6.1]
def change
change_table :case_logs, bulk: true do |t|
t.rename :tenant_economic_status, :person_1_economic_status
end
end
end

8
db/schema.rb

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_10_22_122436) do ActiveRecord::Schema.define(version: 2021_10_27_091521) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -46,13 +46,13 @@ ActiveRecord::Schema.define(version: 2021_10_22_122436) do
t.datetime "created_at", precision: 6, null: false t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false
t.string "tenant_code" t.string "tenant_code"
t.integer "tenant_age" t.integer "person_1_age"
t.string "tenant_gender" t.string "person_1_gender"
t.string "tenant_ethnic_group" t.string "tenant_ethnic_group"
t.string "tenant_nationality" t.string "tenant_nationality"
t.string "previous_housing_situation" t.string "previous_housing_situation"
t.string "armed_forces" t.string "armed_forces"
t.string "tenant_economic_status" t.string "person_1_economic_status"
t.integer "household_number_of_other_members" t.integer "household_number_of_other_members"
t.string "person_2_relationship" t.string "person_2_relationship"
t.integer "person_2_age" t.integer "person_2_age"

25
docs/api/DLUHC-CORE-Data.v1.json

@ -274,7 +274,7 @@
"tenant_nationality": "UK national resident in UK", "tenant_nationality": "UK national resident in UK",
"previous_housing_situation": "Private sector tenancy", "previous_housing_situation": "Private sector tenancy",
"armed_forces": "Yes - a regular", "armed_forces": "Yes - a regular",
"tenant_economic_status": "Full-time - 30 hours or more", "person_1_economic_status": "Full-time - 30 hours or more",
"household_number_of_other_members": 7, "household_number_of_other_members": 7,
"person_2_relationship": "Partner", "person_2_relationship": "Partner",
"person_2_age": 32, "person_2_age": 32,
@ -388,13 +388,13 @@
"type": "string", "type": "string",
"minLength": 1 "minLength": 1
}, },
"tenant_age": { "person_1_age": {
"type": "number", "type": "number",
"description": "The age of the lead tenant", "description": "The age of the lead tenant",
"maximum": 120, "maximum": 120,
"minimum": 0 "minimum": 0
}, },
"tenant_gender": { "person_1_gender": {
"type": "string", "type": "string",
"minLength": 1, "minLength": 1,
"enum": [ "enum": [
@ -459,7 +459,7 @@
"type": "string", "type": "string",
"minLength": 1 "minLength": 1
}, },
"tenant_economic_status": { "person_1_economic_status": {
"type": "string", "type": "string",
"minLength": 1, "minLength": 1,
"enum": [ "enum": [
@ -873,7 +873,8 @@
}, },
"fixed_term_tenancy": { "fixed_term_tenancy": {
"type": "string", "type": "string",
"minLength": 1 "minLength": 1,
"pattern": "((?!1|0)([0-9][0-9]))+"
}, },
"tenancy_type": { "tenancy_type": {
"type": "string", "type": "string",
@ -945,7 +946,13 @@
}, },
"net_income_uc_proportion": { "net_income_uc_proportion": {
"type": "string", "type": "string",
"minLength": 1 "minLength": 1,
"enum": [
"All",
"Some",
"None",
"Do not know"
]
}, },
"housing_benefit": { "housing_benefit": {
"type": "string", "type": "string",
@ -1086,13 +1093,13 @@
}, },
"required": [ "required": [
"tenant_code", "tenant_code",
"tenant_age", "person_1_age",
"tenant_gender", "person_1_gender",
"tenant_ethnic_group", "tenant_ethnic_group",
"tenant_nationality", "tenant_nationality",
"previous_housing_situation", "previous_housing_situation",
"armed_forces", "armed_forces",
"tenant_economic_status", "person_1_economic_status",
"household_number_of_other_members", "household_number_of_other_members",
"person_2_relationship", "person_2_relationship",
"person_2_age", "person_2_age",

2
spec/factories/case_log.rb

@ -6,7 +6,7 @@ FactoryBot.define do
tenant_code { "TH356" } tenant_code { "TH356" }
property_postcode { "SW2 6HI" } property_postcode { "SW2 6HI" }
previous_postcode { "P0 5ST" } previous_postcode { "P0 5ST" }
tenant_age { "12" } person_1_age { "12" }
end end
trait :completed do trait :completed do
status { 2 } status { 2 }

87
spec/features/case_log_spec.rb

@ -7,8 +7,8 @@ RSpec.describe "Test Features" do
question_answers = { question_answers = {
tenant_code: { type: "text", answer: "BZ737" }, tenant_code: { type: "text", answer: "BZ737" },
tenant_age: { type: "numeric", answer: 25 }, person_1_age: { type: "numeric", answer: 25 },
tenant_gender: { type: "radio", answer: "Female" }, person_1_gender: { type: "radio", answer: "Female" },
household_number_of_other_members: { type: "numeric", answer: 2 }, household_number_of_other_members: { type: "numeric", answer: 2 },
} }
@ -93,9 +93,52 @@ RSpec.describe "Test Features" do
) )
end end
context "Validate pregnancy questions" do
it "Cannot answer yes if no female tenants" do
expect {
CaseLog.create!(pregnancy: "Yes",
person_1_gender: "Male",
person_1_age: 20)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Cannot answer yes if no female tenants within age range" do
expect {
CaseLog.create!(pregnancy: "Yes",
person_1_gender: "Female",
person_1_age: 51)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Cannot answer prefer not to say if no valid tenants" do
expect {
CaseLog.create!(pregnancy: "Prefer not to say",
person_1_gender: "Male",
person_1_age: 20)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Can answer yes if valid tenants" do
expect {
CaseLog.create!(pregnancy: "Yes",
person_1_gender: "Female",
person_1_age: 20)
}.not_to raise_error
end
it "Can answer yes if valid second tenant" do
expect {
CaseLog.create!(pregnancy: "Yes",
person_1_gender: "Male", person_1_age: 99,
person_2_gender: "Female",
person_2_age: 20)
}.not_to raise_error
end
end
it "can be accessed by url" do it "can be accessed by url" do
visit("/case_logs/#{id}/tenant_age") visit("/case_logs/#{id}/person_1_age")
expect(page).to have_field("case-log-tenant-age-field") expect(page).to have_field("case-log-person-1-age-field")
end end
it "updates model attributes correctly for each question" do it "updates model attributes correctly for each question" do
@ -140,8 +183,8 @@ RSpec.describe "Test Features" do
end end
it "displays text answers in inputs if they are already saved" do it "displays text answers in inputs if they are already saved" do
visit("/case_logs/#{id}/tenant_age") visit("/case_logs/#{id}/person_1_age")
expect(page).to have_field("case-log-tenant-age-field", with: "12") expect(page).to have_field("case-log-person-1-age-field", with: "12")
end end
it "displays checkbox answers in inputs if they are already saved" do it "displays checkbox answers in inputs if they are already saved" do
@ -173,7 +216,7 @@ RSpec.describe "Test Features" do
it "go back to tenant code page from tenant age page", js: true do it "go back to tenant code page from tenant age page", js: true do
visit("/case_logs/#{id}/tenant_code") visit("/case_logs/#{id}/tenant_code")
click_button("Save and continue") click_button("Save and continue")
visit("/case_logs/#{id}/tenant_age") visit("/case_logs/#{id}/person_1_age")
click_link(text: "Back") click_link(text: "Back")
expect(page).to have_field("case-log-tenant-code-field") expect(page).to have_field("case-log-tenant-code-field")
end end
@ -218,8 +261,8 @@ RSpec.describe "Test Features" do
end end
it "should display answers given by the user for the question in the subsection" do it "should display answers given by the user for the question in the subsection" do
fill_in_number_question(empty_case_log.id, "tenant_age", 28) fill_in_number_question(empty_case_log.id, "person_1_age", 28)
choose("case-log-tenant-gender-non-binary-field") choose("case-log-person-1-gender-non-binary-field")
click_button("Save and continue") click_button("Save and continue")
visit("/case_logs/#{empty_case_log.id}/#{subsection}/check_answers") visit("/case_logs/#{empty_case_log.id}/#{subsection}/check_answers")
expect(page).to have_content("28") expect(page).to have_content("28")
@ -230,15 +273,15 @@ RSpec.describe "Test Features" do
visit("case_logs/#{empty_case_log.id}/#{subsection}/check_answers") visit("case_logs/#{empty_case_log.id}/#{subsection}/check_answers")
assert_selector "a", text: /Answer\z/, count: 4 assert_selector "a", text: /Answer\z/, count: 4
assert_selector "a", text: "Change", count: 0 assert_selector "a", text: "Change", count: 0
expect(page).to have_link("Answer", href: "/case_logs/#{empty_case_log.id}/tenant_age") expect(page).to have_link("Answer", href: "/case_logs/#{empty_case_log.id}/person_1_age")
end end
it "should have a change link for answered questions" do it "should have a change link for answered questions" do
fill_in_number_question(empty_case_log.id, "tenant_age", 28) fill_in_number_question(empty_case_log.id, "person_1_age", 28)
visit("/case_logs/#{empty_case_log.id}/#{subsection}/check_answers") visit("/case_logs/#{empty_case_log.id}/#{subsection}/check_answers")
assert_selector "a", text: /Answer\z/, count: 3 assert_selector "a", text: /Answer\z/, count: 3
assert_selector "a", text: "Change", count: 1 assert_selector "a", text: "Change", count: 1
expect(page).to have_link("Change", href: "/case_logs/#{empty_case_log.id}/tenant_age") expect(page).to have_link("Change", href: "/case_logs/#{empty_case_log.id}/person_1_age")
end end
it "should have a link pointing to the first question if no questions are answered" do it "should have a link pointing to the first question if no questions are answered" do
@ -317,19 +360,19 @@ RSpec.describe "Test Features" do
describe "Question validation" do describe "Question validation" do
context "given an invalid tenant age" do context "given an invalid tenant age" do
it " of less than 0 it shows validation" do it " of less than 0 it shows validation" do
visit("/case_logs/#{id}/tenant_age") visit("/case_logs/#{id}/person_1_age")
fill_in_number_question(empty_case_log.id, "tenant_age", -5) fill_in_number_question(empty_case_log.id, "person_1_age", -5)
expect(page).to have_selector("#error-summary-title") expect(page).to have_selector("#error-summary-title")
expect(page).to have_selector("#case-log-tenant-age-error") expect(page).to have_selector("#case-log-person-1-age-error")
expect(page).to have_selector("#case-log-tenant-age-field-error") expect(page).to have_selector("#case-log-person-1-age-field-error")
end end
it " of greater than 120 it shows validation" do it " of greater than 120 it shows validation" do
visit("/case_logs/#{id}/tenant_age") visit("/case_logs/#{id}/person_1_age")
fill_in_number_question(empty_case_log.id, "tenant_age", 121) fill_in_number_question(empty_case_log.id, "person_1_age", 121)
expect(page).to have_selector("#error-summary-title") expect(page).to have_selector("#error-summary-title")
expect(page).to have_selector("#case-log-tenant-age-error") expect(page).to have_selector("#case-log-person-1-age-error")
expect(page).to have_selector("#case-log-tenant-age-field-error") expect(page).to have_selector("#case-log-person-1-age-field-error")
end end
end end
end end
@ -363,8 +406,8 @@ RSpec.describe "Test Features" do
end end
it "can route based on multiple conditions" do it "can route based on multiple conditions" do
visit("/case_logs/#{id}/tenant_gender") visit("/case_logs/#{id}/person_1_gender")
choose("case-log-tenant-gender-female-field", allow_label_click: true) choose("case-log-person-1-gender-female-field", allow_label_click: true)
click_button("Save and continue") click_button("Save and continue")
visit("/case_logs/#{id}/conditional_question") visit("/case_logs/#{id}/conditional_question")
choose("case-log-pregnancy-yes-field", allow_label_click: true) choose("case-log-pregnancy-yes-field", allow_label_click: true)

8
spec/fixtures/complete_case_log.json vendored

@ -2,13 +2,13 @@
"case_log": "case_log":
{ {
"tenant_code": "T657", "tenant_code": "T657",
"tenant_age": 35, "person_1_age": 35,
"tenant_gender": "Female", "person_1_gender": "Female",
"tenant_ethnic_group": "White: English/Scottish/Welsh/Northern Irish/British", "tenant_ethnic_group": "White: English/Scottish/Welsh/Northern Irish/British",
"tenant_nationality": "UK national resident in UK", "tenant_nationality": "UK national resident in UK",
"previous_housing_situation": "Private sector tenancy", "previous_housing_situation": "Private sector tenancy",
"armed_forces": "Yes - a regular", "armed_forces": "Yes - a regular",
"tenant_economic_status": "Full-time - 30 hours or more", "person_1_economic_status": "Full-time - 30 hours or more",
"household_number_of_other_members": 7, "household_number_of_other_members": 7,
"person_2_relationship": "Partner", "person_2_relationship": "Partner",
"person_2_age": 32, "person_2_age": 32,
@ -51,7 +51,7 @@
"tenancy_code": "BZ757", "tenancy_code": "BZ757",
"tenancy_start_date": "12/03/2019", "tenancy_start_date": "12/03/2019",
"starter_tenancy": "No", "starter_tenancy": "No",
"fixed_term_tenancy": "No", "fixed_term_tenancy": "5",
"tenancy_type": "Fixed term – Secure", "tenancy_type": "Fixed term – Secure",
"letting_type": "Affordable Rent - General Needs", "letting_type": "Affordable Rent - General Needs",
"letting_provider": "This landlord", "letting_provider": "This landlord",

10
spec/fixtures/forms/test_form.json vendored

@ -16,9 +16,9 @@
} }
} }
}, },
"tenant_age": { "person_1_age": {
"questions": { "questions": {
"tenant_age": { "person_1_age": {
"check_answer_label": "Tenant's age", "check_answer_label": "Tenant's age",
"header": "What is the tenant's age?", "header": "What is the tenant's age?",
"type": "numeric", "type": "numeric",
@ -28,9 +28,9 @@
} }
} }
}, },
"tenant_gender": { "person_1_gender": {
"questions": { "questions": {
"tenant_gender": { "person_1_gender": {
"check_answer_label": "Tenant's gender", "check_answer_label": "Tenant's gender",
"header": "Which of these best describes the tenant's gender identity?", "header": "Which of these best describes the tenant's gender identity?",
"type": "radio", "type": "radio",
@ -252,7 +252,7 @@
} }
}, },
"conditional_route_to": { "conditional_route_to": {
"rent": { "pregnancy": "Yes", "tenant_gender": "Female" }, "rent": { "pregnancy": "Yes", "person_1_gender": "Female" },
"conditional_question_yes_page": { "pregnancy": "Yes" }, "conditional_question_yes_page": { "pregnancy": "Yes" },
"conditional_question_no_page": { "pregnancy": "No" } "conditional_question_no_page": { "pregnancy": "No" }
}, },

2
spec/helpers/check_answers_helper_spec.rb

@ -153,7 +153,7 @@ RSpec.describe CheckAnswersHelper do
it "it includes conditional pages and questions that were displayed" do it "it includes conditional pages and questions that were displayed" do
case_log["pregnancy"] = "Yes" case_log["pregnancy"] = "Yes"
case_log["tenant_gender"] = "Female" case_log["person_1_gender"] = "Female"
result = total_questions(conditional_routing_subsection, case_log, form) result = total_questions(conditional_routing_subsection, case_log, form)
expected_keys = %w[pregnancy] expected_keys = %w[pregnancy]
expect(result.keys).to match_array(expected_keys) expect(result.keys).to match_array(expected_keys)

77
spec/models/case_log_spec.rb

@ -3,15 +3,15 @@ require "rails_helper"
RSpec.describe Form, type: :model do RSpec.describe Form, type: :model do
describe "#new" do describe "#new" do
it "validates age is a number" do it "validates age is a number" do
expect { CaseLog.create!(tenant_age: "random") }.to raise_error(ActiveRecord::RecordInvalid) expect { CaseLog.create!(person_1_age: "random") }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validates age is under 120" do it "validates age is under 120" do
expect { CaseLog.create!(tenant_age: 121) }.to raise_error(ActiveRecord::RecordInvalid) expect { CaseLog.create!(person_1_age: 121) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validates age is over 0" do it "validates age is over 0" do
expect { CaseLog.create!(tenant_age: 0) }.to raise_error(ActiveRecord::RecordInvalid) expect { CaseLog.create!(person_1_age: 0) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "validates number of relets is a number" do it "validates number of relets is a number" do
@ -106,20 +106,87 @@ RSpec.describe Form, type: :model do
end end
context "armed forces injured validation" do context "armed forces injured validation" do
it "must be anwered if tenant was a regular or reserve in armed forces" do it "must be answered if tenant was a regular or reserve in armed forces" do
expect { expect {
CaseLog.create!(armed_forces: "Yes - a regular", CaseLog.create!(armed_forces: "Yes - a regular",
armed_forces_injured: nil) armed_forces_injured: nil)
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
it "must be anwered if tenant was not a regular or reserve in armed forces" do it "must be answered if tenant was not a regular or reserve in armed forces" do
expect { expect {
CaseLog.create!(armed_forces: "No", CaseLog.create!(armed_forces: "No",
armed_forces_injured: "Yes") armed_forces_injured: "Yes")
}.to raise_error(ActiveRecord::RecordInvalid) }.to raise_error(ActiveRecord::RecordInvalid)
end end
end end
context "tenant’s income is from Universal Credit, state pensions or benefits" do
it "Cannot be All if person 1 works full time" do
expect {
CaseLog.create!(net_income_uc_proportion: "All", person_1_economic_status: "Full-time - 30 hours or more")
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Cannot be All if person 1 works part time" do
expect {
CaseLog.create!(net_income_uc_proportion: "All", person_1_economic_status: "Part-time - Less than 30 hours")
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Cannot be 1 All if any of persons 2-4 are person 1's partner and work part or full time" do
expect {
CaseLog.create!(net_income_uc_proportion: "All", person_2_relationship: "Partner", person_2_economic_status: "Part-time - Less than 30 hours")
}.to raise_error(ActiveRecord::RecordInvalid)
end
end
context "fixed term tenancy length" do
it "Must not be completed if Type of main tenancy is not responded with either Secure or Assured shorthold " do
expect {
CaseLog.create!(tenancy_type: "Other",
fixed_term_tenancy: 10)
}.to raise_error(ActiveRecord::RecordInvalid)
end
it "Must be completed and between 2 and 99 if type of tenancy is Assured shorthold" do
expect {
CaseLog.create!(tenancy_type: "Fixed term – Assured Shorthold Tenancy (AST)",
fixed_term_tenancy: 1)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed term – Assured Shorthold Tenancy (AST)",
fixed_term_tenancy: nil)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed term – Assured Shorthold Tenancy (AST)",
fixed_term_tenancy: 2)
}.not_to raise_error
end
it "Must be empty or between 2 and 99 if type of tenancy is Secure" do
expect {
CaseLog.create!(tenancy_type: "Fixed term – Secure",
fixed_term_tenancy: 1)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed term – Secure",
fixed_term_tenancy: 100)
}.to raise_error(ActiveRecord::RecordInvalid)
expect {
CaseLog.create!(tenancy_type: "Fixed term – Secure",
fixed_term_tenancy: nil)
}.not_to raise_error
expect {
CaseLog.create!(tenancy_type: "Fixed term – Secure",
fixed_term_tenancy: 2)
}.not_to raise_error
end
end
end end
describe "status" do describe "status" do

6
spec/models/form_spec.rb

@ -5,9 +5,9 @@ RSpec.describe Form, type: :model do
let(:form) { form_handler.get_form("test_form") } let(:form) { form_handler.get_form("test_form") }
describe ".next_page" do describe ".next_page" do
let(:previous_page) { "tenant_age" } let(:previous_page) { "person_1_age" }
it "returns the next page given the previous" do it "returns the next page given the previous" do
expect(form.next_page(previous_page)).to eq("tenant_gender") expect(form.next_page(previous_page)).to eq("person_1_gender")
end end
end end
@ -20,7 +20,7 @@ RSpec.describe Form, type: :model do
describe ".previous_page" do describe ".previous_page" do
context "given a page in the middle of a subsection" do context "given a page in the middle of a subsection" do
let(:current_page) { "tenant_age" } let(:current_page) { "person_1_age" }
it "returns the previous page given the current" do it "returns the previous page given the current" do
expect(form.previous_page(current_page)).to eq("tenant_code") expect(form.previous_page(current_page)).to eq("tenant_code")
end end

14
spec/requests/case_log_controller_spec.rb

@ -24,7 +24,7 @@ RSpec.describe CaseLogsController, type: :request do
describe "POST #create" do describe "POST #create" do
let(:tenant_code) { "T365" } let(:tenant_code) { "T365" }
let(:tenant_age) { 35 } let(:person_1_age) { 35 }
let(:property_number_of_times_relet) { 12 } let(:property_number_of_times_relet) { 12 }
let(:property_postcode) { "SE11 6TY" } let(:property_postcode) { "SE11 6TY" }
let(:in_progress) { "in_progress" } let(:in_progress) { "in_progress" }
@ -33,7 +33,7 @@ RSpec.describe CaseLogsController, type: :request do
let(:params) do let(:params) do
{ {
"tenant_code": tenant_code, "tenant_code": tenant_code,
"tenant_age": tenant_age, "person_1_age": person_1_age,
"property_postcode": property_postcode, "property_postcode": property_postcode,
"property_number_of_times_relet": property_number_of_times_relet, "property_number_of_times_relet": property_number_of_times_relet,
} }
@ -55,18 +55,18 @@ RSpec.describe CaseLogsController, type: :request do
it "creates a case log with the values passed" do it "creates a case log with the values passed" do
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response["tenant_code"]).to eq(tenant_code) expect(json_response["tenant_code"]).to eq(tenant_code)
expect(json_response["tenant_age"]).to eq(tenant_age) expect(json_response["person_1_age"]).to eq(person_1_age)
expect(json_response["property_postcode"]).to eq(property_postcode) expect(json_response["property_postcode"]).to eq(property_postcode)
end end
context "invalid json params" do context "invalid json params" do
let(:tenant_age) { 2000 } let(:person_1_age) { 2000 }
let(:property_number_of_times_relet) { 21 } let(:property_number_of_times_relet) { 21 }
it "validates case log parameters" do it "validates case log parameters" do
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
expect(json_response["errors"]).to match_array([["property_number_of_times_relet", ["Must be between 0 and 20"]], ["tenant_age", ["Tenant age must be between 0 and 120"]]]) expect(json_response["errors"]).to match_array([["property_number_of_times_relet", ["Must be between 0 and 20"]], ["person_1_age", ["Tenant age must be between 0 and 120"]]])
end end
end end
@ -157,7 +157,7 @@ RSpec.describe CaseLogsController, type: :request do
end end
context "invalid case log params" do context "invalid case log params" do
let(:params) { { tenant_age: 200 } } let(:params) { { person_1_age: 200 } }
it "returns 422" do it "returns 422" do
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:unprocessable_entity)
@ -165,7 +165,7 @@ RSpec.describe CaseLogsController, type: :request do
it "returns an error message" do it "returns an error message" do
json_response = JSON.parse(response.body) json_response = JSON.parse(response.body)
expect(json_response["errors"]).to eq({ "tenant_age" => ["Tenant age must be between 0 and 120"] }) expect(json_response["errors"]).to eq({ "person_1_age" => ["Tenant age must be between 0 and 120"] })
end end
end end

Loading…
Cancel
Save