2019-10-15 03:06:19 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-02 19:27:01 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-02-05 21:09:02 +00:00
|
|
|
describe ObjectStorage::MigrateUploadsWorker do
|
2018-04-02 19:27:01 +00:00
|
|
|
let(:model_class) { Project }
|
2018-05-08 14:04:52 +00:00
|
|
|
let(:uploads) { Upload.all }
|
2018-04-02 19:27:01 +00:00
|
|
|
let(:to_store) { ObjectStorage::Store::REMOTE }
|
|
|
|
|
2019-09-20 12:05:52 +00:00
|
|
|
def perform(uploads, store = nil)
|
|
|
|
described_class.new.perform(uploads.ids, model_class.to_s, mounted_as, store || to_store)
|
2018-06-06 20:45:42 +00:00
|
|
|
rescue ObjectStorage::MigrateUploadsWorker::Report::MigrationFailures
|
|
|
|
# swallow
|
|
|
|
end
|
|
|
|
|
2018-05-08 14:04:52 +00:00
|
|
|
context "for AvatarUploader" do
|
|
|
|
let!(:projects) { create_list(:project, 10, :with_avatar) }
|
|
|
|
let(:mounted_as) { :avatar }
|
2018-04-02 19:27:01 +00:00
|
|
|
|
2018-05-08 14:04:52 +00:00
|
|
|
before do
|
|
|
|
stub_uploads_object_storage(AvatarUploader)
|
|
|
|
end
|
|
|
|
|
2018-05-09 19:57:08 +00:00
|
|
|
it_behaves_like "uploads migration worker"
|
2018-06-06 20:45:42 +00:00
|
|
|
|
|
|
|
describe "limits N+1 queries" do
|
2018-06-12 15:22:35 +00:00
|
|
|
it "to N*5" do
|
2018-06-06 20:45:42 +00:00
|
|
|
query_count = ActiveRecord::QueryRecorder.new { perform(uploads) }
|
|
|
|
|
2018-06-12 15:22:35 +00:00
|
|
|
more_projects = create_list(:project, 3, :with_avatar)
|
|
|
|
|
|
|
|
expected_queries_per_migration = 5 * more_projects.count
|
2018-06-06 20:45:42 +00:00
|
|
|
expect { perform(Upload.all) }.not_to exceed_query_limit(query_count).with_threshold(expected_queries_per_migration)
|
|
|
|
end
|
|
|
|
end
|
2018-05-08 14:04:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context "for FileUploader" do
|
|
|
|
let!(:projects) { create_list(:project, 10) }
|
|
|
|
let(:secret) { SecureRandom.hex }
|
|
|
|
let(:mounted_as) { nil }
|
|
|
|
|
2018-06-12 15:22:35 +00:00
|
|
|
def upload_file(project)
|
|
|
|
uploader = FileUploader.new(project)
|
|
|
|
uploader.store!(fixture_file_upload('spec/fixtures/doc_sample.txt'))
|
|
|
|
end
|
|
|
|
|
2018-05-08 14:04:52 +00:00
|
|
|
before do
|
|
|
|
stub_uploads_object_storage(FileUploader)
|
|
|
|
|
2018-06-12 15:22:35 +00:00
|
|
|
projects.map(&method(:upload_file))
|
2018-04-02 19:27:01 +00:00
|
|
|
end
|
2018-05-08 14:04:52 +00:00
|
|
|
|
2018-05-09 19:57:08 +00:00
|
|
|
it_behaves_like "uploads migration worker"
|
2018-06-06 20:45:42 +00:00
|
|
|
|
|
|
|
describe "limits N+1 queries" do
|
2018-06-12 15:22:35 +00:00
|
|
|
it "to N*5" do
|
2018-06-06 20:45:42 +00:00
|
|
|
query_count = ActiveRecord::QueryRecorder.new { perform(uploads) }
|
|
|
|
|
2018-06-12 15:22:35 +00:00
|
|
|
more_projects = create_list(:project, 3)
|
|
|
|
more_projects.map(&method(:upload_file))
|
2018-06-06 20:45:42 +00:00
|
|
|
|
2018-06-12 15:22:35 +00:00
|
|
|
expected_queries_per_migration = 5 * more_projects.count
|
2018-06-06 20:45:42 +00:00
|
|
|
expect { perform(Upload.all) }.not_to exceed_query_limit(query_count).with_threshold(expected_queries_per_migration)
|
|
|
|
end
|
|
|
|
end
|
2018-04-02 19:27:01 +00:00
|
|
|
end
|
|
|
|
end
|