summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/import_export/base/object_builder_spec.rb
blob: d560c8ea5a70d866b0cfd25ed51b66ee146f58a0 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::ImportExport::Base::ObjectBuilder do
  let(:project) do
    create(:project, :repository,
           :builds_disabled,
           :issues_disabled,
           name: 'project',
           path: 'project')
  end
  let(:klass) { Milestone }
  let(:attributes) { { 'title' => 'Test Base::ObjectBuilder Milestone', 'project' => project } }

  subject { described_class.build(klass, attributes) }

  describe '#build' do
    context 'when object exists' do
      context 'when where_clauses are implemented' do
        before do
          allow_next_instance_of(described_class) do |object_builder|
            allow(object_builder).to receive(:where_clauses).and_return([klass.arel_table['title'].eq(attributes['title'])])
          end
        end

        let!(:milestone) { create(:milestone, title: attributes['title'], project: project) }

        it 'finds existing object instead of creating one' do
          expect(subject).to eq(milestone)
        end
      end

      context 'when where_clauses are not implemented' do
        it 'raises NotImplementedError' do
          expect { subject }.to raise_error(NotImplementedError)
        end
      end
    end

    context 'when object does not exist' do
      before do
        allow_next_instance_of(described_class) do |object_builder|
          allow(object_builder).to receive(:find_object).and_return(nil)
        end
      end

      it 'creates new object' do
        expect { subject }.to change { Milestone.count }.from(0).to(1)
      end
    end
  end
end