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

legacy_writer_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: ed4368ba802366fe51fef84384d461e4991b39b5 (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
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe Gitlab::ImportExport::Json::LegacyWriter do
  let(:path) { "#{Dir.tmpdir}/legacy_writer_spec/test.json" }

  subject do
    described_class.new(path, allowed_path: "project")
  end

  after do
    FileUtils.rm_rf(path)
  end

  describe "#write_attributes" do
    it "writes correct json" do
      expected_hash = { "key" => "value_1", "key_1" => "value_2" }
      subject.write_attributes("project", expected_hash)

      expect(subject_json).to eq(expected_hash)
    end

    context 'when invalid path is used' do
      it 'raises an exception' do
        expect { subject.write_attributes("invalid", { "key" => "value" }) }
          .to raise_error(ArgumentError)
      end
    end
  end

  describe "#write_relation" do
    context "when key is already written" do
      it "raises exception" do
        subject.write_relation("project", "key", "old value")

        expect { subject.write_relation("project", "key", "new value") }
          .to raise_exception("key 'key' already written")
      end
    end

    context "when key is not already written" do
      context "when multiple key value pairs are stored" do
        it "writes correct json" do
          expected_hash = { "key" => "value_1", "key_1" => "value_2" }
          expected_hash.each do |key, value|
            subject.write_relation("project", key, value)
          end

          expect(subject_json).to eq(expected_hash)
        end
      end
    end

    context 'when invalid path is used' do
      it 'raises an exception' do
        expect { subject.write_relation("invalid", "key", "value") }
          .to raise_error(ArgumentError)
      end
    end
  end

  describe "#write_relation_array" do
    context 'when array is used' do
      it 'writes correct json' do
        subject.write_relation_array("project", "key", ["value"])

        expect(subject_json).to eq({ "key" => ["value"] })
      end
    end

    context 'when enumerable is used' do
      it 'writes correct json' do
        values = %w(value1 value2)

        enumerator = Enumerator.new do |items|
          values.each { |value| items << value }
        end

        subject.write_relation_array("project", "key", enumerator)

        expect(subject_json).to eq({ "key" => values })
      end
    end

    context "when key is already written" do
      it "raises an exception" do
        subject.write_relation_array("project", "key", %w(old_value))

        expect { subject.write_relation_array("project", "key", %w(new_value)) }
          .to raise_error(ArgumentError)
      end
    end
  end

  def subject_json
    subject.close

    ::JSON.parse(IO.read(subject.path))
  end
end