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

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

require 'spec_helper'

RSpec.describe BulkImports::Pipeline do
  before do
    stub_const('BulkImports::Extractor', Class.new)
    stub_const('BulkImports::Transformer', Class.new)
    stub_const('BulkImports::Loader', Class.new)

    klass = Class.new do
      include BulkImports::Pipeline

      abort_on_failure!

      extractor BulkImports::Extractor, foo: :bar
      transformer BulkImports::Transformer, foo: :bar
      loader BulkImports::Loader, foo: :bar
    end

    stub_const('BulkImports::MyPipeline', klass)
  end

  describe 'pipeline attributes' do
    describe 'getters' do
      it 'retrieves class attributes' do
        expect(BulkImports::MyPipeline.get_extractor).to eq({ klass: BulkImports::Extractor, options: { foo: :bar } })
        expect(BulkImports::MyPipeline.transformers).to contain_exactly({ klass: BulkImports::Transformer, options: { foo: :bar } })
        expect(BulkImports::MyPipeline.get_loader).to eq({ klass: BulkImports::Loader, options: { foo: :bar } })
        expect(BulkImports::MyPipeline.abort_on_failure?).to eq(true)
      end

      context 'when extractor and loader are defined within the pipeline' do
        before do
          klass = Class.new do
            include BulkImports::Pipeline

            def extract; end

            def load; end
          end

          stub_const('BulkImports::AnotherPipeline', klass)
        end

        it 'returns itself when retrieving extractor & loader' do
          pipeline = BulkImports::AnotherPipeline.new(nil)

          expect(pipeline.send(:extractor)).to eq(pipeline)
          expect(pipeline.send(:loader)).to eq(pipeline)
        end
      end
    end

    describe 'setters' do
      it 'sets class attributes' do
        klass = Class.new
        options = { test: :test }

        BulkImports::MyPipeline.extractor(klass, options)
        BulkImports::MyPipeline.transformer(klass, options)
        BulkImports::MyPipeline.loader(klass, options)
        BulkImports::MyPipeline.abort_on_failure!

        expect(BulkImports::MyPipeline.get_extractor).to eq({ klass: klass, options: options })

        expect(BulkImports::MyPipeline.transformers)
          .to contain_exactly(
            { klass: BulkImports::Transformer, options: { foo: :bar } },
            { klass: klass, options: options })

        expect(BulkImports::MyPipeline.get_loader).to eq({ klass: klass, options: options })

        expect(BulkImports::MyPipeline.abort_on_failure?).to eq(true)
      end
    end
  end

  describe '#instantiate' do
    context 'when options are present' do
      it 'instantiates new object with options' do
        expect(BulkImports::Extractor).to receive(:new).with(foo: :bar)
        expect(BulkImports::Transformer).to receive(:new).with(foo: :bar)
        expect(BulkImports::Loader).to receive(:new).with(foo: :bar)

        pipeline = BulkImports::MyPipeline.new(nil)

        pipeline.send(:extractor)
        pipeline.send(:transformers)
        pipeline.send(:loader)
      end
    end

    context 'when options are missing' do
      before do
        klass = Class.new do
          include BulkImports::Pipeline

          extractor BulkImports::Extractor
          transformer BulkImports::Transformer
          loader BulkImports::Loader
        end

        stub_const('BulkImports::NoOptionsPipeline', klass)
      end

      it 'instantiates new object without options' do
        expect(BulkImports::Extractor).to receive(:new).with(no_args)
        expect(BulkImports::Transformer).to receive(:new).with(no_args)
        expect(BulkImports::Loader).to receive(:new).with(no_args)

        pipeline = BulkImports::NoOptionsPipeline.new(nil)

        pipeline.send(:extractor)
        pipeline.send(:transformers)
        pipeline.send(:loader)
      end
    end
  end
end