6 changed files with 64 additions and 0 deletions
@ -0,0 +1,13 @@ |
|||||||
|
namespace :data_import do |
||||||
|
desc "Import local authorities data" |
||||||
|
task :local_authorities, %i[path] => :environment do |_task, args| |
||||||
|
path = args[:path] |
||||||
|
|
||||||
|
raise "Usage: rake data_import:local_authorities['path/to/csv_file']" if path.blank? |
||||||
|
|
||||||
|
service = Imports::LocalAuthoritiesService.new(path:) |
||||||
|
service.call |
||||||
|
|
||||||
|
pp "Created/updated #{service.count} local authority records" unless Rails.env.test? |
||||||
|
end |
||||||
|
end |
||||||
|
@ -0,0 +1,42 @@ |
|||||||
|
require "rails_helper" |
||||||
|
require "rake" |
||||||
|
|
||||||
|
RSpec.describe "data_import" do |
||||||
|
describe ":local_authorities", type: :task do |
||||||
|
subject(:task) { Rake::Task["data_import:local_authorities"] } |
||||||
|
|
||||||
|
before do |
||||||
|
Rake.application.rake_require("tasks/local_authorities") |
||||||
|
Rake::Task.define_task(:environment) |
||||||
|
task.reenable |
||||||
|
end |
||||||
|
|
||||||
|
context "when the rake task is run" do |
||||||
|
let(:local_authorities_file_path) { "./spec/fixtures/files/local_authorities.csv" } |
||||||
|
let(:wrong_file_path) { "/test/no_csv_here.csv" } |
||||||
|
|
||||||
|
it "creates new local authorities records" do |
||||||
|
expect { task.invoke(local_authorities_file_path) }.to change(LocalAuthority, :count).by(5) |
||||||
|
expect(LocalAuthority.where(code: "S12000041").exists?).to be true |
||||||
|
end |
||||||
|
|
||||||
|
it "raises an error when no path is given" do |
||||||
|
expect { task.invoke(nil) }.to raise_error(RuntimeError, "Usage: rake data_import:local_authorities['path/to/csv_file']") |
||||||
|
end |
||||||
|
|
||||||
|
it "raises an error when no file exists at the given path" do |
||||||
|
expect { task.invoke(wrong_file_path) }.to raise_error(Errno::ENOENT) |
||||||
|
end |
||||||
|
|
||||||
|
context "when a record already exists with a matching code index" do |
||||||
|
let!(:local_authority) { LocalAuthority.create(code: "S12000041", la_name: "Angus", start_date: Time.zone.local(2021, 4, 1), previous_location_only: false) } |
||||||
|
|
||||||
|
it "updates local authority if the record is matched on code" do |
||||||
|
task.invoke(local_authorities_file_path) |
||||||
|
local_authority.reload |
||||||
|
expect(local_authority.previous_location_only).to eq(true) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
Loading…
Reference in new issue