Browse Source

Import data

pull/484/head
baarkerlounger 3 years ago
parent
commit
f8d16bf098
  1. 1
      app/models/organisation_la.rb
  2. 23
      app/services/imports/organisation_la_import_service.rb
  3. 2
      lib/tasks/data_import.rake
  4. 0
      spec/fixtures/softwire_imports/organisation_las/00013f30e159d7f72a3abe9ea93fb5b685d311e4.xml
  5. 39
      spec/services/imports/organisation_la_import_service_spec.rb

1
app/models/organisation_la.rb

@ -1,2 +1,3 @@
class OrganisationLa < ApplicationRecord class OrganisationLa < ApplicationRecord
belongs_to :organisation
end end

23
app/services/imports/organisation_la_import_service.rb

@ -0,0 +1,23 @@
module Imports
class OrganisationLaImportService < ImportService
def create_organisation_las(folder)
import_from(folder, :create_organisation_la)
end
private
def create_organisation_la(xml_document)
xml_doc = xml_document.remove_namespaces!
organisation = Organisation.find_by(old_org_id: record_field_value(xml_document, "InstitutionId"))
OrganisationLa.create!(
organisation:,
ons_code: record_field_value(xml_document, "ONSCode"),
)
end
def record_field_value(xml_document, field)
xml_document.at_xpath("//#{field}")&.text
end
end
end

2
lib/tasks/data_import.rake

@ -14,6 +14,8 @@ namespace :core do
Imports::UserImportService.new(storage_service).create_users(path) Imports::UserImportService.new(storage_service).create_users(path)
when "data-protection-confirmation" when "data-protection-confirmation"
Imports::DataProtectionConfirmationImportService.new(storage_service).create_data_protection_confirmations(path) Imports::DataProtectionConfirmationImportService.new(storage_service).create_data_protection_confirmations(path)
when "organisation_las"
Imports::OrganisationLaImportService.new(storage_service).create_organisation_la(path)
else else
raise "Type #{type} is not supported by data_import" raise "Type #{type} is not supported by data_import"
end end

0
spec/fixtures/softwire_imports/organisation_la/00013f30e159d7f72a3abe9ea93fb5b685d311e4.xml → spec/fixtures/softwire_imports/organisation_las/00013f30e159d7f72a3abe9ea93fb5b685d311e4.xml vendored

39
spec/services/imports/organisation_la_import_service_spec.rb

@ -0,0 +1,39 @@
require "rails_helper"
RSpec.describe Imports::OrganisationLaImportService do
let(:fixture_directory) { "spec/fixtures/softwire_imports/organisation_las" }
let(:old_org_id) { "44026acc7ed5c29516b26f2a5deb639e5e37966d" }
let(:old_id) { "00013f30e159d7f72a3abe9ea93fb5b685d311e4" }
let(:import_file) { File.open("#{fixture_directory}/#{old_id}.xml") }
let(:storage_service) { instance_double(StorageService) }
context "when importing data protection confirmations" do
subject(:import_service) { described_class.new(storage_service) }
before do
allow(storage_service)
.to receive(:list_files)
.and_return(["organisation_la_directory/#{old_id}.xml"])
allow(storage_service)
.to receive(:get_file_io)
.with("organisation_la_directory/#{old_id}.xml")
.and_return(import_file)
end
context "when the organisation in the import file doesn't exist in the system" do
it "does not create an organisation la record" do
expect { import_service.create_organisation_las("organisation_la_directory") }
.to raise_error(ActiveRecord::RecordInvalid, /Organisation must exist/)
end
end
context "when the organisation does exist" do
let!(:organisation) { FactoryBot.create(:organisation, old_org_id:) }
it "successfully create an organisation la record with the expected data" do
import_service.create_organisation_las("organisation_la_directory")
expect(Organisation.find_by(old_org_id:).organisation_las.pluck("ons_code")).to eq(["E07000041"])
end
end
end
end
Loading…
Cancel
Save