diff --git a/app/services/model_exporters/experiment_exporter.rb b/app/services/model_exporters/experiment_exporter.rb index f37b1ccc2..b74d3edb7 100644 --- a/app/services/model_exporters/experiment_exporter.rb +++ b/app/services/model_exporters/experiment_exporter.rb @@ -11,7 +11,7 @@ module ModelExporters def export_template_to_dir @asset_counter = 0 - @experiment.transaction(isolation: :serializable) do + @experiment.transaction do @experiment.uuid ||= SecureRandom.uuid @dir_to_export = FileUtils.mkdir_p( File.join("tmp/experiment_#{@experiment.id}" \ @@ -30,6 +30,7 @@ module ModelExporters end puts "Exported assets: #{@asset_counter}" puts 'Done!' + return @dir_to_export end end diff --git a/app/services/templates_service.rb b/app/services/templates_service.rb index 04d7ea5f9..b3cd76c18 100644 --- a/app/services/templates_service.rb +++ b/app/services/templates_service.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true class TemplatesService - def initialize - templates_dir_pattern = "#{Rails.root}/app/assets/templates/experiment_*/" + def initialize(base_dir = nil) + @base_dir = base_dir ? base_dir : "#{Rails.root}/app/assets/templates" + templates_dir_pattern = "#{@base_dir}/experiment_*/" @experiment_templates = {} Dir.glob(templates_dir_pattern).each do |tmplt_dir| id = /[0-9]+/.match(tmplt_dir.split('/').last)[0] @@ -21,7 +22,7 @@ class TemplatesService .user return unless owner.present? updated = false - exp_tmplt_dir_prefix = "#{Rails.root}/app/assets/templates/experiment_" + exp_tmplt_dir_prefix = "#{@base_dir}/experiment_" existing = project.experiments.where.not(uuid: nil).pluck(:uuid) @experiment_templates.except(*existing).each_value do |id| importer_service = TeamImporter.new diff --git a/app/utilities/first_time_data_generator.rb b/app/utilities/first_time_data_generator.rb index 4c29d7227..591d98072 100644 --- a/app/utilities/first_time_data_generator.rb +++ b/app/utilities/first_time_data_generator.rb @@ -1061,7 +1061,7 @@ module FirstTimeDataGenerator project: project, created_at: created_at, updated_at: created_at, - message: t('activities.add_comment_to_project', + message: I18n.t('activities.add_comment_to_project', user: user.full_name, project: project.name) ).sneaky_save @@ -1082,7 +1082,7 @@ module FirstTimeDataGenerator my_module: my_module, created_at: created_at, updated_at: created_at, - message: t('activities.add_comment_to_module', + message: I18n.t('activities.add_comment_to_module', user: user.full_name, module: my_module.name) ).sneaky_save @@ -1103,7 +1103,7 @@ module FirstTimeDataGenerator my_module: result.my_module, created_at: created_at, updated_at: created_at, - message: t('activities.add_comment_to_result', + message: I18n.t('activities.add_comment_to_result', user: user.full_name, result: result.name) ).sneaky_save @@ -1124,7 +1124,7 @@ module FirstTimeDataGenerator my_module: step.protocol.my_module, created_at: created_at, updated_at: created_at, - message: t('activities.add_comment_to_step', + message: I18n.t('activities.add_comment_to_step', user: user.full_name, step: step.position + 1, step_name: step.name) diff --git a/spec/services/templates_service_spec.rb b/spec/services/templates_service_spec.rb new file mode 100644 index 000000000..717460afa --- /dev/null +++ b/spec/services/templates_service_spec.rb @@ -0,0 +1,95 @@ +# frozen_string_literal: true + +require 'rails_helper' +require 'fileutils' + +describe TemplatesService do + include FirstTimeDataGenerator + let!(:main_team) { create :team } + let!(:admin_user) { create :user } + + describe '#update_project' do + context 'update templates project' do + it 'experiment is added to templates project' do + create(:user_team, user: admin_user, team: main_team) + seed_demo_data(main_team.created_by, main_team) + dj_worker = Delayed::Worker.new + # Two runs are needed to execute additional jobs which get generated + # during first run + Delayed::Job.all.each { |job| dj_worker.run(job) } + Delayed::Job.all.each { |job| dj_worker.run(job) } + demo_exp = main_team.projects.first.experiments.first + exp_exporter = ModelExporters::ExperimentExporter.new(demo_exp.id) + exp_dir = exp_exporter.export_template_to_dir + tmplts_dir = "#{Rails.root}/tmp/testing_templates" + FileUtils.remove_dir(tmplts_dir) if Dir.exist?(tmplts_dir) + FileUtils.mkdir(tmplts_dir) + FileUtils.mv(exp_dir, "#{tmplts_dir}/experiment_#{demo_exp.id}") + templates_project = create :project, name: 'Templates', template: true + create( + :user_project, :owner, project: templates_project, user: admin_user + ) + ts = TemplatesService.new(tmplts_dir) + ts.update_project(templates_project) + Delayed::Job.all.each { |job| dj_worker.run(job) } + tmpl_exp = templates_project.experiments.first + + expect(tmpl_exp.name).to eq(demo_exp.name) + expect(tmpl_exp.uuid).to_not eq(nil) + expect(tmpl_exp.my_modules.pluck(:name)) + .to match_array(demo_exp.my_modules.pluck(:name)) + tmpl_tasks = tmpl_exp.my_modules + demo_tasks = demo_exp.my_modules + demo_tasks.each do |demo_task| + tmpl_task = tmpl_tasks.find_by_name(demo_task.name) + expect(tmpl_task.name).to eq(demo_task.name) + expect(tmpl_task.task_comments.size) + .to eq(demo_task.task_comments.size) + demo_task.results.each do |demo_res| + tmpl_res = tmpl_task.results.find_by_name(demo_res.name) + expect(tmpl_res.name).to eq(demo_res.name) + if demo_res.asset + expect(tmpl_res.asset.file.exists?).to eq(true) + expect(demo_res.asset.file_file_name) + .to eq(tmpl_res.asset.file_file_name) + elsif demo_res.table + expect(demo_res.table.contents).to eq(tmpl_res.table.contents) + elsif demo_res.result_text + expect(demo_res.result_text.text).to eq(tmpl_res.result_text.text) + end + expect(demo_res.result_comments.size) + .to eq(tmpl_res.result_comments.size) + end + unless demo_task.protocol.present? && + demo_task.protocol.steps.size.positive? + next + end + demo_task.protocol.steps.each do |demo_step| + tmpl_step = tmpl_task.protocol.steps.find_by_name(demo_step.name) + expect(demo_step.name).to eq(tmpl_step.name) + expect(demo_step.description).to eq(tmpl_step.description) + if demo_step.assets.present? + expect(demo_step.assets.pluck(:file_file_name)) + .to match_array(tmpl_step.assets.pluck(:file_file_name)) + end + tmpl_step.assets.each do |asset| + expect(asset.file.exists?).to eq(true) + end + if demo_step.tables.present? + expect(demo_step.tables.pluck(:contents)) + .to match_array(tmpl_step.tables.pluck(:contents)) + end + if demo_step.checklists.present? + expect(demo_step.checklists.pluck(:name)) + .to match_array(tmpl_step.checklists.pluck(:name)) + end + expect(demo_step.step_comments.size) + .to eq(tmpl_step.step_comments.size) + end + end + Asset.all.each(&:paperclip_delete) + FileUtils.remove_dir(tmplts_dir) + end + end + end +end