From 831f59c6da229296db1c47418fadb85fa81e9b8a Mon Sep 17 00:00:00 2001 From: Dushan <47317567+dushan-madetech@users.noreply.github.com> Date: Mon, 13 Dec 2021 16:48:02 +0000 Subject: [PATCH] Cldc 650 derived startdate (#164) * add fields for deriving startdate * Add test for inferring startdate, fix other tests * infer day, month, year instead --- app/models/case_log.rb | 5 +++++ ...2642_add_day_month_year_fields_for_start_date.rb | 9 +++++++++ db/schema.rb | 5 ++++- spec/factories/case_log.rb | 3 +++ spec/fixtures/complete_case_log.json | 3 +++ spec/models/case_log_spec.rb | 13 +++++++++++++ 6 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20211213122642_add_day_month_year_fields_for_start_date.rb diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 672ea89ae..7ea9af741 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -184,6 +184,11 @@ private self.mrcmonth = mrcdate.month self.mrcyear = mrcdate.year end + if startdate.present? + self.day = startdate.day + self.month = startdate.month + self.year = startdate.year + end self.incref = 1 if net_income_known == "Prefer not to say" self.hhmemb = other_hhmemb + 1 if other_hhmemb.present? self.renttype = RENT_TYPE_MAPPING[rent_type] diff --git a/db/migrate/20211213122642_add_day_month_year_fields_for_start_date.rb b/db/migrate/20211213122642_add_day_month_year_fields_for_start_date.rb new file mode 100644 index 000000000..c0c7c51d9 --- /dev/null +++ b/db/migrate/20211213122642_add_day_month_year_fields_for_start_date.rb @@ -0,0 +1,9 @@ +class AddDayMonthYearFieldsForStartDate < ActiveRecord::Migration[6.1] + def change + change_table :case_logs, bulk: true do |t| + t.column :day, :integer + t.column :month, :integer + t.column :year, :integer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 38fe84eb0..7f5ba379d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_12_03_144855) do +ActiveRecord::Schema.define(version: 2021_12_13_122642) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -171,6 +171,9 @@ ActiveRecord::Schema.define(version: 2021_12_03_144855) do t.integer "lettype" t.integer "postcode_known" t.integer "la_known" + t.integer "day" + t.integer "month" + t.integer "year" t.index ["discarded_at"], name: "index_case_logs_on_discarded_at" t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id" t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id" diff --git a/spec/factories/case_log.rb b/spec/factories/case_log.rb index cca98c002..b459e38fc 100644 --- a/spec/factories/case_log.rb +++ b/spec/factories/case_log.rb @@ -134,6 +134,9 @@ FactoryBot.define do incref { 0 } sale_completion_date { nil } startdate { Time.zone.now } + day { Time.zone.now.day } + month { Time.zone.now.month } + year { Time.zone.now.year } armedforces { 1 } builtype { 1 } unitletas { 2 } diff --git a/spec/fixtures/complete_case_log.json b/spec/fixtures/complete_case_log.json index b41538a8f..c7cc65314 100644 --- a/spec/fixtures/complete_case_log.json +++ b/spec/fixtures/complete_case_log.json @@ -51,6 +51,9 @@ "condition_effects": "dummy", "tenancy_code": "BZ757", "startdate": "12/12/2020", + "day": 12, + "month": 12, + "year": 2020, "startertenancy": "No", "tenancylength": "5", "tenancy": "Secure (including flexible)", diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index b2d9e5820..8086aa3e3 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -937,6 +937,7 @@ RSpec.describe Form, type: :model do property_postcode: "M1 1AE", previous_postcode: "M2 2AE", # rubocop:disable Style/DateTime + startdate: DateTime.new(2021, 10, 10), mrcdate: DateTime.new(2021, 5, 4), # rubocop:enable Style/DateTime net_income_known: "Prefer not to say", @@ -1003,5 +1004,17 @@ RSpec.describe Form, type: :model do expect(case_log.lettype).to eq("Intermediate Rent General needs PRP") expect(record_from_db["lettype"]).to eq(9) end + + it "correctly derives and saves day, month, year from start date" do + case_log.reload + + record_from_db = ActiveRecord::Base.connection.execute("select day, month, year, startdate from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["startdate"].day).to eq(10) + expect(record_from_db["startdate"].month).to eq(10) + expect(record_from_db["startdate"].year).to eq(2021) + expect(record_from_db["day"]).to eq(10) + expect(record_from_db["month"]).to eq(10) + expect(record_from_db["year"]).to eq(2021) + end end end