2019-02-02 01:06:36 +08:00
|
|
|
# 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
|
2019-02-11 20:18:57 +08:00
|
|
|
# Two runs are needed to execute additional jobs which get generated
|
|
|
|
# during first run
|
2019-02-02 01:06:36 +08:00
|
|
|
Delayed::Job.all.each { |job| dj_worker.run(job) }
|
2019-02-04 20:50:38 +08:00
|
|
|
Delayed::Job.all.each { |job| dj_worker.run(job) }
|
2019-02-02 01:06:36 +08:00
|
|
|
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}")
|
2019-02-13 22:17:33 +08:00
|
|
|
templates_project =
|
|
|
|
create :project, name: 'Templates', template: true, team: main_team
|
2019-02-02 01:06:36 +08:00
|
|
|
create(
|
|
|
|
:user_project, :owner, project: templates_project, user: admin_user
|
|
|
|
)
|
|
|
|
ts = TemplatesService.new(tmplts_dir)
|
2019-02-13 22:17:33 +08:00
|
|
|
ts.update_team(main_team)
|
2019-02-04 20:50:38 +08:00
|
|
|
Delayed::Job.all.each { |job| dj_worker.run(job) }
|
2019-02-02 01:06:36 +08:00
|
|
|
tmpl_exp = templates_project.experiments.first
|
|
|
|
|
2019-02-04 20:50:38 +08:00
|
|
|
expect(tmpl_exp.name).to eq(demo_exp.name)
|
2019-02-02 01:06:36 +08:00
|
|
|
expect(tmpl_exp.uuid).to_not eq(nil)
|
|
|
|
expect(tmpl_exp.my_modules.pluck(:name))
|
2019-02-13 18:49:01 +08:00
|
|
|
.to match_array(demo_exp.active_my_modules.pluck(:name))
|
2019-02-07 18:23:40 +08:00
|
|
|
tmpl_tasks = tmpl_exp.my_modules
|
2019-02-13 18:49:01 +08:00
|
|
|
demo_tasks = demo_exp.active_my_modules
|
2019-02-04 20:50:38 +08:00
|
|
|
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)
|
2019-02-02 01:06:36 +08:00
|
|
|
expect(tmpl_task.task_comments.size)
|
2019-02-04 20:50:38 +08:00
|
|
|
.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)
|
2019-02-02 01:06:36 +08:00
|
|
|
if demo_res.asset
|
2019-02-04 20:50:38 +08:00
|
|
|
expect(tmpl_res.asset.file.exists?).to eq(true)
|
2019-02-02 01:06:36 +08:00
|
|
|
expect(demo_res.asset.file_file_name)
|
2019-02-04 20:50:38 +08:00
|
|
|
.to eq(tmpl_res.asset.file_file_name)
|
2019-02-02 01:06:36 +08:00
|
|
|
elsif demo_res.table
|
2019-02-04 20:50:38 +08:00
|
|
|
expect(demo_res.table.contents).to eq(tmpl_res.table.contents)
|
2019-02-02 01:06:36 +08:00
|
|
|
elsif demo_res.result_text
|
2019-02-04 20:50:38 +08:00
|
|
|
expect(demo_res.result_text.text).to eq(tmpl_res.result_text.text)
|
2019-02-02 01:06:36 +08:00
|
|
|
end
|
|
|
|
expect(demo_res.result_comments.size)
|
2019-02-04 20:50:38 +08:00
|
|
|
.to eq(tmpl_res.result_comments.size)
|
2019-02-02 01:06:36 +08:00
|
|
|
end
|
|
|
|
unless demo_task.protocol.present? &&
|
|
|
|
demo_task.protocol.steps.size.positive?
|
|
|
|
next
|
|
|
|
end
|
2019-02-04 20:50:38 +08:00
|
|
|
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)
|
2019-02-02 01:06:36 +08:00
|
|
|
if demo_step.assets.present?
|
|
|
|
expect(demo_step.assets.pluck(:file_file_name))
|
2019-02-04 20:50:38 +08:00
|
|
|
.to match_array(tmpl_step.assets.pluck(:file_file_name))
|
2019-02-02 01:06:36 +08:00
|
|
|
end
|
2019-02-07 18:23:40 +08:00
|
|
|
tmpl_step.assets.each do |asset|
|
2019-02-04 20:50:38 +08:00
|
|
|
expect(asset.file.exists?).to eq(true)
|
2019-02-02 01:06:36 +08:00
|
|
|
end
|
|
|
|
if demo_step.tables.present?
|
|
|
|
expect(demo_step.tables.pluck(:contents))
|
2019-02-04 20:50:38 +08:00
|
|
|
.to match_array(tmpl_step.tables.pluck(:contents))
|
2019-02-02 01:06:36 +08:00
|
|
|
end
|
|
|
|
if demo_step.checklists.present?
|
|
|
|
expect(demo_step.checklists.pluck(:name))
|
2019-02-04 20:50:38 +08:00
|
|
|
.to match_array(tmpl_step.checklists.pluck(:name))
|
2019-02-02 01:06:36 +08:00
|
|
|
end
|
|
|
|
expect(demo_step.step_comments.size)
|
2019-02-04 20:50:38 +08:00
|
|
|
.to eq(tmpl_step.step_comments.size)
|
2019-02-02 01:06:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
FileUtils.remove_dir(tmplts_dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|