baarkerlounger
3 years ago
4 changed files with 83 additions and 0 deletions
@ -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,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