Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rename_descendants_service_spec.rb « routes « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 72e43ddca2650e545c1cf147420eaeb8ba079a69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Routes::RenameDescendantsService, feature_category: :groups_and_projects do
  let_it_be(:parent_group) { create(:group, name: 'old-name', path: 'old-path') }
  let_it_be(:parent_route) { parent_group.route }
  let_it_be(:subgroups) { create_list(:group, 4, parent: parent_group) }
  let_it_be(:subgroup_projects) { subgroups.map { |subgroup| create(:project, group: subgroup) } }

  let(:subgroup_routes) { Route.for_routable(subgroups) }
  let(:subgroup_projects_routes) { Route.for_routable(subgroup_projects) }

  let(:subgroup_routes_with_old_path) { subgroup_routes.where('path LIKE ?', '%old-path%') }
  let(:subgroup_projects_routes_with_old_path) { subgroup_projects_routes.where('path LIKE ?', '%old-path%') }
  let(:subgroup_routes_with_new_path) { subgroup_routes.where('path LIKE ?', '%new-path%') }
  let(:subgroup_projects_routes_with_new_path) { subgroup_projects_routes.where('path LIKE ?', '%new-path%') }

  let(:subgroup_routes_with_old_name) { subgroup_routes.where('name LIKE ?', '%old-name%') }
  let(:subgroup_projects_routes_with_old_name) { subgroup_projects_routes.where('name LIKE ?', '%old-name%') }
  let(:subgroup_routes_with_new_name) { subgroup_routes.where('name LIKE ?', '%new-name%') }
  let(:subgroup_projects_routes_with_new_name) { subgroup_projects_routes.where('name LIKE ?', '%new-name%') }

  describe '#execute' do
    shared_examples_for 'descendant paths are updated' do
      it do
        expect { execute }.to change {
          subgroup_routes_with_old_path.size
        }.from(4).to(0).and change {
          subgroup_projects_routes_with_old_path.size
        }.from(4).to(0).and change {
          subgroup_routes_with_new_path.size
        }.from(0).to(4).and change {
          subgroup_projects_routes_with_new_path.size
        }.from(0).to(4)
      end
    end

    shared_examples_for 'descendant paths are not updated' do
      it do
        expect { execute }.to change {
          subgroup_routes_with_old_path.size
        }.by(0).and change {
          subgroup_projects_routes_with_old_path.size
        }.by(0).and change {
          subgroup_routes_with_new_path.size
        }.by(0).and change {
          subgroup_projects_routes_with_new_path.size
        }.by(0)
      end
    end

    shared_examples_for 'descendant names are updated' do
      it do
        expect { execute }.to change {
          subgroup_routes_with_old_name.size
        }.from(4).to(0).and change {
          subgroup_projects_routes_with_old_name.size
        }.from(4).to(0).and change {
          subgroup_routes_with_new_name.size
        }.from(0).to(4).and change {
          subgroup_projects_routes_with_new_name.size
        }.from(0).to(4)
      end
    end

    shared_examples_for 'descendant names are not updated' do
      it do
        expect { execute }.to change {
          subgroup_routes_with_old_name.size
        }.by(0).and change {
          subgroup_projects_routes_with_old_name.size
        }.by(0).and change {
          subgroup_routes_with_new_name.size
        }.by(0).and change {
          subgroup_projects_routes_with_new_name.size
        }.by(0)
      end
    end

    shared_examples_for 'creates redirect_routes for all descendants' do
      let(:subgroup_redirect_routes) { RedirectRoute.where(source: subgroups) }
      let(:subgroup_projects_redirect_routes) { RedirectRoute.where(source: subgroup_projects) }

      it do
        expect { execute }.to change {
          subgroup_redirect_routes.where('path LIKE ?', '%old-path%').size
        }.from(0).to(4).and change {
          subgroup_projects_redirect_routes.where('path LIKE ?', '%old-path%').size
        }.from(0).to(4)
      end
    end

    shared_examples_for 'does not create any redirect_routes' do
      it do
        expect { execute }.not_to change { RedirectRoute.count }
      end
    end

    subject(:execute) do
      described_class.new(parent_route).execute(changes)
    end

    before do
      parent_route.name = 'new-name'
      parent_route.path = 'new-path'
    end

    context 'on updating both name and path' do
      let!(:changes) do
        {
          path: { saved: true, old_value: 'old-path' },
          name: { saved: true, old_value: 'old-name' }
        }
      end

      it_behaves_like 'descendant paths are updated'
      it_behaves_like 'descendant names are updated'
      it_behaves_like 'creates redirect_routes for all descendants'
    end

    context 'on updating only path' do
      let!(:changes) do
        {
          path: { saved: true, old_value: 'old-path' },
          name: { saved: false, old_value: 'old-name' }
        }
      end

      it_behaves_like 'descendant paths are updated'
      it_behaves_like 'descendant names are not updated'
      it_behaves_like 'creates redirect_routes for all descendants'
    end

    context 'on updating only name' do
      let!(:changes) do
        {
          path: { saved: false, old_value: 'old-path' },
          name: { saved: true, old_value: 'old-name' }
        }
      end

      it_behaves_like 'descendant paths are not updated'
      it_behaves_like 'descendant names are updated'
      it_behaves_like 'does not create any redirect_routes'
    end

    context 'on not updating both path and name' do
      let!(:changes) do
        {
          path: { saved: false, old_value: 'old-path' },
          name: { saved: false, old_value: 'old-name' }
        }
      end

      it_behaves_like 'descendant paths are not updated'
      it_behaves_like 'descendant names are not updated'
      it_behaves_like 'does not create any redirect_routes'
    end

    context 'when `changes` are not in the expected format' do
      let!(:changes) do
        {
          not_path: { saved: false, old_value: 'old-path' },
          name: { saved: true, old_value: 'old-name' }
        }
      end

      it 'errors out' do
        expect { execute }.to raise_error(KeyError)
      end
    end

    context 'for batching' do
      before do
        stub_const("#{described_class.name}::BATCH_SIZE", 2)
      end

      let!(:changes) do
        {
          path: { saved: true, old_value: 'old-path' },
          name: { saved: true, old_value: 'old-name' }
        }
      end

      it 'bulk updates and bulk inserts records in batches' do
        query_recorder = ActiveRecord::QueryRecorder.new do
          execute
        end

        # There are 8 descendants to this group.
        # 4 subgroups, and 1 project each in each subgroup == total of 8.
        # With a batch size of 2, that is
        # 4 queries to update `routes` and 4 queries to insert `redirect_routes`
        update_routes_queries = query_recorder.log.grep(
          /INSERT INTO "routes" .* ON CONFLICT \("id"\) DO UPDATE SET/
        )

        insert_redirect_routes_queries = query_recorder.log.grep(
          /INSERT INTO "redirect_routes" .* ON CONFLICT \(lower\(\(path\)::text\) varchar_pattern_ops\) DO NOTHING/
        )

        expect(update_routes_queries.count).to eq(4)
        expect(insert_redirect_routes_queries.count).to eq(4)
      end
    end
  end
end