summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/import_export/project/sample/sample_data_relation_tree_restorer_spec.rb
blob: f173345a4c6452071fd0d8653142c85e50367b06 (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
# frozen_string_literal: true

# This spec is a lightweight version of:
#   * project/tree_restorer_spec.rb
#
# In depth testing is being done in the above specs.
# This spec tests that restore of the sample project works
# but does not have 100% relation coverage.

require 'spec_helper'

RSpec.describe Gitlab::ImportExport::Project::Sample::SampleDataRelationTreeRestorer do
  include_context 'relation tree restorer shared context'

  let(:sample_data_relation_tree_restorer) do
    described_class.new(
      user:                  user,
      shared:                shared,
      relation_reader:       relation_reader,
      object_builder:        object_builder,
      members_mapper:        members_mapper,
      relation_factory:      relation_factory,
      reader:                reader,
      importable:            importable,
      importable_path:       importable_path,
      importable_attributes: attributes
    )
  end

  subject { sample_data_relation_tree_restorer.restore }

  shared_examples 'import project successfully' do
    it 'restores project tree' do
      expect(subject).to eq(true)
    end

    describe 'imported project' do
      let(:project) { Project.find_by_path('project') }

      before do
        subject
      end

      it 'has the project attributes and relations', :aggregate_failures do
        expect(project.description).to eq('Nisi et repellendus ut enim quo accusamus vel magnam.')
        expect(project.issues.count).to eq(10)
        expect(project.milestones.count).to eq(3)
        expect(project.labels.count).to eq(2)
        expect(project.project_feature).not_to be_nil
      end

      it 'has issues with correctly updated due dates' do
        due_dates = due_dates(project.issues)

        expect(due_dates).to match_array([Date.today - 7.days, Date.today, Date.today + 7.days])
      end

      it 'has milestones with correctly updated due dates' do
        due_dates = due_dates(project.milestones)

        expect(due_dates).to match_array([Date.today - 7.days, Date.today, Date.today + 7.days])
      end

      def due_dates(relations)
        due_dates = relations.map { |relation| relation['due_date'] }
        due_dates.compact!
        due_dates.sort
      end
    end
  end

  context 'when restoring a project' do
    let(:importable) { create(:project, :builds_enabled, :issues_disabled, name: 'project', path: 'project') }
    let(:importable_name) { 'project' }
    let(:importable_path) { 'project' }
    let(:object_builder) { Gitlab::ImportExport::Project::ObjectBuilder }
    let(:relation_factory) { Gitlab::ImportExport::Project::RelationFactory }
    let(:reader) { Gitlab::ImportExport::Reader.new(shared: shared) }

    context 'using ndjson reader' do
      let(:path) { 'spec/fixtures/lib/gitlab/import_export/sample_data/tree' }
      let(:relation_reader) { Gitlab::ImportExport::JSON::NdjsonReader.new(path) }

      it_behaves_like 'import project successfully'
    end
  end
end