2020-10-23 11:08:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-11-20 04:09:06 -05:00
|
|
|
RSpec.describe BulkImports::Pipeline do
|
2021-03-18 11:09:04 -04:00
|
|
|
let(:context) { instance_double(BulkImports::Pipeline::Context, tracker: nil) }
|
|
|
|
|
2021-02-19 07:11:06 -05:00
|
|
|
before do
|
|
|
|
stub_const('BulkImports::Extractor', Class.new)
|
|
|
|
stub_const('BulkImports::Transformer', Class.new)
|
|
|
|
stub_const('BulkImports::Loader', Class.new)
|
2020-10-23 11:08:42 -04:00
|
|
|
|
2021-02-19 07:11:06 -05:00
|
|
|
klass = Class.new do
|
|
|
|
include BulkImports::Pipeline
|
2020-11-30 10:09:21 -05:00
|
|
|
|
2021-02-19 07:11:06 -05:00
|
|
|
abort_on_failure!
|
2020-10-23 11:08:42 -04:00
|
|
|
|
2021-02-19 07:11:06 -05:00
|
|
|
extractor BulkImports::Extractor, foo: :bar
|
|
|
|
transformer BulkImports::Transformer, foo: :bar
|
|
|
|
loader BulkImports::Loader, foo: :bar
|
2020-10-23 11:08:42 -04:00
|
|
|
end
|
|
|
|
|
2021-02-19 07:11:06 -05:00
|
|
|
stub_const('BulkImports::MyPipeline', klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'pipeline attributes' do
|
2020-10-23 11:08:42 -04:00
|
|
|
describe 'getters' do
|
|
|
|
it 'retrieves class attributes' do
|
2021-01-12 01:10:31 -05:00
|
|
|
expect(BulkImports::MyPipeline.get_extractor).to eq({ klass: BulkImports::Extractor, options: { foo: :bar } })
|
2020-10-23 11:08:42 -04:00
|
|
|
expect(BulkImports::MyPipeline.transformers).to contain_exactly({ klass: BulkImports::Transformer, options: { foo: :bar } })
|
2021-01-12 01:10:31 -05:00
|
|
|
expect(BulkImports::MyPipeline.get_loader).to eq({ klass: BulkImports::Loader, options: { foo: :bar } })
|
2020-11-30 10:09:21 -05:00
|
|
|
expect(BulkImports::MyPipeline.abort_on_failure?).to eq(true)
|
2020-10-23 11:08:42 -04:00
|
|
|
end
|
2021-02-19 07:11:06 -05:00
|
|
|
|
|
|
|
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
|
2021-03-18 11:09:04 -04:00
|
|
|
pipeline = BulkImports::AnotherPipeline.new(context)
|
2021-02-19 07:11:06 -05:00
|
|
|
|
|
|
|
expect(pipeline.send(:extractor)).to eq(pipeline)
|
|
|
|
expect(pipeline.send(:loader)).to eq(pipeline)
|
|
|
|
end
|
|
|
|
end
|
2020-10-23 11:08:42 -04:00
|
|
|
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)
|
2020-11-30 10:09:21 -05:00
|
|
|
BulkImports::MyPipeline.abort_on_failure!
|
2020-10-23 11:08:42 -04:00
|
|
|
|
2021-01-12 01:10:31 -05:00
|
|
|
expect(BulkImports::MyPipeline.get_extractor).to eq({ klass: klass, options: options })
|
2020-10-23 11:08:42 -04:00
|
|
|
|
|
|
|
expect(BulkImports::MyPipeline.transformers)
|
|
|
|
.to contain_exactly(
|
|
|
|
{ klass: BulkImports::Transformer, options: { foo: :bar } },
|
|
|
|
{ klass: klass, options: options })
|
|
|
|
|
2021-01-12 01:10:31 -05:00
|
|
|
expect(BulkImports::MyPipeline.get_loader).to eq({ klass: klass, options: options })
|
2020-11-30 10:09:21 -05:00
|
|
|
|
|
|
|
expect(BulkImports::MyPipeline.abort_on_failure?).to eq(true)
|
2020-10-23 11:08:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-19 07:11:06 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-03-18 11:09:04 -04:00
|
|
|
pipeline = BulkImports::MyPipeline.new(context)
|
2021-02-19 07:11:06 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-03-18 11:09:04 -04:00
|
|
|
pipeline = BulkImports::NoOptionsPipeline.new(context)
|
2021-02-19 07:11:06 -05:00
|
|
|
|
|
|
|
pipeline.send(:extractor)
|
|
|
|
pipeline.send(:transformers)
|
|
|
|
pipeline.send(:loader)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-22 13:10:55 -05:00
|
|
|
|
|
|
|
describe '#transformers' do
|
|
|
|
before do
|
|
|
|
klass = Class.new do
|
|
|
|
include BulkImports::Pipeline
|
|
|
|
|
|
|
|
transformer BulkImports::Transformer
|
|
|
|
|
|
|
|
def transform; end
|
|
|
|
end
|
|
|
|
|
|
|
|
stub_const('BulkImports::TransformersPipeline', klass)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has instance transform method first to run' do
|
|
|
|
transformer = double
|
|
|
|
allow(BulkImports::Transformer).to receive(:new).and_return(transformer)
|
|
|
|
|
2021-03-18 11:09:04 -04:00
|
|
|
pipeline = BulkImports::TransformersPipeline.new(context)
|
2021-02-22 13:10:55 -05:00
|
|
|
|
|
|
|
expect(pipeline.send(:transformers)).to eq([pipeline, transformer])
|
|
|
|
end
|
|
|
|
end
|
2020-10-23 11:08:42 -04:00
|
|
|
end
|