From 1c677cce3cd9e7c81d73c489f4df0f12fb192316 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Wed, 25 Jan 2023 11:28:03 +0000 Subject: [PATCH] test: add purchase price rangeimport service tests --- spec/fixtures/files/purchase_price_ranges.csv | 5 ++ .../tasks/purchase_price_range_import_spec.rb | 47 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 spec/fixtures/files/purchase_price_ranges.csv create mode 100644 spec/lib/tasks/purchase_price_range_import_spec.rb diff --git a/spec/fixtures/files/purchase_price_ranges.csv b/spec/fixtures/files/purchase_price_ranges.csv new file mode 100644 index 000000000..f53654aa8 --- /dev/null +++ b/spec/fixtures/files/purchase_price_ranges.csv @@ -0,0 +1,5 @@ +la_name,la,bedrooms,soft_min,soft_max +Adur,E07000223,1,105000,369000 +Adur,E07000223,2,177000,384000 +Adur,E07000223,3,246000,539000 +Adur,E07000223,4,310000,860000 diff --git a/spec/lib/tasks/purchase_price_range_import_spec.rb b/spec/lib/tasks/purchase_price_range_import_spec.rb new file mode 100644 index 000000000..a9d080534 --- /dev/null +++ b/spec/lib/tasks/purchase_price_range_import_spec.rb @@ -0,0 +1,47 @@ +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: 177000, soft_max: 384000, 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(384000) + end + end + end + end +end