4 changed files with 82 additions and 43 deletions
@ -1,17 +1,40 @@ |
|||||||
require "rspec" |
require "rails_helper" |
||||||
|
|
||||||
describe ImportService do |
RSpec.describe ImportService do |
||||||
let(:storage_service) { instance_double(StorageService) } |
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 |
context "when importing organisations" do |
||||||
subject(:import_service) { described_class.new(storage_service) } |
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 |
it "successfully create a new organisation if it does not exist" do |
||||||
import_service.update_organisations() |
expect(storage_service).to receive(:list_files).with(folder_name) |
||||||
# 1. call import with folder name |
expect(storage_service).to receive(:get_file_io).with(filenames[0]).ordered |
||||||
# 2. check that it calls storage lists files |
expect(storage_service).to receive(:get_file_io).with(filenames[1]).ordered |
||||||
# 3. check that it calls read file on each files |
|
||||||
# 4. create a temporary organisation object |
expect { import_service.update_organisations(folder_name) }.to change(Organisation, :count).by(2) |
||||||
# 5. update/insert (upsert) organisation (settings?) |
expect(Organisation).to exist(old_visible_id: 1) |
||||||
|
expect(Organisation).to exist(old_visible_id: 2) |
||||||
end |
end |
||||||
end |
end |
||||||
end |
end |
||||||
|
Loading…
Reference in new issue