Commit 89da2cff authored by hh1966's avatar hh1966
Browse files

Add spec for Export::ExportCollections and Import::ImportCollections

parent a8f1f832
......@@ -7,7 +7,7 @@ module Import
@import_id = import_id
@current_user_id = current_user_id
@zip_file_path = File.join('tmp', 'import', '#{import_id}.zip')
@zip_file_path = File.join('tmp', 'import', "#{import_id}.zip")
@directory = File.join('tmp', 'import', import_id)
@data = nil
......
# frozen_string_literal: true
require 'rails_helper'
# test for ExportJson ImportJson
RSpec.describe 'ExportImportCollection' do
let(:user) {
create(:person, first_name: 'Ulf', last_name: 'User', name_abbreviation: 'UU')
}
let(:collection) {
create(:collection, user_id: user.id, label: 'Awesome Collection')
}
let(:molfile){
IO.read(Rails.root.join("spec", "fixtures", "test_2.mol"))
}
let(:svg) {
IO.read(Rails.root.join("spec", "fixtures", "images", "molecule.svg"))
}
let(:sample) {
build(
:sample, created_by: user.id, name: 'Sample zero', molfile: molfile,
collections: [collection]
)
}
let(:molecule_name_name) { 'Awesome Molecule' }
let(:molecule_name) {
build(
:molecule_name, user_id: user.id, name: molecule_name_name, molecule_id: sample.molecule_id
)
}
let(:job_id) {
SecureRandom.uuid
}
before do
user.save!
collection.save!
sample.save!
molecule_name.save!
sample.update!(molecule_name_id: molecule_name.id)
end
context 'creates an export file, ' do
before do
export = Export::ExportCollections.new(job_id, [collection.id], 'zip', true)
export.prepare_data
export.to_file
end
it 'which exists' do
file_path = File.join('public', 'zip', "#{job_id}.zip")
expect(File.exist?(file_path)).to be true
end
it 'which is a zip file containing export.json, schema.json and description.txt' do
file_names = []
file_path = File.join('public', 'zip', "#{job_id}.zip")
Zip::File.open(file_path) do |files|
files.each do |file|
file_names << file.name
end
end
expect(file_names).to include('export.json', 'schema.json', 'description.txt')
end
end
end
# frozen_string_literal: true
require 'rails_helper'
# test for ExportJson ImportJson
RSpec.describe 'ExportImportCollection' do
let(:user) {
create(:person, first_name: 'Ulf', last_name: 'User', name_abbreviation: 'UU')
}
before do
user.save!
end
context 'imports from a file' do
before do
# create the `tmp/imports/` if it does not exist yet
import_path = File.join('tmp', 'import')
FileUtils.mkdir_p(import_path) unless Dir.exist?(import_path)
# store the file as `tmp/imports/<import_id>.zip`
import_id = '2541a423-11d9-4c76-a7e1-0da470644012'
import_file_path = File.join('spec', 'fixtures', 'import', "#{import_id}.zip")
zip_file_path = File.join('tmp', 'import', "#{import_id}.zip")
File.open(zip_file_path, 'wb') do |file|
file.write(File.open(import_file_path).read)
end
import = Import::ImportCollections.new(import_id, user.id)
import.extract
import.read
import.import
import.cleanup
end
it 'creates a collection' do
collection = Collection.find_by(label: 'Awesome Collection')
expect(collection).to be_present
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment