Browse Source

Add unique index and rake task

pull/1395/head
Kat 3 years ago
parent
commit
ccfd870e6c
  1. 1
      app/services/imports/local_authorities_service.rb
  2. 1
      db/migrate/20230308101826_create_local_authorities.rb
  3. 1
      db/schema.rb
  4. 13
      lib/tasks/local_authorities.rake
  5. 6
      spec/fixtures/files/local_authorities.csv
  6. 42
      spec/lib/tasks/local_authorities_import_spec.rb

1
app/services/imports/local_authorities_service.rb

@ -17,6 +17,7 @@ module Imports
start_date: Time.zone.local(row["start_year"], 4, 1), start_date: Time.zone.local(row["start_year"], 4, 1),
end_date: (Time.zone.local(row["end_year"], 3, 31) if row["end_year"]), end_date: (Time.zone.local(row["end_year"], 3, 31) if row["end_year"]),
previous_location_only: row["previous_location_only"] || false }, previous_location_only: row["previous_location_only"] || false },
unique_by: %i[code],
) )
@count += 1 @count += 1
end end

1
db/migrate/20230308101826_create_local_authorities.rb

@ -6,6 +6,7 @@ class CreateLocalAuthorities < ActiveRecord::Migration[7.0]
t.datetime :start_date t.datetime :start_date
t.datetime :end_date t.datetime :end_date
t.boolean :previous_location_only, default: false t.boolean :previous_location_only, default: false
t.index %w[code], name: "index_local_authority_code", unique: true
t.timestamps t.timestamps
end end

1
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.boolean "previous_location_only", default: false
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
t.index ["code"], name: "index_local_authority_code", unique: true
end end
create_table "location_deactivation_periods", force: :cascade do |t| create_table "location_deactivation_periods", force: :cascade do |t|

13
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

6
spec/fixtures/files/local_authorities.csv vendored

@ -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
1 code la_name start_year end_year previous_location_only
2 S12000033 Aberdeen City 2021 true
3 S12000034 Aberdeenshire 2021 true
4 E07000223 Adur 2021
5 E07000032 Amber Valley 2021
6 S12000041 Angus 2021 true

42
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
Loading…
Cancel
Save