Browse Source

Add import

pull/487/head
baarkerlounger 3 years ago
parent
commit
48ac937df7
  1. 22
      app/services/imports/organisation_rent_period_import_service.rb
  2. 2
      lib/tasks/data_import.rake
  3. 18
      spec/lib/tasks/data_import_spec.rb
  4. 41
      spec/services/imports/organisation_rent_period_import_service_spec.rb

22
app/services/imports/organisation_rent_period_import_service.rb

@ -0,0 +1,22 @@
module Imports
class OrganisationRentPeriodImportService < ImportService
def create_organisation_rent_periods(folder)
import_from(folder, :create_organisation_rent_period)
end
private
def create_organisation_rent_period(xml_document)
organisation = Organisation.find_by(old_org_id: record_field_value(xml_document, "institution"))
OrganisationRentPeriod.create!(
organisation:,
rent_period: Integer(record_field_value(xml_document, "period")),
)
end
def record_field_value(xml_document, field)
field_value(xml_document, "rent-period", field)
end
end
end

2
lib/tasks/data_import.rake

@ -16,6 +16,8 @@ namespace :core do
Imports::DataProtectionConfirmationImportService.new(storage_service).create_data_protection_confirmations(path) Imports::DataProtectionConfirmationImportService.new(storage_service).create_data_protection_confirmations(path)
when "organisation-las" when "organisation-las"
Imports::OrganisationLaImportService.new(storage_service).create_organisation_las(path) Imports::OrganisationLaImportService.new(storage_service).create_organisation_las(path)
when "organisation-rent-periods"
Imports::OrganisationRentPeriodImportService.new(storage_service).create_organisation_rent_periods(path)
else else
raise "Type #{type} is not supported by data_import" raise "Type #{type} is not supported by data_import"
end end

18
spec/lib/tasks/data_import_spec.rb

@ -90,6 +90,24 @@ describe "rake core:data_import", type: :task do
end end
end end
context "when importing organisation rent period data" do
let(:type) { "organisation-rent-periods" }
let(:import_service) { instance_double(Imports::OrganisationRentPeriodImportService) }
let(:fixture_path) { "spec/fixtures/softwire_imports/organisation_rent_periods" }
before do
allow(Imports::OrganisationRentPeriodImportService).to receive(:new).and_return(import_service)
end
it "creates an organisation la from the given XML file" do
expect(StorageService).to receive(:new).with(paas_config_service, instance_name)
expect(Imports::OrganisationRentPeriodImportService).to receive(:new).with(storage_service)
expect(import_service).to receive(:create_organisation_rent_periods).with(fixture_path)
task.invoke(type, fixture_path)
end
end
it "raises an exception if no parameters are provided" do it "raises an exception if no parameters are provided" do
expect { task.invoke }.to raise_error(/Usage/) expect { task.invoke }.to raise_error(/Usage/)
end end

41
spec/services/imports/organisation_rent_period_import_service_spec.rb

@ -0,0 +1,41 @@
require "rails_helper"
RSpec.describe Imports::OrganisationRentPeriodImportService do
let(:fixture_directory) { "spec/fixtures/softwire_imports/organisation_rent_periods" }
let(:old_org_id) { "44026acc7ed5c29516b26f2a5deb639e5e37966d" }
let(:old_id) { "ebd22326d33e389e9f1bfd546979d2c05f9e68d6" }
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_rent_period_directory/#{old_id}.xml"])
allow(storage_service)
.to receive(:get_file_io)
.with("organisation_rent_period_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 rent period record" do
expect { import_service.create_organisation_rent_periods("organisation_rent_period_directory") }
.to raise_error(ActiveRecord::RecordInvalid, /Organisation must exist/)
end
end
context "when the organisation does exist" do
before do
FactoryBot.create(:organisation, old_org_id:)
end
it "successfully create an organisation rent period record with the expected data" do
import_service.create_organisation_rent_periods("organisation_rent_period_directory")
expect(Organisation.find_by(old_org_id:).organisation_rent_periods.pluck("rent_period")).to eq([1])
end
end
end
end
Loading…
Cancel
Save