File: export_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (179 lines) | stat: -rw-r--r-- 5,471 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BulkImports::Export, type: :model, feature_category: :importers do
  describe 'associations' do
    it { is_expected.to belong_to(:group) }
    it { is_expected.to belong_to(:project) }
    it { is_expected.to have_one(:upload) }
    it { is_expected.to have_many(:batches) }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:relation) }
    it { is_expected.to validate_presence_of(:status) }

    context 'when not associated with a group or project' do
      it 'is invalid' do
        export = build(:bulk_import_export, group: nil, project: nil)

        expect(export).not_to be_valid
      end
    end

    context 'when associated with a group' do
      it 'is valid' do
        export = build(:bulk_import_export, group: build(:group), project: nil)

        expect(export).to be_valid
      end
    end

    context 'when associated with a project' do
      it 'is valid' do
        export = build(:bulk_import_export, group: nil, project: build(:project))

        expect(export).to be_valid
      end
    end

    context 'when relation is invalid' do
      it 'is invalid' do
        export = build(:bulk_import_export, relation: 'unsupported')

        expect(export).not_to be_valid
        expect(export.errors).to include(:relation)
      end
    end
  end

  describe 'scopes' do
    describe '.for_status' do
      let(:export_1) { create(:bulk_import_export, :finished, relation: 'labels') }
      let(:export_2) { create(:bulk_import_export, :started, relation: 'user_contributions') }

      it 'returns bulk_import_exports for the given status' do
        expect(described_class.for_status(0)).to contain_exactly(export_2)
      end
    end
  end

  describe 'state machine transitions', :clean_gitlab_redis_shared_state do
    describe '#finish!' do
      let_it_be(:project) { create(:project) }

      let(:export) { create(:bulk_import_export, :started, project: project) }
      let(:cache_key) { "bulk_imports/#{project.class.name}/#{project.id}/user_contribution_ids" }

      subject(:finish_export) { export.finish! }

      before do
        Gitlab::Cache::Import::Caching.set_add(cache_key, [1, 2, 3])
      end

      it 'sets the status to finished' do
        expect { finish_export }.to change { export.status }.from(0).to(1)
      end

      context 'when export is for user_contributions' do
        let(:export) { create(:bulk_import_export, :started, project: project, relation: 'user_contributions') }

        it 'clears cached contributing user_ids' do
          expect { finish_export }.to change {
            Gitlab::Cache::Import::Caching.values_from_set(cache_key).length
          }.from(3).to(0)
        end
      end

      context 'when export is not for user_contributions' do
        let(:export) { create(:bulk_import_export, :started, project: project, relation: 'issues') }

        it 'does clear cached contributing user_ids' do
          expect { finish_export }.not_to change {
            Gitlab::Cache::Import::Caching.values_from_set(cache_key).length
          }.from(3)
        end
      end
    end
  end

  describe '#portable' do
    context 'when associated with project' do
      it 'returns project' do
        export = create(:bulk_import_export, project: create(:project), group: nil)

        expect(export.portable).to be_instance_of(Project)
      end
    end

    context 'when associated with group' do
      it 'returns group' do
        export = create(:bulk_import_export)

        expect(export.portable).to be_instance_of(Group)
      end
    end
  end

  describe '#config' do
    context 'when associated with project' do
      it 'returns project config' do
        export = create(:bulk_import_export, project: create(:project), group: nil)

        expect(export.config).to be_instance_of(BulkImports::FileTransfer::ProjectConfig)
      end
    end

    context 'when associated with group' do
      it 'returns group config' do
        export = create(:bulk_import_export)

        expect(export.config).to be_instance_of(BulkImports::FileTransfer::GroupConfig)
      end
    end
  end

  describe '#remove_existing_upload!' do
    context 'when upload exists' do
      it 'removes the upload' do
        export = create(:bulk_import_export)
        upload = create(:bulk_import_export_upload, export: export)
        upload.update!(export_file: fixture_file_upload('spec/fixtures/bulk_imports/gz/labels.ndjson.gz'))

        expect_any_instance_of(BulkImports::ExportUpload) do |upload|
          expect(upload).to receive(:remove_export_file!)
          expect(upload).to receive(:save!)
        end

        export.remove_existing_upload!
      end
    end

    context 'when upload does not exist' do
      it 'returns' do
        export = build(:bulk_import_export)

        expect { export.remove_existing_upload! }.not_to change { export.upload }
      end
    end
  end

  describe '#relation_has_user_contributions?' do
    let(:export) { build(:bulk_import_export, project: build(:project), relation: relation) }

    subject { export.relation_has_user_contributions? }

    context 'when the relation has user contribitions' do
      let(:relation) { 'issues' }

      it { is_expected.to eq(true) }
    end

    context 'when the relation does not have user contribitions' do
      let(:relation) { 'labels' }

      it { is_expected.to eq(false) }
    end
  end
end