diff --git a/app/services/imports/local_authorities_service.rb b/app/services/imports/local_authorities_service.rb index fd989f777..f2573ac10 100644 --- a/app/services/imports/local_authorities_service.rb +++ b/app/services/imports/local_authorities_service.rb @@ -17,6 +17,7 @@ module Imports start_date: Time.zone.local(row["start_year"], 4, 1), end_date: (Time.zone.local(row["end_year"], 3, 31) if row["end_year"]), previous_location_only: row["previous_location_only"] || false }, + unique_by: %i[code], ) @count += 1 end diff --git a/db/migrate/20230308101826_create_local_authorities.rb b/db/migrate/20230308101826_create_local_authorities.rb index f0531d7d0..0004a25da 100644 --- a/db/migrate/20230308101826_create_local_authorities.rb +++ b/db/migrate/20230308101826_create_local_authorities.rb @@ -6,6 +6,7 @@ class CreateLocalAuthorities < ActiveRecord::Migration[7.0] t.datetime :start_date t.datetime :end_date t.boolean :previous_location_only, default: false + t.index %w[code], name: "index_local_authority_code", unique: true t.timestamps end diff --git a/db/schema.rb b/db/schema.rb index f274cd9e8..e7c534738 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -297,6 +297,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_08_101826) do t.boolean "previous_location_only", default: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.index ["code"], name: "index_local_authority_code", unique: true end create_table "location_deactivation_periods", force: :cascade do |t| diff --git a/lib/tasks/local_authorities.rake b/lib/tasks/local_authorities.rake new file mode 100644 index 000000000..b812bbc94 --- /dev/null +++ b/lib/tasks/local_authorities.rake @@ -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 diff --git a/spec/fixtures/files/local_authorities.csv b/spec/fixtures/files/local_authorities.csv new file mode 100644 index 000000000..93cdd5170 --- /dev/null +++ b/spec/fixtures/files/local_authorities.csv @@ -0,0 +1,6 @@ +code,la_name,start_year,end_year,previous_location_only +S12000033,Aberdeen City,2021,,true +S12000034,Aberdeenshire,2021,,true +E07000223,Adur,2021,, +E07000032,Amber Valley,2021,, +S12000041,Angus,2021,,true \ No newline at end of file diff --git a/spec/lib/tasks/local_authorities_import_spec.rb b/spec/lib/tasks/local_authorities_import_spec.rb new file mode 100644 index 000000000..beef36605 --- /dev/null +++ b/spec/lib/tasks/local_authorities_import_spec.rb @@ -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