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

legacy_reader_spec.rb « json « import_export « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0009a5f81de4f4a7ea8979cad34b5a5b8b805dde (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
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::ImportExport::JSON::LegacyReader::User do
  let(:relation_names) { [] }
  let(:legacy_reader) { described_class.new(tree_hash, relation_names) }

  describe '#valid?' do
    subject { legacy_reader.valid? }

    context 'tree_hash not present' do
      let(:tree_hash) { nil }

      it { is_expected.to be false }
    end

    context 'tree_hash presents' do
      let(:tree_hash) { { "issues": [] } }

      it { is_expected.to be true }
    end
  end
end

describe Gitlab::ImportExport::JSON::LegacyReader::File do
  let(:fixture) { 'spec/fixtures/lib/gitlab/import_export/light/project.json' }
  let(:project_tree) { JSON.parse(File.read(fixture)) }
  let(:relation_names) { [] }
  let(:legacy_reader) { described_class.new(path, relation_names) }

  describe '#valid?' do
    subject { legacy_reader.valid? }

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

      it { is_expected.to be true }
    end

    context 'given invalid path' do
      let(:path) { 'spec/non-existing-folder/do-not-create-this-file.json' }

      it { is_expected.to be false }
    end
  end

  describe '#root_attributes' do
    let(:path) { fixture }

    subject { legacy_reader.root_attributes(excluded_attributes) }

    context 'No excluded attributes' do
      let(:excluded_attributes) { [] }
      let(:relation_names) { [] }

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

    context 'Some attributes are excluded' do
      let(:excluded_attributes) { %w[milestones labels issues services snippets] }
      let(:relation_names) { %w[import_type archived] }

      it 'returns hash without excluded attributes and relations' do
        expect(subject).not_to include('milestones', 'labels', 'issues', 'services', 'snippets', 'import_type', 'archived')
      end
    end
  end

  describe '#consume_relation' do
    let(:path) { fixture }
    let(:key) { 'description' }

    context 'block not given' do
      it 'returns value of the key' do
        expect(legacy_reader).to receive(:relations).and_return({ key => 'test value' })
        expect(legacy_reader.consume_relation(key)).to eq('test value')
      end
    end

    context 'key has been consumed' do
      before do
        legacy_reader.consume_relation(key)
      end

      it 'does not yield' do
        expect do |blk|
          legacy_reader.consume_relation(key, &blk)
        end.not_to yield_control
      end
    end

    context 'value is nil' do
      before do
        expect(legacy_reader).to receive(:relations).and_return({ key => nil })
      end

      it 'does not yield' do
        expect do |blk|
          legacy_reader.consume_relation(key, &blk)
        end.not_to yield_control
      end
    end

    context 'value is not array' do
      before do
        expect(legacy_reader).to receive(:relations).and_return({ key => 'value' })
      end

      it 'yield the value with index 0' do
        expect do |blk|
          legacy_reader.consume_relation(key, &blk)
        end.to yield_with_args('value', 0)
      end
    end

    context 'value is an array' do
      before do
        expect(legacy_reader).to receive(:relations).and_return({ key => %w[item1 item2 item3] })
      end

      it 'yield each array element with index' do
        expect do |blk|
          legacy_reader.consume_relation(key, &blk)
        end.to yield_successive_args(['item1', 0], ['item2', 1], ['item3', 2])
      end
    end
  end

  describe '#tree_hash' do
    let(:path) { fixture }

    subject { legacy_reader.send(:tree_hash) }

    it 'parses the JSON into the expected tree' do
      expect(subject).to eq(project_tree)
    end

    context 'invalid JSON' do
      let(:path) { 'spec/fixtures/lib/gitlab/import_export/invalid_json/project.json' }

      it 'raise Exception' do
        expect { subject }.to raise_exception(Gitlab::ImportExport::Error, 'Incorrect JSON format')
      end
    end
  end
end