Browse Source

Add import task for la links

pull/1402/head
Kat 3 years ago
parent
commit
ededa6de4a
  1. 22
      app/services/imports/local_authority_links_service.rb
  2. 35
      config/local_authorities_data/local_authority_links_2023.csv
  3. 13
      lib/tasks/local_authority_links.rake
  4. 6
      spec/fixtures/files/local_authority_links_2023.csv
  5. 40
      spec/lib/tasks/local_authority_links_import_spec.rb

22
app/services/imports/local_authority_links_service.rb

@ -0,0 +1,22 @@
require "csv"
module Imports
class LocalAuthorityLinksService
attr_reader :path, :count
def initialize(path:)
@path = path
@count = 0
end
def call
CSV.foreach(path, headers: true) do |row|
LocalAuthorityLink.upsert(
{ local_authority_id: LocalAuthority.find_by(code: row["local_authority_code"]).id,
linked_local_authority_id: LocalAuthority.find_by(code: row["linked_local_authority_code"]).id },
)
@count += 1
end
end
end
end

35
config/local_authorities_data/local_authority_links_2023.csv

@ -0,0 +1,35 @@
local_authority_code,linked_local_authority_code
E06000063,E07000027
E06000063,E07000030
E06000063,E07000031
E07000027,E06000063
E07000030,E06000063
E07000031,E06000063
E06000064,E07000026
E06000064,E07000028
E06000064,E07000029
E07000026,E06000064
E07000028,E06000064
E07000029,E06000064
E06000065,E07000163
E06000065,E07000164
E06000065,E07000165
E06000065,E07000166
E06000065,E07000167
E06000065,E07000168
E06000065,E07000169
E07000163,E06000065
E07000164,E06000065
E07000165,E06000065
E07000166,E06000065
E07000167,E06000065
E07000168,E06000065
E07000169,E06000065
E06000066,E07000187
E06000066,E07000188
E06000066,E07000246
E06000066,E07000189
E07000187,E06000066
E07000188,E06000066
E07000246,E06000066
E07000189,E06000066
1 local_authority_code linked_local_authority_code
2 E06000063 E07000027
3 E06000063 E07000030
4 E06000063 E07000031
5 E07000027 E06000063
6 E07000030 E06000063
7 E07000031 E06000063
8 E06000064 E07000026
9 E06000064 E07000028
10 E06000064 E07000029
11 E07000026 E06000064
12 E07000028 E06000064
13 E07000029 E06000064
14 E06000065 E07000163
15 E06000065 E07000164
16 E06000065 E07000165
17 E06000065 E07000166
18 E06000065 E07000167
19 E06000065 E07000168
20 E06000065 E07000169
21 E07000163 E06000065
22 E07000164 E06000065
23 E07000165 E06000065
24 E07000166 E06000065
25 E07000167 E06000065
26 E07000168 E06000065
27 E07000169 E06000065
28 E06000066 E07000187
29 E06000066 E07000188
30 E06000066 E07000246
31 E06000066 E07000189
32 E07000187 E06000066
33 E07000188 E06000066
34 E07000246 E06000066
35 E07000189 E06000066

13
lib/tasks/local_authority_links.rake

@ -0,0 +1,13 @@
namespace :data_import do
desc "Import local authority links data"
task :local_authority_links, %i[path] => :environment do |_task, args|
path = args[:path]
raise "Usage: rake data_import:local_authority_links['path/to/csv_file']" if path.blank?
service = Imports::LocalAuthorityLinksService.new(path:)
service.call
pp "Created/updated #{service.count} local authority link records" unless Rails.env.test?
end
end

6
spec/fixtures/files/local_authority_links_2023.csv vendored

@ -0,0 +1,6 @@
local_authority_code,linked_local_authority_code
E06000063,E07000027
E06000063,E07000030
E06000063,E07000031
E07000027,E06000063
E07000030,E06000063
1 local_authority_code linked_local_authority_code
2 E06000063 E07000027
3 E06000063 E07000030
4 E06000063 E07000031
5 E07000027 E06000063
6 E07000030 E06000063

40
spec/lib/tasks/local_authority_links_import_spec.rb

@ -0,0 +1,40 @@
require "rails_helper"
require "rake"
RSpec.describe "data_import" do
describe ":local_authority_links", type: :task do
subject(:task) { Rake::Task["data_import:local_authority_links"] }
before do
LocalAuthorityLink.destroy_all
Rake.application.rake_require("tasks/local_authority_links")
Rake::Task.define_task(:environment)
task.reenable
end
context "when the rake task is run" do
let(:local_authority_links_file_path) { "./spec/fixtures/files/local_authority_links_2023.csv" }
let(:wrong_file_path) { "/test/no_csv_here.csv" }
it "creates new local authority links records" do
expect { task.invoke(local_authority_links_file_path) }.to change(LocalAuthorityLink, :count).by(5)
expect(LocalAuthorityLink.where(local_authority_id: LocalAuthority.find_by(code: "E06000063").id).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_authority_links['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 ids" do
it "does not create a new link" do
task.invoke(local_authority_links_file_path)
expect { task.invoke(local_authority_links_file_path) }.to change(LocalAuthorityLink, :count).by(0)
end
end
end
end
end
Loading…
Cancel
Save