Browse Source

Rake task to import organisation data (#221)

* Rake task to import organisation data

* Make spec a little more robust
pull/223/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
bb7206f789
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 45
      db/migrate/20220114105351_additional_org_fields.rb
  2. 20
      db/schema.rb
  3. 45
      lib/tasks/data_import/organisations.rake
  4. 23
      spec/fixtures/softwire_imports/organisations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml
  5. 18
      spec/lib/tasks/data_import/organisations_spec.rb

45
db/migrate/20220114105351_additional_org_fields.rb

@ -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

20
db/schema.rb

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_01_12_151048) do
ActiveRecord::Schema.define(version: 2022_01_14_105351) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -191,7 +191,7 @@ ActiveRecord::Schema.define(version: 2022_01_12_151048) do
create_table "organisations", force: :cascade do |t|
t.string "name"
t.integer "phone"
t.string "phone"
t.integer "providertype"
t.string "address_line1"
t.string "address_line2"
@ -202,6 +202,22 @@ ActiveRecord::Schema.define(version: 2022_01_12_151048) do
t.string "managing_agents"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.boolean "active"
t.integer "old_association_type"
t.string "software_supplier_id"
t.string "housing_management_system"
t.boolean "choice_based_lettings"
t.boolean "common_housing_register"
t.boolean "choice_allocation_policy"
t.integer "cbl_proportion_percentage"
t.boolean "enter_affordable_logs"
t.boolean "owns_affordable_logs"
t.string "housing_registration_no"
t.integer "general_needs_units"
t.integer "supported_housing_units"
t.integer "unspecified_units"
t.string "old_org_id"
t.integer "old_visible_id"
end
create_table "users", force: :cascade do |t|

45
lib/tasks/data_import/organisations.rake

@ -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

23
spec/fixtures/softwire_imports/organisations/7c5bd5fb549c09a2c55d7cb90d7ba84927e64618.xml vendored

@ -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>

18
spec/lib/tasks/data_import/organisations_spec.rb

@ -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…
Cancel
Save