Browse Source

Refactor organisations rake task WIP

pull/267/head
Kat 3 years ago committed by Stéphane Meny
parent
commit
32fbd1223b
No known key found for this signature in database
GPG Key ID: 9D0AFEA988527923
  1. 44
      app/services/import_service.rb
  2. 38
      lib/tasks/data_import/organisations.rake
  3. 4
      spec/lib/tasks/data_import/organisations_spec.rb
  4. 39
      spec/services/import_service_spec.rb

44
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

38
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"),
)
storage_service = StorageService.new(PaasConfigurationService.new, ENV["IMPORT_PAAS_INSTANCE"])
import_service = ImportService.new(storage_service)
import_service.update_organisations(directory)
end
end
end
private
def field_value(doc, field)
doc.at_xpath("//institution:#{field}")&.text
end
def to_boolean(input_string)
input_string == "true"
end

4
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

39
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

Loading…
Cancel
Save