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

ndjson_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: 486d179ae05f878e17ed4007dda99a09b5abb3ef (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
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Gitlab::ImportExport::Json::NdjsonWriter, feature_category: :importers do
  include ImportExport::CommonUtil

  let(:path) { "#{Dir.tmpdir}/ndjson_writer_spec/tree" }
  let(:exportable_path) { 'projects' }

  subject { described_class.new(path) }

  after do
    FileUtils.rm_rf(path)
  end

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

      expect(consume_attributes(path, exportable_path)).to eq(expected_hash)
    end
  end

  describe "#write_relation" do
    context "when single relation is serialized" do
      it "appends json in correct file" do
        relation = "relation"
        value =  { "key" => "value_1", "key_1" => "value_1" }
        subject.write_relation(exportable_path, relation, value)

        expect(consume_relations(path, exportable_path, relation)).to eq([value])
      end
    end

    context "when single relation is already serialized" do
      it "appends to the existing file" do
        values = [{ "key" => "value_1", "key_1" => "value_1" }, { "key" => "value_2", "key_1" => "value_2" }]
        relation = "relation"
        file_path = File.join(path, exportable_path, "#{relation}.ndjson")
        subject.write_relation(exportable_path, relation, values[0])

        expect { subject.write_relation(exportable_path, relation, values[1]) }.not_to raise_exception

        file_data = File.read(file_path)

        expect(file_data).to include(values[0].to_json)
        expect(file_data).to include(values[1].to_json)
      end
    end
  end

  describe "#write_relation_array" do
    it "writes json in correct files" do
      values =  [{ "key" => "value_1", "key_1" => "value_1" }, { "key" => "value_2", "key_1" => "value_2" }]
      relations = %w(relation1 relation2)
      relations.each do |relation|
        subject.write_relation_array(exportable_path, relation, values.to_enum)
      end
      subject.close

      relations.each do |relation|
        expect(consume_relations(path, exportable_path, relation)).to eq(values)
      end
    end
  end
end