aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/import_spec.rb
blob: 9a9d7326d97f63ac2d3c30c8078315dc3c20256a (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
RSpec.describe Import, :type => :model do

  it { should belong_to(:referential) }
  it { should belong_to(:workbench) }
  it { should belong_to(:parent) }

  it { should enumerize(:status).in("aborted", "canceled", "failed", "new", "pending", "running", "successful") }

  it { should validate_presence_of(:file) }
  it { should validate_presence_of(:workbench) }
  it { should validate_presence_of(:creator) }

  let(:workbench_import) { build_stubbed(:workbench_import) }
  let(:workbench_import_with_completed_steps) do
    workbench_import = build_stubbed(
      :workbench_import,
      total_steps: 2,
      current_step: 2
    )
  end

  let(:netex_import) do
    netex_import = build_stubbed(
      :netex_import,
      parent: workbench_import
    )
  end

  describe "#notify_parent" do
    it "must call #child_change on its parent" do
      allow(netex_import).to receive(:update)

      expect(workbench_import).to receive(:child_change)

      netex_import.notify_parent
    end

    it "must update the :notified_parent_at field of the child import" do
      allow(workbench_import).to receive(:child_change)

      Timecop.freeze(DateTime.now) do
        expect(netex_import).to receive(:update).with(
          notified_parent_at: DateTime.now
        )

        netex_import.notify_parent
      end
    end
  end

  describe "#child_change" do
    shared_examples(
      "updates :status to failed when child status indicates failure"
    ) do |failure_status|
      it "updates :status to failed when child status indicates failure" do
        workbench_import = create(:workbench_import)
        create(
          :netex_import,
          parent: workbench_import,
          status: failure_status
        )

        expect(workbench_import).to receive(:update).with(
          current_step: 1,
          status: 'failed'
        )

        workbench_import.child_change
      end
    end

    include_examples(
      "updates :status to failed when child status indicates failure",
      "failed"
    )
    include_examples(
      "updates :status to failed when child status indicates failure",
      "aborted"
    )
    include_examples(
      "updates :status to failed when child status indicates failure",
      "canceled"
    )

    it "updates :status to successful when #ready?" do
      expect(workbench_import).to receive(:update).with(status: 'successful')

      workbench_import.child_change
    end

    it "updates :status to failed when #ready? and child is failed" do
      build_stubbed(
        :netex_import,
        parent: workbench_import,
        status: :failed
      )

      expect(workbench_import).to receive(:update).with(status: 'failed')

      workbench_import.child_change
    end

    shared_examples(
      "doesn't update :status if parent import status is finished"
    ) do |finished_status|
      it "doesn't update :status if parent import status is finished" do
        workbench_import = build_stubbed(
          :workbench_import,
          total_steps: 2,
          current_step: 2,
          status: finished_status
        )
        double('Import')

        expect(workbench_import).not_to receive(:update)

        workbench_import.child_change
      end
    end

    include_examples(
      "doesn't update :status if parent import status is finished",
      "successful"
    )
    include_examples(
      "doesn't update :status if parent import status is finished",
      "failed"
    )
    include_examples(
      "doesn't update :status if parent import status is finished",
      "aborted"
    )
    include_examples(
      "doesn't update :status if parent import status is finished",
      "canceled"
    )

    it "calls #update_status" do
      allow(workbench_import).to receive(:update)

      expect(workbench_import).to receive(:update_status)
      workbench_import.child_change
    end

    it "calls #update_referential" do
      allow(workbench_import).to receive(:update)

      expect(workbench_import).to receive(:update_referential)
      workbench_import.child_change
    end
  end

  describe "#ready?" do
    it "returns true if #current_step == #total_steps" do
      import = build_stubbed(
        :import,
        total_steps: 4,
        current_step: 4
      )

      expect(import.ready?).to be true
    end

    it "returns false if #current_step != #total_steps" do
      import = build_stubbed(
        :import,
        total_steps: 6,
        current_step: 3
      )

      expect(import.ready?).to be false
    end
  end
end