2019-01-15 20:45:05 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Experiments::MoveToProjectService do
|
2021-09-30 17:32:11 +08:00
|
|
|
let(:user) { create :user }
|
2022-06-07 00:21:57 +08:00
|
|
|
let(:team) { create :team, created_by: user }
|
2019-01-15 20:45:05 +08:00
|
|
|
let(:project) do
|
2021-09-30 17:32:11 +08:00
|
|
|
create :project, team: team, created_by: user
|
2019-01-15 20:45:05 +08:00
|
|
|
end
|
|
|
|
let(:new_project) do
|
2021-09-30 17:32:11 +08:00
|
|
|
create :project, team: team, created_by: user
|
2019-01-15 20:45:05 +08:00
|
|
|
end
|
|
|
|
let(:experiment) do
|
2019-03-13 16:06:48 +08:00
|
|
|
create :experiment, :with_tasks, name: 'MyExp', project: project
|
2019-01-15 20:45:05 +08:00
|
|
|
end
|
|
|
|
let(:service_call) do
|
|
|
|
Experiments::MoveToProjectService.call(experiment_id: experiment.id,
|
|
|
|
project_id: new_project.id,
|
|
|
|
user_id: user.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when call with valid params' do
|
|
|
|
it 'unnasigns experiment from project' do
|
|
|
|
service_call
|
|
|
|
|
|
|
|
expect(project.experiments.pluck(:id)).to_not include(experiment.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'assigns experiment to new_project' do
|
|
|
|
service_call
|
|
|
|
|
|
|
|
expect(new_project.experiments.pluck(:id)).to include(experiment.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'copies tags to new project' do
|
|
|
|
expect { service_call }.to(change { new_project.tags.count })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'leaves tags on an old project' do
|
|
|
|
experiment # explicit call to create tags
|
|
|
|
expect { service_call }.not_to(change { project.tags.count })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets new project tags to modules' do
|
|
|
|
service_call
|
|
|
|
new_tags = experiment.my_modules.map { |m| m.tags.map { |t| t } }.flatten
|
|
|
|
tags_project_id = new_tags.map(&:project_id).uniq.first
|
|
|
|
|
|
|
|
expect(tags_project_id).to be == new_project.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds Activity record' do
|
|
|
|
expect { service_call }.to(change { Activity.all.count })
|
|
|
|
end
|
2019-03-05 18:39:57 +08:00
|
|
|
|
|
|
|
it 'calls create activity service' do
|
|
|
|
expect(Activities::CreateActivityService).to receive(:call)
|
|
|
|
.with(hash_including(activity_type: :move_experiment))
|
|
|
|
|
|
|
|
service_call
|
|
|
|
end
|
2019-01-15 20:45:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when call with invalid params' do
|
|
|
|
it 'returns an error when can\'t find user and project' do
|
|
|
|
allow(Project).to receive(:find).and_return(nil)
|
|
|
|
allow(User).to receive(:find).and_return(nil)
|
|
|
|
|
|
|
|
expect(service_call.errors).to have_key(:invalid_arguments)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error on validate' do
|
2021-09-30 17:32:11 +08:00
|
|
|
allow_any_instance_of(Experiment).to(receive(:movable_projects).and_return([]))
|
2020-12-18 23:44:15 +08:00
|
|
|
|
2019-01-15 20:45:05 +08:00
|
|
|
expect(service_call.succeed?).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an error on saving' do
|
2020-12-18 23:44:15 +08:00
|
|
|
experiment.name = Faker::Lorem.characters(number: 300)
|
|
|
|
experiment.save(validate: false)
|
2019-01-15 20:45:05 +08:00
|
|
|
|
|
|
|
expect(service_call.succeed?).to be_falsey
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'rollbacks cloned tags after unsucessful save' do
|
2020-12-18 23:44:15 +08:00
|
|
|
experiment.name = Faker::Lorem.characters(number: 300)
|
|
|
|
experiment.save(validate: false)
|
2019-01-15 20:45:05 +08:00
|
|
|
|
|
|
|
expect { service_call }.not_to(change { Tag.count })
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error if teams is not the same' do
|
2022-06-07 00:21:57 +08:00
|
|
|
t = create :team
|
2019-01-15 20:45:05 +08:00
|
|
|
project.update(team: t)
|
|
|
|
|
|
|
|
expect(service_call.errors).to have_key(:target_project_not_valid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|