summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/import_export/json/ndjson_reader_spec.rb
blob: b477ac4557710e64666e602cb2d3bd3013d52dcb (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::ImportExport::JSON::NdjsonReader do
  include ImportExport::CommonUtil

  let(:fixture) { 'spec/fixtures/lib/gitlab/import_export/light/tree' }
  let(:root_tree) { Gitlab::Json.parse(File.read(File.join(fixture, 'project.json'))) }
  let(:ndjson_reader) { described_class.new(dir_path) }
  let(:importable_path) { 'project' }

  describe '#exist?' do
    subject { ndjson_reader.exist? }

    context 'given valid dir_path' do
      let(:dir_path) { fixture }

      it { is_expected.to be true }
    end

    context 'given invalid dir_path' do
      let(:dir_path) { 'invalid-dir-path' }

      it { is_expected.to be false }
    end
  end

  describe '#legacy?' do
    let(:dir_path) { fixture }

    subject { ndjson_reader.legacy? }

    it { is_expected.to be false }
  end

  describe '#consume_attributes' do
    let(:dir_path) { fixture }

    subject { ndjson_reader.consume_attributes(importable_path) }

    it 'returns the whole root tree from parsed JSON' do
      expect(subject).to eq(root_tree)
    end
  end

  describe '#consume_relation' do
    let(:dir_path) { fixture }

    subject { ndjson_reader.consume_relation(importable_path, key) }

    context 'given any key' do
      let(:key) { 'any-key' }

      it 'returns an Enumerator' do
        expect(subject).to be_an_instance_of(Enumerator)
      end
    end

    context 'key has been consumed' do
      let(:key) { 'issues' }

      before do
        ndjson_reader.consume_relation(importable_path, key).first
      end

      it 'yields nothing to the Enumerator' do
        expect(subject.to_a).to eq([])
      end

      context 'with mark_as_consumed: false' do
        subject { ndjson_reader.consume_relation(importable_path, key, mark_as_consumed: false) }

        it 'yields every relation value to the Enumerator' do
          expect(subject.count).to eq(1)
        end
      end
    end

    context 'key has not been consumed' do
      context 'relation file does not exist' do
        let(:key) { 'non-exist-relation-file-name' }

        before do
          relation_file_path = File.join(dir_path, importable_path, "#{key}.ndjson")
          expect(File).to receive(:exist?).with(relation_file_path).and_return(false)
        end

        it 'yields nothing to the Enumerator' do
          expect(subject.to_a).to eq([])
        end
      end

      context 'relation file is empty' do
        let(:key) { 'empty' }

        it 'yields nothing to the Enumerator' do
          expect(subject.to_a).to eq([])
        end
      end

      context 'relation file contains multiple lines' do
        let(:key) { 'custom_attributes' }
        let(:attr_1) { Gitlab::Json.parse('{"id":201,"project_id":5,"created_at":"2016-06-14T15:01:51.315Z","updated_at":"2016-06-14T15:01:51.315Z","key":"color","value":"red"}') }
        let(:attr_2) { Gitlab::Json.parse('{"id":202,"project_id":5,"created_at":"2016-06-14T15:01:51.315Z","updated_at":"2016-06-14T15:01:51.315Z","key":"size","value":"small"}') }

        it 'yields every relation value to the Enumerator' do
          expect(subject.to_a).to eq([[attr_1, 0], [attr_2, 1]])
        end
      end
    end
  end
end