diff --git a/app/models/la_purchase_price_range.rb b/app/models/la_purchase_price_range.rb deleted file mode 100644 index 2884a07a3..000000000 --- a/app/models/la_purchase_price_range.rb +++ /dev/null @@ -1,2 +0,0 @@ -class LaPurchasePriceRange < ApplicationRecord -end diff --git a/app/models/la_sale_range.rb b/app/models/la_sale_range.rb new file mode 100644 index 000000000..55cdc1bf6 --- /dev/null +++ b/app/models/la_sale_range.rb @@ -0,0 +1,2 @@ +class LaSaleRange < ApplicationRecord +end diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index e4fcf1168..b59a72046 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -204,10 +204,10 @@ class SalesLog < Log end def purchase_price_soft_min - LaPurchasePriceRange.find_by(start_year: collection_start_year, la:, bedrooms: beds).soft_min + LaSaleRange.find_by(start_year: collection_start_year, la:, bedrooms: beds).soft_min end def purchase_price_soft_max - LaPurchasePriceRange.find_by(start_year: collection_start_year, la:, bedrooms: beds).soft_max + LaSaleRange.find_by(start_year: collection_start_year, la:, bedrooms: beds).soft_max end end diff --git a/app/models/validations/sales/soft_validations.rb b/app/models/validations/sales/soft_validations.rb index d1005b471..ecb8d90d7 100644 --- a/app/models/validations/sales/soft_validations.rb +++ b/app/models/validations/sales/soft_validations.rb @@ -46,20 +46,20 @@ module Validations::Sales::SoftValidations def purchase_price_out_of_soft_range? return unless value && beds && la - purchase_price_range.present? && !value.between?(purchase_price_range.soft_min, purchase_price_range.soft_max) + sale_range.present? && !value.between?(sale_range.soft_min, sale_range.soft_max) end def purchase_price_min_or_max_text - value < purchase_price_range.soft_min ? "minimum" : "maximum" + value < sale_range.soft_min ? "minimum" : "maximum" end def purchase_price_soft_min_or_soft_max - value < purchase_price_range.soft_min ? purchase_price_range.soft_min : purchase_price_range.soft_max + value < sale_range.soft_min ? sale_range.soft_min : sale_range.soft_max end private - def purchase_price_range - LaPurchasePriceRange.find_by(start_year: collection_start_year, la:, bedrooms: beds) + def sale_range + LaSaleRange.find_by(start_year: collection_start_year, la:, bedrooms: beds) end end diff --git a/app/services/imports/purchase_price_ranges_service.rb b/app/services/imports/sale_ranges_service.rb similarity index 88% rename from app/services/imports/purchase_price_ranges_service.rb rename to app/services/imports/sale_ranges_service.rb index 0181d364d..44b0b0819 100644 --- a/app/services/imports/purchase_price_ranges_service.rb +++ b/app/services/imports/sale_ranges_service.rb @@ -1,7 +1,7 @@ require "csv" module Imports - class PurchasePriceRangesService + class SaleRangesService attr_reader :start_year, :path, :count def initialize(start_year:, path:) @@ -12,7 +12,7 @@ module Imports def call CSV.foreach(path, headers: true) do |row| - LaPurchasePriceRange.upsert( + LaSaleRange.upsert( { start_year:, la: row["la"], bedrooms: row["bedrooms"], diff --git a/config/purchase_price_range_data/2022.csv b/config/sale_range_data/2022.csv similarity index 100% rename from config/purchase_price_range_data/2022.csv rename to config/sale_range_data/2022.csv diff --git a/db/migrate/20230124111328_create_la_purchase_price_ranges.rb b/db/migrate/20230124111328_create_la_sale_ranges.rb similarity index 62% rename from db/migrate/20230124111328_create_la_purchase_price_ranges.rb rename to db/migrate/20230124111328_create_la_sale_ranges.rb index d122ed768..4f3b5d622 100644 --- a/db/migrate/20230124111328_create_la_purchase_price_ranges.rb +++ b/db/migrate/20230124111328_create_la_sale_ranges.rb @@ -1,13 +1,13 @@ -class CreateLaPurchasePriceRanges < ActiveRecord::Migration[7.0] +class CreateLaSaleRanges < ActiveRecord::Migration[7.0] def change - create_table :la_purchase_price_ranges do |t| + create_table :la_sale_ranges do |t| t.string :la t.integer :bedrooms t.decimal :soft_min, precision: 10, scale: 2 t.decimal :soft_max, precision: 10, scale: 2 t.integer :start_year - t.index %i[start_year bedrooms la], unique: true, name: "index_la_purchase_price_ranges_on_start_year_bedrooms_la" + t.index %i[start_year bedrooms la], unique: true, name: "index_la_sale_ranges_on_start_year_bedrooms_la" t.timestamps end end diff --git a/db/schema.rb b/db/schema.rb index 176250c57..1bb8e14a2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -54,30 +54,30 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_24_111328) do t.index ["organisation_id"], name: "index_data_protection_confirmations_on_organisation_id" end - create_table "la_purchase_price_ranges", force: :cascade do |t| + create_table "la_rent_ranges", force: :cascade do |t| + t.integer "ranges_rent_id" + t.integer "lettype" t.string "la" - t.integer "bedrooms" + t.integer "beds" t.decimal "soft_min", precision: 10, scale: 2 t.decimal "soft_max", precision: 10, scale: 2 + t.decimal "hard_min", precision: 10, scale: 2 + t.decimal "hard_max", precision: 10, scale: 2 t.integer "start_year" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["start_year", "bedrooms", "la"], name: "index_la_purchase_price_ranges_on_start_year_bedrooms_la", unique: true + t.index ["start_year", "lettype", "beds", "la"], name: "index_la_rent_ranges_on_start_year_and_lettype_and_beds_and_la", unique: true end - create_table "la_rent_ranges", force: :cascade do |t| - t.integer "ranges_rent_id" - t.integer "lettype" + create_table "la_sale_ranges", force: :cascade do |t| t.string "la" - t.integer "beds" + t.integer "bedrooms" t.decimal "soft_min", precision: 10, scale: 2 t.decimal "soft_max", precision: 10, scale: 2 - t.decimal "hard_min", precision: 10, scale: 2 - t.decimal "hard_max", precision: 10, scale: 2 t.integer "start_year" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["start_year", "lettype", "beds", "la"], name: "index_la_rent_ranges_on_start_year_and_lettype_and_beds_and_la", unique: true + t.index ["start_year", "bedrooms", "la"], name: "index_la_sale_ranges_on_start_year_bedrooms_la", unique: true end create_table "legacy_users", force: :cascade do |t| diff --git a/db/seeds.rb b/db/seeds.rb index e7444a267..f9c9124b1 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -311,8 +311,8 @@ unless Rails.env.test? end end - if LaPurchasePriceRange.count.zero? - Dir.glob("config/purchase_price_range_data/*.csv").each do |path| + if LaSaleRange.count.zero? + Dir.glob("config/sale_range_data/*.csv").each do |path| start_year = File.basename(path, ".csv") service = Imports::PurchasePriceRangesService.new(start_year:, path:) service.call diff --git a/lib/tasks/purchase_price_ranges.rake b/lib/tasks/purchase_price_ranges.rake deleted file mode 100644 index 464ed940c..000000000 --- a/lib/tasks/purchase_price_ranges.rake +++ /dev/null @@ -1,14 +0,0 @@ -namespace :data_import do - desc "Import annual purchase price range data" - task :purchase_price_ranges, %i[start_year path] => :environment do |_task, args| - start_year = args[:start_year] - path = args[:path] - - raise "Usage: rake data_import:purchase_price_ranges[start_year,'path/to/csv_file']" if path.blank? || start_year.blank? - - service = Imports::PurchasePriceRangesService.new(start_year:, path:) - service.call - - pp "Created/updated #{service.count} LA Purchase Price Range records for #{start_year}" unless Rails.env.test? - end -end diff --git a/lib/tasks/sale_ranges.rake b/lib/tasks/sale_ranges.rake new file mode 100644 index 000000000..88f275a1a --- /dev/null +++ b/lib/tasks/sale_ranges.rake @@ -0,0 +1,14 @@ +namespace :data_import do + desc "Import annual sale range data" + task :sale_ranges, %i[start_year path] => :environment do |_task, args| + start_year = args[:start_year] + path = args[:path] + + raise "Usage: rake data_import:sale_ranges[start_year,'path/to/csv_file']" if path.blank? || start_year.blank? + + service = Imports::SaleRangesService.new(start_year:, path:) + service.call + + pp "Created/updated #{service.count} LA Sale Range records for #{start_year}" unless Rails.env.test? + end +end diff --git a/spec/fixtures/files/purchase_price_ranges.csv b/spec/fixtures/files/sale_ranges.csv similarity index 100% rename from spec/fixtures/files/purchase_price_ranges.csv rename to spec/fixtures/files/sale_ranges.csv diff --git a/spec/lib/tasks/purchase_price_range_import_spec.rb b/spec/lib/tasks/purchase_price_range_import_spec.rb deleted file mode 100644 index ca1e5f04b..000000000 --- a/spec/lib/tasks/purchase_price_range_import_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -require "rails_helper" -require "rake" - -RSpec.describe "data_import" do - describe ":purchase_price_ranges", type: :task do - subject(:task) { Rake::Task["data_import:purchase_price_ranges"] } - - before do - Rake.application.rake_require("tasks/purchase_price_ranges") - Rake::Task.define_task(:environment) - task.reenable - end - - context "when the rake task is run" do - let(:start_year) { 2022 } - let(:purchase_price_ranges_file_path) { "./spec/fixtures/files/purchase_price_ranges.csv" } - let(:wrong_file_path) { "/test/no_csv_here.csv" } - - it "creates new rent range records" do - expect { task.invoke(start_year, purchase_price_ranges_file_path) }.to change(LaPurchasePriceRange, :count).by(4) - expect(LaPurchasePriceRange.where(bedrooms: 1).exists?).to be true - end - - it "raises an error when no path is given" do - expect { task.invoke(start_year, nil) }.to raise_error(RuntimeError, "Usage: rake data_import:purchase_price_ranges[start_year,'path/to/csv_file']") - end - - it "raises an error when no file exists at the given path" do - expect { task.invoke(start_year, wrong_file_path) }.to raise_error(Errno::ENOENT) - end - - it "asks for a start year if it is not given" do - expect { task.invoke(nil, purchase_price_ranges_file_path) }.to raise_error(RuntimeError, "Usage: rake data_import:purchase_price_ranges[start_year,'path/to/csv_file']") - end - - context "when a record already exists with a matching index of la, bedrooms and start year" do - let!(:purchase_price_range) { LaPurchasePriceRange.create(la: "E07000223", bedrooms: 2, soft_min: 177_000, soft_max: 384_000, start_year: 2022) } - - it "updates rent ranges if the record is matched on la, bedrooms and start year" do - task.invoke(start_year, purchase_price_ranges_file_path) - purchase_price_range.reload - expect(purchase_price_range.soft_max).to eq(384_000) - end - end - end - end -end diff --git a/spec/lib/tasks/sale_range_import_spec.rb b/spec/lib/tasks/sale_range_import_spec.rb new file mode 100644 index 000000000..5e0da7d4b --- /dev/null +++ b/spec/lib/tasks/sale_range_import_spec.rb @@ -0,0 +1,47 @@ +require "rails_helper" +require "rake" + +RSpec.describe "data_import" do + describe ":sale_ranges", type: :task do + subject(:task) { Rake::Task["data_import:sale_ranges"] } + + before do + Rake.application.rake_require("tasks/sale_ranges") + Rake::Task.define_task(:environment) + task.reenable + end + + context "when the rake task is run" do + let(:start_year) { 2022 } + let(:sale_ranges_file_path) { "./spec/fixtures/files/sale_ranges.csv" } + let(:wrong_file_path) { "/test/no_csv_here.csv" } + + it "creates new rent range records" do + expect { task.invoke(start_year, sale_ranges_file_path) }.to change(LaSaleRange, :count).by(4) + expect(LaSaleRange.where(bedrooms: 1).exists?).to be true + end + + it "raises an error when no path is given" do + expect { task.invoke(start_year, nil) }.to raise_error(RuntimeError, "Usage: rake data_import:sale_ranges[start_year,'path/to/csv_file']") + end + + it "raises an error when no file exists at the given path" do + expect { task.invoke(start_year, wrong_file_path) }.to raise_error(Errno::ENOENT) + end + + it "asks for a start year if it is not given" do + expect { task.invoke(nil, sale_ranges_file_path) }.to raise_error(RuntimeError, "Usage: rake data_import:sale_ranges[start_year,'path/to/csv_file']") + end + + context "when a record already exists with a matching index of la, bedrooms and start year" do + let!(:sale_range) { LaSaleRange.create(la: "E07000223", bedrooms: 2, soft_min: 177_000, soft_max: 384_000, start_year: 2022) } + + it "updates rent ranges if the record is matched on la, bedrooms and start year" do + task.invoke(start_year, sale_ranges_file_path) + sale_range.reload + expect(sale_range.soft_max).to eq(384_000) + end + end + end + end +end diff --git a/spec/models/validations/sales/soft_validations_spec.rb b/spec/models/validations/sales/soft_validations_spec.rb index f0d1c5d45..a360bd7bf 100644 --- a/spec/models/validations/sales/soft_validations_spec.rb +++ b/spec/models/validations/sales/soft_validations_spec.rb @@ -332,7 +332,7 @@ RSpec.describe Validations::Sales::SoftValidations do describe "purchase_price_out_of_soft_range" do before do - LaPurchasePriceRange.create!( + LaSaleRange.create!( la: "E07000223", bedrooms: 2, soft_min: 177_000,