From 32fbd1223bd387fc755d9c88cfe4e7fe70fd9f7a Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 2 Feb 2022 16:07:42 +0000 Subject: [PATCH] Refactor organisations rake task WIP --- app/services/import_service.rb | 44 +++++++++++++++++++ lib/tasks/data_import/organisations.rake | 38 ++-------------- .../tasks/data_import/organisations_spec.rb | 4 ++ spec/services/import_service_spec.rb | 39 ++++++++++++---- 4 files changed, 82 insertions(+), 43 deletions(-) diff --git a/app/services/import_service.rb b/app/services/import_service.rb index 28c8005a4..936b78f07 100644 --- a/app/services/import_service.rb +++ b/app/services/import_service.rb @@ -2,4 +2,48 @@ class ImportService def initialize(storage_service) @storage_service = storage_service end + + def update_organisations(folder) + filenames = @storage_service.list_files(folder) + filenames.each do |filename| + file_io = @storage_service.get_file_io(filename) + create_organisation(file_io) + end + end + +private + + def create_organisation(file_io) + doc = Nokogiri::XML(file_io) + Organisation.create!( + name: field_value(doc, "name"), + providertype: field_value(doc, "institution-type"), + phone: field_value(doc, "telephone-number"), + holds_own_stock: to_boolean(field_value(doc, "holds-stock")), + active: to_boolean(field_value(doc, "active")), + old_association_type: field_value(doc, "old-association-type"), + software_supplier_id: field_value(doc, "software-supplier-id"), + housing_management_system: field_value(doc, "housing-management-system"), + choice_based_lettings: to_boolean(field_value(doc, "choice-based-lettings")), + common_housing_register: to_boolean(field_value(doc, "common-housing-register")), + choice_allocation_policy: to_boolean(field_value(doc, "choice-allocation-policy")), + cbl_proportion_percentage: field_value(doc, "cbl-proportion-percentage"), + enter_affordable_logs: to_boolean(field_value(doc, "enter-affordable-logs")), + owns_affordable_logs: to_boolean(field_value(doc, "owns-affordable-rent")), + housing_registration_no: field_value(doc, "housing-registration-no"), + general_needs_units: field_value(doc, "general-needs-units"), + supported_housing_units: field_value(doc, "supported-housing-units"), + unspecified_units: field_value(doc, "unspecified-units"), + old_org_id: field_value(doc, "id"), + old_visible_id: field_value(doc, "visible-id"), + ) + end + + def field_value(doc, field) + doc.at_xpath("//institution:#{field}")&.text + end + + def to_boolean(input_string) + input_string == "true" + end end diff --git a/lib/tasks/data_import/organisations.rake b/lib/tasks/data_import/organisations.rake index 5caf1ecef..72c59f6ce 100644 --- a/lib/tasks/data_import/organisations.rake +++ b/lib/tasks/data_import/organisations.rake @@ -6,40 +6,8 @@ namespace :data_import do # rake data_import:organisations['path/to/xml_files'] task :organisations, %i[path] => :environment do |_task, args| directory = args.path - Dir.glob("#{directory}/*.xml").each do |file| - doc = Nokogiri::XML(File.open(file)) - Organisation.create!( - name: field_value(doc, "name"), - providertype: field_value(doc, "institution-type"), - phone: field_value(doc, "telephone-number"), - holds_own_stock: to_boolean(field_value(doc, "holds-stock")), - active: to_boolean(field_value(doc, "active")), - old_association_type: field_value(doc, "old-association-type"), - software_supplier_id: field_value(doc, "software-supplier-id"), - housing_management_system: field_value(doc, "housing-management-system"), - choice_based_lettings: to_boolean(field_value(doc, "choice-based-lettings")), - common_housing_register: to_boolean(field_value(doc, "common-housing-register")), - choice_allocation_policy: to_boolean(field_value(doc, "choice-allocation-policy")), - cbl_proportion_percentage: field_value(doc, "cbl-proportion-percentage"), - enter_affordable_logs: to_boolean(field_value(doc, "enter-affordable-logs")), - owns_affordable_logs: to_boolean(field_value(doc, "owns-affordable-rent")), - housing_registration_no: field_value(doc, "housing-registration-no"), - general_needs_units: field_value(doc, "general-needs-units"), - supported_housing_units: field_value(doc, "supported-housing-units"), - unspecified_units: field_value(doc, "unspecified-units"), - old_org_id: field_value(doc, "id"), - old_visible_id: field_value(doc, "visible-id"), - ) - end + storage_service = StorageService.new(PaasConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"]) + import_service = ImportService.new(storage_service) + import_service.update_organisations(directory) end end - -private - -def field_value(doc, field) - doc.at_xpath("//institution:#{field}")&.text -end - -def to_boolean(input_string) - input_string == "true" -end diff --git a/spec/lib/tasks/data_import/organisations_spec.rb b/spec/lib/tasks/data_import/organisations_spec.rb index 34a1a6b4c..2e1b93dc8 100644 --- a/spec/lib/tasks/data_import/organisations_spec.rb +++ b/spec/lib/tasks/data_import/organisations_spec.rb @@ -10,6 +10,10 @@ describe "rake data_import:organisations", type: :task do Rake.application.rake_require("tasks/data_import/organisations") Rake::Task.define_task(:environment) task.reenable + + allow(ENV).to receive(:[]).with("VCAP_SERVICE").and_return(vcap_service) + allow(ENV).to receive(:[]).with("IMPORT_PAAS_INSTANCE").and_return("my_instance") + end it "creates an organisation from the given XML file" do diff --git a/spec/services/import_service_spec.rb b/spec/services/import_service_spec.rb index ebbdfa5c1..bf154ab4d 100644 --- a/spec/services/import_service_spec.rb +++ b/spec/services/import_service_spec.rb @@ -1,17 +1,40 @@ -require "rspec" +require "rails_helper" -describe ImportService do +RSpec.describe ImportService do let(:storage_service) { instance_double(StorageService) } + let(:folder_name) { "organisations" } + let(:filenames) { %w[my_folder/my_file1.xml my_folder/my_file2.xml] } + let(:fixture_directory) { "spec/fixtures/softwire_imports/organisations" } + + def create_organisation_file(fixture_directory, visible_id) + file = File.open("#{fixture_directory}/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml") + doc = Nokogiri::XML(file) + doc.at_xpath("//institution:visible-id").content = visible_id + StringIO.new(doc.to_xml) + end context "when importing organisations" 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_file(fixture_directory, 1)) + allow(storage_service).to receive(:get_file_io) + .with(filenames[1]) + .and_return(create_organisation_file(fixture_directory, 2)) + end + it "successfully create a new organisation if it does not exist" do - import_service.update_organisations() - # 1. call import with folder name - # 2. check that it calls storage lists files - # 3. check that it calls read file on each files - # 4. create a temporary organisation object - # 5. update/insert (upsert) organisation (settings?) + 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.update_organisations(folder_name) }.to change(Organisation, :count).by(2) + expect(Organisation).to exist(old_visible_id: 1) + expect(Organisation).to exist(old_visible_id: 2) end end end