|
36 | 36 |
|
37 | 37 | context "with a single project" do
|
38 | 38 | let(:project) { create(:project) }
|
39 |
| - let(:instance) { described_class.new(user:, project:, project_custom_field:) } |
| 39 | + let(:instance) { described_class.new(user:, projects: [project], project_custom_field:) } |
40 | 40 |
|
41 | 41 | it "creates the mappings" do
|
42 | 42 | expect { instance.call }.to change(ProjectCustomFieldProjectMapping, :count).by(1)
|
|
49 | 49 | end
|
50 | 50 |
|
51 | 51 | context "with subprojects" do
|
| 52 | + let(:projects) { create_list(:project, 2) } |
| 53 | + let!(:subproject) { create(:project, parent: projects.first) } |
| 54 | + let!(:subproject2) { create(:project, parent: subproject) } |
| 55 | + |
| 56 | + it "creates the mappings for the project and sub-projects" do |
| 57 | + create_service = described_class.new(user:, projects: projects.map(&:reload), project_custom_field:, |
| 58 | + include_sub_projects: true) |
| 59 | + |
| 60 | + expect { create_service.call }.to change(ProjectCustomFieldProjectMapping, :count).by(4) |
| 61 | + |
| 62 | + aggregate_failures "creates the mapping for the correct project and custom field" do |
| 63 | + expect(ProjectCustomFieldProjectMapping.where(project_custom_field:).pluck(:project_id)) |
| 64 | + .to contain_exactly(*projects.map(&:id), subproject.id, subproject2.id) |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + context "with multiple projects including subprojects" do |
52 | 70 | let(:project) { create(:project) }
|
53 | 71 | let!(:subproject) { create(:project, parent: project) }
|
54 |
| - let!(:subproject2) { create(:project, parent: subproject) } |
55 | 72 |
|
56 | 73 | it "creates the mappings for the project and sub-projects" do
|
57 |
| - create_service = described_class.new(user:, project: project.reload, project_custom_field:, |
| 74 | + create_service = described_class.new(user:, projects: [project.reload, subproject], project_custom_field:, |
58 | 75 | include_sub_projects: true)
|
59 | 76 |
|
60 |
| - expect { create_service.call }.to change(ProjectCustomFieldProjectMapping, :count).by(3) |
| 77 | + expect { create_service.call }.to change(ProjectCustomFieldProjectMapping, :count).by(2) |
61 | 78 |
|
62 | 79 | aggregate_failures "creates the mapping for the correct project and custom field" do
|
63 | 80 | expect(ProjectCustomFieldProjectMapping.where(project_custom_field:).pluck(:project_id))
|
64 |
| - .to contain_exactly(project.id, subproject.id, subproject2.id) |
| 81 | + .to contain_exactly(project.id, subproject.id) |
65 | 82 | end
|
66 | 83 | end
|
67 | 84 | end
|
|
80 | 97 | end
|
81 | 98 |
|
82 | 99 | let(:project) { create(:project) }
|
83 |
| - let(:instance) { described_class.new(user:, project:, project_custom_field:) } |
| 100 | + let(:instance) { described_class.new(user:, projects: [project], project_custom_field:) } |
84 | 101 |
|
85 | 102 | it "creates the mappings" do
|
86 | 103 | expect { instance.call }.to change(ProjectCustomFieldProjectMapping, :count).by(1)
|
|
103 | 120 | })
|
104 | 121 | end
|
105 | 122 | let(:project) { create(:project) }
|
106 |
| - let(:instance) { described_class.new(user:, project:, project_custom_field:) } |
| 123 | + let(:instance) { described_class.new(user:, projects: [project], project_custom_field:) } |
107 | 124 |
|
108 | 125 | it "does not create the mappings" do
|
109 | 126 | expect { instance.call }.not_to change(ProjectCustomFieldProjectMapping, :count)
|
110 | 127 | expect(instance.call).to be_failure
|
111 | 128 | end
|
112 | 129 | end
|
| 130 | + |
| 131 | + context "with empty projects" do |
| 132 | + let(:user) { create(:admin) } |
| 133 | + let(:instance) { described_class.new(user:, projects: [], project_custom_field:) } |
| 134 | + |
| 135 | + it "does not create the mappings" do |
| 136 | + service_result = instance.call |
| 137 | + expect(service_result).to be_failure |
| 138 | + expect(service_result.errors).to eq("not found") |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + context "with archived projects" do |
| 143 | + let(:user) { create(:admin) } |
| 144 | + let(:archived_project) { create(:project, active: false) } |
| 145 | + let(:active_project) { create(:project) } |
| 146 | + |
| 147 | + let(:instance) { described_class.new(user:, projects: [archived_project, active_project], project_custom_field:) } |
| 148 | + |
| 149 | + it "only creates mappins for the active project" do |
| 150 | + expect { instance.call }.to change(ProjectCustomFieldProjectMapping, :count).by(1) |
| 151 | + |
| 152 | + aggregate_failures "creates the mapping for the correct project and custom field" do |
| 153 | + expect(ProjectCustomFieldProjectMapping.where(project_custom_field:).pluck(:project_id)) |
| 154 | + .to contain_exactly(active_project.id) |
| 155 | + end |
| 156 | + end |
| 157 | + end |
113 | 158 | end
|
0 commit comments