aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/referential_cloning_spec.rb
blob: b017a37e980d262e195a900f26a1692d1c3a4936 (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
require 'spec_helper'

RSpec.describe ReferentialCloning, :type => :model do

  it 'should have a valid factory' do
    expect(FactoryGirl.build(:referential_cloning)).to be_valid
  end

  it { should belong_to :source_referential }
  it { should belong_to :target_referential }

  describe 'after commit' do
    let(:referential_cloning) { FactoryGirl.create(:referential_cloning) }

    it 'invoke clone method' do
    expect(referential_cloning).to receive(:clone)
    referential_cloning.run_callbacks(:commit)
    end
  end

  describe '#clone' do
    let(:referential_cloning) { FactoryGirl.create(:referential_cloning) }

    it "should schedule a job in worker" do
      expect{referential_cloning.clone}.to change {ReferentialCloningWorker.jobs.count}.by(1)
    end
  end

  describe '#clone!' do
    let(:source_referential) { Referential.new slug: "source"}
    let(:target_referential) { Referential.new slug: "target"}
    let(:referential_cloning) do
      ReferentialCloning.new source_referential: source_referential,
                             target_referential: target_referential
    end

    let(:cloner) { double }

    it 'creates a schema cloner with source and target schemas and clone schema' do
      %w{dump_command sed_command restore_command}.each do |command|
        allow(referential_cloning).to receive(command).and_return(command)
      end
      allow(referential_cloning).to receive(:clean)

      expect(referential_cloning).to receive(:system).with("dump_command | sed_command | restore_command").and_return(true)

      referential_cloning.clone!
    end
  end

  describe '#clone_with_status!' do
    let(:referential_cloning) do
      ReferentialCloning.new(target_referential: Referential.new(slug: "target"))
    end

    before do
      allow(referential_cloning).to receive(:clone!)
    end

    it 'invokes clone! method' do
      expect(referential_cloning).to receive(:clone!)
      referential_cloning.clone_with_status!
    end

    context 'when clone_schema is performed without error' do
      it "should have successful status" do
        referential_cloning.clone_with_status!
        expect(referential_cloning.status).to eq("successful")
      end
    end

    context 'when clone_schema raises an error' do
      it "should have failed status" do
        expect(referential_cloning).to receive(:clone!).and_raise("#fail")
        referential_cloning.clone_with_status!
        expect(referential_cloning.status).to eq("failed")
      end
    end

    it "defines started_at" do
      referential_cloning.clone_with_status!
      expect(referential_cloning.started_at).not_to be_nil
    end

    it "defines ended_at" do
      referential_cloning.clone_with_status!
      expect(referential_cloning.ended_at).not_to be_nil
    end

  end
end