BigW Consortium Gitlab

reader_spec.rb 3.79 KB
Newer Older
1
require 'spec_helper'
2

3
describe Gitlab::ImportExport::Reader, lib: true  do
4
  let(:shared) { Gitlab::ImportExport::Shared.new(relative_path: '') }
5 6 7
  let(:test_config) { 'spec/support/import_export/import_export.yml' }
  let(:project_tree_hash) do
    {
James Lopez committed
8 9 10 11 12 13 14
      only: [:name, :path],
      include: [:issues, :labels,
                { merge_requests: {
                  only: [:id],
                  except: [:iid],
                  include: [:merge_request_diff, :merge_request_test]
                } },
15 16
                { commit_statuses: { include: :commit } },
                { project_members: { include: { user: { only: [:email] } } } }]
17 18 19
    }
  end

20 21 22 23
  before do
    allow_any_instance_of(Gitlab::ImportExport).to receive(:config_file).and_return(test_config)
  end

24
  it 'generates hash from project tree config' do
25
    expect(described_class.new(shared: shared).project_tree).to match(project_tree_hash)
26
  end
27 28 29 30 31 32 33 34

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

      expect(described_class.new(shared: shared).project_tree).to match(include: [:issues])
    end

35 36 37 38 39 40
    it 'generates the correct hash for a single project feature relation' do
      setup_yaml(project_tree: [:project_feature])

      expect(described_class.new(shared: shared).project_tree).to match(include: [:project_feature])
    end

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
    it 'generates the correct hash for a multiple project relation' do
      setup_yaml(project_tree: [:issues, :snippets])

      expect(described_class.new(shared: shared).project_tree).to match(include: [:issues, :snippets])
    end

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

      expect(described_class.new(shared: shared).project_tree).to match(include: [{ issues: { include: :notes } }])
    end

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

      expect(described_class.new(shared: shared).project_tree).to match(include: [{ merge_requests: { include: [:notes, :merge_request_diff] } }])
    end

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

      expect(described_class.new(shared: shared).project_tree).to match(include: [{ merge_requests: { include: { notes: { include: :author } } } }])
    end

    it 'generates the correct hash for a relation with included attributes' do
James Lopez committed
66
      setup_yaml(project_tree: [:issues], included_attributes: { issues: [:name, :description] })
67 68 69 70 71

      expect(described_class.new(shared: shared).project_tree).to match(include: [{ issues: { only: [:name, :description] } }])
    end

    it 'generates the correct hash for a relation with excluded attributes' do
James Lopez committed
72
      setup_yaml(project_tree: [:issues], excluded_attributes: { issues: [:name] })
73 74 75 76 77

      expect(described_class.new(shared: shared).project_tree).to match(include: [{ issues: { except: [:name] } }])
    end

    it 'generates the correct hash for a relation with both excluded and included attributes' do
James Lopez committed
78
      setup_yaml(project_tree: [:issues], excluded_attributes: { issues: [:name] }, included_attributes: { issues: [:description] })
79

James Lopez committed
80
      expect(described_class.new(shared: shared).project_tree).to match(include: [{ issues: { except: [:name], only: [:description] } }])
81 82 83
    end

    it 'generates the correct hash for a relation with custom methods' do
James Lopez committed
84
      setup_yaml(project_tree: [:issues], methods: { issues: [:name] })
85 86 87 88 89 90 91 92

      expect(described_class.new(shared: shared).project_tree).to match(include: [{ issues: { methods: [:name] } }])
    end

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