Commit abd78e4d authored by Alex Kalderimis's avatar Alex Kalderimis

Add unique_file class method to FileUpload

This encapsulates the logic for loading a fixture and assigning a new
unique name (with the same extension) into a single method.
parent fdc2417f
...@@ -13,10 +13,6 @@ describe Mutations::DesignManagement::Upload do ...@@ -13,10 +13,6 @@ describe Mutations::DesignManagement::Upload do
described_class.new(object: nil, context: { current_user: user }, field: nil) described_class.new(object: nil, context: { current_user: user }, field: nil)
end end
def unique_file(upload)
::Gitlab::FileUpload.new(upload).tap { |f| f.original_filename = generate(:jpeg_file) }
end
def run_mutation(files_to_upload = files, project_path = project.full_path, iid = issue.iid) def run_mutation(files_to_upload = files, project_path = project.full_path, iid = issue.iid)
mutation = described_class.new(object: nil, context: { current_user: user }, field: nil) mutation = described_class.new(object: nil, context: { current_user: user }, field: nil)
mutation.resolve(project_path: project_path, iid: iid, files: files_to_upload) mutation.resolve(project_path: project_path, iid: iid, files: files_to_upload)
...@@ -53,7 +49,7 @@ describe Mutations::DesignManagement::Upload do ...@@ -53,7 +49,7 @@ describe Mutations::DesignManagement::Upload do
['dk.png', 'rails_sample.jpg', 'banana_sample.gif'] ['dk.png', 'rails_sample.jpg', 'banana_sample.gif']
.cycle .cycle
.take(Concurrent.processor_count * 2) .take(Concurrent.processor_count * 2)
.map { |f| unique_file(fixture_file_upload("spec/fixtures/#{f}")) } .map { |f| RenameableUpload.unique_file(f) }
end end
def creates_designs def creates_designs
......
...@@ -21,7 +21,6 @@ describe DesignManagement::SaveDesignsService do ...@@ -21,7 +21,6 @@ describe DesignManagement::SaveDesignsService do
before do before do
project.add_developer(developer) project.add_developer(developer)
# allow(::DesignManagement::NewVersionWorker).to receive(:perform_async)
end end
def run_service(files_to_upload = nil) def run_service(files_to_upload = nil)
...@@ -101,10 +100,10 @@ describe DesignManagement::SaveDesignsService do ...@@ -101,10 +100,10 @@ describe DesignManagement::SaveDesignsService do
it 'can run the same command in parallel' do it 'can run the same command in parallel' do
blocks = Array.new(10).map do blocks = Array.new(10).map do
wrapped_files = files.map { |f| Gitlab::FileUpload.new(f) } unique_files = %w(rails_sample.jpg dk.png)
wrapped_files.each { |f| f.original_filename = generate(:jpeg_file) } .map { |name| RenameableUpload.unique_file(name) }
-> { run_service(wrapped_files) } -> { run_service(unique_files) }
end end
expect { run_parallel(blocks) }.to change(DesignManagement::Version, :count).by(10) expect { run_parallel(blocks) }.to change(DesignManagement::Version, :count).by(10)
......
# frozen_string_literal: true
class Gitlab::FileUpload < SimpleDelegator
attr_accessor :original_filename
end
# frozen_string_literal: true
class RenameableUpload < SimpleDelegator
attr_accessor :original_filename
# Get a fixture file with a new unique name, and the same extension
def self.unique_file(name)
upload = new(fixture_file_upload("spec/fixtures/#{name}"))
ext = File.extname(name)
new_name = File.basename(FactoryBot.generate(:filename), '.*')
upload.original_filename = new_name + ext
upload
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