Browse Source
* Add rent ranges table and basic hard range validation Co-authored-by: baarkerlounger <baarkerlounger@users.noreply.github.com> * Add import rent ranges rake task Co-authored-by: baarkerlounger <baarkerlounger@users.noreply.github.com> * Update the rent range validation to validate all related fields and check for the correct collection year * Add ranges data * add error messages * Add readme step * extract get collection year method * rename method * create instead of find and create laRentRange record Co-authored-by: baarkerlounger <baarkerlounger@users.noreply.github.com>pull/414/head
kosiakkatrina
3 years ago
committed by
GitHub
12 changed files with 7979 additions and 13 deletions
@ -0,0 +1,2 @@
|
||||
class LaRentRange < ApplicationRecord |
||||
end |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,18 @@
|
||||
class CreateLaRentRanges < ActiveRecord::Migration[7.0] |
||||
def change |
||||
create_table :la_rent_ranges do |t| |
||||
t.integer :ranges_rent_id |
||||
t.integer :lettype |
||||
t.string :la |
||||
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.index %i[start_year lettype beds la], unique: true |
||||
t.timestamps |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,29 @@
|
||||
require "csv" |
||||
|
||||
namespace :data_import do |
||||
desc "Import annual rent range data" |
||||
task :rent_ranges, %i[start_year path] => :environment do |_task, args| |
||||
start_year = args[:start_year] |
||||
path = args[:path] |
||||
count = 0 |
||||
|
||||
raise "Usage: rake data_import:rent_ranges[start_year,'path/to/csv_file']" if path.blank? || start_year.blank? |
||||
|
||||
CSV.foreach(path, headers: true) do |row| |
||||
LaRentRange.upsert( |
||||
{ ranges_rent_id: row["ranges_rent_id"], |
||||
lettype: row["lettype"], |
||||
beds: row["beds"], |
||||
start_year:, |
||||
la: row["la"], |
||||
soft_min: row["soft_min"], |
||||
soft_max: row["soft_max"], |
||||
hard_min: row["hard_min"], |
||||
hard_max: row["hard_max"] }, |
||||
unique_by: %i[start_year lettype beds la], |
||||
) |
||||
count += 1 |
||||
end |
||||
pp "Created/updated #{count} records" |
||||
end |
||||
end |
|
@ -0,0 +1,51 @@
|
||||
require "rails_helper" |
||||
require "rake" |
||||
|
||||
RSpec.describe "data_import" do |
||||
describe ":rent_ranges", type: :task do |
||||
subject(:task) { Rake::Task["data_import:rent_ranges"] } |
||||
|
||||
before do |
||||
Rake.application.rake_require("tasks/rent_ranges") |
||||
Rake::Task.define_task(:environment) |
||||
task.reenable |
||||
end |
||||
|
||||
context "when the rake task is run" do |
||||
let(:start_year) { 2021 } |
||||
let(:rent_ranges_file_path) { "./spec/fixtures/files/rent_ranges.csv" } |
||||
let(:wrong_file_path) { "/test/no_csv_here.csv" } |
||||
|
||||
before do |
||||
LaRentRange.delete_all |
||||
end |
||||
|
||||
it "creates new rent range records" do |
||||
expect { task.invoke(start_year, rent_ranges_file_path) }.to change(LaRentRange, :count).by(5) |
||||
expect(LaRentRange.where(ranges_rent_id: 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:rent_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, rent_ranges_file_path) }.to raise_error(RuntimeError, "Usage: rake data_import:rent_ranges[start_year,'path/to/csv_file']") |
||||
end |
||||
|
||||
context "when a record already exists with a matching index of la, beds, start year and lettype" do |
||||
let!(:rent_range) { LaRentRange.create(lettype: 1, la: "E07000223", beds: 2, soft_min: 53.5, soft_max: 149.4, hard_min: 20.36, hard_max: 200.57, start_year: 2021) } |
||||
|
||||
it "updates rent ranges if the record is matched on la, beds, start year and lettype" do |
||||
task.invoke(start_year, rent_ranges_file_path) |
||||
rent_range.reload |
||||
expect(rent_range.hard_max).to eq(190.57) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue