Browse Source
* Rake task to import organisation data * Make spec a little more robustpull/223/head
baarkerlounger
3 years ago
committed by
GitHub
5 changed files with 149 additions and 2 deletions
@ -0,0 +1,45 @@ |
|||||||
|
class AdditionalOrgFields < ActiveRecord::Migration[7.0] |
||||||
|
def up |
||||||
|
change_table :organisations, bulk: true do |t| |
||||||
|
t.column :active, :boolean |
||||||
|
t.column :old_association_type, :integer |
||||||
|
t.column :software_supplier_id, :string |
||||||
|
t.column :housing_management_system, :string |
||||||
|
t.column :choice_based_lettings, :boolean |
||||||
|
t.column :common_housing_register, :boolean |
||||||
|
t.column :choice_allocation_policy, :boolean |
||||||
|
t.column :cbl_proportion_percentage, :integer |
||||||
|
t.column :enter_affordable_logs, :boolean |
||||||
|
t.column :owns_affordable_logs, :boolean |
||||||
|
t.column :housing_registration_no, :string |
||||||
|
t.column :general_needs_units, :integer |
||||||
|
t.column :supported_housing_units, :integer |
||||||
|
t.column :unspecified_units, :integer |
||||||
|
t.column :old_org_id, :string |
||||||
|
t.column :old_visible_id, :integer |
||||||
|
t.change :phone, :string |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
def down |
||||||
|
change_table :organisations, bulk: true do |t| |
||||||
|
t.remove :active |
||||||
|
t.remove :old_association_type |
||||||
|
t.remove :software_supplier_id |
||||||
|
t.remove :housing_management_system |
||||||
|
t.remove :choice_based_lettings |
||||||
|
t.remove :common_housing_register |
||||||
|
t.remove :choice_allocation_policy |
||||||
|
t.remove :cbl_proportion_percentage |
||||||
|
t.remove :enter_affordable_logs |
||||||
|
t.remove :owns_affordable_logs |
||||||
|
t.remove :housing_registration_no |
||||||
|
t.remove :general_needs_units |
||||||
|
t.remove :supported_housing_units |
||||||
|
t.remove :unspecified_units |
||||||
|
t.remove :old_org_id |
||||||
|
t.remove :old_visible_id |
||||||
|
t.change :phone, "integer USING phone::integer" |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,45 @@ |
|||||||
|
require "nokogiri" |
||||||
|
|
||||||
|
namespace :data_import do |
||||||
|
desc "Import Organisation XMLs from Softwire system" |
||||||
|
|
||||||
|
# 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 |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def field_value(doc, field) |
||||||
|
doc.at_xpath("//institution:#{field}")&.text |
||||||
|
end |
||||||
|
|
||||||
|
def to_boolean(input_string) |
||||||
|
input_string == "true" |
||||||
|
end |
@ -0,0 +1,23 @@ |
|||||||
|
<institution:institution xmlns:institution="dclg:institution"> |
||||||
|
<institution:id>7c5bd5fb549c09z2c55d9cb90d7ba84927e64618</institution:id> |
||||||
|
<institution:name>HA Ltd</institution:name> |
||||||
|
<institution:institution-type>HOUSING-ASSOCIATION</institution:institution-type> |
||||||
|
<institution:active>true</institution:active> |
||||||
|
<institution:almsyn>false</institution:almsyn> |
||||||
|
<institution:old-association-type>CHHA</institution:old-association-type> |
||||||
|
<institution:telephone-number>xxxxxxxx</institution:telephone-number> |
||||||
|
<institution:software-supplier-id>false</institution:software-supplier-id> |
||||||
|
<institution:housing-management-system/> |
||||||
|
<institution:choice-based-letting>false</institution:choice-based-letting> |
||||||
|
<institution:common-housing-register>false</institution:common-housing-register> |
||||||
|
<institution:choice-allocation-policy>false</institution:choice-allocation-policy> |
||||||
|
<institution:cbl-proportion-percentage/> |
||||||
|
<institution:enter-affordable-logs>true</institution:enter-affordable-logs> |
||||||
|
<institution:owns-affordable-rent>true</institution:owns-affordable-rent> |
||||||
|
<institution:housing-registration-no>LH9999</institution:housing-registration-no> |
||||||
|
<institution:visible-id>1034</institution:visible-id> |
||||||
|
<institution:holds-stock>true</institution:holds-stock> |
||||||
|
<institution:general-needs-units>1104</institution:general-needs-units> |
||||||
|
<institution:supported-housing-units>217</institution:supported-housing-units> |
||||||
|
<institution:unspecified-units>0</institution:unspecified-units> |
||||||
|
</institution:institution> |
@ -0,0 +1,18 @@ |
|||||||
|
require "rails_helper" |
||||||
|
require "rake" |
||||||
|
|
||||||
|
describe "rake data_import:organisations", type: :task do |
||||||
|
subject(:task) { Rake::Task["data_import:organisations"] } |
||||||
|
let(:fixture_path) { "spec/fixtures/softwire_imports/organisations" } |
||||||
|
|
||||||
|
before do |
||||||
|
Rake.application.rake_require("tasks/data_import/organisations") |
||||||
|
Rake::Task.define_task(:environment) |
||||||
|
task.reenable |
||||||
|
end |
||||||
|
|
||||||
|
it "creates an organisation from the given XML file" do |
||||||
|
expect { task.invoke(fixture_path) }.to change(Organisation, :count).by(1) |
||||||
|
expect(Organisation.find_by(old_visible_id: 1034).name).to eq("HA Ltd") |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue