From 800569b5f6afd05e4658adb2065f2b855f1532cc Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Fri, 11 Aug 2023 17:39:56 +0100 Subject: [PATCH] feat: add tests for relationship import --- .../lettings_logs_field_import_service.rb | 2 - ...rganisation_relationship_import_service.rb | 15 ++++- app/services/imports/scheme_import_service.rb | 2 - .../test_institution_link.xml | 5 ++ ...sation_relationship_import_service_spec.rb | 60 +++++++++++++++++++ 5 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 spec/fixtures/imports/institution-link/test_institution_link.xml create mode 100644 spec/services/imports/organisation_relationship_import_service_spec.rb diff --git a/app/services/imports/lettings_logs_field_import_service.rb b/app/services/imports/lettings_logs_field_import_service.rb index 6aaac63a7..dfffe3a70 100644 --- a/app/services/imports/lettings_logs_field_import_service.rb +++ b/app/services/imports/lettings_logs_field_import_service.rb @@ -123,8 +123,6 @@ module Imports tenant_code = string_or_nil(xml_doc, "_2bTenCode") if tenant_code.present? && record.tenancycode.blank? record.update!(tenancycode: tenant_code) - else - # Continue end else @logger.warn("Could not find record matching legacy ID #{old_id}") diff --git a/app/services/imports/organisation_relationship_import_service.rb b/app/services/imports/organisation_relationship_import_service.rb index d13ae4d87..baf3f27f6 100644 --- a/app/services/imports/organisation_relationship_import_service.rb +++ b/app/services/imports/organisation_relationship_import_service.rb @@ -1,7 +1,7 @@ module Imports class OrganisationRelationshipImportService < ImportService def create_organisation_relationships(folder) - import_from(folder, :create_organisation_relationships) + import_from(folder, :create_organisation_relationship) end private @@ -14,5 +14,18 @@ module Imports OrganisationRelationship.find_or_create_by!(parent_organisation_id:, child_organisation_id:) 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 diff --git a/app/services/imports/scheme_import_service.rb b/app/services/imports/scheme_import_service.rb index d07ed9d14..4ddb0e7c3 100644 --- a/app/services/imports/scheme_import_service.rb +++ b/app/services/imports/scheme_import_service.rb @@ -14,8 +14,6 @@ module Imports old_id: attributes["old_id"], old_visible_id: attributes["old_visible_id"], ) - else - # Continue end rescue ActiveRecord::RecordInvalid @logger.error("Scheme #{attributes['old_visible_id']}: Failed to import") diff --git a/spec/fixtures/imports/institution-link/test_institution_link.xml b/spec/fixtures/imports/institution-link/test_institution_link.xml new file mode 100644 index 000000000..7030fbc47 --- /dev/null +++ b/spec/fixtures/imports/institution-link/test_institution_link.xml @@ -0,0 +1,5 @@ + + 1 + 1 + 2 + diff --git a/spec/services/imports/organisation_relationship_import_service_spec.rb b/spec/services/imports/organisation_relationship_import_service_spec.rb new file mode 100644 index 000000000..4d6cc500a --- /dev/null +++ b/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