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

attributes_finder_spec.rb « import_export « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6536b895b2fbd3ed2a23c0a3d454352961f70800 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe Gitlab::ImportExport::AttributesFinder do
  describe '#find_root' do
    subject { described_class.new(config: config).find_root(model_key) }

    let(:test_config) { 'spec/support/import_export/import_export.yml' }
    let(:config) { Gitlab::ImportExport::Config.new.to_h }
    let(:model_key) { :project }

    let(:project_tree_hash) do
      {
        except: [:id, :created_at],
        include: [
          { issues: { include: [] } },
          { labels: { include: [] } },
          { merge_requests: {
            except: [:iid],
            include: [
              { merge_request_diff: {
                include: [],
                preload: { source_project: nil }
              } },
              { merge_request_test: { include: [] } }
            ],
            only: [:id],
            preload: {
              merge_request_diff: { source_project: nil },
              merge_request_test: nil
            }
            } },
          { commit_statuses: {
              include: [{ commit: { include: [] } }],
              preload: { commit: nil }
            } },
          { project_members: {
              include: [{ user: { include: [],
                                  only: [:email] } }],
              preload: { user: nil }
          } }
        ],
        preload: {
          commit_statuses: {
            commit: nil
          },
          issues: nil,
          labels: nil,
          merge_requests: {
            merge_request_diff: { source_project: nil },
            merge_request_test: nil
          },
          project_members: {
            user: nil
          }
        }
      }
    end

    before do
      allow(Gitlab::ImportExport).to receive(:config_file).and_return(test_config)
    end

    it 'generates hash from project tree config' do
      is_expected.to match(project_tree_hash)
    end

    context 'individual scenarios' do
      it 'generates the correct hash for a single project relation' do
        setup_yaml(tree: { project: [:issues] })

        is_expected.to match(
          include: [{ issues: { include: [] } }],
          preload: { issues: nil }
        )
      end

      it 'generates the correct hash for a single project feature relation' do
        setup_yaml(tree: { project: [:project_feature] })

        is_expected.to match(
          include: [{ project_feature: { include: [] } }],
          preload: { project_feature: nil }
        )
      end

      it 'generates the correct hash for a multiple project relation' do
        setup_yaml(tree: { project: [:issues, :snippets] })

        is_expected.to match(
          include: [{ issues: { include: [] } },
                    { snippets: { include: [] } }],
          preload: { issues: nil, snippets: nil }
        )
      end

      it 'generates the correct hash for a single sub-relation' do
        setup_yaml(tree: { project: [issues: [:notes]] })

        is_expected.to match(
          include: [{ issues: { include: [{ notes: { include: [] } }],
                                preload: { notes: nil } } }],
          preload: { issues: { notes: nil } }
        )
      end

      it 'generates the correct hash for a multiple sub-relation' do
        setup_yaml(tree: { project: [merge_requests: [:notes, :merge_request_diff]] })

        is_expected.to match(
          include: [{ merge_requests:
                      { include: [{ notes: { include: [] } },
                                  { merge_request_diff: { include: [] } }],
                        preload: { merge_request_diff: nil, notes: nil } } }],
          preload: { merge_requests: { merge_request_diff: nil, notes: nil } }
        )
      end

      it 'generates the correct hash for a sub-relation with another sub-relation' do
        setup_yaml(tree: { project: [merge_requests: [notes: [:author]]] })

        is_expected.to match(
          include: [{ merge_requests: {
                      include: [{ notes: { include: [{ author: { include: [] } }],
                                           preload: { author: nil } } }],
                      preload: { notes: { author: nil } }
                    } }],
          preload: { merge_requests: { notes: { author: nil } } }
        )
      end

      it 'generates the correct hash for a relation with included attributes' do
        setup_yaml(tree: { project: [:issues] },
                   included_attributes: { issues: [:name, :description] })

        is_expected.to match(
          include: [{ issues: { include: [],
                                only: [:name, :description] } }],
          preload: { issues: nil }
        )
      end

      it 'generates the correct hash for a relation with excluded attributes' do
        setup_yaml(tree: { project: [:issues] },
                   excluded_attributes: { issues: [:name] })

        is_expected.to match(
          include: [{ issues: { except: [:name],
                                include: [] } }],
          preload: { issues: nil }
        )
      end

      it 'generates the correct hash for a relation with both excluded and included attributes' do
        setup_yaml(tree: { project: [:issues] },
                   excluded_attributes: { issues: [:name] },
                   included_attributes: { issues: [:description] })

        is_expected.to match(
          include: [{ issues: { except: [:name],
                                include: [],
                                only: [:description] } }],
          preload: { issues: nil }
        )
      end

      it 'generates the correct hash for a relation with custom methods' do
        setup_yaml(tree: { project: [:issues] },
                   methods: { issues: [:name] })

        is_expected.to match(
          include: [{ issues: { include: [],
                                methods: [:name] } }],
          preload: { issues: nil }
        )
      end

      def setup_yaml(hash)
        allow(YAML).to receive(:load_file).with(test_config).and_return(hash)
      end
    end
  end

  describe '#find_relations_tree' do
    subject { described_class.new(config: config).find_relations_tree(model_key) }

    let(:tree) { { project: { issues: {} } } }
    let(:model_key) { :project }

    context 'when initialized with config including tree' do
      let(:config) { { tree: tree } }

      context 'when relation is in top-level keys of the tree' do
        it { is_expected.to eq({ issues: {} }) }
      end

      context 'when the relation is not in top-level keys' do
        let(:model_key) { :issues }

        it { is_expected.to be_nil }
      end
    end

    context 'when tree is not present in config' do
      let(:config) { {} }

      it { is_expected.to be_nil }
    end
  end

  describe '#find_excluded_keys' do
    subject { described_class.new(config: config).find_excluded_keys(klass_name) }

    let(:klass_name) { 'project' }

    context 'when initialized with excluded_attributes' do
      let(:config) { { excluded_attributes: excluded_attributes } }
      let(:excluded_attributes) { { project: [:name, :path], issues: [:milestone_id] } }

      it { is_expected.to eq(%w[name path]) }
    end

    context 'when excluded_attributes are not present in config' do
      let(:config) { {} }

      it { is_expected.to eq([]) }
    end
  end
end