Submit social housing lettings and sales data (CORE)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

369 lines
14 KiB

require "rails_helper"
require "shared/shared_examples_for_derived_fields"
RSpec.describe SalesLog, type: :model do
let(:owning_organisation) { create(:organisation) }
let(:created_by_user) { 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
end
it "is a sales log" do
sales_log = build(:sales_log, created_by: created_by_user)
expect(sales_log.lettings?).to be false
end
describe "#new" do
context "when creating a record" do
let(:sales_log) do
described_class.create
end
it "attaches the correct custom validator" do
expect(sales_log._validators.values.flatten.map(&:class))
.to include(SalesLogValidator)
end
end
end
describe "#update" do
let(:sales_log) { FactoryBot.create(:sales_log, created_by: created_by_user) }
let(:validator) { sales_log._validators[nil].first }
after do
sales_log.update(age1: 25)
end
it "validates other household member details" do
expect(validator).to receive(:validate_household_number_of_other_members)
end
end
describe "#optional_fields" do
let(:sales_log) { build(:sales_log) }
it "returns optional fields" do
expect(sales_log.optional_fields).to eq(%w[saledate_check purchid monthly_charges_value_check old_persons_shared_ownership_value_check])
end
end
describe "#form" do
let(:sales_log) { build(:sales_log, created_by: created_by_user) }
let(:sales_log_2) { build(:sales_log, saledate: Time.zone.local(2022, 5, 1), created_by: created_by_user) }
it "has returns the correct form based on the start date" do
expect(sales_log.form_name).to be_nil
expect(sales_log.form).to be_a(Form)
expect(sales_log_2.form_name).to eq("current_sales")
expect(sales_log_2.form).to be_a(Form)
end
end
describe "status" do
let!(:empty_sales_log) { create(:sales_log) }
let!(:in_progress_sales_log) { create(:sales_log, :in_progress) }
let!(:completed_sales_log) { create(:sales_log, :completed) }
it "is set to not started for an empty sales log" do
expect(empty_sales_log.not_started?).to be(true)
expect(empty_sales_log.in_progress?).to be(false)
expect(empty_sales_log.completed?).to be(false)
end
it "is set to in progress for a started sales log" do
expect(in_progress_sales_log.in_progress?).to be(true)
expect(in_progress_sales_log.not_started?).to be(false)
expect(in_progress_sales_log.completed?).to be(false)
end
it "is set to completed for a completed sales log" do
expect(completed_sales_log.in_progress?).to be(false)
expect(completed_sales_log.not_started?).to be(false)
expect(completed_sales_log.completed?).to be(true)
end
end
context "when filtering by organisation" do
let(:organisation_1) { create(:organisation) }
let(:organisation_2) { create(:organisation) }
let(:organisation_3) { create(:organisation) }
before do
create(:sales_log, :in_progress, owning_organisation: organisation_1)
create(:sales_log, :completed, owning_organisation: organisation_1)
create(:sales_log, :completed, owning_organisation: organisation_2)
end
it "filters by given organisation" do
expect(described_class.filter_by_organisation([organisation_1]).count).to eq(2)
expect(described_class.filter_by_organisation([organisation_1, organisation_2]).count).to eq(3)
expect(described_class.filter_by_organisation([organisation_3]).count).to eq(0)
end
end
Cldc 1776 sales information sections (#1106) * Mark subsection as completed if it is not displayed in the tasklist and hide it from the UI * Add sale_information section * add sales information subsections * Cldc 1531 staircasing (#1109) * Add staircase field to sales_logs table * Add staircase question and page * add staircase page to the shared ownership scheme subsection fix file name * Cldc 1539 previous bedrooms (#1108) * add frombeds field * Add previous_bedrooms page and question * add Previous Bedrooms page to the shared ownership subsection * Cldc 1532 about staircasing (#1110) * Add stairboughts and stairowned fields to the database * Add about staircasing page and questions * Add about staircasing page to the shared wnership scheme subsection * Add a space before percent * Cldc 1546 monthly rent (#1111) * Add monthly rent column to sales logs table * Add monthly rent question and page * Add monthly rent page to the shared ownership scheme subsection * Cldc 1535 exchange contracts (#1112) * Add exdate to sales logs table * Add exchange date page and question * Add exchange contracts page to the shared ownership subsection * derive exday, exmonth and exyear, tests and lint * rebase tests * Cldc 1538 la nominations (#1115) * Add la nominations column to sales logs table * Add La nominations page and questions * Add la nominations to the shared ownership subsection * Add accidentally removed files * Cldc 1545 about the deposit (#1113) * feat: add question(s) without depends_on behaviour * feat: separate ids for diff sections * test: update tests * test: add tests * tests: test tweaks * refactor: linting * Cldc 1538 la nominations (#1115) * Add la nominations column to sales logs table * Add La nominations page and questions * Add la nominations to the shared ownership subsection * Add accidentally removed files * feat: add question(s) without depends_on behaviour Co-authored-by: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> * Cldc 1533 is resale (#1118) * feat: add resale question and page * tests: add new tests * test: update previous tests * refactor: linting * refactor: linting * Cldc 1576 buyer prp (#1117) * Add soctenant field to sales logs * Add buyer previous page and question * Add buyer previous page to shared ownership scheme subsection * rebase migrate * rebase too * Cldc 1540 about price (#1121) * add price fields to the sales logs table * Add about proce questions * add about proce pages * Add about price pages to sale information sections * rebase changes * happy new year * Switch the order of sales log sections * Cldc 1540 fixes (#1130) * Update order of pages and remove wrong id's * Add spacing to the hint text * Cldc 1521 living before purchase (#1129) * feat: add new page and question and update db * test: add and update tests * feat: reset db * Only display from beds for social housing (#1131) * Cldc 1544 mortgage amount (#1136) * feat: add sales log seeds * feat: add question and page and update db * tests: add and update tests * Cldc 1540 fixes part two (#1135) * Change about_price_social_housing to about_price_shared_ownership and display it in all shared ownership cases * Change about_price to purchase_price and display it for outright sale * lint * Move exchange contracts date page (#1143) * feat: PO fix and tests (#1144) * feat: PO fix and tests * feat: simmplification * refactor: linting * feat: copy tweak Co-authored-by: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Co-authored-by: natdeanlewissoftwire <nat.dean-lewis@softwire.com>
2 years ago
describe "derived variables" do
let(:sales_log) { FactoryBot.create(:sales_log, :completed) }
Cldc 1776 sales information sections (#1106) * Mark subsection as completed if it is not displayed in the tasklist and hide it from the UI * Add sale_information section * add sales information subsections * Cldc 1531 staircasing (#1109) * Add staircase field to sales_logs table * Add staircase question and page * add staircase page to the shared ownership scheme subsection fix file name * Cldc 1539 previous bedrooms (#1108) * add frombeds field * Add previous_bedrooms page and question * add Previous Bedrooms page to the shared ownership subsection * Cldc 1532 about staircasing (#1110) * Add stairboughts and stairowned fields to the database * Add about staircasing page and questions * Add about staircasing page to the shared wnership scheme subsection * Add a space before percent * Cldc 1546 monthly rent (#1111) * Add monthly rent column to sales logs table * Add monthly rent question and page * Add monthly rent page to the shared ownership scheme subsection * Cldc 1535 exchange contracts (#1112) * Add exdate to sales logs table * Add exchange date page and question * Add exchange contracts page to the shared ownership subsection * derive exday, exmonth and exyear, tests and lint * rebase tests * Cldc 1538 la nominations (#1115) * Add la nominations column to sales logs table * Add La nominations page and questions * Add la nominations to the shared ownership subsection * Add accidentally removed files * Cldc 1545 about the deposit (#1113) * feat: add question(s) without depends_on behaviour * feat: separate ids for diff sections * test: update tests * test: add tests * tests: test tweaks * refactor: linting * Cldc 1538 la nominations (#1115) * Add la nominations column to sales logs table * Add La nominations page and questions * Add la nominations to the shared ownership subsection * Add accidentally removed files * feat: add question(s) without depends_on behaviour Co-authored-by: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> * Cldc 1533 is resale (#1118) * feat: add resale question and page * tests: add new tests * test: update previous tests * refactor: linting * refactor: linting * Cldc 1576 buyer prp (#1117) * Add soctenant field to sales logs * Add buyer previous page and question * Add buyer previous page to shared ownership scheme subsection * rebase migrate * rebase too * Cldc 1540 about price (#1121) * add price fields to the sales logs table * Add about proce questions * add about proce pages * Add about price pages to sale information sections * rebase changes * happy new year * Switch the order of sales log sections * Cldc 1540 fixes (#1130) * Update order of pages and remove wrong id's * Add spacing to the hint text * Cldc 1521 living before purchase (#1129) * feat: add new page and question and update db * test: add and update tests * feat: reset db * Only display from beds for social housing (#1131) * Cldc 1544 mortgage amount (#1136) * feat: add sales log seeds * feat: add question and page and update db * tests: add and update tests * Cldc 1540 fixes part two (#1135) * Change about_price_social_housing to about_price_shared_ownership and display it in all shared ownership cases * Change about_price to purchase_price and display it for outright sale * lint * Move exchange contracts date page (#1143) * feat: PO fix and tests (#1144) * feat: PO fix and tests * feat: simmplification * refactor: linting * feat: copy tweak Co-authored-by: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Co-authored-by: natdeanlewissoftwire <nat.dean-lewis@softwire.com>
2 years ago
it "correctly derives and saves exday, exmonth and exyear" do
sales_log.update!(exdate: Time.gm(2022, 5, 4), saledate: Time.gm(2022, 7, 4), ownershipsch: 1, staircase: 2, resale: 2)
Cldc 1776 sales information sections (#1106) * Mark subsection as completed if it is not displayed in the tasklist and hide it from the UI * Add sale_information section * add sales information subsections * Cldc 1531 staircasing (#1109) * Add staircase field to sales_logs table * Add staircase question and page * add staircase page to the shared ownership scheme subsection fix file name * Cldc 1539 previous bedrooms (#1108) * add frombeds field * Add previous_bedrooms page and question * add Previous Bedrooms page to the shared ownership subsection * Cldc 1532 about staircasing (#1110) * Add stairboughts and stairowned fields to the database * Add about staircasing page and questions * Add about staircasing page to the shared wnership scheme subsection * Add a space before percent * Cldc 1546 monthly rent (#1111) * Add monthly rent column to sales logs table * Add monthly rent question and page * Add monthly rent page to the shared ownership scheme subsection * Cldc 1535 exchange contracts (#1112) * Add exdate to sales logs table * Add exchange date page and question * Add exchange contracts page to the shared ownership subsection * derive exday, exmonth and exyear, tests and lint * rebase tests * Cldc 1538 la nominations (#1115) * Add la nominations column to sales logs table * Add La nominations page and questions * Add la nominations to the shared ownership subsection * Add accidentally removed files * Cldc 1545 about the deposit (#1113) * feat: add question(s) without depends_on behaviour * feat: separate ids for diff sections * test: update tests * test: add tests * tests: test tweaks * refactor: linting * Cldc 1538 la nominations (#1115) * Add la nominations column to sales logs table * Add La nominations page and questions * Add la nominations to the shared ownership subsection * Add accidentally removed files * feat: add question(s) without depends_on behaviour Co-authored-by: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> * Cldc 1533 is resale (#1118) * feat: add resale question and page * tests: add new tests * test: update previous tests * refactor: linting * refactor: linting * Cldc 1576 buyer prp (#1117) * Add soctenant field to sales logs * Add buyer previous page and question * Add buyer previous page to shared ownership scheme subsection * rebase migrate * rebase too * Cldc 1540 about price (#1121) * add price fields to the sales logs table * Add about proce questions * add about proce pages * Add about price pages to sale information sections * rebase changes * happy new year * Switch the order of sales log sections * Cldc 1540 fixes (#1130) * Update order of pages and remove wrong id's * Add spacing to the hint text * Cldc 1521 living before purchase (#1129) * feat: add new page and question and update db * test: add and update tests * feat: reset db * Only display from beds for social housing (#1131) * Cldc 1544 mortgage amount (#1136) * feat: add sales log seeds * feat: add question and page and update db * tests: add and update tests * Cldc 1540 fixes part two (#1135) * Change about_price_social_housing to about_price_shared_ownership and display it in all shared ownership cases * Change about_price to purchase_price and display it for outright sale * lint * Move exchange contracts date page (#1143) * feat: PO fix and tests (#1144) * feat: PO fix and tests * feat: simmplification * refactor: linting * feat: copy tweak Co-authored-by: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Co-authored-by: natdeanlewissoftwire <nat.dean-lewis@softwire.com>
2 years ago
record_from_db = ActiveRecord::Base.connection.execute("select exday, exmonth, exyear from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["exday"]).to eq(4)
expect(record_from_db["exmonth"]).to eq(5)
expect(record_from_db["exyear"]).to eq(2022)
end
it "correctly derives and saves deposit for outright sales when no mortgage is used" do
sales_log.update!(value: 123_400, deposit: nil, mortgageused: 2, ownershipsch: 3, type: 10, companybuy: 1, jointpur: 1, jointmore: 1)
record_from_db = ActiveRecord::Base.connection.execute("select deposit from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["deposit"]).to eq(123_400)
end
it "does not derive deposit if the sale isn't outright" do
sales_log.update!(value: 123_400, deposit: nil, mortgageused: 2, ownershipsch: 2)
record_from_db = ActiveRecord::Base.connection.execute("select deposit from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["deposit"]).to eq(nil)
end
it "does not derive deposit if the mortgage is used" do
sales_log.update!(value: 123_400, deposit: nil, mortgageused: 1, ownershipsch: 3, type: 10, companybuy: 1, jointpur: 1, jointmore: 1)
record_from_db = ActiveRecord::Base.connection.execute("select deposit from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["deposit"]).to eq(nil)
end
it "correctly derives and saves pcode1 and pcode1 and pcode2" do
sales_log.update!(postcode_full: "W6 0SP")
record_from_db = ActiveRecord::Base.connection.execute("select pcode1, pcode2 from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["pcode1"]).to eq("W6")
expect(record_from_db["pcode2"]).to eq("0SP")
end
it "derives a mortgage value of 0 when mortgage is not used" do
# to avoid log failing validations when mortgage value is removed:
new_grant_value = sales_log.grant + sales_log.mortgage
sales_log.update!(mortgageused: 2, grant: new_grant_value)
record_from_db = ActiveRecord::Base.connection.execute("select mortgage from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["mortgage"]).to eq(0.0)
end
end
context "when saving addresses" do
before do
stub_request(:get, /api.postcodes.io/)
.to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\",\"codes\":{\"admin_district\": \"E08000003\"}}}", headers: {})
end
def check_postcode_fields(postcode_field)
record_from_db = ActiveRecord::Base.connection.execute("select #{postcode_field} from sales_logs where id=#{address_sales_log.id}").to_a[0]
expect(address_sales_log[postcode_field]).to eq("M1 1AE")
expect(record_from_db[postcode_field]).to eq("M1 1AE")
end
let!(:address_sales_log) do
FactoryBot.create(
:sales_log,
:completed,
owning_organisation:,
created_by: created_by_user,
pcodenk: 0,
postcode_full: "M1 1AE",
)
end
def check_property_postcode_fields
check_postcode_fields("postcode_full")
end
it "correctly formats previous postcode" do
address_sales_log.update!(postcode_full: "M1 1AE")
check_property_postcode_fields
address_sales_log.update!(postcode_full: "m1 1ae")
check_property_postcode_fields
address_sales_log.update!(postcode_full: "m11Ae")
check_property_postcode_fields
address_sales_log.update!(postcode_full: "m11ae")
check_property_postcode_fields
end
it "correctly infers la" do
record_from_db = ActiveRecord::Base.connection.execute("select la from sales_logs where id=#{address_sales_log.id}").to_a[0]
expect(address_sales_log.la).to eq("E08000003")
expect(record_from_db["la"]).to eq("E08000003")
end
it "errors if the property postcode is emptied" do
expect { address_sales_log.update!({ postcode_full: "" }) }
.to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/)
end
it "errors if the property postcode is not valid" do
expect { address_sales_log.update!({ postcode_full: "invalid_postcode" }) }
.to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/)
end
context "when the local authority lookup times out" do
before do
allow(Timeout).to receive(:timeout).and_raise(Timeout::Error)
end
it "logs a warning" do
expect(Rails.logger).to receive(:warn).with("Postcodes.io lookup timed out")
address_sales_log.update!({ pcodenk: 1, postcode_full: "M1 1AD" })
end
end
it "correctly resets all fields if property postcode not known" do
address_sales_log.update!({ pcodenk: 1 })
record_from_db = ActiveRecord::Base.connection.execute("select la, postcode_full from sales_logs where id=#{address_sales_log.id}").to_a[0]
expect(record_from_db["postcode_full"]).to eq(nil)
expect(address_sales_log.la).to eq(nil)
expect(record_from_db["la"]).to eq(nil)
end
it "changes the LA if property postcode changes from not known to known and provided" do
address_sales_log.update!({ pcodenk: 1 })
address_sales_log.update!({ la: "E09000033" })
record_from_db = ActiveRecord::Base.connection.execute("select la, postcode_full from sales_logs where id=#{address_sales_log.id}").to_a[0]
expect(record_from_db["postcode_full"]).to eq(nil)
expect(address_sales_log.la).to eq("E09000033")
expect(record_from_db["la"]).to eq("E09000033")
address_sales_log.update!({ pcodenk: 0, postcode_full: "M1 1AD" })
record_from_db = ActiveRecord::Base.connection.execute("select la, postcode_full from sales_logs where id=#{address_sales_log.id}").to_a[0]
expect(record_from_db["postcode_full"]).to eq("M1 1AD")
expect(address_sales_log.la).to eq("E08000003")
expect(record_from_db["la"]).to eq("E08000003")
end
Cldc 1776 sales information sections (#1106) * Mark subsection as completed if it is not displayed in the tasklist and hide it from the UI * Add sale_information section * add sales information subsections * Cldc 1531 staircasing (#1109) * Add staircase field to sales_logs table * Add staircase question and page * add staircase page to the shared ownership scheme subsection fix file name * Cldc 1539 previous bedrooms (#1108) * add frombeds field * Add previous_bedrooms page and question * add Previous Bedrooms page to the shared ownership subsection * Cldc 1532 about staircasing (#1110) * Add stairboughts and stairowned fields to the database * Add about staircasing page and questions * Add about staircasing page to the shared wnership scheme subsection * Add a space before percent * Cldc 1546 monthly rent (#1111) * Add monthly rent column to sales logs table * Add monthly rent question and page * Add monthly rent page to the shared ownership scheme subsection * Cldc 1535 exchange contracts (#1112) * Add exdate to sales logs table * Add exchange date page and question * Add exchange contracts page to the shared ownership subsection * derive exday, exmonth and exyear, tests and lint * rebase tests * Cldc 1538 la nominations (#1115) * Add la nominations column to sales logs table * Add La nominations page and questions * Add la nominations to the shared ownership subsection * Add accidentally removed files * Cldc 1545 about the deposit (#1113) * feat: add question(s) without depends_on behaviour * feat: separate ids for diff sections * test: update tests * test: add tests * tests: test tweaks * refactor: linting * Cldc 1538 la nominations (#1115) * Add la nominations column to sales logs table * Add La nominations page and questions * Add la nominations to the shared ownership subsection * Add accidentally removed files * feat: add question(s) without depends_on behaviour Co-authored-by: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> * Cldc 1533 is resale (#1118) * feat: add resale question and page * tests: add new tests * test: update previous tests * refactor: linting * refactor: linting * Cldc 1576 buyer prp (#1117) * Add soctenant field to sales logs * Add buyer previous page and question * Add buyer previous page to shared ownership scheme subsection * rebase migrate * rebase too * Cldc 1540 about price (#1121) * add price fields to the sales logs table * Add about proce questions * add about proce pages * Add about price pages to sale information sections * rebase changes * happy new year * Switch the order of sales log sections * Cldc 1540 fixes (#1130) * Update order of pages and remove wrong id's * Add spacing to the hint text * Cldc 1521 living before purchase (#1129) * feat: add new page and question and update db * test: add and update tests * feat: reset db * Only display from beds for social housing (#1131) * Cldc 1544 mortgage amount (#1136) * feat: add sales log seeds * feat: add question and page and update db * tests: add and update tests * Cldc 1540 fixes part two (#1135) * Change about_price_social_housing to about_price_shared_ownership and display it in all shared ownership cases * Change about_price to purchase_price and display it for outright sale * lint * Move exchange contracts date page (#1143) * feat: PO fix and tests (#1144) * feat: PO fix and tests * feat: simmplification * refactor: linting * feat: copy tweak Co-authored-by: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Co-authored-by: natdeanlewissoftwire <nat.dean-lewis@softwire.com>
2 years ago
end
Cldc 1440 household situation section (#1132) * feat: add question page and subsection (#1120) * feat: add question page and subsection * refactor: linting * feat: remove schema rows from other branch * feat: slight refactor, fix tag behaviour and add section tests * test: update and add tests * feat: update status behaviour * feat: update subsection status tag * refactor: linting * [CLDC-857] Add household wheelchair check (#1122) * [CLDC-857] Add household wheelchair check * Hide only if answered * Cldc 1497 ever served armed forces (#1124) * feat: add question and page * test: update tests * refactor: linting and slight test updates * test: fix tests * Cldc 1498 still serving armed forces (#1123) * feat: new question and tests * test: update subsection spec * test: update tests * feat: (future) conflict resolving * feat: more conflict resolution * feat: update db field * test: update id * test: updates * Cldc 1488 last accomodation (#1125) * Add postcode fields * Add previous postcode page and questions * Add last accommodation page to household situation subsection * add previous la known to the db * infer correct location fields * styling * Reorder disability questions (#1127) * [CLDC-1487] Add buyer1 previous tenure question (#1133) * use collection_start_year instead of the startdate (#1128) * [CLDC-1487] Add buyer 1 previous tenure Co-authored-by: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> * feat: fix routing (#1141) * Add last accommodation la question (#1142) * move hint text (#1146) Co-authored-by: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Co-authored-by: Jack S <113976590+bibblobcode@users.noreply.github.com>
2 years ago
context "when deriving household variables" do
let!(:sales_log) do
FactoryBot.create(
:sales_log,
:completed,
jointpur: 1,
hholdcount: 4,
details_known_3: 1,
details_known_4: 1,
details_known_5: 1,
details_known_6: 1,
relat2: "C",
relat3: "C",
relat4: "X",
relat5: "X",
relat6: "P",
ecstat2: 9,
ecstat3: 7,
age1: 47,
age2: 14,
age3: 17,
age4: 88,
age5: 19,
age6: 46,
)
end
it "correctly derives and saves hhmemb" do
record_from_db = ActiveRecord::Base.connection.execute("select hhmemb from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["hhmemb"]).to eq(6)
end
it "correctly derives and saves totchild" do
record_from_db = ActiveRecord::Base.connection.execute("select totchild from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["totchild"]).to eq(2)
end
it "correctly derives and saves totadult" do
record_from_db = ActiveRecord::Base.connection.execute("select totadult from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["totadult"]).to eq(4)
end
it "correctly derives and saves hhtype" do
record_from_db = ActiveRecord::Base.connection.execute("select hhtype from sales_logs where id=#{sales_log.id}").to_a[0]
expect(record_from_db["hhtype"]).to eq(9)
end
end
Cldc 1440 household situation section (#1132) * feat: add question page and subsection (#1120) * feat: add question page and subsection * refactor: linting * feat: remove schema rows from other branch * feat: slight refactor, fix tag behaviour and add section tests * test: update and add tests * feat: update status behaviour * feat: update subsection status tag * refactor: linting * [CLDC-857] Add household wheelchair check (#1122) * [CLDC-857] Add household wheelchair check * Hide only if answered * Cldc 1497 ever served armed forces (#1124) * feat: add question and page * test: update tests * refactor: linting and slight test updates * test: fix tests * Cldc 1498 still serving armed forces (#1123) * feat: new question and tests * test: update subsection spec * test: update tests * feat: (future) conflict resolving * feat: more conflict resolution * feat: update db field * test: update id * test: updates * Cldc 1488 last accomodation (#1125) * Add postcode fields * Add previous postcode page and questions * Add last accommodation page to household situation subsection * add previous la known to the db * infer correct location fields * styling * Reorder disability questions (#1127) * [CLDC-1487] Add buyer1 previous tenure question (#1133) * use collection_start_year instead of the startdate (#1128) * [CLDC-1487] Add buyer 1 previous tenure Co-authored-by: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> * feat: fix routing (#1141) * Add last accommodation la question (#1142) * move hint text (#1146) Co-authored-by: natdeanlewissoftwire <94526761+natdeanlewissoftwire@users.noreply.github.com> Co-authored-by: Jack S <113976590+bibblobcode@users.noreply.github.com>
2 years ago
context "when saving previous address" do
def check_previous_postcode_fields(postcode_field)
record_from_db = ActiveRecord::Base.connection.execute("select #{postcode_field} from sales_logs where id=#{address_sales_log.id}").to_a[0]
expect(address_sales_log[postcode_field]).to eq("M1 1AE")
expect(record_from_db[postcode_field]).to eq("M1 1AE")
end
before do
stub_request(:get, /api.postcodes.io/)
.to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\", \"codes\":{\"admin_district\": \"E08000003\"}}}", headers: {})
end
let!(:address_sales_log) do
described_class.create({
owning_organisation:,
created_by: created_by_user,
ppcodenk: 1,
ppostcode_full: "M1 1AE",
})
end
def previous_postcode_fields
check_previous_postcode_fields("ppostcode_full")
end
it "correctly formats previous postcode" do
address_sales_log.update!(ppostcode_full: "M1 1AE")
previous_postcode_fields
address_sales_log.update!(ppostcode_full: "m1 1ae")
previous_postcode_fields
address_sales_log.update!(ppostcode_full: "m11Ae")
previous_postcode_fields
address_sales_log.update!(ppostcode_full: "m11ae")
previous_postcode_fields
end
it "correctly infers prevloc" do
record_from_db = ActiveRecord::Base.connection.execute("select prevloc from sales_logs where id=#{address_sales_log.id}").to_a[0]
expect(address_sales_log.prevloc).to eq("E08000003")
expect(record_from_db["prevloc"]).to eq("E08000003")
end
it "errors if the previous postcode is emptied" do
expect { address_sales_log.update!({ ppostcode_full: "" }) }
.to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/)
end
it "errors if the previous postcode is not valid" do
expect { address_sales_log.update!({ ppostcode_full: "invalid_postcode" }) }
.to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/)
end
it "correctly resets all fields if previous postcode not known" do
address_sales_log.update!({ ppcodenk: 1 })
record_from_db = ActiveRecord::Base.connection.execute("select prevloc, ppostcode_full from sales_logs where id=#{address_sales_log.id}").to_a[0]
expect(record_from_db["ppostcode_full"]).to eq(nil)
expect(address_sales_log.prevloc).to eq(nil)
expect(record_from_db["prevloc"]).to eq(nil)
end
end
describe "expected_shared_ownership_deposit_value" do
let!(:completed_sales_log) { create(:sales_log, :completed, ownershipsch: 1, type: 2, value: 1000, equity: 50) }
it "is set to completed for a completed sales log" do
expect(completed_sales_log.expected_shared_ownership_deposit_value).to eq(500)
end
end
end