baarkerlounger
3 years ago
committed by
GitHub
17 changed files with 225 additions and 5 deletions
@ -0,0 +1,3 @@ |
|||||||
|
class OrganisationRentPeriod < ApplicationRecord |
||||||
|
belongs_to :organisation |
||||||
|
end |
@ -0,0 +1,5 @@ |
|||||||
|
class RentPeriod |
||||||
|
def self.rent_period_mappings |
||||||
|
FormHandler.instance.current_form.get_question("period", nil).answer_options |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,22 @@ |
|||||||
|
module Imports |
||||||
|
class OrganisationRentPeriodImportService < ImportService |
||||||
|
def create_organisation_rent_periods(folder) |
||||||
|
import_from(folder, :create_organisation_rent_period) |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def create_organisation_rent_period(xml_document) |
||||||
|
organisation = Organisation.find_by(old_org_id: record_field_value(xml_document, "institution")) |
||||||
|
|
||||||
|
OrganisationRentPeriod.create!( |
||||||
|
organisation:, |
||||||
|
rent_period: Integer(record_field_value(xml_document, "period")), |
||||||
|
) |
||||||
|
end |
||||||
|
|
||||||
|
def record_field_value(xml_document, field) |
||||||
|
field_value(xml_document, "rent-period", field) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,10 @@ |
|||||||
|
class OrganisationRentPeriod < ActiveRecord::Migration[7.0] |
||||||
|
def change |
||||||
|
create_table :organisation_rent_periods do |t| |
||||||
|
t.belongs_to :organisation |
||||||
|
t.column :rent_period, :integer |
||||||
|
|
||||||
|
t.timestamps |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,5 @@ |
|||||||
|
<rent-period:rent-period xmlns:rent-period="dclg:rent-period"> |
||||||
|
<rent-period:id>ebd22326d33e389e9f1bfd546979d2c05f9e68d6</rent-period:id> |
||||||
|
<rent-period:institution>44026acc7ed5c29516b26f2a5deb639e5e37966d</rent-period:institution> |
||||||
|
<rent-period:period>1</rent-period:period> |
||||||
|
</rent-period:rent-period> |
@ -0,0 +1,16 @@ |
|||||||
|
require "rails_helper" |
||||||
|
|
||||||
|
RSpec.describe RentPeriod, type: :model do |
||||||
|
describe "rent period mapping" do |
||||||
|
let(:form) { Form.new("spec/fixtures/forms/2021_2022.json", "2021_2022") } |
||||||
|
|
||||||
|
before do |
||||||
|
allow(FormHandler.instance).to receive(:current_form).and_return(form) |
||||||
|
end |
||||||
|
|
||||||
|
it "maps rent period id to display names" do |
||||||
|
expect(described_class.rent_period_mappings).to be_a(Hash) |
||||||
|
expect(described_class.rent_period_mappings["2"]).to eq({ "value" => "Weekly for 52 weeks" }) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,41 @@ |
|||||||
|
require "rails_helper" |
||||||
|
|
||||||
|
RSpec.describe Imports::OrganisationRentPeriodImportService do |
||||||
|
let(:fixture_directory) { "spec/fixtures/softwire_imports/organisation_rent_periods" } |
||||||
|
let(:old_org_id) { "44026acc7ed5c29516b26f2a5deb639e5e37966d" } |
||||||
|
let(:old_id) { "ebd22326d33e389e9f1bfd546979d2c05f9e68d6" } |
||||||
|
let(:import_file) { File.open("#{fixture_directory}/#{old_id}.xml") } |
||||||
|
let(:storage_service) { instance_double(StorageService) } |
||||||
|
|
||||||
|
context "when importing data protection confirmations" do |
||||||
|
subject(:import_service) { described_class.new(storage_service) } |
||||||
|
|
||||||
|
before do |
||||||
|
allow(storage_service) |
||||||
|
.to receive(:list_files) |
||||||
|
.and_return(["organisation_rent_period_directory/#{old_id}.xml"]) |
||||||
|
allow(storage_service) |
||||||
|
.to receive(:get_file_io) |
||||||
|
.with("organisation_rent_period_directory/#{old_id}.xml") |
||||||
|
.and_return(import_file) |
||||||
|
end |
||||||
|
|
||||||
|
context "when the organisation in the import file doesn't exist in the system" do |
||||||
|
it "does not create an organisation rent period record" do |
||||||
|
expect { import_service.create_organisation_rent_periods("organisation_rent_period_directory") } |
||||||
|
.to raise_error(ActiveRecord::RecordInvalid, /Organisation must exist/) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "when the organisation does exist" do |
||||||
|
before do |
||||||
|
FactoryBot.create(:organisation, old_org_id:) |
||||||
|
end |
||||||
|
|
||||||
|
it "successfully create an organisation rent period record with the expected data" do |
||||||
|
import_service.create_organisation_rent_periods("organisation_rent_period_directory") |
||||||
|
expect(Organisation.find_by(old_org_id:).organisation_rent_periods.pluck("rent_period")).to eq([1]) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue