natdeanlewissoftwire
2 years ago
15 changed files with 88 additions and 88 deletions
@ -1,2 +0,0 @@ |
|||||||
class LaPurchasePriceRange < ApplicationRecord |
|
||||||
end |
|
@ -0,0 +1,2 @@ |
|||||||
|
class LaSaleRange < ApplicationRecord |
||||||
|
end |
|
@ -1,13 +1,13 @@ |
|||||||
class CreateLaPurchasePriceRanges < ActiveRecord::Migration[7.0] |
class CreateLaSaleRanges < ActiveRecord::Migration[7.0] |
||||||
def change |
def change |
||||||
create_table :la_purchase_price_ranges do |t| |
create_table :la_sale_ranges do |t| |
||||||
t.string :la |
t.string :la |
||||||
t.integer :bedrooms |
t.integer :bedrooms |
||||||
t.decimal :soft_min, precision: 10, scale: 2 |
t.decimal :soft_min, precision: 10, scale: 2 |
||||||
t.decimal :soft_max, precision: 10, scale: 2 |
t.decimal :soft_max, precision: 10, scale: 2 |
||||||
t.integer :start_year |
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 |
t.timestamps |
||||||
end |
end |
||||||
end |
end |
@ -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 |
|
@ -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 |
|
@ -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 |
|
@ -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 |
Loading…
Reference in new issue