Browse Source

feat: add tests for relationship import

full-import-optimisation
natdeanlewissoftwire 1 year ago
parent
commit
800569b5f6
  1. 2
      app/services/imports/lettings_logs_field_import_service.rb
  2. 15
      app/services/imports/organisation_relationship_import_service.rb
  3. 2
      app/services/imports/scheme_import_service.rb
  4. 5
      spec/fixtures/imports/institution-link/test_institution_link.xml
  5. 60
      spec/services/imports/organisation_relationship_import_service_spec.rb

2
app/services/imports/lettings_logs_field_import_service.rb

@ -123,8 +123,6 @@ module Imports
tenant_code = string_or_nil(xml_doc, "_2bTenCode") tenant_code = string_or_nil(xml_doc, "_2bTenCode")
if tenant_code.present? && record.tenancycode.blank? if tenant_code.present? && record.tenancycode.blank?
record.update!(tenancycode: tenant_code) record.update!(tenancycode: tenant_code)
else
# Continue
end end
else else
@logger.warn("Could not find record matching legacy ID #{old_id}") @logger.warn("Could not find record matching legacy ID #{old_id}")

15
app/services/imports/organisation_relationship_import_service.rb

@ -1,7 +1,7 @@
module Imports module Imports
class OrganisationRelationshipImportService < ImportService class OrganisationRelationshipImportService < ImportService
def create_organisation_relationships(folder) def create_organisation_relationships(folder)
import_from(folder, :create_organisation_relationships) import_from(folder, :create_organisation_relationship)
end end
private private
@ -14,5 +14,18 @@ module Imports
OrganisationRelationship.find_or_create_by!(parent_organisation_id:, child_organisation_id:) OrganisationRelationship.find_or_create_by!(parent_organisation_id:, child_organisation_id:)
end end
def find_organisation_id(xml_doc, id_field)
old_visible_id = string_or_nil(xml_doc, id_field)
organisation = Organisation.find_by(old_visible_id:)
raise "Organisation not found with legacy ID #{old_visible_id}" if organisation.nil?
organisation.id
end
def string_or_nil(xml_doc, attribute)
str = field_value(xml_doc, "institution-link", attribute)
str.presence
end
end end
end end

2
app/services/imports/scheme_import_service.rb

@ -14,8 +14,6 @@ module Imports
old_id: attributes["old_id"], old_id: attributes["old_id"],
old_visible_id: attributes["old_visible_id"], old_visible_id: attributes["old_visible_id"],
) )
else
# Continue
end end
rescue ActiveRecord::RecordInvalid rescue ActiveRecord::RecordInvalid
@logger.error("Scheme #{attributes['old_visible_id']}: Failed to import") @logger.error("Scheme #{attributes['old_visible_id']}: Failed to import")

5
spec/fixtures/imports/institution-link/test_institution_link.xml vendored

@ -0,0 +1,5 @@
<institution-link:institution-link xmlns:institution-link="dclg:institution-link">
<institution-link:id>1</institution-link:id>
<institution-link:parent-institution>1</institution-link:parent-institution>
<institution-link:child-institution>2</institution-link:child-institution>
</institution-link:institution-link>

60
spec/services/imports/organisation_relationship_import_service_spec.rb

@ -0,0 +1,60 @@
require "rails_helper"
RSpec.describe Imports::OrganisationRelationshipImportService do
let(:storage_service) { instance_double(Storage::S3Service) }
let(:logger) { instance_double(Rails::Rack::Logger) }
let(:folder_name) { "organisation_relationships" }
let(:filenames) { %w[my_folder/my_file1.xml my_folder/my_file2.xml] }
let(:fixture_directory) { "spec/fixtures/imports/institution-link" }
let!(:child_organisation) { create(:organisation, old_visible_id: 1) }
let!(:parent_organisation) { create(:organisation, old_visible_id: 2) }
let!(:grandparent_organisation) { create(:organisation, old_visible_id: 3) }
def create_organisation_relationship_file(fixture_directory, child_organisation_id, parent_organisation_id)
file = File.open("#{fixture_directory}/test_institution_link.xml")
doc = Nokogiri::XML(file)
doc.at_xpath("//institution-link:parent-institution").content = parent_organisation_id if parent_organisation_id
doc.at_xpath("//institution-link:child-institution").content = child_organisation_id if child_organisation_id
StringIO.new(doc.to_xml)
end
context "when importing organisation relationships" do
subject(:import_service) { described_class.new(storage_service) }
before do
allow(storage_service).to receive(:list_files)
.and_return(filenames)
allow(storage_service).to receive(:get_file_io)
.with(filenames[0])
.and_return(create_organisation_relationship_file(fixture_directory, 1, 2))
allow(storage_service).to receive(:get_file_io)
.with(filenames[1])
.and_return(create_organisation_relationship_file(fixture_directory, 2, 3))
end
it "successfully create an organisation relationship with the expected data" do
import_service.create_organisation_relationships(folder_name)
organisation_relationship = OrganisationRelationship.find { |r| r.child_organisation == child_organisation }
expect(organisation_relationship.child_organisation).to eq(child_organisation)
expect(organisation_relationship.parent_organisation).to eq(parent_organisation)
end
it "doesn't re-import duplicates" do
import_service.create_organisation_relationships(folder_name)
import_service.create_organisation_relationships(folder_name)
expect(OrganisationRelationship.count).to eq(2)
end
it "successfully creates multiple organisation relationships" do
expect(storage_service).to receive(:list_files).with(folder_name)
expect(storage_service).to receive(:get_file_io).with(filenames[0]).ordered
expect(storage_service).to receive(:get_file_io).with(filenames[1]).ordered
expect { import_service.create_organisation_relationships(folder_name) }.to change(OrganisationRelationship, :count).by(2)
expect(OrganisationRelationship).to exist(child_organisation:, parent_organisation:)
expect(OrganisationRelationship).to exist(child_organisation: parent_organisation, parent_organisation: grandparent_organisation)
end
end
end
Loading…
Cancel
Save