Skip to content
This repository has been archived by the owner on Jan 5, 2021. It is now read-only.

Commit

Permalink
Use correct use types in file fixtures
Browse files Browse the repository at this point in the history
Corrects a problem with the file fixtures used in file sets. The use
types were not being correctly set.

Additionally, refactors the FileFactory code in conjunction with the
file set factory bot.
  • Loading branch information
awead committed Mar 13, 2019
1 parent c1b8ab1 commit ec8e988
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 75 deletions.
16 changes: 8 additions & 8 deletions spec/cho/repository/downloads_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
specify do
get :download, params: { id: file_set.id }
expect(response).to be_success
expect(response.body).to eq('Hello World!')
expect(response.body).to eq('Hello World! (preservation)')
end
end

Expand All @@ -20,7 +20,7 @@
specify do
get :download, params: { id: file_set.id }
expect(response).to be_success
expect(response.body).to eq('Hello World!')
expect(response.body).to eq('Hello World! (redacted preservation)')
end
end

Expand All @@ -30,7 +30,7 @@
specify do
get :download, params: { id: file_set.id }
expect(response).to be_success
expect(response.body).to eq('Hello World!')
expect(response.body).to eq('Hello World! (service)')
end
end

Expand All @@ -40,7 +40,7 @@
specify do
get :download, params: { id: file_set.id, use_type: 'PreservationMasterFile' }
expect(response).to be_success
expect(response.body).to eq('Hello World!')
expect(response.body).to eq('Hello World! (redacted preservation)')
end
end

Expand All @@ -50,7 +50,7 @@
specify do
get :download, params: { id: file_set.id, use_type: 'PreservationMasterFile' }
expect(response).to be_success
expect(response.body).to eq('Hello World!')
expect(response.body).to eq('Hello World! (preservation)')
end
end

Expand All @@ -60,7 +60,7 @@
specify do
get :download, params: { id: file_set.id, use_type: 'AccessFile' }
expect(response).to be_success
expect(response.body).to eq('Hello World!')
expect(response.body).to eq('Hello World! (access)')
end
end

Expand All @@ -70,7 +70,7 @@
specify do
get :download, params: { id: file_set.id, use_type: 'ServiceFile' }
expect(response).to be_success
expect(response.body).to eq('Hello World!')
expect(response.body).to eq('Hello World! (service)')
end
end

Expand All @@ -80,7 +80,7 @@
specify do
get :download, params: { id: file_set.id, use_type: :bogus }
expect(response).to be_success
expect(response.body).to eq('Hello World!')
expect(response.body).to eq('Hello World! (service)')
end
end

Expand Down
83 changes: 25 additions & 58 deletions spec/factories/file_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,90 +23,57 @@

trait :with_member_file do
to_create do |resource, attributes|
temp_file = Rack::Test::UploadedFile.new(Rails.root.join('spec', 'fixtures', 'hello_world.txt'))
file = Work::File.new(original_filename: temp_file.original_filename)
file_change_set = Work::FileChangeSet.new(file)
result = Transaction::Operations::File::Create.new.call(file_change_set, temp_file: temp_file)
raise result.failure if result.failure?

resource.member_ids = [result.success.id]
fileset = Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: resource)
work = attributes.work
work ||= FactoryBot.build(:work_submission)
work.member_ids << fileset.id
Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: work)
fileset
file_factory = FileFactory.new
file_factory.build_file_set(resource: resource, attributes: attributes)
end
end

trait :with_preservation_file do
to_create do |resource, attributes|
result = FileFactory.new.hello_world

resource.member_ids = [result.success.id]
fileset = Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: resource)
work = attributes.work
work ||= FactoryBot.build(:work_submission)
work.member_ids << fileset.id
Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: work)
fileset
file_factory = FileFactory.new(
use: Valkyrie::Vocab::PCDMUse.PreservationMasterFile,
text: 'Hello World! (preservation)'
)
file_factory.build_file_set(resource: resource, attributes: attributes)
end
end

trait :with_missing_file do
to_create do |resource, attributes|
result = FileFactory.new.hello_world
FileUtils.rm_f(result.success.resource.path)

resource.member_ids = [result.success.id]
fileset = Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: resource)
work = attributes.work
work ||= FactoryBot.build(:work_submission)
work.member_ids << fileset.id
Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: work)
file_factory = FileFactory.new
fileset = file_factory.build_file_set(resource: resource, attributes: attributes)
FileUtils.rm_f(fileset.files.first.path)
fileset
end
end

trait :with_redacted_preservation_file do
to_create do |resource, attributes|
result = FileFactory.new(use: Vocab::FileUse.RedactedPreservationMasterFile).hello_world

resource.member_ids = [result.success.id]
fileset = Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: resource)
work = attributes.work
work ||= FactoryBot.build(:work_submission)
work.member_ids << fileset.id
Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: work)
fileset
file_factory = FileFactory.new(
use: Vocab::FileUse.RedactedPreservationMasterFile,
text: 'Hello World! (redacted preservation)'
)
file_factory.build_file_set(resource: resource, attributes: attributes)
end
end

trait :with_service_file do
to_create do |resource, attributes|
result = FileFactory.new(use: Valkyrie::Vocab::PCDMUse.ServiceFile).hello_world

resource.member_ids = [result.success.id]
fileset = Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: resource)
work = attributes.work
work ||= FactoryBot.build(:work_submission)
work.member_ids << fileset.id
Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: work)
fileset
file_factory = FileFactory.new(
use: Valkyrie::Vocab::PCDMUse.ServiceFile,
text: 'Hello World! (service)'
)
file_factory.build_file_set(resource: resource, attributes: attributes)
end
end

trait :with_access_file do
to_create do |resource, attributes|
result = FileFactory.new(use: Vocab::FileUse.AccessFile).hello_world

resource.member_ids = [result.success.id]
fileset = Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: resource)
work = attributes.work
work ||= FactoryBot.build(:work_submission)
work.member_ids << fileset.id
Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: work)
fileset
file_factory = FileFactory.new(
use: Vocab::FileUse.AccessFile,
text: 'Hello World! (access)'
)
file_factory.build_file_set(resource: resource, attributes: attributes)
end
end
end
40 changes: 31 additions & 9 deletions spec/support/file_factory.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
# frozen_string_literal: true

class FileFactory
attr_reader :use
attr_reader :use, :text, :original_filename

def initialize(use: nil)
def initialize(use: nil, text: nil, original_filename: nil)
@use = use || Valkyrie::Vocab::PCDMUse.PreservationMasterFile
@text = text || 'Hello World!'
@original_filename = original_filename || 'hello_world.txt'
end

def hello_world
temp_file = Rack::Test::UploadedFile.new(Rails.root.join('spec', 'fixtures', 'hello_world.txt'))
file = Work::File.new(original_filename: temp_file.original_filename, use: use)
file_change_set = Work::FileChangeSet.new(file)
result = Transaction::Operations::File::Create.new.call(file_change_set, temp_file: temp_file)
raise result.failure if result.failure?
result
def build_file_set(attributes:, resource:)
raise text_file.failure if text_file.failure?
resource.member_ids = [text_file.success.id]
fileset = Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: resource)
work = attributes.work
work ||= FactoryBot.build(:work_submission)
work.member_ids << fileset.id
Valkyrie::MetadataAdapter.find(:indexing_persister).persister.save(resource: work)
fileset
end

private

def text_file
@text_file ||= begin
temp_file = temp_text_file(text)
file = Work::File.new(original_filename: original_filename, use: use)
file_change_set = Work::FileChangeSet.new(file)
Transaction::Operations::File::Create.new.call(file_change_set, temp_file: temp_file)
end
end

def temp_text_file(text)
file = Tempfile.new
file.write(text)
file.close
file
end
end

0 comments on commit ec8e988

Please sign in to comment.